Skip to content

Commit

Permalink
Avoid referring to internal Guice classes
Browse files Browse the repository at this point in the history
In Guice 4.2, guice-multibindings was merged into guice core, and in Guice 5, multibindings was deleted. When trying to upgrade to Guice5, I tried explicitly removing the multibindings dep as it is not rolled in, but some internal classes are no longer exposed.

In order to ease upgrading, Do a runtime check of the CircularDependency class, which was removed in Guice 5 ( https://github.com/google/guice/pull/1298/files ).
  • Loading branch information
carl-mastrangelo committed Apr 10, 2021
1 parent ffaa7e8 commit da7f0b6
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
import com.google.inject.internal.CircularDependencyProxy;
import com.netflix.governator.internal.AbstractScope;

import java.util.Map;
Expand All @@ -35,6 +34,19 @@ class FineGrainedLazySingletonScopeImpl extends AbstractScope
private static final Object NULL = new Object();
private static final Map<Key<?>, LockRecord> locks = Maps.newHashMap();

private static final Class<?> circularProxyClazz;

static {
Class<?> clz;
try {
clz = Class.forName("com.google.inject.internal.CircularDependencyProxy");
} catch (ClassNotFoundException e) {
clz = null;
// discard exception.
}
circularProxyClazz = clz;
}

private static class LockRecord
{
private final Object lock = new Object();
Expand Down Expand Up @@ -68,7 +80,8 @@ public T get()
T provided = creator.get();

// don't remember proxies; these exist only to serve circular dependencies
if ( provided instanceof CircularDependencyProxy )
// Don't do an instanceof check to avoid referencing Guice internal classes.
if (circularProxyClazz != null && circularProxyClazz.isInstance(provided))
{
return provided;
}
Expand Down

0 comments on commit da7f0b6

Please sign in to comment.