aspakh / google-gin

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

@Inject( optional = true) doesn't work with method injection #70

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. add to a GIN-managed class a method like:
@Inject(optional = true) void injectA(A object){ }
, where A is a class not configured anywhere in GIN modules

2. request this object from GIN

What is the expected output? What do you see instead?
Expected no change in behaviour - object of this class is not available,
but injection is optional.

Instead the result is an error:
[ERROR] Deferred binding result type 'A' should not be abstract

Original issue reported on code.google.com by bochensk...@gmail.com on 7 Dec 2009 at 1:42

GoogleCodeExporter commented 8 years ago
You're running into a subtle but intended behavior here: Gin tries to create 
your
type by calling GWT.create() instead of failing for @Optional. 

I assume that A is an interface in your case. Unfortunately, Gin (currently) 
cannot
tell whether there is a GWT binding for A and so falls back to trying that GWT
binding before taking @Optional into account. 

Original comment by aragos on 8 Dec 2009 at 2:21

GoogleCodeExporter commented 8 years ago
If it is intended that optional method injection should not be supported, then 
what is the recommended method for getting optional injections?

Original comment by smbec...@gmail.com on 25 Aug 2010 at 7:41

GoogleCodeExporter commented 8 years ago
Optional injection on methods is supported. It will break in the case where you 
inject a type with default constructor (interface or class) and Gin attempts to 
call GWT.create on your type.

Original comment by aragos on 25 Aug 2010 at 7:55

GoogleCodeExporter commented 8 years ago
Then I don't understand why this was closed out. Shouldn't that be a supported 
scenario? All of the types that I am injecting are interfaces as I suspect is 
the case for many other people that are using a dependency injection pattern.

Original comment by smbec...@gmail.com on 26 Aug 2010 at 4:14

GoogleCodeExporter commented 8 years ago
Indeed, why was this closed? Seems like a valid use case. Optional injection 
simply doesn't work for interfaces or classes with default constructors. 
Perhaps we need a new annotation to suppress GIN's "magic" deferred binding 
call?

Original comment by mcart...@gmail.com on 14 Aug 2012 at 3:05

GoogleCodeExporter commented 8 years ago
I'm reopening the issue so we can look further into it. I am not sure how 
complicated it would be to enable the suppression of Gin's magic but I bet 
there's a way to do it.

Original comment by aragos on 21 Aug 2012 at 2:38