Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [SOLVED] How to access to "com.google.inject.Injector.Injector" from JavaValidator(Get register instance of injector for bindings within the class JavaValidator)
[SOLVED] How to access to "com.google.inject.Injector.Injector" from JavaValidator [message #639234] Mon, 15 November 2010 17:16 Go to next message
Ariel Santana is currently offline Ariel SantanaFriend
Messages: 11
Registered: April 2010
Location: Buenos Aires, Argentina
Junior Member
Hello
I presented the following problem when running the workflow XXXGenerator.mwe2, I access the "injector" (com.google.inject.Injector) from GUIDSLJavaValidator class to instantiate a concrete class (using the binding), but in all cases returns me null.

On the other side tried to use the method createInjectorAndDoEMFRegistration (), but when you register, all modules are reloaded, so runs recursively until throws StackOverflowException (enter in infinite loop).

Thanks.

public class GUIDSLJavaValidator extends AbstractGUIDSLJavaValidator {
	@Inject
	private Injector injector = null;
	@Inject
	private ISentence _sentence;

	public GUIDSLJavaValidator() {
		injector = super.getInjector(); // return null

		injector = new GUIDSLStandaloneSetupGenerated()
				.createInjectorAndDoEMFRegistration(); //enter in infinite loop

		_sentence = injector.getInstance(ISentence.class); // never execute


public class GUIDSLRuntimeModule extends
		unlp.info.guidsl.AbstractGUIDSLRuntimeModule {

	@Override
	public void configure(Binder binder) {

		super.configure(binder);
		binder.bind(ISentence.class).to(CSharpSentence.class);

[Updated on: Wed, 17 November 2010 15:33]

Report message to a moderator

Re: How to access to "com.google.inject.Injector.Injector" from JavaValidator [message #639372 is a reply to message #639234] Tue, 16 November 2010 10:06 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 760
Registered: July 2009
Location: Hamburg
Senior Member
In general, you should never try to access the Injector directly. The
Java validator is usually created by Guice already, so all you have to
do to get other language specific things it to have them injected, e.g.

public class GUIDSLJavaValidator extends AbstractGUIDSLJavaValidator {
@Inject
private ISentence _sentence;
...

If _sentence stays null, this either means you haven't bound any
implementation to the interface ISentence or the GUIDSLJavaValidator has
not been created by Guice but by an explicit constructor call.

Am 15.11.10 18:16, schrieb Ariel Santana:
> Hello
> I presented the following problem when running the workflow
> XXXGenerator.mwe2, I access the "injector" (com.google.inject.Injector)
> from GUIDSLJavaValidator class to instantiate a concrete class (using
> the binding), but in all cases returns me null.
>
> On the other side tried to use the method
> createInjectorAndDoEMFRegistration (), but when you register, all
> modules are reloaded, so runs recursively until throws
> StackOverflowException (enter in infinite loop).
>
> Thanks.
>
>
> public class GUIDSLJavaValidator extends AbstractGUIDSLJavaValidator {
> @Inject
> private Injector injector = null;
> @Inject
> private ISentence _sentence;
>
> public GUIDSLJavaValidator() {
> injector = super.getInjector(); // return null
>
> injector = new GUIDSLStandaloneSetupGenerated()
> .createInjectorAndDoEMFRegistration(); //enter in infinite loop
>
> _sentence = injector.getInstance(ISentence.class); // never execute
>
>
>
> public class GUIDSLRuntimeModule extends
> unlp.info.guidsl.AbstractGUIDSLRuntimeModule {
>
> @Override
> public void configure(Binder binder) {
>
> super.configure(binder);
> binder.bind(ISentence.class).to(CSharpSentence.class);
>


--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com


---
Get professional support from the Xtext committers at www.typefox.io
Re: How to access to "com.google.inject.Injector.Injector" from JavaValidator [message #639424 is a reply to message #639372] Tue, 16 November 2010 13:58 Go to previous messageGo to next message
Ariel Santana is currently offline Ariel SantanaFriend
Messages: 11
Registered: April 2010
Location: Buenos Aires, Argentina
Junior Member
Jan Koehnlein wrote on Tue, 16 November 2010 08:06

...
public class GUIDSLJavaValidator extends AbstractGUIDSLJavaValidator {
@Inject
private ISentence _sentence;
...

If _sentence stays null, this either means you haven't bound any
implementation to the interface ISentence or the GUIDSLJavaValidator has
not been created by Guice but by an explicit constructor call.



Hi Jan, thanks for responding.

I mention that I followed your example, but the variable _sentence is not initialized automatically (has the value null).

Alternatives as you give, my case is probably the second option (you GUIDSLJavaValidator
Not been created by Guice But by an explicit constructor call
).

In this case, as I can do about it?

As, for example, if I using JAVA calls from templates, the method Guice.createInjector works perfectly:

Test.xpt
«DEFINE expAction FOR Action»
    Expand action: «expandAction(this)» // call extension


Extension.ext
import unlp::info::guidsl::gUIDSL;

String expandAction(Action action) :
	JAVA unlp.info.guidsl.core.sentence.Sentence.toString(					unlp.info.guidsl.gUIDSL.Action); // call java method in Sentence class


Sentence.java
public class Sentence {

	@Inject
	private ISentence _sentence;

	public Sentence() {
		Injector injector = Guice.createInjector(new GUIDSLRuntimeModule()); // works fine
		_sentence = injector.getInstance(ISentence.class); // works fine
	}

	public String toString(Action action) {
		return _sentence.toString(action); //work fine
	}
Re: How to access to "com.google.inject.Injector.Injector" from JavaValidator [message #639550 is a reply to message #639424] Tue, 16 November 2010 20:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

following works fine for me:

package org.xtext.example.mydsl.validation;

public interface IHelper {
	
	public boolean help();

}


package org.xtext.example.mydsl.validation;

public class Helper implements IHelper {

	@Override
	public boolean help() {
		return true;
	}

}


package org.xtext.example.mydsl.validation;

import org.eclipse.xtext.validation.Check;
import org.xtext.example.mydsl.myDsl.Greeting;
import org.xtext.example.mydsl.myDsl.MyDslPackage;

import com.google.inject.Inject;
 

public class MyDslJavaValidator extends AbstractMyDslJavaValidator {
	
	@Inject
	private IHelper helper;

	@Check
	public void checkGreetingStartsWithCapital(Greeting greeting) {
		if (helper.help()) {
			error("Helper returned true", MyDslPackage.GREETING__NAME);
		}
	}

}


package org.xtext.example.mydsl;

import org.xtext.example.mydsl.validation.Helper;
import org.xtext.example.mydsl.validation.IHelper;

import com.google.inject.Binder;

/**
 * Use this class to register components to be used at runtime / without the Equinox extension registry.
 */
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {

	@Override
	public void configure(Binder binder) {

		super.configure(binder);
		binder.bind(IHelper.class).to(Helper.class);
	}
	
}


~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[SOLVED] Re: How to access to "com.google.inject.Injector.Injector" from JavaValidator [message #639571 is a reply to message #639550] Tue, 16 November 2010 23:09 Go to previous message
Ariel Santana is currently offline Ariel SantanaFriend
Messages: 11
Registered: April 2010
Location: Buenos Aires, Argentina
Junior Member
Christian Dietrich wrote on Tue, 16 November 2010 18:58
Hi,

following works fine for me:
...

~Christian



Hi Christian,
I tried your example and it worked perfectly.
And I realized that my example will also work, in the methods @Check, the variable "_sentence" is initialized. The problem is that I evaluated the variable inside the constructor method, if there was null.

Thank you very much everybody. Razz
Previous Topic:ResourceDescriptions in Validator
Next Topic:"Hidden" model elements
Goto Forum:
  


Current Time: Wed Apr 24 15:57:26 GMT 2024

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

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

Back to the top