how to retrieve the Injector [message #655053] |
Fri, 18 February 2011 07:33 |
|
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
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
|
Re: how to retrieve the Injector [message #656743 is a reply to message #655325] |
Mon, 28 February 2011 11:57 |
|
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
HOME: http://www.lorenzobettini.it
TDD Book: https://leanpub.com/tdd-buildautomation-ci
Xtext Book: https://www.packtpub.com/application-development/implementing-domain-specific-languages-xtext-and-xtend-second-edition
|
|
|
Re: how to retrieve the Injector [message #656755 is a reply to message #656743] |
Mon, 28 February 2011 12:48 |
Jan Koehnlein Messages: 760 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 |
|
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 #1730555 is a reply to message #1730550] |
Tue, 26 April 2016 16:04 |
|
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 #1774746 is a reply to message #1774743] |
Thu, 19 October 2017 09:27 |
|
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
5. run check value of injector
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02953 seconds