Is there something like getInstaceOnlyIfCreated on the Injector?

32 views
Skip to first unread message

David Leonhartsberger

unread,
Oct 10, 2014, 9:48:43 AM10/10/14
to google...@googlegroups.com
I was wondering If I can check on the Injector if an instance of some class/interface was already or 
have a method that does only return the instance if it was already created.

Why I need this:
In the shutdown of my integration tests I clean up all instances in the Service that I am testing.
In most of the tests however some dependencies (TimeoutManager)  are never instaniated but will now in the shutdown code only to destroy them right afterwards, this takes some time and causes exceptions and a lot of confusing output in the test log.


     /**
     * Called by junit after each integration test.
     */
    @After
    public final void afterIntegrationTest() {
        
     if (service != null) {
         service.destroy();
     }
            
           // more shutdow
    }

private void tryCloseInstances() {
               // current code 
        TimeoutManager timeoutManager = injector.getInstance(TimeoutManager.class);
        AutoCloseables.tryClose(timeoutManager);

              // what i would like to do               
          TimeoutManager timeoutManager = injector.getInstanceOnlyIfCreated(TimeoutManager.class);
          AutoCloseables.tryCloseIfNotNull(timeoutManager);
    }

Br
David L. 

Sam Berlin

unread,
Oct 10, 2014, 9:51:32 AM10/10/14
to google...@googlegroups.com
There's no way to do this in the injector, because "creation" of an object depends on the scope.  However, you could build something using a ProvisionListener and some knowledge of your object graph and capture instances as they're created.

sam

--
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 http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/9d7440eb-d99c-4853-a499-f466b85282fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nate Bauernfeind

unread,
Oct 10, 2014, 9:59:44 AM10/10/14
to google...@googlegroups.com
In my opinion, you have a few options:
1) You can create a class (and inject it) that optionally injects all of the things you might shutdown, a la something like this:
class TestShutdownHelper {
@Inject(optional = true) public TimeoutManager timeoutManager = null;
...
 
public void cleanUp() {
AutoCloseables.tryCloseIfNotNull(timeoutManager);
// ...
}
}

2) Or you could change the way that you do injection for things you want to be able to close and inject them with OptionalBinder:

You could then pull out Optional<TimeoutManager> instead (this is likely to be lots more overhead esp. if you're doing it only for tests).

3) And my last suggestion would be to use MulitBinder and have bind each of your Closeable classes to a Set<Closeable> -- then pull out the set from the injector, iterate, and close each bound instance.

Nate


On Fri, Oct 10, 2014 at 9:48 AM, David Leonhartsberger <leoz...@gmail.com> wrote:

--
Reply all
Reply to author
Forward
0 new messages