How do I create a Processing library that adds a new function to the language? - function

I want to create a Processing library that adds a single function to Processing. A single command. How do I do this?
So I want to be able to write on Processing this:
void setup() {
drawMyCustomShape()
}
In a way that drawMyCustomShape will be on my custom library implementation.
Thanks!
Note: this question is not about creating a new library in processing. Is about creating a library that exports one new command (so you can using without caring of the container class instance).

First of all, are you sure you really need to create an entire library? You could just add that class to your sketch without needing to deploy it as a library. If you're worried about clutter, just put it in its own tab.
If you really need to create a library, then there are three tutorials that you need to read:
Library Overview
Library Basics
Library Guidelines
But basically, you need to create a Java project (in an IDE like eclipse, or with a basic text editor and the command line) that uses Processing as a library. That's where you'd put your MyLibrary class. You'd then export it as a .jar file, and then import that .jar file into Processing. You would then be able to use your class exactly like you can use any other Processing library.
Your proposed setup has some other issues (how are you going to access the sketch variable from the static function?), but I'd suggest treating them as separate questions after you get the basics in place.

It sounds like you are actually looking to create your own extension of the Processing library, as in actually change the core jar file.
You can extend the actual Processing library by forking off of its main branch on Github. By writing your function drawMyCustomShape into the actual core in the forked version, you can then build the Processing Development Environment from your copy of the code. Using that particular copy of the PDE, you could do what you're describing.
Once you compile this build, you could actually distribute this copy of the PDE to your college students. They would be able to use your function as if nothing were changed. (I'm guessing that this is for an intro-level class at the college level, so that's why you would have to hide implementation from your students?)
Here's some links to get you started:
Processing github
Instructions for building the PDE from source

So, finally I found the most adequate answer for my case.
The solution for this is to implement a new Processing Mode that extends the builtin Java Mode. To include static members to the main processing program you will need to add a new static import to the ones that processing adds to your code.
You can do this by forking the Mode Template for 3.0 that #joelmoniz created from #martinleopold:
https://github.com/joelmoniz/TemplateMode/tree/3.0-compatibility
There is a good tutorial here:
http://pvcresin.hatenablog.com/entry/2016/03/17/210135
Why is the most adequate solution? : I think this is the best way to achieve new static methods in processing code and ensure an easy distribution! You just have to set the mode folder in your sketchbook/modes folder. If I were to fork processing it would be a big deal to prepare distributions for all operative systems and also to keep update with main project.
My particular solution:
To add my static imports into Processing I implemented a custom mode where I overrode the PdePreprocessor class which wraps the processing code with all the Java procesing code. So, the idea was to add more imports to the imports that the PdePreprocessor generates on the generated Java source.
In my custom PdePreprocessor I overrode the getCoreImports method to add my custom methods. I did this here because I consider the new imports are part of the core of my custom mode. You could also achieve this by overriding writeImports method.
In order to use my PdePreprocessor implementation I had to overrode the following classes:
Commander
JavaBuild
JavaEditor
JavaMode
JavaEditor
I had to implement a new JavaBuild which preprocesses the Sketch with my custom PdePreprocessor. And also use my custom JavaBuild in all the places where the Processing Java Mode instances the build class. Please share with us if there is a better way to do what I did.
Here is the github for my solution: http://github.com/arypbatista/processing-inpr/

Related

Can the AS3 API reference be accessed trough code

I am making a code completion system for a code editor, and i would like to show a tooltip for every parameter containing its reference data. I would like to emulate the code completion from Eclipse, so i was wondering if the API reference is available in code, or if i have to manually import it using the file system or something like that.
You could reflect the current class in question with flash.utils.describeType
This will return a xml with informations about methods, properties and so on.
This can be very heavy. If you use it all the time, try to use a caching system. The Flex framework has a class for it mx.utils.DescribeTypeCache

How to replace gwt json with my own json implementation

I need to find a method to work json implementation in my gwt-project com.google.gwt.json.client package instead of json in GWT com.google.gwt.json package.
In my GWT project I want to use json implementation written by myself and stored in a package in my project with same path as that of GWT json package.
Files inside this packages are com.google.gwt.json.client implemented by myself and keep in same package in my project, how to configure in project to use these packages instead of original.
Any suggestions regarding this will be appreciated.
Thanks to all..
UPDATES:
For more clarification on what I am looking for:
While parsing with JSONUtils in GWT, make parser to use JSONObject written in my project com.google.gwt.json package, insted of GWT-json in com.google.gwt.json package.
Sometimes it is necessary to modify certain GWT core classes because either, you need to fix an issue, or you need to add a new feature to them.
To override any GWT implementation class, for instance com.google.gwt.json.client.JSONObject, you only have to copy and modify that class in your src folder with the same path: src/main/java/com/google/gwt/json/client/JSONObject.java if you are using maven, src/com/google/gwt/json/client/JSONObject.java otherwise.
The only care you need is that your src folder is first in your classpath than the gwt sdk, with maven it is so by default. You should be aware as well that when you update the gwt version, perhaps you would need to update your implementations.
If the instances of the class you were trying to override, are created in GWT using the GWT.create() call, you could replace the class with your own implementation with a <replace-with> tag in your .gwt.xml. This technique is called deferred-binding. This is not the case since JSONObject is normally created with new.
Finally the <super-source> tag can be used to override any class implementation in compile time. Although super-source is designed to override jre classes by gwt implementations, replacing gwt with other gwt implementations works. In this case you have to put your modified classes, with the same namespace structure, in the folder pointed by the super-source tag.

Sharing source between 2 projects?

I have a project containing a big package "global" of classes which is designed for Web, I need to share these classes with a new mobile project, but when i add them with :
Properties -> Flex Build Path -> Source path -> Add Folder
they start appearing with index [source path] before the package name, and since them Flash Builder start trowing error messages :
"A file found in a source-path must have the same package structure '', as the definition's package, 'global'."
How can i fix this issue ?
As we've discussed in the comments, I think it would be a better approach to compile your "global" classes into a library (.swc).
You were concerned about loading unnecessary classes: when you link to a library as 'merged', only the classes you use are actually compiled into the main application (and any classes they depend on), so there's no need to worry about that.
As a last argument I also think this is a more flexible approach. A compiled library is easier to reuse and version, so the code can more easily be distributed to other developers on your team.
Rename one of the packages with right click->refactor. Than is should work.
If not you can also try to have your two codes available at the same project, and then you can select which to run in Flash Builder, by right-clicking to that .as or .mxml file, and selecting set as ... (or something like that)
I guess if you will include 'src' fonder instead of 'src/global' that problem will disappear.

How do I disable code generation in my test plugin?

I have a couple of test files written in my DSL in my tests plugin/project. Most of the tests use inline multi-line strings and Xtend but in four cases, I need to test code which does some magic with URLs and the classpath, so I really need resources in the classpath for that.
Since loading the resources only works when the extension is correct, I can't give the files a fake extension.
Now my problem: My DSL also has a code generator. This means that eventually, I end up with a couple of generated files in places where I can't have them (they don't compile, for example, and one even contains an error to test error handling when information is split across several files).
I can't disable the Xtext nature because the tests project uses Xtend so for these files, I do need code generation.
Since the generator runs inside Eclipse (I have the DSL plugins installed for other projects), there is no way to override the code generator in Guice.
How can I disable the code generator in this case?
There is a simple way to achieve this:
Open the properties of your project
Expand the entry for your DSL
Select "Compiler"
Select "Enable project specific settings"
Disable/deselect "Compiler is activated" under "General"
If you don't have a properties entry for your DSL:
Add this fragment to your .mwe2 workflow file:
fragment = generator.GeneratorFragment {}
Regenerate your projects
Merge the new code from plugin.xml_gen into plugin.xml both in the base and the UI plugins.
The interesting parts are the two extension points org.eclipse.ui.preferencePages and org.eclipse.ui.propertyPages

How to override a Magento function in app/code/core/Mage/Core/functions.php

I need to override a function in this file:
app/code/core/Mage/Core/functions.php
The problem is that this is so core that there’s no class associated to it, probably because Core isn’t even a module. Does anybody know how to override a function in the file without a class?
Any help would be appreciated.
Copying the file to app/code/local/Mage/Core/functions.php should not be used because of the following reasons:
The entire file has to be copied over making it harder for us to identify what changes have been made.
Future upgrades could introduce new features that would not be available unless it is remembered to copy across the new version of that file and implement the changes again.
Future upgrades could address bugs with core that we would miss unless it is remembered to copy across the new version of that file and implement the changes again.
In respect to points 2 & 3 each upgrade could change the way things work that means revisiting what changes we need to make. In some cases this will be true for overriden methods as well but at least we can easily identify where those changes effect us.
What do you do if another person wants to use the same technique? Being able to identify what is core code and what is ours becomes more and more complex.
Keeping our code together as a “module” becomes more difficult as by copying in the core file means that we have effectively locked it into being “guaranteed” to run on the version of the software that we have copied the original code from. It also means reusing this work is a lot more difficult to do.
Identifying why the code was changed it much harder as it is outside our namespacing, ie all development related to “Example_Module” is in the namespace:
/app/code/core/local/Example/Module
whereas code copied to app/code/core/local/Mage only indicates that we have made a change to support an unknown feature etc.
Also Magento occasionally release patches which fixes bugs – these will only patch files inside core leaving your copied file without the patch.
What I would suggest instead is that you write your own function to do what you want and override the function to call your new function instead.
Maybe I did not understood your question right but why not just copy this file into
app/code/**local**/Mage/Core/functions.php
and modify it there in any way you want?
As mentioned by #tweakmag the disadvantages of creating a folder structure and copying the entire Model or controller for a single function override, most important being,
"Future upgrades could introduce new features that would not be
available unless it is remembered to copy across the new version of
that file and implement the changes again."
Thus a solution can be, to extend the core class (Model or controller) and just write the method you want to override, instead of copying all the methods.
For example, if you want to say, override a method getProductCollection in Mage_Catalog_Model_Category, here will be the steps:
Create a Custom Namespace/Module folder with etc folder in it
Register the module in app/etc folder by creating Namespace_Module.xml
setup the config.xml in Namespace/Module/etc/ :
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Module>
<version>1.0</version>
</Namespace_Module>
</modules>
<global>
<models>
<catalog>
<rewrite>
<category>Namespace_Module_Model_Category</category>
</rewrite>
</catalog>
</models>
</global>
</config>
create Model folder in Namespace_Module and a Php File Category.php
Now the main difference here is we extend the Magento's core class instead of writing all the methods
<?php
/**
* Catalog category model
*/
class Namespace_Module_Model_Category extends Mage_Catalog_Model_Category
{
public function getProductCollection()
{
// Include your custom code here!
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this);
return $collection;
}
}
The getProductCollection method is overridden so it'll be called instead of the method defined in the core model class.
Also an important point is:
When you override any methods in models, you should make sure that the data type of the return value of that method matches with the data type of the base class method. Since model methods are called from several core modules, we should make sure that it doesn't break other features!
This link gives a step by step approach to it
1. UOPZ
There's a way to do it without having to copy and maintain functions.php file from core, but it involves extension uopz (pecl install uopz), then you can rename magento's function (from foo to foo_uopzOLD for example) and define your own (https://secure.php.net/uopz)
It works and is very useful for magento - usually you'll bump into something you can not change. Uopz is very helpful in such cases.
pros: works ;), you don't have to redo it everytime you update Magento (if you do it right, because inside you still can call foo_uopzOLD so you can assure some backward compatibility... in some cases).
cons: it's little bit implicit
2. Composer post-install-cmd
If you don't like the above, but you use composer you can patch any file you want:
"scripts": {
"post-install-cmd": "patch -p0 < change-core-functions.patch"
}
pros: explicit (when patch fail - composer install fails), and since it's explicit - you can revisit and fix the patch every time you upgrade magento
cons: you have modified core file, so you probably would want to add it to .gitignore
3. Ugly solution (uglier than those above)
If none of the above is possible for you (really, give it a try with composer - there's no excuse for not using it). But when you really can not the only way I can think of
- create app/local/Mage/Core/functions.php
- define this one function you need
- load original /app/core/Mage/Core/functions.php
- surround every function foo() {...} with
if(!function_exists("foo"){
function foo() {...}
}
hold on to your chair and
eval this SOB ;)