fmgasparino / google-gin

Automatically exported from code.google.com/p/google-gin
Apache License 2.0
0 stars 0 forks source link

Feature: Allow classes created by a Generator to participate in dependency injection. #95

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Right now, there doesn't seem to be an easy way for a user-made Generator to 
output a class that 
participates in dependency injection. I've been using the workaround proposed 
here:
http://groups.google.com/group/google-web-
toolkit/browse_frm/thread/acde3be9181d1803/d3e00ec35ac03a0b

To summarize this workaround the idea is to:
- Add a static instance of the ginjector within your custom ginjector interface 
;
- Add a configuration property to the .gwt.xml module pointing to this 
ginjector interface ;
- In the ginjector interface, define methods for all injectable types that need 
to be used by 
the generated class ;
- In the generated class, access the static injector instance and get the 
injectable types.

This is quite a bit of instrumentation. Moreover, it doesn't work if the 
generated class is 
bound .asEagerSingleton() because the static ginjector instance is not yet 
available when the 
generated class' constructor is called.

What I'd really like is for my generated class to be be non-default 
constructible and to use 
@Inject annotations. Some "GinGenerator" could then intercept this class, wrap 
it in a default-
constructible class performing the injection, and return this as the result of 
the generate() 
class.

Original issue reported on code.google.com by philippe.beaudoin on 26 Mar 2010 at 6:34

GoogleCodeExporter commented 9 years ago
important issue

Original comment by kudlaty....@gmail.com on 12 Apr 2010 at 11:40

GoogleCodeExporter commented 9 years ago
This is a pretty complicated issue and comes up in all kinds of places. Ideally 
we
want all generated classes to be available to Gin (and the underlying Guice) at
compile time. The main block here is that the generated classes never got 
compiled to
bytecode but live only in the GWT-internal AST.

To get around the block and use the generated classes just like regular 
classes, I'd
propose using a custom classloader or to change Guice to be able to employ 
mechanisms
other than reflection to inspect classes. Both are possible but quite 
complicated.

AFAIK GWT already uses a special classloader to do similar things for dev mode 
so
maybe we can go from there.

Original comment by aragos on 12 Apr 2010 at 9:20

GoogleCodeExporter commented 9 years ago
Thanks for your answer, when I have some time I'll look into the custom 
classloader 
approach and see if I can come up with something.

Original comment by philippe.beaudoin on 12 Apr 2010 at 9:27

GoogleCodeExporter commented 9 years ago
See discussion at http://groups.google.com/group/google-gin/t/7dd7382be803e00b

Original comment by t.broyer on 21 Nov 2010 at 9:08

GoogleCodeExporter commented 9 years ago
I used a different work-around.  

1. in your interface for you generated class create an inner class with static 
injected member providers.  
2. use requestStaticInjection(YourClass.YourInnerClass.class); in your module
2. in your generated class, create an instance of the inner class and call some 
init function with your class ref as a parameter
3. in the init function of your inner class, set the class members

I preferred this method because I didn't have to modify my Ginjector for each 
class which wants @Inject members

Original comment by gamma...@gmail.com on 19 Oct 2012 at 12:32

GoogleCodeExporter commented 9 years ago
Hi guys. Thanks for great DI Engine for gwt.

I've reviewed the discussion threads and codereview issues (links from thread) 
related to this concrete issue. A long time (the year and the half) there was 
no actions around this issues, so I'm just wondering if there are any plans for 
it? 
Huge work was done for Private Modules and architecture itself and that's was a 
very complex thing. Hope now involving generated classes into the DI context 
will be easier to implement.
Cheers and best regards.

Original comment by Nikolaz...@gmail.com on 24 Oct 2012 at 6:57

GoogleCodeExporter commented 9 years ago
Thanks for your interest! While this remains one of the largest feature 
requests for Gin no one so far has made large inroads into it and knowing my 
own schedule for next few months I will not be working on it either.

Original comment by aragos on 27 Oct 2012 at 6:44

GoogleCodeExporter commented 9 years ago
I wrote a project called GinJitsu which allows you to add generators to your 
GIN context

http://gammagec.dlinkddns.com/node/25

Original comment by gamma...@gmail.com on 29 Oct 2012 at 11:06