aboutsummaryrefslogtreecommitdiff
path: root/core/test/com/google/inject/KeyTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'core/test/com/google/inject/KeyTest.java')
-rw-r--r--core/test/com/google/inject/KeyTest.java43
1 files changed, 43 insertions, 0 deletions
diff --git a/core/test/com/google/inject/KeyTest.java b/core/test/com/google/inject/KeyTest.java
index c0401e06..d9dd943f 100644
--- a/core/test/com/google/inject/KeyTest.java
+++ b/core/test/com/google/inject/KeyTest.java
@@ -19,10 +19,12 @@ package com.google.inject;
import static com.google.inject.Asserts.assertContains;
import static com.google.inject.Asserts.assertEqualsBothWays;
import static com.google.inject.Asserts.assertNotSerializable;
+import static com.google.inject.Asserts.awaitClear;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
+import com.google.inject.spi.Dependency;
import com.google.inject.util.Types;
import junit.framework.TestCase;
@@ -31,12 +33,15 @@ import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
+import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.concurrent.atomic.AtomicReference;
/**
* @author crazybob@google.com (Bob Lee)
@@ -275,4 +280,42 @@ public class KeyTest extends TestCase {
@Marker
class HasAnnotations {}
+ public void testAnonymousClassesDontHoldRefs() {
+ final AtomicReference<Provider<List<String>>> stringProvider =
+ new AtomicReference<Provider<List<String>>>();
+ final AtomicReference<Provider<List<Integer>>> intProvider =
+ new AtomicReference<Provider<List<Integer>>>();
+ final Object foo = new Object() {
+ @SuppressWarnings("unused") @Inject List<String> list;
+ };
+ Module module = new AbstractModule() {
+ @Override protected void configure() {
+ bind(new Key<List<String>>() {}).toInstance(new ArrayList<String>());
+ bind(new TypeLiteral<List<Integer>>() {}).toInstance(new ArrayList<Integer>());
+
+ stringProvider.set(getProvider(new Key<List<String>>() {}));
+ intProvider.set(binder().getProvider(Dependency.get(new Key<List<Integer>>() {})));
+
+ binder().requestInjection(new TypeLiteral<Object>() {}, foo);
+ }
+ };
+ WeakReference<Module> moduleRef = new WeakReference<Module>(module);
+ final Injector injector = Guice.createInjector(module);
+ module = null;
+ awaitClear(moduleRef); // Make sure anonymous keys & typeliterals don't hold the module.
+
+ Runnable runner = new Runnable() {
+ @Override public void run() {
+ injector.getInstance(new Key<Typed<String>>() {});
+ injector.getInstance(Key.get(new TypeLiteral<Typed<Integer>>() {}));
+ }
+ };
+ WeakReference<Runnable> runnerRef = new WeakReference<Runnable>(runner);
+ runner.run();
+ runner = null;
+ awaitClear(runnerRef); // also make sure anonymous keys & typeliterals don't hold for JITs
+ }
+
+ static class Typed<T> {}
+
}