GeneratorRegistry unleashed :-) [message #1064239] |
Tue, 18 June 2013 11:16 |
Andy Geiss Messages: 5 Registered: June 2013 |
Junior Member |
|
|
Hello guys,
I'm currently working on DSL project which should support multi generators.
In addition to that, these generators must be seperate plugins, to support a modular structure. With that structure you are able to deploy "features" per customer without changing your code.
So I found an easy solution for me and will share it with you:
1. Create a new Project and add a class named "GeneratorRegistry":
package de.ageiss.xtext.sam.generator.util
import java.util.List
import org.eclipse.xtext.generator.IGenerator
import java.util.Vector
class GeneratorRegistry {
static List<IGenerator> generators = new Vector()
def static List<IGenerator> getGenerators() {
return generators
}
def static void registerGenerator(IGenerator generator) {
if (generator != null) {
generators.add(generator)
println("[+] generator '" + generator + "' added to registry.")
}
}
}
2. Modify your current Generator as follow:
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
println("[+] generator '" + this + "' starting...")
generate(resource.contents.head as Model, fsa)
for (generator : GeneratorRegistry::generators) {
generator.doGenerate(resource, fsa)
}
println("[+] generator '" + this + "' done.")
}
Now your generator (primary) is able to call the generate method for all the registred generators.
3. Create a new Plugin-Project for your new seperate generator with an Activator class as follows:
public void start(BundleContext context) throws Exception {
System.out.println("[+] activator '" + this + "' starting...");
GeneratorRegistry.registerGenerator(new YourNewGenerator());
super.start(context);
plugin = this;
System.out.println("[+] activator '" + this + "' done.");
}
The generator is now able to register himself at startup into the GeneratorRegistry.
4. Create a new class named "Startup" which implements the "IStartup" interface and add create a plugin.xml as follows:
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
<extension point="org.eclipse.ui.startup">
<startup class="your.package.generator.Startup" />
</extension>
</plugin>
[Updated on: Tue, 18 June 2013 15:00] Report message to a moderator
|
|
|
|
|
Re: GeneratorRegistry unleashed :-) [message #1064421 is a reply to message #1064239] |
Wed, 19 June 2013 08:06 |
|
Hi Andy,
Two things:
You could create a Dispatcher generator, which gets injected other generators. Then there is no need for more magic:
class MyGenerator implements IGenerator {
@Inject Generator1 gen1
@Inject Generator2 gen2
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
gen1.doGenerate(resource,fsa)
gen2.doGenerate(resource,fsa)
}
}
You approach of course works, but I see these flaws:
- It requires a plugin with early startup. If this is forgotten to configure the approach will fail. Usually early startup should be avoided whenever possible.
- The static approach is a memory leak. The list never gets free. Not a big deal here, since the generators should be usually stateless. However, it would be better to hold a static instance which is created on start() and set to null on stop().
Best wishes,
~Karsten
Need professional support for Xtext, EMF, Eclipse IDE?
Go to: http://devhub.karakun.com
Twitter : @kthoms
Blog : www.karsten-thoms.de
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04663 seconds