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