Auto Generate code in java based on templates - eclipse-plugin

I have a eclipse plugin and I want to add a capability to generate java code blocks based on simple template or anything simple. Something like generate source for constructors or getters and setters that java editor provides.
I have seen the JET articles : https://eclipse.org/articles/Article-JET2/jet_tutorial2.html but I want something simpler.

Eclipse already some has it built in:
1. Highlight your field
2. Right Click -> Source -> Generate Getters and Setters
Also you can use the wizards to create a basic java class.
1. File -> New -> Class
etc.
You can also look up this related question:
A Java API to generate Java source files

Related

Template engine/pre-processor that works from *within* the target language

I'm using Eclipse as my IDE, and I now have looked at about a dozen template engines, and they all seem to work in a similar way:
You create a template file in the template language
The IDE plugin, if any, recognize the template syntax, rather then the target language syntax.
The generated/modified file, in the target language, is read-only, since it will get replaced on next generation.
This greatly limits the use of template engines/pre-processor, because you loose the IDE support for the target language.
Generally, the templating syntax will be simple enough anyway, that you could easily get by without IDE support for it. What is really needed, is to get the target language support from within the template.
In other words, is there some pre-processor/template engine, that does not require you to create "template files", but rather register itself in the IDE between the file system and the target language support, modifying the content of the file on the fly such that the target language plugin sees the file after generation, while the file is saved in the file system as it was before the generation, that is, it contains the template on the file system, rather then the generated code.
In other other words, I want to write the templates in the target language, and have them recognized as such by the IDE editor, such that the editor sees the code after generation (maybe hide the template instructions in comments?).
Such a plugin would then be totally generic, and independent of the actual file type containing the template (as long as it's a text file). All that would be needed, is then some configuration file telling the template engine in which files/directories to look for templates.
Alternatively, if no such product exist, does it seem like something that could be implemented in Eclipse as a plugin, or is it more a case that there are no such plugin because it would be basically impossible?
Here a list of a few things I've looked at, all of which don't seem to support the desired use case:
Xtend
eclipseME - Preprocessing
Java Comment Preprocessor
Preprocessing Java with Munge Munge
Velocity
Jamon
JET
StringTemplate
Xpand
Freemarker
Mustache Java
Jade4J
Closure Templates in Java
Rythm Engine
Related question: Saving self-written code - xtend

How can i add custom expression functions for Live templates in Intellij

How can add new custom functions for Live Templates in Idea Intellij.
For example i need a custom function which can convert a live template variable from Camel Case to Spaced.
i.e in Live Template on variable has to be inserted at multiple places with & without space.
Eg. "MyVar" and "My Var". So i want to insert an expression to convert 'MyVar' to 'My Var'.
No expression available by default can be used for this.
Thanks.
There is OpenAPI for providing Live Template functions. One can create IntelliJ IDEA plug-in that will add more functions.
See the Macro abstract class. Plug-in should define extension point, like this one:
<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CapitalizeMacro"/>
Search IntelliJ IDEA Community code base for sample implementations.

Find child plug-ins of features in eclipse workspace

I want to find all the child plug-ins of a feature ? I know the name of the feature, but IWorkspaceRoot.getProject(String) does not really help me. I get an IProject that I don't know how to convert to a feature object (IFeature ?). Maybe I am on the wrong track and there is a better / easier way to do this. Any ideas ?
You could check that a selected IProject is a feature project by checking for the nature called org.eclipse.pde.FeatureNature.
Then you could try to use IProject.getAdapter(IFeature.class) call, the cast the result to IFeature. I did not try this with feature projects, but works well with Java projects.
The correct answer is the use of PDECore static class. This class provides a FeatureModelManager, that would provide the corresponding information:
FeatureModelManager manager = PDECore.getDefault().getFeatureModelManager();
How to obtain this information? I looked with the plug-in spy to find which project defines the 'Deployable Features' export wizard (use Alt+Shift+F3 when the wizard is selected), and then looked at the implementation of the wizard class, where the addPages() method contains the previously described code block.

script generation in GMF

i have created my own graphical editor using GMF.
i want to generate code based on the diagram created by editor? any pointers how to proceed
This might help you:
http://www.vogella.de/articles/EclipseBuilder/article.html
That page doesn't have the actual code example but the points to the necessary concepts: Builders. For example, the java compiler is hooked in through a builder that's registered for the .java type.

Integrate JET Templates to an EMF-GMF plugin

I created an plugin that lets me model JSF Applications. I created it using Emfatic, Ecore, EMF, GMF.
So now i have 5 projects in my workspace:
myapp
myapp.diagram
myapp.edit
myapp.editor
myapp.tests
If i run myapp as a Eclipse Application i can draw/design a .myapp_diagram based on the meta-model.
Now I want to know how can i integrate JET Templates with what i have here.
I have the diagram, i know how to build JET Templates, i just want to know how can i feed the diagram as input to the JET temples so that code will be generated.
What i have done until now is convert the "myapp" project to JET Project and wrote the template files. But if i now run "myapp" and draw a diagram, theres nowhere a GENERATE CODE button/option.
Unfortunately none of the Eclipse-based modelling projects provide a "generate code" button. There are a wide range of possible model sources and code generation platforms (JET, OAW, Xpand), so you will have to implement the button and the relevant source code yourself. However, the process is fairly straightforward.
One approach is to implement an org.eclipse.ui.popupMenus extension point, which then calls the Java class necessary to generate the code.
As an example, you can check out the plugin.xml used in the IAML project to generate PHP/Javascript source code, using openArchitectureWare (OAW) templates. The relevant Eclipse Action is GenerateCodeAction - check out the method doExecute(). Hope this helps :)