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 toInstance() injections #82

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Add support for toInstance() injections.

Proposed Solution: Add lite runtime emulation in the package
com.google.inject.client.impl, implementing the Ginjector interface to
store the instances being injected, and invoke the GinModule.configure()
methods. Each instance should be bound as EAGER_SINGLETON. If no runtime
support is needed, the generated code should be the same as classic GIN.
There are two possible implementations to store the instances:

-1 Indexed by order of injection (array of instances):
Instances are stored in an array in the same order that are injected. A
ToInstanceBinding, generates a creator method that returns the instance by
index:

* toInstance() implementation:
void toInstance(T instance){
  instances[instanceCount++] = instance;
}

* creator:
private Foo create_foo(){
  return instances[10];
}

PRO: Good performance, could be optimized by enhanced constant folding and
propagation.
CON: Order of injection is not deterministic.

-2 Indexed by Key.toString() ( Map<String, Object> ): This adds a more
complex runtime support, where each TypeLiteral.toString() must be emulated
in runtime to enable Key.toString() for indexation of toInstance():

* toInstance() implementation:
void toInstance(T instance){
  instances.put(key.toString());
}

* creator:
private Foo create_foo(){
  return instances.get("Key[type=com.google.gwt.inject.client.test.Foo,
annotation=[none]]");
}

PRO: Deterministic injection.
CON: Performance impact, mantaining maps and support Key.toString()

A patch for the solution 1 is attached.

Original issue reported on code.google.com by andres.a...@gmail.com on 21 Feb 2010 at 3:08

Attachments:

GoogleCodeExporter commented 9 years ago
Thanks for writing this patch! I'll try to do the code review soon and will 
leave
some comments there.

Original comment by aragos on 24 Feb 2010 at 6:21

GoogleCodeExporter commented 9 years ago
Since the review doesn't seem to go anywhere right now, I'll set this back to 
"accepted" until we come up with a working idea. :)

Original comment by aragos on 7 Jul 2010 at 4:13

GoogleCodeExporter commented 9 years ago
Hi,
are there any plans to support toInstance() in future? How should this feature 
be implemented? I'll try to do the thing if you can give some hints. The 
proposed patch seems not to be a valid solution?!

Original comment by Sebastia...@googlemail.com on 8 Mar 2015 at 7:20