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 <http://google.github.io/guice/api-docs/latest/javadoc/index.html?com/google/inject/spi/ProvisionListener.html> and some knowledge of your object graph and capture instances as they're created.
sam On Fri, Oct 10, 2014 at 9:48 AM, David Leonhartsberger <[email protected]> wrote: > 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. > > -- > 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 [email protected]. > To post to this group, send email to [email protected]. > 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 > <https://groups.google.com/d/msgid/google-guice/9d7440eb-d99c-4853-a499-f466b85282fd%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- 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 [email protected]. To post to this group, send email to [email protected]. 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/CAJEBNUc9OM-hKwafoCJC7n5y81S-_ZSWuD9dkSu2NCp8vaJ3Lg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
