Can I Inject x Class?

82 views
Skip to first unread message

Joe DeSantis

unread,
Jun 20, 2018, 4:45:44 PM6/20/18
to google-guice
I was working with a JUnit 5 extension for Guice, and we had the need to determine if a particular class was able to be injected by an injector.
The only solution we came up with is attempting to get an instance with that Injector. (Literally calling the getInstance method, in a try/catch block).
So my question is simply, what is the best way of determining if you can get an instance of a particular class given an injector.

The discussion on the implementation is here for anyone who is curious,

Thanks!

Stephan Classen

unread,
Jun 21, 2018, 2:51:29 AM6/21/18
to google...@googlegroups.com, Joe DeSantis

Jukito takes another approach. They use an ElementVisitor to collect all bindings and then decide if the required binding is in the module(s).
Have a look at the BindingsCollector.java class

https://github.com/ArcBees/Jukito/blob/master/jukito/src/main/java/org/jukito/BindingsCollector.java

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/dbbc0674-7e82-47de-97ee-6bdf9258d2fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luke Sandberg

unread,
Jun 21, 2018, 9:59:59 AM6/21/18
to google...@googlegroups.com, Joe DeSantis
Injector.getBinding is probably the right method

Joe DeSantis

unread,
Jun 21, 2018, 10:30:24 AM6/21/18
to google-guice
The method that was used prior was: injector.getExistingBinding(key)).isPresent()
It did accurately identify all explicit bindings, however it did not work with just-in-time bindings.
Reply all
Reply to author
Forward
0 new messages