ankandutta1983 / google-gin

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

No @Inject or default constructor found for class com.google.gwt.event.shared.HandlerManager #64

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

EventBus.java

@Singleton
public class EventBus extends HandlerManager {

  @Inject
  public EventBus(){
    super(null);
  }

}

ClientModule.java

public class ClientModule extends AbstractGinModule {

  @Override
  protected void configure() {
    bind(EventBus.class).in(Singleton.class);
    ... or ...
    bind(EventBus.class);
    ... or ...
    <nothing>
  }

}

What is the expected output? What do you see instead?
I wish it would compile but I get the following:

No @Inject or default constructor found for class
com.google.gwt.event.shared.HandlerManager

What version of the product are you using? On what operating system?
Mac OS X v10.6.1
gin.jar built from source 5 minutes ago
GWT 1.7.1

-Tristan

Original issue reported on code.google.com by Tristan....@gmail.com on 8 Nov 2009 at 2:44

GoogleCodeExporter commented 8 years ago
Hi,

This looks like somewhere in your code, you try to inject an HandlerManager 
directly,
instead of injecting your EventBus.

For example, if you have a constructor somewhere like:
@Inject
public MyConstructor(HandlerManager manager, ...)

Gin will try to inject the HandlerManager class directly, which is not possible 
since
HandlerManager has no constructor defined with @Inject

If this is the case, there are two solutions:
a)Find all fields/methods/constructors injected with HandlerManager, and 
replace it
with EventBus

b) Bind HandlerManager to EventBus in your module:
bind(HandlerManager.class).to(EventBus.class).in(Singleton.class);

That way, when Gin needs to inject an HandlerManager, it will use the EventBus
subclass, which is indeed injectable...

Note: with the second solution, you might have scoping problems... Maybe you 
will end
up with 2 singletons (the first one for the HandlerManager binding, the second 
one
for the EventBus binding...).

Best regards,
-Etienne

Original comment by nev...@gmail.com on 8 Nov 2009 at 11:50

GoogleCodeExporter commented 8 years ago
Etienne,

Issue solved. Thank you for pointing out what probably seemed obvious. I was 
racking
my brain but the issue was exactly as you described. I was injecting 
HandlerManager
directly, and I was specifying

public HandlerManager getEventBus();

in my Ginjector.

After I changed everything to EventBus it worked like a charm.

All the best,
-Tristan

Original comment by Tristan....@gmail.com on 8 Nov 2009 at 4:15

GoogleCodeExporter commented 8 years ago
I think your bindings should have included the HandlerManager, after all, 
that's what
Guice/Gin is about: :)

bind(HandlerManager.class).to(EventBus.class).in(Singleton.class);

Original comment by aragos on 9 Nov 2009 at 10:32

GoogleCodeExporter commented 8 years ago
@Tristan:
No problems, I'm glad I could help :) We all make mistakes!

@Aragos:
Even better, I think the "EventBus" should be an interface (not a subclass of
HandlerManager), with a "DefaultEventBus" that extends HandlerManager and 
implements
this EventBus interface.

Then
bind(EventBus.class).to(DefaultEventBus.class).in(Singleton.class);

Then, in the code, inject the EventBus interface everywhere. That way you 
abstract
out all implementation details, and you may switch out the HandlerManager
"implementation" for another one if needed... And you may now mock your 
EventBus more
easily in your tests :)

Original comment by nev...@gmail.com on 10 Nov 2009 at 10:55