Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Bug in Mwe2ExecutionEngine?(Use Guice for element creation)
Bug in Mwe2ExecutionEngine? [message #715033] Fri, 12 August 2011 07:43 Go to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Hi.

I just wanted to implement my code generator and an according MWE2 workflow. In my workflow I want to allow to specify which IGenerator implementation should be used and created a custom workflow component with a IGenerator-getter/setter.

But the Mwe2Execution engine does not use Guice to create new instances of the declared classes within a mwe file. Therefore I cannot use any injected components within my IGenerator.

In the Mwe2ExecutionEngine:144 I found this piece of code:
	protected Object create(JvmType jvmType) {
		Class<?> class1 = reflectAccess.getRawType(jvmType);
		if (class1==null)
			throw new IllegalStateException("Couldn't find java.lang.Class for name '"+jvmType.getIdentifier()+"'");
		try {
			return class1.newInstance();
		} catch (Exception e) {
			throw new WrappedException(e);
		}
	}


Shouldn't the class1.newInstance() be replaced with injector.newInstance(class1)? I need to use injected instances within my in-workflow declared instances. The Mwe2ExecutionEngine is injected within the Mwe2Runner. Is there a possibility to inject an own Mwe2ExecutionEngine into the Mwe2Runner at execution time?

Cheers
Daniel
Re: Bug in Mwe2ExecutionEngine? [message #715035 is a reply to message #715033] Fri, 12 August 2011 07:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Ähm,

the intended use is that you give the (std) generator component an ISetup and it initializes itself with the setups injector
(register=bla.BlubbStandloneSetup{})

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 12 August 2011 07:47]

Report message to a moderator

Re: Bug in Mwe2ExecutionEngine? [message #715042 is a reply to message #715035] Fri, 12 August 2011 08:14 Go to previous messageGo to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Quite true, I just recognized that I need to use my own injector rather than use the injector of the Mwe Context.

My workflow looks like this:
 
JavaCompiler {  // JavaCompiler extends Workflow
    // component = Reader { slot = "model" ...}
    // component = CompilerComponent 
    // CompilerComponent extends GeneratorComponent
	// -> sets the setup and injector 
     
    // Reader.modelPath 
	modelPath = "src"
    // CompilerComponent.outlet.path
	targetDirectory = "src-java" 
    // CompilerComponent.template
	template = JavaTemplate {} // implements IGenerator
	   
    // CompilerComponent.settings
	settings = {
		javaPackage = "at.bachmann.smi"
	}
}


I want to let the injector of the CompilerComponent create my JavaTemplate instance where I need some injected stuff. I have built a workaround by specifying a full qualified class-name as string for the template. The MWE2 grammar does not allow to assign Class<?> references and something like template = at.mydsl.compiler.java.JavaTemplate would lead to an unresolvable reference. Is there a more clean way to specify a sub-component to be created by my injector?

I think of this solution: A special workflow component which can provide an injector. The Mwe2ExecutionEngine checks whether the current component provides an injector and uses it for any instance to be created within this component.

// IWorkflowComponentWithInjector.java
public interface IWorkflowComponentWithInjector extends IWorkflowComponent {
    public Injector getInjector();
}

// CompilerComponent.java
public class CompilerComponent implements IWorkflowComponentWithInjector {
    private Injector injector;
    public void setRegister(ISetup setup) { injector = setup.createInjectorAndDoEMFRegistration(); } 
    public Injector getInjector() { return injector; }
}

// CompileToJava.mwe2
Workflow {
    component = CompilerComponent {
        template = MyTemplate {} // gets created by the injector of CompilerComponent
    }
}

// Mwe2ExecutionEngine
private Injector currentComponentInjector;
...
protected Object create(JvmType jvmType) {
    ...
    if(currentComponentInjector != null) {
        return currentComponentInjector.getInstance(...);
    }
    else {
      ...
    } 
    ...
}


[Updated on: Fri, 12 August 2011 08:23]

Report message to a moderator

Re: Bug in Mwe2ExecutionEngine? [message #715046 is a reply to message #715042] Fri, 12 August 2011 08:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

i guess this is currently not possible. otherwise xtext won't do stuff like

fragment = validation.JavaValidatorFragment {
composedCheck = "org.eclipse.xtext.validation.ImportUriValidator"
composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}

feel free to file an enhancement request against mwe2 to be capable of "Class Literals" too

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Fri, 12 August 2011 08:23]

Report message to a moderator

Re: Bug in Mwe2ExecutionEngine? [message #715057 is a reply to message #715046] Fri, 12 August 2011 08:50 Go to previous message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
I filed a bug here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=354591
Shouldn't be too much work to add this feature.

I think I will file a feature request for the "injection-scope" too. I like the idea of specifying which injector to use for instance creation in within the current and child components.

[Edit]
I found another workaround that works.

In my CompilerComponent I simply inject the members to the existing object.
Awesome that Guice can inject members even into objects that are already existing.
...
private CompilerTemplate template;
public CompilerTemplate getTemplate() {return template;}
public void setTemplate(CompilerTemplate template) { this.template = template; }
...
public void preInvoke() {
    ...
    injector.injectMembers(template);
    super.preInvoke();
}

[Updated on: Fri, 12 August 2011 09:36]

Report message to a moderator

Previous Topic:How cross-reference is found by Xtext?
Next Topic:Access a feature in a Xtext Model
Goto Forum:
  


Current Time: Fri Apr 26 14:09:37 GMT 2024

Powered by FUDForum. Page generated in 0.03226 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top