From ec9c6be9b26391a09a8135b65e553b6d6d2f541e Mon Sep 17 00:00:00 2001 From: Christian Edward Gruber Date: Fri, 14 Jun 2013 18:15:54 -0700 Subject: [PATCH 1/5] Remove reflective fallback for modules (and provides methods), and clean up the attendant code. --- core/src/main/java/dagger/ObjectGraph.java | 5 ++ .../java/dagger/internal/FailoverLoader.java | 46 ++++++------ .../src/main/java/dagger/internal/Loader.java | 3 +- .../internal/loaders/GeneratedAdapters.java | 18 ++--- core/src/test/java/dagger/ExtensionTest.java | 8 +- .../java/dagger/ExtensionWithStateTest.java | 3 +- .../test/java/dagger/InjectStaticsTest.java | 7 +- .../test/java/dagger/InjectionOfLazyTest.java | 3 +- core/src/test/java/dagger/InjectionTest.java | 75 ++++++++++--------- .../test/java/dagger/LazyInjectionTest.java | 11 +-- .../test/java/dagger/MembersInjectorTest.java | 17 +++-- core/src/test/java/dagger/ModuleTest.java | 31 ++++---- .../test/java/dagger/ProblemDetectorTest.java | 7 +- core/src/test/java/dagger/SetBindingTest.java | 7 +- .../test/java/dagger/ThreadSafetyTest.java | 7 +- .../test/java/dagger/UnusedProviderTest.java | 9 ++- .../internal/TestOnlyModuleAdapter.java} | 17 ++--- .../java/dagger/internal/TestingLoader.java | 51 +++++++++++++ 18 files changed, 197 insertions(+), 128 deletions(-) rename core/src/{main/java/dagger/internal/loaders/ReflectiveModuleAdapter.java => test/java/dagger/internal/TestOnlyModuleAdapter.java} (92%) create mode 100644 core/src/test/java/dagger/internal/TestingLoader.java diff --git a/core/src/main/java/dagger/ObjectGraph.java b/core/src/main/java/dagger/ObjectGraph.java index 9c6214080e1..1fe64b40767 100644 --- a/core/src/main/java/dagger/ObjectGraph.java +++ b/core/src/main/java/dagger/ObjectGraph.java @@ -126,6 +126,11 @@ public static ObjectGraph create(Object... modules) { return DaggerObjectGraph.makeGraph(null, new FailoverLoader(), modules); } + // visible for testing + static ObjectGraph createWith(Loader loader, Object... modules) { + return DaggerObjectGraph.makeGraph(null, loader, modules); + } + static class DaggerObjectGraph extends ObjectGraph { private final DaggerObjectGraph base; private final Linker linker; diff --git a/core/src/main/java/dagger/internal/FailoverLoader.java b/core/src/main/java/dagger/internal/FailoverLoader.java index 674b2595350..877fb0d2002 100644 --- a/core/src/main/java/dagger/internal/FailoverLoader.java +++ b/core/src/main/java/dagger/internal/FailoverLoader.java @@ -19,7 +19,6 @@ import dagger.internal.loaders.GeneratedAdapters; import dagger.internal.loaders.ReflectiveAtInjectBinding; -import dagger.internal.loaders.ReflectiveModuleAdapter; import dagger.internal.loaders.ReflectiveStaticInjection; /** @@ -33,37 +32,40 @@ public final class FailoverLoader implements Loader { * Obtains a module adapter for {@code module} from the first responding resolver. */ @Override public ModuleAdapter getModuleAdapter(Class type, T instance) { - ModuleAdapter result = GeneratedAdapters.initModuleAdapter(type); - if (result == null) { - result = ReflectiveModuleAdapter.create(type); + try { + ModuleAdapter result = GeneratedAdapters.initModuleAdapter(type); + result.module = (instance != null) ? instance : result.newModule(); + return result; + } catch (ClassNotFoundException e) { + throw new TypeNotPresentException(type + GeneratedAdapters.MODULE_ADAPTER_SUFFIX, e); } - result.module = (instance != null) ? instance : result.newModule(); - return result; } @Override public Binding getAtInjectBinding(String key, String className, ClassLoader classLoader, boolean mustHaveInjections) { - try { - Binding result = GeneratedAdapters.initInjectAdapter(className, classLoader); - if (result == null) { - // A null classloader is the system classloader. - classLoader = (classLoader != null) ? classLoader : ClassLoader.getSystemClassLoader(); - Class c = classLoader.loadClass(className); - if (!c.isInterface()) { - result = ReflectiveAtInjectBinding.create(c, mustHaveInjections); + try { + return GeneratedAdapters.initInjectAdapter(className, classLoader); + } catch (ClassNotFoundException ignored) { + try { + // A null classloader is the system classloader. + classLoader = (classLoader != null) ? classLoader : ClassLoader.getSystemClassLoader(); + Class type = classLoader.loadClass(className); + if (!type.isInterface()) { + return null; // Short-circuit since we can't build reflective bindings for interfaces. + } + return ReflectiveAtInjectBinding.create(type, mustHaveInjections); + } catch (ClassNotFoundException e) { + throw new TypeNotPresentException( + String.format("Could not find %s needed for binding %s", className, key), e); } } - return result; - } catch (ClassNotFoundException e) { - throw new RuntimeException("Could not find " + className + " needed for binding " + key, e); - } } @Override public StaticInjection getStaticInjection(Class injectedClass) { - StaticInjection result = GeneratedAdapters.initStaticInjection(injectedClass); - if (result == null) { - result = ReflectiveStaticInjection.create(injectedClass); + try { + return GeneratedAdapters.initStaticInjection(injectedClass); + } catch (ClassNotFoundException ignored) { + return ReflectiveStaticInjection.create(injectedClass); } - return result; } } diff --git a/core/src/main/java/dagger/internal/Loader.java b/core/src/main/java/dagger/internal/Loader.java index c2016a5e106..1d2d10f2d6f 100644 --- a/core/src/main/java/dagger/internal/Loader.java +++ b/core/src/main/java/dagger/internal/Loader.java @@ -30,7 +30,8 @@ Binding getAtInjectBinding( String key, String className, ClassLoader classLoader, boolean mustHaveInjections); /** - * Returns a module adapter for {@code module}. + * Returns a module adapter for {@code module} or throws a {@code TypeNotPresentException} if + * none can be found. */ ModuleAdapter getModuleAdapter(Class moduleClass, T module); diff --git a/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java b/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java index 2682da0f6fa..0907ad0cc34 100644 --- a/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java +++ b/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java @@ -21,7 +21,6 @@ import dagger.internal.StaticInjection; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; -import java.util.logging.Level; import java.util.logging.Logger; /** @@ -36,20 +35,24 @@ public final class GeneratedAdapters { private GeneratedAdapters() { } - public static ModuleAdapter initModuleAdapter(Class moduleClass) { + public static ModuleAdapter initModuleAdapter(Class moduleClass) + throws ClassNotFoundException { return instantiate(moduleClass.getName() + MODULE_ADAPTER_SUFFIX, moduleClass.getClassLoader()); } - public static Binding initInjectAdapter(String className, ClassLoader classLoader) { + public static Binding initInjectAdapter(String className, ClassLoader classLoader) + throws ClassNotFoundException { return instantiate(className + INJECT_ADAPTER_SUFFIX, classLoader); } - public static StaticInjection initStaticInjection(Class injectedClass) { + public static StaticInjection initStaticInjection(Class injectedClass) + throws ClassNotFoundException { return instantiate(injectedClass.getName() + STATIC_INJECTION_SUFFIX, injectedClass.getClassLoader()); } - private static T instantiate(String name, ClassLoader classLoader) { + private static T instantiate(String name, ClassLoader classLoader) + throws ClassNotFoundException { try { // A null classloader is the system classloader. classLoader = (classLoader != null) ? classLoader : ClassLoader.getSystemClassLoader(); @@ -57,11 +60,6 @@ private static T instantiate(String name, ClassLoader classLoader) { Constructor constructor = generatedClass.getDeclaredConstructor(); constructor.setAccessible(true); return (T) constructor.newInstance(); - } catch (ClassNotFoundException e) { - if (logger.isLoggable(Level.FINE)) { - logger.log(Level.FINE, name + " could not be found.", e); - } - return null; // Not finding a class is not inherently an error, unlike finding a bad class. } catch (NoSuchMethodException e) { throw new RuntimeException("No default constructor found on " + name, e); } catch (InstantiationException e) { diff --git a/core/src/test/java/dagger/ExtensionTest.java b/core/src/test/java/dagger/ExtensionTest.java index f688f2d58b1..06f9b865e59 100644 --- a/core/src/test/java/dagger/ExtensionTest.java +++ b/core/src/test/java/dagger/ExtensionTest.java @@ -16,6 +16,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import java.util.Arrays; import javax.inject.Inject; import javax.inject.Singleton; @@ -55,11 +56,12 @@ static class D { static class ExtensionModule { } @Test public void basicExtension() { - assertNotNull(ObjectGraph.create(new RootModule()).plus(new ExtensionModule())); + assertNotNull(ObjectGraph.createWith(new TestingLoader(), new RootModule()) + .plus(new ExtensionModule())); } @Test public void basicInjection() { - ObjectGraph root = ObjectGraph.create(new RootModule()); + ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); assertThat(root.get(A.class)).isNotNull(); assertThat(root.get(A.class)).isSameAs(root.get(A.class)); // Present and Singleton. assertThat(root.get(B.class)).isNotSameAs(root.get(B.class)); // Not singleton. @@ -77,7 +79,7 @@ static class ExtensionModule { } } @Test public void scopedGraphs() { - ObjectGraph app = ObjectGraph.create(new RootModule()); + ObjectGraph app = ObjectGraph.createWith(new TestingLoader(), new RootModule()); assertThat(app.get(A.class)).isNotNull(); assertThat(app.get(A.class)).isSameAs(app.get(A.class)); assertThat(app.get(B.class)).isNotSameAs(app.get(B.class)); diff --git a/core/src/test/java/dagger/ExtensionWithStateTest.java b/core/src/test/java/dagger/ExtensionWithStateTest.java index da2f7acdf62..ff35e2a872a 100644 --- a/core/src/test/java/dagger/ExtensionWithStateTest.java +++ b/core/src/test/java/dagger/ExtensionWithStateTest.java @@ -16,6 +16,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import javax.inject.Inject; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,7 +49,7 @@ static class ExtensionModule { } @Test public void basicInjectionWithExtension() { A a = new A(); - ObjectGraph root = ObjectGraph.create(new RootModule(a)); + ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule(a)); assertThat(root.get(A.class)).isSameAs(a); // Extension graph behaves as the root graph would for root-ish things. diff --git a/core/src/test/java/dagger/InjectStaticsTest.java b/core/src/test/java/dagger/InjectStaticsTest.java index 0421b1e2294..a19456b8db5 100644 --- a/core/src/test/java/dagger/InjectStaticsTest.java +++ b/core/src/test/java/dagger/InjectStaticsTest.java @@ -15,6 +15,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import javax.inject.Inject; import org.junit.Before; import org.junit.Test; @@ -47,7 +48,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(),new TestModule()); assertThat(InjectsOneField.staticField).isNull(); graph.injectStatics(); assertThat(InjectsOneField.staticField).isEqualTo("static"); @@ -66,7 +67,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(InjectsStaticAndNonStatic.staticField).isNull(); graph.injectStatics(); assertThat(InjectsStaticAndNonStatic.staticField).isEqualTo("static"); @@ -85,7 +86,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(InjectsStaticAndNonStatic.staticField).isNull(); InjectsStaticAndNonStatic object = new InjectsStaticAndNonStatic(); graph.inject(object); diff --git a/core/src/test/java/dagger/InjectionOfLazyTest.java b/core/src/test/java/dagger/InjectionOfLazyTest.java index 1133f3b2bc0..7156c968c9c 100644 --- a/core/src/test/java/dagger/InjectionOfLazyTest.java +++ b/core/src/test/java/dagger/InjectionOfLazyTest.java @@ -16,6 +16,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import java.util.concurrent.atomic.AtomicInteger; import javax.inject.Inject; import javax.inject.Provider; @@ -127,6 +128,6 @@ class TestModule { } private T injectWithModule(T ep, Object ... modules) { - return ObjectGraph.create(modules).inject(ep); + return ObjectGraph.createWith(new TestingLoader(), modules).inject(ep); } } diff --git a/core/src/test/java/dagger/InjectionTest.java b/core/src/test/java/dagger/InjectionTest.java index a12fb2c5ff9..7fdb60932e2 100644 --- a/core/src/test/java/dagger/InjectionTest.java +++ b/core/src/test/java/dagger/InjectionTest.java @@ -16,6 +16,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import java.util.AbstractList; import java.util.ArrayList; import java.util.Arrays; @@ -53,7 +54,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); G g = entryPoint.gProvider.get(); assertThat(g.a).isNotNull(); assertThat(g.b).isNotNull(); @@ -112,7 +113,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.aProvider.get()).isNotNull(); assertThat(entryPoint.aProvider.get()).isNotNull(); @@ -134,7 +135,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.fProvider.get()).isSameAs(entryPoint.fProvider.get()); assertThat(entryPoint.iProvider.get()).isSameAs(entryPoint.iProvider.get()); } @@ -165,7 +166,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.a).isNotNull(); assertThat(one).isSameAs(entryPoint.aOne); assertThat(two).isSameAs(entryPoint.aTwo); @@ -190,7 +191,7 @@ class TestModule { TestEntryPoint entryPoint = new TestEntryPoint(); TestModule module = new TestModule(); - ObjectGraph.create(module).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), module).inject(entryPoint); entryPoint.lProvider.get(); assertThat(module.a1).isNotNull(); @@ -221,7 +222,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.f1).isSameAs(entryPoint.f2); assertThat(entryPoint.f1).isSameAs(entryPoint.n1.f1); @@ -247,7 +248,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -268,7 +269,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.q.f).isNotNull(); } @@ -282,7 +283,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.t).isNotNull(); } @@ -323,7 +324,7 @@ class TestModule { R.injected = false; TestEntryPoint entryPoint = new TestEntryPoint(); TestModule module = new TestModule(); - ObjectGraph.create(module).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), module).inject(entryPoint); assertThat(R.injected).isFalse(); assertThat(module.sInjected).isFalse(); @@ -349,7 +350,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); fail(); } catch (IllegalArgumentException expected) { } @@ -367,7 +368,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); fail(); } catch (IllegalArgumentException expected) { } @@ -386,7 +387,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.aProvider.get()).isSameAs(entryPoint.aProvider.get()); } @@ -413,7 +414,7 @@ class OverridesModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new BaseModule(), new OverridesModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new BaseModule(), new OverridesModule()).inject(entryPoint); E e = entryPoint.eProvider.get(); assertThat(e).isNotNull(); assertThat(e.f).isNotNull(); @@ -428,7 +429,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -449,7 +450,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); assertThat(graph.get(Runnable.class)).isSameAs(runnable); } @@ -463,7 +464,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -498,7 +499,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.extendsParameterizedType.string).isEqualTo("injected"); } @@ -515,7 +516,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); assertThat(entryPoint.listOfStrings).isEqualTo(Arrays.asList("a", "b")); } @@ -532,7 +533,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); fail(); } catch (UnsupportedOperationException expected) { } @@ -555,7 +556,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -568,7 +569,7 @@ class TestModule { class TestModule { } - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); } @Test public void getInstance() { @@ -581,7 +582,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat((int) graph.get(Integer.class)).isEqualTo(0); assertThat((int) graph.get(Integer.class)).isEqualTo(1); } @@ -594,7 +595,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(Integer.class); fail(); @@ -610,7 +611,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertEquals(1, (int) graph.get(int.class)); } @@ -622,7 +623,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertEquals("[1, 2, 3]", Arrays.toString(graph.get(int[].class))); } @@ -645,7 +646,7 @@ BoundTwoWays provideBoundTwoWays() { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); BoundTwoWays provided = graph.get(BoundTwoWays.class); assertEquals("Pepsi", provided.s); @@ -664,7 +665,7 @@ static class NoInjections { class TestModule { } - ObjectGraph.create(new TestModule()).validate(); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).validate(); } static class InjectMembersOnly { @@ -681,7 +682,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(InjectMembersOnly.class); fail(); @@ -701,7 +702,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -724,7 +725,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -745,7 +746,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()).inject(new TestEntryPoint()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(new TestEntryPoint()); fail(); } catch (ClassCastException e) { assertThat(e.getMessage()).isEqualTo("foo"); @@ -764,7 +765,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()).get(ThrowsOnConstruction.class); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).get(ThrowsOnConstruction.class); fail(); } catch (ClassCastException e) { assertThat(e.getMessage()).isEqualTo("foo"); @@ -782,7 +783,7 @@ static class RootModule { } static class ExtensionModule { } @Test public void testSingletonLinkingThroughExtensionGraph() { - ObjectGraph root = ObjectGraph.create(new RootModule()); + ObjectGraph root = ObjectGraph.createWith(new TestingLoader(), new RootModule()); // DO NOT CALL root.get(C.class)) HERE to get forced-linking behaviour from plus(); ObjectGraph extension = root.plus(new ExtensionModule()); assertThat(extension.get(SingletonLinkedFromExtension.class).c).isSameAs(root.get(C.class)); @@ -801,7 +802,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()).inject(new Test()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(new Test()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("Can't inject private field: "); @@ -819,7 +820,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()).get(Test.class); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).get(Test.class); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()).contains("Can't inject private constructor: "); @@ -832,7 +833,7 @@ class TestModule { class TestModule { } - ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); objectGraph.validate(); try { objectGraph.get(ArrayList.class); diff --git a/core/src/test/java/dagger/LazyInjectionTest.java b/core/src/test/java/dagger/LazyInjectionTest.java index 522a344cdfe..849a43128c0 100644 --- a/core/src/test/java/dagger/LazyInjectionTest.java +++ b/core/src/test/java/dagger/LazyInjectionTest.java @@ -15,6 +15,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import javax.inject.Inject; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,7 +30,7 @@ public final class LazyInjectionTest { class TestModule { } - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(lazyEntryPointLoaded).isFalse(); } @@ -48,7 +49,7 @@ class TestModule { } } - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(lazyProvidesParameterLoaded).isFalse(); } @@ -67,7 +68,7 @@ class TestModule { } } - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(lazyProvidesResultLoaded).isFalse(); } @@ -83,7 +84,7 @@ static class LazyProvidesResult { class TestModule { } - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); assertThat(LazyInjectStaticsLoaded).isFalse(); } @@ -109,7 +110,7 @@ class TestModule { } } - ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.injected).isEqualTo("5"); diff --git a/core/src/test/java/dagger/MembersInjectorTest.java b/core/src/test/java/dagger/MembersInjectorTest.java index 75408eb1a60..5a0197f2012 100644 --- a/core/src/test/java/dagger/MembersInjectorTest.java +++ b/core/src/test/java/dagger/MembersInjectorTest.java @@ -15,6 +15,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import javax.inject.Inject; import javax.inject.Provider; import javax.inject.Singleton; @@ -44,7 +45,7 @@ class StringModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new StringModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new StringModule()).inject(entryPoint); Injectable injectable = new Injectable(); entryPoint.membersInjector.injectMembers(injectable); assertThat(injectable.injected).isEqualTo("injected"); @@ -75,7 +76,7 @@ class StringModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new StringModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new StringModule()).inject(entryPoint); Unconstructable object = new Unconstructable("constructor"); entryPoint.membersInjector.injectMembers(object); assertThat(object.constructor).isEqualTo("constructor"); @@ -92,7 +93,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); @@ -109,7 +110,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); @@ -126,7 +127,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); @@ -160,7 +161,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); NonStaticInner nonStaticInner = new NonStaticInner(); entryPoint.membersInjector.injectMembers(nonStaticInner); assertThat(nonStaticInner.injected).isEqualTo("injected"); @@ -175,7 +176,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.get(TestEntryPoint.class); fail(); @@ -206,7 +207,7 @@ class TestModule { } TestEntryPoint entryPoint = new TestEntryPoint(); - ObjectGraph.create(new TestModule()).inject(entryPoint); + ObjectGraph.createWith(new TestingLoader(), new TestModule()).inject(entryPoint); InjectsString provided = entryPoint.provider.get(); assertThat(provided.value).isEqualTo("provides"); diff --git a/core/src/test/java/dagger/ModuleTest.java b/core/src/test/java/dagger/ModuleTest.java index 571d1b29489..56dcecc51e1 100644 --- a/core/src/test/java/dagger/ModuleTest.java +++ b/core/src/test/java/dagger/ModuleTest.java @@ -15,6 +15,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import javax.inject.Inject; import javax.inject.Provider; import org.junit.Test; @@ -24,6 +25,8 @@ import static org.fest.assertions.Assertions.assertThat; import static org.junit.Assert.fail; +//TODO: Migrate to compiler. + @RunWith(JUnit4.class) public final class ModuleTest { static class TestEntryPoint { @@ -42,7 +45,7 @@ class TestModule { } } - ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = objectGraph.get(TestEntryPoint.class); assertThat(entryPoint.s).isEqualTo("injected"); } @@ -63,7 +66,7 @@ class TestModule { } } - ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestStaticInjection.s = null; objectGraph.injectStatics(); assertThat(TestStaticInjection.s).isEqualTo("injected"); @@ -85,7 +88,7 @@ static class ModuleWithBinding { class TestModule { } - ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("injected"); @@ -104,7 +107,7 @@ static class ModuleWithChildModule { class TestModule { } - ObjectGraph objectGraph = ObjectGraph.create(new TestModule()); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("injected"); @@ -129,7 +132,7 @@ class TestModule { } try { - ObjectGraph.create(new TestModule()); + ObjectGraph.createWith(new TestingLoader(), new TestModule()); fail(); } catch (IllegalArgumentException expected) { } @@ -144,7 +147,7 @@ class TestModule { class TestModule { } - ObjectGraph objectGraph = ObjectGraph.create(new ModuleWithConstructor("a"), new TestModule()); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new ModuleWithConstructor("a"), new TestModule()); TestEntryPoint entryPoint = new TestEntryPoint(); objectGraph.inject(entryPoint); assertThat(entryPoint.s).isEqualTo("a"); @@ -162,13 +165,13 @@ static class B { @Inject A a; } @Test public void autoInstantiationOfModules() { // Have to make these non-method-scoped or instantiation errors occur. - ObjectGraph objectGraph = ObjectGraph.create(TestModuleA.class); + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), TestModuleA.class); assertThat(objectGraph.get(A.class)).isNotNull(); } @Test public void autoInstantiationOfIncludedModules() { // Have to make these non-method-scoped or instantiation errors occur. - ObjectGraph objectGraph = ObjectGraph.create(new TestModuleB()); // TestModuleA auto-created. + ObjectGraph objectGraph = ObjectGraph.createWith(new TestingLoader(), new TestModuleB()); // TestModuleA auto-created. assertThat(objectGraph.get(A.class)).isNotNull(); assertThat(objectGraph.get(B.class).a).isNotNull(); } @@ -180,7 +183,7 @@ static class ChildModuleMissingModuleAnnotation {} @Test(expected = IllegalArgumentException.class) public void childModuleMissingModuleAnnotation() { - ObjectGraph.create(new ChildModuleMissingModuleAnnotation()); + ObjectGraph.createWith(new TestingLoader(), new TestingLoader(), new ChildModuleMissingModuleAnnotation()); } @Module @@ -188,7 +191,7 @@ static class ThreadModule extends Thread {} @Test public void moduleExtendingClassThrowsException() { try { - ObjectGraph.create(new ThreadModule()); + ObjectGraph.createWith(new TestingLoader(), new ThreadModule()); fail(); } catch (IllegalArgumentException e) { assertThat(e.getMessage()).startsWith("Modules must not extend from other classes: "); @@ -203,7 +206,7 @@ class ProvidesProviderModule { } } try { - ObjectGraph.create(new ProvidesProviderModule()); + ObjectGraph.createWith(new TestingLoader(), new ProvidesProviderModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()) // @@ -220,7 +223,7 @@ class ProvidesRawProviderModule { } } try { - ObjectGraph.create(new ProvidesRawProviderModule()); + ObjectGraph.createWith(new TestingLoader(), new ProvidesRawProviderModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()) // @@ -237,7 +240,7 @@ class ProvidesLazyModule { } } try { - ObjectGraph.create(new ProvidesLazyModule()); + ObjectGraph.createWith(new TestingLoader(), new ProvidesLazyModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()) // @@ -254,7 +257,7 @@ class ProvidesRawLazyModule { } } try { - ObjectGraph.create(new ProvidesRawLazyModule()); + ObjectGraph.createWith(new TestingLoader(), new ProvidesRawLazyModule()); fail(); } catch (IllegalStateException e) { assertThat(e.getMessage()) // diff --git a/core/src/test/java/dagger/ProblemDetectorTest.java b/core/src/test/java/dagger/ProblemDetectorTest.java index 83b1124bddd..a6b7c43c1b1 100644 --- a/core/src/test/java/dagger/ProblemDetectorTest.java +++ b/core/src/test/java/dagger/ProblemDetectorTest.java @@ -15,6 +15,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import javax.inject.Inject; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,7 +34,7 @@ class TestEntryPoint { class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -52,7 +53,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); @@ -71,7 +72,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); } diff --git a/core/src/test/java/dagger/SetBindingTest.java b/core/src/test/java/dagger/SetBindingTest.java index 7be7a9ec8cf..1393ecc06f4 100644 --- a/core/src/test/java/dagger/SetBindingTest.java +++ b/core/src/test/java/dagger/SetBindingTest.java @@ -16,6 +16,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Arrays; @@ -108,7 +109,7 @@ class TestModule { @Provides(type=SET) Integer b() { return defaultCounter.getAndIncrement(); } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); TestEntryPoint1 ep1 = graph.inject(new TestEntryPoint1()); TestEntryPoint2 ep2 = graph.inject(new TestEntryPoint2()); assertEquals(set(100, 200), ep1.objects1); @@ -184,7 +185,7 @@ class TestModule { @Provides(type=SET) String provideString2() { return "string2"; } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); } @@ -212,7 +213,7 @@ static interface LogSink { } private T injectWithModule(T ep, Object ... modules) { - return ObjectGraph.create(modules).inject(ep); + return ObjectGraph.createWith(new TestingLoader(), modules).inject(ep); } private Set set(T... ts) { diff --git a/core/src/test/java/dagger/ThreadSafetyTest.java b/core/src/test/java/dagger/ThreadSafetyTest.java index 2622d123862..ba4986d73c7 100644 --- a/core/src/test/java/dagger/ThreadSafetyTest.java +++ b/core/src/test/java/dagger/ThreadSafetyTest.java @@ -16,6 +16,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; @@ -73,7 +74,8 @@ static class LatchingModule { @Test public void concurrentSingletonAccess() throws Exception { final List> futures = new ArrayList>(); - final ObjectGraph graph = ObjectGraph.create(new LatchingModule(latch)); + final ObjectGraph graph = + ObjectGraph.createWith(new TestingLoader(), new LatchingModule(latch)); for (int i = 0; i < THREAD_COUNT; i++) { futures.add(es.submit(new Callable() { @Override public Long call() { @@ -92,7 +94,8 @@ static class LatchingModule { @Test public void concurrentLazyAccess() throws Exception { final List> futures = new ArrayList>(); - final ObjectGraph graph = ObjectGraph.create(new LatchingModule(latch)); + final ObjectGraph graph = + ObjectGraph.createWith(new TestingLoader(), new LatchingModule(latch)); final LazyEntryPoint lep = graph.get(LazyEntryPoint.class); for (int i = 0; i < THREAD_COUNT; i++) { futures.add(es.submit(new Callable() { diff --git a/core/src/test/java/dagger/UnusedProviderTest.java b/core/src/test/java/dagger/UnusedProviderTest.java index f9ffbef41b8..683c6dcdf03 100644 --- a/core/src/test/java/dagger/UnusedProviderTest.java +++ b/core/src/test/java/dagger/UnusedProviderTest.java @@ -15,6 +15,7 @@ */ package dagger; +import dagger.internal.TestingLoader; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -35,7 +36,7 @@ class BagOfMoney { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); } @@ -52,7 +53,7 @@ class BagOfMoney { } try { - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph.validate(); fail("Validation should have exploded!"); } catch (IllegalStateException expected) { @@ -75,7 +76,7 @@ class BagOfMoney { @Module(injects = EntryPoint.class) class TestModule { } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); graph = graph.plus(new ExampleLibraryModule()); graph.validate(); } @@ -88,7 +89,7 @@ class TestModule { } } - ObjectGraph graph = ObjectGraph.create(new TestModule()); + ObjectGraph graph = ObjectGraph.createWith(new TestingLoader(), new TestModule()); try { graph.validate(); fail(); diff --git a/core/src/main/java/dagger/internal/loaders/ReflectiveModuleAdapter.java b/core/src/test/java/dagger/internal/TestOnlyModuleAdapter.java similarity index 92% rename from core/src/main/java/dagger/internal/loaders/ReflectiveModuleAdapter.java rename to core/src/test/java/dagger/internal/TestOnlyModuleAdapter.java index 1bb36f7838b..a2b1a204573 100644 --- a/core/src/main/java/dagger/internal/loaders/ReflectiveModuleAdapter.java +++ b/core/src/test/java/dagger/internal/TestOnlyModuleAdapter.java @@ -13,16 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package dagger.internal.loaders; +package dagger.internal; import dagger.Lazy; import dagger.Module; import dagger.Provides; -import dagger.internal.Binding; -import dagger.internal.Keys; -import dagger.internal.Linker; -import dagger.internal.ModuleAdapter; -import dagger.internal.SetBinding; import java.lang.annotation.Annotation; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; @@ -34,10 +29,11 @@ import javax.inject.Provider; import javax.inject.Singleton; -public final class ReflectiveModuleAdapter extends ModuleAdapter { +//TODO: Reduce the complexity of this and/or replace with a mock or fake. +public final class TestOnlyModuleAdapter extends ModuleAdapter { final Class moduleClass; - public ReflectiveModuleAdapter(Class moduleClass, Module annotation) { + public TestOnlyModuleAdapter(Class moduleClass, Module annotation) { super( injectableTypesToKeys(annotation.injects()), annotation.staticInjections(), @@ -130,7 +126,7 @@ private void handleSetBindings(Map> bindings, Method meth } /** - * Creates a ReflectiveModuleAdapter or throws an {@code IllegalArgumentException}. + * Creates a TestOnlyModuleAdapter or throws an {@code IllegalArgumentException}. */ @SuppressWarnings("unchecked") // Runtime checks validate that the result type matches 'T'. public static ModuleAdapter create(Class moduleClass) { @@ -142,7 +138,7 @@ public static ModuleAdapter create(Class moduleClass) { throw new IllegalArgumentException( "Modules must not extend from other classes: " + moduleClass.getName()); } - return (ModuleAdapter) new ReflectiveModuleAdapter(moduleClass, annotation); + return (ModuleAdapter) new TestOnlyModuleAdapter(moduleClass, annotation); } /** @@ -172,7 +168,6 @@ public ProviderMethodBinding(Method method, String key, Object instance, boolean } } - @SuppressWarnings("unchecked") // We defined 'T' in terms of the method's return type. @Override public T get() { Object[] args = new Object[parameters.length]; for (int i = 0; i < parameters.length; i++) { diff --git a/core/src/test/java/dagger/internal/TestingLoader.java b/core/src/test/java/dagger/internal/TestingLoader.java new file mode 100644 index 00000000000..512c4b68737 --- /dev/null +++ b/core/src/test/java/dagger/internal/TestingLoader.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2013 Square, Inc. + * Copyright (C) 2013 Google, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package dagger.internal; + + +import dagger.internal.loaders.ReflectiveAtInjectBinding; +import dagger.internal.loaders.ReflectiveStaticInjection; + +/** + * A test-only loader that merely uses reflection to test internals. + */ +public final class TestingLoader implements Loader { + + @Override public ModuleAdapter getModuleAdapter(Class type, T instance) { + ModuleAdapter adapter = TestOnlyModuleAdapter.create(type); + adapter.module = (instance != null) ? instance : adapter.newModule(); + return adapter; + } + + @Override public Binding getAtInjectBinding(String key, String className, ClassLoader ignored, + boolean mustHaveInjections) { + try { + Class type = getClass().getClassLoader().loadClass(className); + if (type.isInterface()) { + return null; // Short-circuit since we can't build reflective bindings for interfaces. + } + return ReflectiveAtInjectBinding.create(type, mustHaveInjections); + } catch (ClassNotFoundException e) { + throw new TypeNotPresentException( + String.format("Could not find %s needed for binding %s", className, key), e); + } + } + + @Override public StaticInjection getStaticInjection(Class injectedClass) { + return ReflectiveStaticInjection.create(injectedClass); + } +} From 525cb97df2458da90a4ff99c917b098c7c8b1db9 Mon Sep 17 00:00:00 2001 From: Christian Edward Gruber Date: Fri, 28 Jun 2013 09:33:41 -0700 Subject: [PATCH 2/5] Add some javadocs, plus more precisely narrow where an exception is caught. --- .../main/java/dagger/internal/FailoverLoader.java | 7 ++++--- .../dagger/internal/loaders/GeneratedAdapters.java | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/dagger/internal/FailoverLoader.java b/core/src/main/java/dagger/internal/FailoverLoader.java index 877fb0d2002..48b3e38eee0 100644 --- a/core/src/main/java/dagger/internal/FailoverLoader.java +++ b/core/src/main/java/dagger/internal/FailoverLoader.java @@ -32,13 +32,14 @@ public final class FailoverLoader implements Loader { * Obtains a module adapter for {@code module} from the first responding resolver. */ @Override public ModuleAdapter getModuleAdapter(Class type, T instance) { + ModuleAdapter result = null; try { - ModuleAdapter result = GeneratedAdapters.initModuleAdapter(type); - result.module = (instance != null) ? instance : result.newModule(); - return result; + result = GeneratedAdapters.initModuleAdapter(type); } catch (ClassNotFoundException e) { throw new TypeNotPresentException(type + GeneratedAdapters.MODULE_ADAPTER_SUFFIX, e); } + result.module = (instance != null) ? instance : result.newModule(); + return result; } @Override public Binding getAtInjectBinding(String key, String className, diff --git a/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java b/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java index 0907ad0cc34..6ef7445f0be 100644 --- a/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java +++ b/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java @@ -35,16 +35,28 @@ public final class GeneratedAdapters { private GeneratedAdapters() { } + /** + * Attempts to load an adapter named from the provided type plus a constant suffix + * {@link #MODULE_ADAPTER_SUFFIX}, or throws a ClassNotFoundException. + */ public static ModuleAdapter initModuleAdapter(Class moduleClass) throws ClassNotFoundException { return instantiate(moduleClass.getName() + MODULE_ADAPTER_SUFFIX, moduleClass.getClassLoader()); } + /** + * Attempts to load an adapter named from the provided class name plus a constant suffix + * {@link #INJECT_ADAPTER_SUFFIX}, or throws a ClassNotFoundException. + */ public static Binding initInjectAdapter(String className, ClassLoader classLoader) throws ClassNotFoundException { return instantiate(className + INJECT_ADAPTER_SUFFIX, classLoader); } + /** + * Attempts to load an adapter named from the provided type plus a constant suffix + * {@link #STATIC_INJECTION_SUFFIX}, or throws a ClassNotFoundException. + */ public static StaticInjection initStaticInjection(Class injectedClass) throws ClassNotFoundException { return instantiate(injectedClass.getName() + STATIC_INJECTION_SUFFIX, From f457d87cb62d5cc5d66ac818f45dc1c2bf17a336 Mon Sep 17 00:00:00 2001 From: Christian Edward Gruber Date: Fri, 28 Jun 2013 09:35:37 -0700 Subject: [PATCH 3/5] Mark a fail-over case as such. --- core/src/main/java/dagger/internal/FailoverLoader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/dagger/internal/FailoverLoader.java b/core/src/main/java/dagger/internal/FailoverLoader.java index 48b3e38eee0..52f923ee361 100644 --- a/core/src/main/java/dagger/internal/FailoverLoader.java +++ b/core/src/main/java/dagger/internal/FailoverLoader.java @@ -46,7 +46,7 @@ public final class FailoverLoader implements Loader { ClassLoader classLoader, boolean mustHaveInjections) { try { return GeneratedAdapters.initInjectAdapter(className, classLoader); - } catch (ClassNotFoundException ignored) { + } catch (ClassNotFoundException ignored /* failover case */) { try { // A null classloader is the system classloader. classLoader = (classLoader != null) ? classLoader : ClassLoader.getSystemClassLoader(); From 2fa92e61fc5b6eac0129eb36e514fbab11990731 Mon Sep 17 00:00:00 2001 From: Christian Edward Gruber Date: Fri, 28 Jun 2013 09:41:07 -0700 Subject: [PATCH 4/5] fix some javadoc links. --- .../java/dagger/internal/loaders/GeneratedAdapters.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java b/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java index 6ef7445f0be..91a974a9757 100644 --- a/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java +++ b/core/src/main/java/dagger/internal/loaders/GeneratedAdapters.java @@ -37,7 +37,7 @@ private GeneratedAdapters() { } /** * Attempts to load an adapter named from the provided type plus a constant suffix - * {@link #MODULE_ADAPTER_SUFFIX}, or throws a ClassNotFoundException. + * {@link #MODULE_ADAPTER_SUFFIX}, or throws a {@code ClassNotFoundException}. */ public static ModuleAdapter initModuleAdapter(Class moduleClass) throws ClassNotFoundException { @@ -46,7 +46,7 @@ public static ModuleAdapter initModuleAdapter(Class moduleCl /** * Attempts to load an adapter named from the provided class name plus a constant suffix - * {@link #INJECT_ADAPTER_SUFFIX}, or throws a ClassNotFoundException. + * {@link #INJECT_ADAPTER_SUFFIX}, or throws a {@code ClassNotFoundException}. */ public static Binding initInjectAdapter(String className, ClassLoader classLoader) throws ClassNotFoundException { @@ -55,7 +55,7 @@ public static Binding initInjectAdapter(String className, ClassLoader classLo /** * Attempts to load an adapter named from the provided type plus a constant suffix - * {@link #STATIC_INJECTION_SUFFIX}, or throws a ClassNotFoundException. + * {@link #STATIC_INJECTION_SUFFIX}, or throws a {@code ClassNotFoundException}. */ public static StaticInjection initStaticInjection(Class injectedClass) throws ClassNotFoundException { From e3a26c8b2c949badcf1f8b628fa6661d3bf86577 Mon Sep 17 00:00:00 2001 From: Christian Edward Gruber Date: Sat, 29 Jun 2013 19:43:34 -0700 Subject: [PATCH 5/5] Fix (irrelevant) error in test, and strengthen it to ensure the right failure is being caught. --- core/src/test/java/dagger/ModuleTest.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/core/src/test/java/dagger/ModuleTest.java b/core/src/test/java/dagger/ModuleTest.java index 56dcecc51e1..ffe818aed55 100644 --- a/core/src/test/java/dagger/ModuleTest.java +++ b/core/src/test/java/dagger/ModuleTest.java @@ -181,9 +181,14 @@ static class ModuleMissingModuleAnnotation {} @Module(includes = ModuleMissingModuleAnnotation.class) static class ChildModuleMissingModuleAnnotation {} - @Test(expected = IllegalArgumentException.class) + @Test public void childModuleMissingModuleAnnotation() { - ObjectGraph.createWith(new TestingLoader(), new TestingLoader(), new ChildModuleMissingModuleAnnotation()); + try { + ObjectGraph.createWith(new TestingLoader(), new ChildModuleMissingModuleAnnotation()); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage()) + .contains("No @Module on dagger.ModuleTest$ModuleMissingModuleAnnotation"); + } } @Module