fmgasparino / google-gin

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

Add support for <T> T inject(T injectee), <T> T get(Class<T> class) and auto injection for GWT.create(Class<T> classLiteral) #89

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Add support for <T> T inject(T injectee), <T> T get(Class<T> class) and 
auto injection for GWT.create(Class<T> classLiteral)

Hi,

    I made an extension for gin-1.0 and I think it can be useful for more 
people. Below I will provide a brief explanation of the proposed solution.

Problem: Create support for the generic methods "<T> T inject(T injectee)" 
and "<T> T get(Class<T> class)", making it unnecessary to create additional 
methods in Ginjector interfaces.

Proposed solution: Add inject method for each injectable type at compile 
time and use a visitor to redirect the generic inject method calls to the 
right typed ones.

I'm attaching an jar with the modifications I made. The jar is an extension 
to the gin-1.0.jar published in the gin page. Some classes were mirrored in 
order to make this extension, but all the hard work is still made by the 
original gin jar (needed to run this extension). This jar includes a 
generator for the extended Ginjector : GinjectorExtGenerator.

Other thing that this extension made possible is to inject instances via 
deferred binding (GWT.create), and that feature is already included in the 
extension. The marker interface for this 
com.google.gwt.inject.client.GwtCreateAutoInjected and the genartor is 
GwtCreateAutoInjectedGenerator.

For debuging a GeneratorContext decorator was included. If you use "context 
= new DebugGeneratorContext(context)" in any generator, it will print the 
generated classes to the console.

Use example:

inherit Module com.google.gwt.inject.InjectExt

// client classes are in com.google.gwt.inject.client package
// GinjectorExtDefault implements GinjectorExt, the extended marker 
ginjector interface

// inject method
CanInject injector = GWT.create(GinjectorExtDefault.class);
AnyInjectableType instance = new AnyInjectableType(); // GWT.create will 
also do
injector.inject(instance);

// get method
CanInject injector = GWT.create(GinjectorExtDefault.class);
AnyInjectableType instance = injector.inject(AnyInjectableType.class); // 
GWT.create will also do

// deferred binding, GwtCreateAutoInjected implements GwtCreateAutoInjected
// The deferred binding uses DefaultInjector.inject and DefaultInjector.get 
in order
// to do the injection
GwtCreateAutoInjectedType instance = 
GWT.create(GwtCreateAutoInjectedType.class); 

I hope it can be useful to you guys. 
Best regards,
Lucas Eustaquio

Original issue reported on code.google.com by lucas.eustaquio@gmail.com on 23 Mar 2010 at 4:49

Attachments:

GoogleCodeExporter commented 9 years ago
This issue is a enhancement, not a defect. And I forgot to say that the source 
code is 
fully included in the jar.

Original comment by lucas.eustaquio@gmail.com on 23 Mar 2010 at 5:01

GoogleCodeExporter commented 9 years ago
Hi Lucas, thanks for your interest in contributing to Gin! 

The reason why we didn't implement Gin in this way (which is really the same as
guice) is that it creates a huge overhead in code size: code needs to be 
generated
for *all* injectable types instead of just the ones that are used by the final 
code.
Since GWT is a lot about optimizing the compiled code, this is not an option.

Original comment by aragos on 23 Mar 2010 at 8:53

GoogleCodeExporter commented 9 years ago
Would it be possible to get the best of both worlds by using an annotation on 
the 
types that should be available from the injector? I find it somewhat of a 
burden to 
add a method in the injector for each of my injectable types. 

I didn't do this before, since I didn't have much need to use the injector 
directly 
(constructor injection and method injection were OK), but since I've started 
writing 
my own generator, I found it necessary to manually inject types through the 
injector. 
(This is discussed here: http://groups.google.com/group/google-web-
toolkit/browse_frm/thread/acde3be9181d1803) And now I have to maintain the 
injector 
in parallel with my injectable types.

Original comment by philippe.beaudoin on 25 Mar 2010 at 5:37

GoogleCodeExporter commented 9 years ago
If you want it, you can take the attached jar and make this modification 
yourself. you 
just need to change the method GinjectorExtUtils.getInjectableTypes. Then you 
should 
call DefaultInjector.inject in your generated code, therefore eliminating the 
need of 
maintaining your parallel injector. 

Original comment by lucas.eustaquio@gmail.com on 25 Mar 2010 at 8:20

GoogleCodeExporter commented 9 years ago
I gave a thought about aragos commentary, and I a believe that is way to do 
some 
workaround  about the code overhead. It could load asynchronously the injection 
code 
when it is needed based in the injected type dependecy tree. 

Original comment by lucas.eustaquio@gmail.com on 25 Mar 2010 at 8:30