Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » how to retrieve the Injector
how to retrieve the Injector [message #655053] Fri, 18 February 2011 07:33 Go to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1738
Registered: July 2009
Location: Firenze, Italy
Senior Member
Hi

what is the right way of retrieving the google guice injector of a DSL?

in junit tests you can do this like that

Injector injector =
new MyStandaloneSetup().createInjectorAndDoEMFRegistration();

but I wanted to retrieve it for junit PLUGIN tests, (I need to retrieve
IDocumentEditor, see my other post)... the documentation says

Injector injector = Guice.createInjector(new MyDslRuntimeModule());

but the ui module wants a plugin to be passed as an argument...

I noted that in the ui plugin you get a generated activator in src-gen,
in an ".internal" package... with that one I can retrieve the Injector:

Injector injector = MyDSLActivator.getInstance().getInjector(
"org.mydsl.MyDsl");

but is that the correct way? I'm asking because this way I need to
access an .internal package (which usually is not advised), and since I
need it in junit plugin test, I need to export that internal package in
the ui dsl plugin...

thus, in general, which is the right way to retrieve the Injector?

thanks in advance
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Re: how to retrieve the Injector [message #655325 is a reply to message #655053] Sat, 19 February 2011 12:17 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3098
Registered: July 2009
Senior Member
Hi Lorenzo,

> Injector injector = MyDSLActivator.getInstance().getInjector(
> "org.mydsl.MyDsl");
>
> but is that the correct way? I'm asking because this way I need to
> access an .internal package (which usually is not advised), and since I
> need it in junit plugin test, I need to export that internal package in
> the ui dsl plugin...
>
> thus, in general, which is the right way to retrieve the Injector?

Yes, it is. You may want to export the internal package only for the
test project by means of the x-friends directive:
http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/misc/bundle_manifest.html

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
Re: how to retrieve the Injector [message #656743 is a reply to message #655325] Mon, 28 February 2011 11:57 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1738
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 02/19/2011 01:17 PM, Sebastian Zarnekow wrote:
> Hi Lorenzo,
>
>> Injector injector = MyDSLActivator.getInstance().getInjector(
>> "org.mydsl.MyDsl");
>>
>> but is that the correct way? I'm asking because this way I need to
>> access an .internal package (which usually is not advised), and since I
>> need it in junit plugin test, I need to export that internal package in
>> the ui dsl plugin...
>>
>> thus, in general, which is the right way to retrieve the Injector?
>
> Yes, it is. You may want to export the internal package only for the
> test project by means of the x-friends directive:
> http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/misc/bundle_manifest.html

Thanks, I'll try that!

I noted that the way to retrieve the Injector from the ui plugin is not
documented in Xtext documentation, is it?

I think it would be good to document it, since it might not be clear...

I also have another question about retrieving the Injector: what is the
right way to retrieve the injector from the NON ui plugin?

thanks again
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net
Re: how to retrieve the Injector [message #656755 is a reply to message #656743] Mon, 28 February 2011 12:48 Go to previous messageGo to next message
Jan Koehnlein is currently offline Jan KoehnleinFriend
Messages: 752
Registered: July 2009
Location: Hamburg
Senior Member
Accessing the injector from the Activator is indeed not documented.
There are only a few occasions where you have to do that, e.g. when
reusing Xtext components in other frameworks. In any case you should
limit direct access to the injector to a minimum, as this is quite
against the paradigm of DI.

OTOH, using the search facility of the Eclipse documentation, I found 3
places in the docs where we describe how to obtain the Injector in the
non-Eclipse runtime scenario :-) Hint: Search "injector" or have a look
at your language's standalone setup.

Am 28.02.11 12:57, schrieb Lorenzo Bettini:
> On 02/19/2011 01:17 PM, Sebastian Zarnekow wrote:
>> Hi Lorenzo,
>>
>>> Injector injector = MyDSLActivator.getInstance().getInjector(
>>> "org.mydsl.MyDsl");
>>>
>>> but is that the correct way? I'm asking because this way I need to
>>> access an .internal package (which usually is not advised), and since I
>>> need it in junit plugin test, I need to export that internal package in
>>> the ui dsl plugin...
>>>
>>> thus, in general, which is the right way to retrieve the Injector?
>>
>> Yes, it is. You may want to export the internal package only for the
>> test project by means of the x-friends directive:
>> http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse. platform.doc.isv/reference/misc/bundle_manifest.html
>>
>
> Thanks, I'll try that!
>
> I noted that the way to retrieve the Injector from the ui plugin is not
> documented in Xtext documentation, is it?
>
> I think it would be good to document it, since it might not be clear...
>
> I also have another question about retrieving the Injector: what is the
> right way to retrieve the injector from the NON ui plugin?
>
> thanks again
> Lorenzo
>


--
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 retrieve the Injector [message #1730546 is a reply to message #656755] Tue, 26 April 2016 15:12 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
Hello

I've read this discussion and rihgt now my understanding is :
There is not legal way to get instance of injectot in non-ui plugin. Am I right?

Seems to me using MyDSLActivator in non-ui plugin is bad idea, because of in this case we will have cyclic dependency

Thanks
Alex
Re: how to retrieve the Injector [message #1730550 is a reply to message #1730546] Tue, 26 April 2016 15:41 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 11629
Registered: July 2009
Senior Member
can you explain your usecase?

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: how to retrieve the Injector [message #1730555 is a reply to message #1730550] Tue, 26 April 2016 16:04 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
I have a module
public class xxxxx_runtime_module extends AbstractModule {

    
    @Override
    public void configure() {
      
        install(new FactoryModuleBuilder()
                .implement(IXXX.class, Names.named(IXXX.XXX_NAME),() XXXSimulator.class)
                .implement(IXXX.class, Names.named(IXXX.YYY_NAME), YYYYSimulator.class)
                .implement(ISimulator.class, Names.named(IXXX.ZZZ_NAME),ZZZZSimulator.class)
                .build(i_simulator_factory.class));
    }
}

factory intreface is 
public interface i_simulator_factory {
    @Named(XXX_NAMEX) ISimulator create_xxx_simulator();
    @Named(XXX_NAME)Y ISimulator create_yyy_simulator();
    @Named(ZZZ_NAMEZ) ISimulator create_zzzl_simulator();
}


so in client i can write
simulator_factory = this._inject.getInstance(i_simulator_factory.class)

IXXX xxx = simulator_factory.create_xxx_simulator()
IXXX yyy = simulator_factory.create_yyy_simulator()

or make a loop over table with name of simulator


if I don't have instance of injector

I should write
@Inject
@Named("xxx-name")
IXXX xxx_simulator

etc.



So i would like to use generating of factory functionality


[Updated on: Tue, 26 April 2016 16:12]

Report message to a moderator

Re: how to retrieve the Injector [message #1730561 is a reply to message #1730555] Tue, 26 April 2016 16:11 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 11629
Registered: July 2009
Senior Member
you can do

@Inject
Injector injector;


Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: how to retrieve the Injector [message #1730644 is a reply to message #1730561] Wed, 27 April 2016 10:37 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
Thanks a lot Christian
Re: how to retrieve the Injector [message #1774736 is a reply to message #1730644] Thu, 19 October 2017 08:26 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
Seems to me this trick does not work in xtext-2.12 + neon. But was available in previous version (xtext-2.11)
Re: how to retrieve the Injector [message #1774737 is a reply to message #1774736] Thu, 19 October 2017 08:28 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 11629
Registered: July 2009
Senior Member
which trick

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: how to retrieve the Injector [message #1774738 is a reply to message #1774737] Thu, 19 October 2017 08:34 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
@Inject
Injector inject


I used to setup this in previous version of DSL that based on xtext 2.11
This code was inserted in MyDslGenerator that is my entry point

[Updated on: Thu, 19 October 2017 08:35]

Report message to a moderator

Re: how to retrieve the Injector [message #1774739 is a reply to message #1774738] Thu, 19 October 2017 08:42 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 11629
Registered: July 2009
Senior Member
and what are the symptoms ?

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: how to retrieve the Injector [message #1774740 is a reply to message #1774739] Thu, 19 October 2017 08:53 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
injector is null
Re: how to retrieve the Injector [message #1774743 is a reply to message #1774740] Thu, 19 October 2017 09:11 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 11629
Registered: July 2009
Senior Member
can you please tell how to reproduce?

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: how to retrieve the Injector [message #1774746 is a reply to message #1774743] Thu, 19 October 2017 09:27 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
1. Create MyDsl project
2. create class C1.java and its interface I1.java (something like hello world)
3. Update MyDslRunTimeModule . add something
like
 public void configureMyDsl(Binder binder) {
        binder.bind(I1.class).to(C1.class);
    }

4. in MyDslGenerator class add
@Inject
Injector inject


5. run check value of injector
Re: how to retrieve the Injector [message #1774747 is a reply to message #1774746] Thu, 19 October 2017 09:34 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 11629
Registered: July 2009
Senior Member
still cant reproduce

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: how to retrieve the Injector [message #1774751 is a reply to message #1774747] Thu, 19 October 2017 10:25 Go to previous messageGo to next message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
It is pity. I will try to investigate late too. In any case thanks for support

[Updated on: Thu, 19 October 2017 10:34]

Report message to a moderator

Re: how to retrieve the Injector [message #1774752 is a reply to message #1774751] Thu, 19 October 2017 10:34 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 11629
Registered: July 2009
Senior Member
if you can share sample project ...

Need professional support for Xtext, Xpand, EMF?
Go to: http://xtext.itemis.com
Twitter : @chrdietrich
Blog : christiandietrich.wordpress.com
Re: how to retrieve the Injector [message #1775235 is a reply to message #1774752] Thu, 26 October 2017 14:55 Go to previous message
Alex Gor is currently offline Alex GorFriend
Messages: 126
Registered: November 2014
Location: Russia
Senior Member
I found out the root cause of issue. It was my fault. Chain of injection was broken
Previous Topic:Xbase: TypeComputer for own generated Java Classes
Next Topic:Couldn't resolve reference to EnumElem 'bottle'
Goto Forum:
  


Current Time: Wed Jan 24 10:10:12 GMT 2018

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

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