Jump to: navigation, search

Difference between revisions of "EMF/ExtendingCodeGeneration"

< EMF
(Summary)
m (Package Generator Adapter)
(23 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
The intension of this page to collect experiences of implementing a Generation Adapter.
 
The intension of this page to collect experiences of implementing a Generation Adapter.
  
The a reference implementation is the project org.eclipse.emf.examples.generator.validator  
+
A sample implementation is the project org.eclipse.emf.examples.generator.validator
 +
 
 
(see :pserver:anonymous@dev.eclipse.org:/cvsroot/modeling under org.eclipse.emf/org.eclipse.emf/examples)
 
(see :pserver:anonymous@dev.eclipse.org:/cvsroot/modeling under org.eclipse.emf/org.eclipse.emf/examples)
  
Line 15: Line 16:
 
{
 
{
 
</source>
 
</source>
 +
 +
The GenBaseGeneratorAdapter base class  provide you the basic implementation you have to override:
 +
* canGenerate
 +
* generateXXXX
 +
 +
It's depend of the project type what generate method's you override. The following types you know from the menu in the Gen Model.
 +
 +
* MODEL_PROJECT_TYPE
 +
* EDIT_PROJECT_TYPE
 +
* EDITOR_PROJECT_TYPE
 +
* TESTS_PROJECT_TYPE
 +
 +
In the method canGenerated you have to return true for your project types.
 +
 +
The prefix "GenClass" says this adapter works with the GenClass class of the genmodel, but this is just a naming convention .
 +
 +
In accordance to this you have to configure the extension point or you write a GeneratorAdapterFactory.
 +
All  adapters types are:
 +
* GenPackage Adapter
 +
* GenEnum Adapter
 +
* GenModel Adapter
 +
* GenClass Adapter
 +
 +
Take care not all combinations and of project type and adapter type produce output. (e.g. I had no luck in the combination GenClass and EDITOR project)
 +
 +
== Package Generator Adapter ==
 +
 +
If you write a GenPackage generator adapter you should verify in your generate method (in most cases) if it is required to generate an e.g. new class for it.
 +
The package in the model may on a higher level, but you get a call for all (selected) packages in the model.
 +
 +
<source lang="java">
 +
 +
if (genPackage.hasConcreteClasses()) {
 +
  message = "Generating class XYZ"; /
 +
  monitor.subTask(message);
 +
 +
  generateJava(....);
 +
} else {
 +
  monitor.worked(1);
 +
}
 +
</source>
 +
 +
A good example is the class GenPackageGeneratorAdapter of EMF itself, you can find it simply  by the "Open Type..." command.
 +
 +
=  Dynamic Templates & Generator Adapter =
 +
 +
To combine a Generator Adapter and a Dynamic Templates you can add the following URI in the GemModel "Template Directory" property
 +
* platform:/plugin/<plugin-id>/templates
 +
 +
If you have some problems during generation (e.g. it generate still the old version) it may help to remove the (hidden) project ".JETEmitter" from you testing workspace (if you just delete it don't forget to refresh the workspace)
 +
 +
=  JET Templates =
 +
 +
The included JET Templates or better the static JET template classes have to be generated with the "old/basic" JET implementation.
 +
(choose "Convert Project to JET Project" in new wizard and do not create a "JET Transformation Project")
 +
 +
The M2T JET project even in JET 1 mode produces a not compatible code: see [https://bugs.eclipse.org/bugs/show_bug.cgi?id=333811 BUG 333811]
  
 
= Links =  
 
= Links =  
  
 
* http://www.eclipse.org/forums/index.php?t=msg&S=26948a7500a8f8b49cfde1a577ad810c&th=155261&goto=48916
 
* http://www.eclipse.org/forums/index.php?t=msg&S=26948a7500a8f8b49cfde1a577ad810c&th=155261&goto=48916
 +
 +
Forum entries related to how to combine a generator and dynamic templates
 +
 
* http://www.eclipse.org/forums/index.php?t=msg&goto=647243&S=26948a7500a8f8b49cfde1a577ad810c#msg_647243
 
* http://www.eclipse.org/forums/index.php?t=msg&goto=647243&S=26948a7500a8f8b49cfde1a577ad810c#msg_647243
 +
* http://www.eclipse.org/forums/index.php?t=rview&goto=480066&th=152145
 +
* http://www.eclipse.org/forums/index.php?t=msg&goto=415810&S=6b757ff8567e4b1a93cbdc4439a9d208
 +
 +
Here another example:
 +
http://code.google.com/a/eclipselabs.org/p/emf/source/browse/#hg%2Forg.eclipselabs.emf.codegen.addon

Revision as of 14:49, 21 January 2011

Summary

The intension of this page to collect experiences of implementing a Generation Adapter.

A sample implementation is the project org.eclipse.emf.examples.generator.validator

(see :pserver:anonymous@dev.eclipse.org:/cvsroot/modeling under org.eclipse.emf/org.eclipse.emf/examples)


GeneratorAdapter Class

The definition links like this

public class GenClassValidatorGeneratorAdapter extends GenBaseGeneratorAdapter
{

The GenBaseGeneratorAdapter base class provide you the basic implementation you have to override:

  • canGenerate
  • generateXXXX

It's depend of the project type what generate method's you override. The following types you know from the menu in the Gen Model.

  • MODEL_PROJECT_TYPE
  • EDIT_PROJECT_TYPE
  • EDITOR_PROJECT_TYPE
  • TESTS_PROJECT_TYPE

In the method canGenerated you have to return true for your project types.

The prefix "GenClass" says this adapter works with the GenClass class of the genmodel, but this is just a naming convention .

In accordance to this you have to configure the extension point or you write a GeneratorAdapterFactory. All adapters types are:

  • GenPackage Adapter
  • GenEnum Adapter
  • GenModel Adapter
  • GenClass Adapter

Take care not all combinations and of project type and adapter type produce output. (e.g. I had no luck in the combination GenClass and EDITOR project)

Package Generator Adapter

If you write a GenPackage generator adapter you should verify in your generate method (in most cases) if it is required to generate an e.g. new class for it. The package in the model may on a higher level, but you get a call for all (selected) packages in the model.

if (genPackage.hasConcreteClasses()) {
  message = "Generating class XYZ"; /
  monitor.subTask(message);
 
   generateJava(....);
} else {
  monitor.worked(1);
}

A good example is the class GenPackageGeneratorAdapter of EMF itself, you can find it simply by the "Open Type..." command.

Dynamic Templates & Generator Adapter

To combine a Generator Adapter and a Dynamic Templates you can add the following URI in the GemModel "Template Directory" property

  • platform:/plugin/<plugin-id>/templates

If you have some problems during generation (e.g. it generate still the old version) it may help to remove the (hidden) project ".JETEmitter" from you testing workspace (if you just delete it don't forget to refresh the workspace)

JET Templates

The included JET Templates or better the static JET template classes have to be generated with the "old/basic" JET implementation. (choose "Convert Project to JET Project" in new wizard and do not create a "JET Transformation Project")

The M2T JET project even in JET 1 mode produces a not compatible code: see BUG 333811

Links

Forum entries related to how to combine a generator and dynamic templates

Here another example: http://code.google.com/a/eclipselabs.org/p/emf/source/browse/#hg%2Forg.eclipselabs.emf.codegen.addon