diff --git a/core/src/com/google/inject/name/Named.java b/core/src/com/google/inject/name/Named.java index 23b3d5dc1a..b1d9a4e886 100644 --- a/core/src/com/google/inject/name/Named.java +++ b/core/src/com/google/inject/name/Named.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,7 +19,6 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import com.google.inject.BindingAnnotation; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; @@ -30,7 +29,7 @@ * @author crazybob@google.com (Bob Lee) */ @Retention(RUNTIME) -@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) +@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) @BindingAnnotation public @interface Named { String value(); diff --git a/core/src/com/google/inject/name/NamedImpl.java b/core/src/com/google/inject/name/NamedImpl.java index 5fecb1d393..12e00649f8 100644 --- a/core/src/com/google/inject/name/NamedImpl.java +++ b/core/src/com/google/inject/name/NamedImpl.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/core/src/com/google/inject/name/Names.java b/core/src/com/google/inject/name/Names.java index f7ec971a25..619de81e6c 100644 --- a/core/src/com/google/inject/name/Names.java +++ b/core/src/com/google/inject/name/Names.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,7 +18,6 @@ import com.google.inject.Binder; import com.google.inject.Key; - import java.util.Enumeration; import java.util.Map; import java.util.Properties; @@ -32,17 +31,12 @@ public class Names { private Names() {} - /** - * Creates a {@link Named} annotation with {@code name} as the value. - */ + /** Creates a {@link Named} annotation with {@code name} as the value. */ public static Named named(String name) { return new NamedImpl(name); } - /** - * Creates a constant binding to {@code @Named(key)} for each entry in - * {@code properties}. - */ + /** Creates a constant binding to {@code @Named(key)} for each entry in {@code properties}. */ public static void bindProperties(Binder binder, Map properties) { binder = binder.skipSources(Names.class); for (Map.Entry entry : properties.entrySet()) { @@ -53,9 +47,8 @@ public static void bindProperties(Binder binder, Map properties) } /** - * Creates a constant binding to {@code @Named(key)} for each property. This - * method binds all properties including those inherited from - * {@link Properties#defaults defaults}. + * Creates a constant binding to {@code @Named(key)} for each property. This method binds all + * properties including those inherited from {@link Properties#defaults defaults}. */ public static void bindProperties(Binder binder, Properties properties) { binder = binder.skipSources(Names.class); diff --git a/core/src/com/google/inject/name/package-info.java b/core/src/com/google/inject/name/package-info.java index d6bf05da25..8d4482d197 100644 --- a/core/src/com/google/inject/name/package-info.java +++ b/core/src/com/google/inject/name/package-info.java @@ -14,7 +14,5 @@ * limitations under the License. */ -/** - * Support for binding to string-based names. - */ -package com.google.inject.name; \ No newline at end of file +/** Support for binding to string-based names. */ +package com.google.inject.name; diff --git a/core/test/com/google/inject/util/NoopOverrideTest.java b/core/test/com/google/inject/util/NoopOverrideTest.java index aba1f3a57b..9f2063b515 100644 --- a/core/test/com/google/inject/util/NoopOverrideTest.java +++ b/core/test/com/google/inject/util/NoopOverrideTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -20,9 +20,7 @@ import com.google.inject.spi.ElementVisitor; import com.google.inject.spi.ElementsTest; -/** - * @author jessewilson@google.com (Jesse Wilson) - */ +/** @author jessewilson@google.com (Jesse Wilson) */ public class NoopOverrideTest extends ElementsTest { @Override @@ -30,4 +28,4 @@ protected void checkModule(Module module, ElementVisitor... visitors) { Module overridden = Modules.override(module).with(Modules.EMPTY_MODULE); super.checkModule(overridden, visitors); } -} \ No newline at end of file +} diff --git a/core/test/com/google/inject/util/OverrideModuleTest.java b/core/test/com/google/inject/util/OverrideModuleTest.java index 0d4b7de5df..4759764bdc 100644 --- a/core/test/com/google/inject/util/OverrideModuleTest.java +++ b/core/test/com/google/inject/util/OverrideModuleTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -45,9 +45,6 @@ import com.google.inject.name.Names; import com.google.inject.spi.InjectionPoint; import com.google.inject.spi.ModuleAnnotatedMethodScanner; - -import junit.framework.TestCase; - import java.lang.annotation.Annotation; import java.lang.annotation.Documented; import java.lang.annotation.Retention; @@ -55,10 +52,9 @@ import java.util.Date; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; +import junit.framework.TestCase; -/** - * @author sberlin@gmail.com (Sam Berlin) - */ +/** @author sberlin@gmail.com (Sam Berlin) */ public class OverrideModuleTest extends TestCase { private static final Key key2 = Key.get(String.class, named("2")); @@ -76,8 +72,9 @@ public void testOverride() { } public void testOverrideMultiple() { - Module module = Modules.override(newModule("A"), newModule(1), newModule(0.5f)) - .with(newModule("B"), newModule(2), newModule(1.5d)); + Module module = + Modules.override(newModule("A"), newModule(1), newModule(0.5f)) + .with(newModule("B"), newModule(2), newModule(1.5d)); Injector injector = createInjector(module); assertEquals("B", injector.getInstance(String.class)); assertEquals(2, injector.getInstance(Integer.class).intValue()); @@ -91,29 +88,35 @@ public void testOverrideUnmatchedTolerated() { } public void testOverrideConstant() { - Module original = new AbstractModule() { - @Override protected void configure() { - bindConstant().annotatedWith(named("Test")).to("A"); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bindConstant().annotatedWith(named("Test")).to("A"); + } + }; - Module replacements = new AbstractModule() { - @Override protected void configure() { - bindConstant().annotatedWith(named("Test")).to("B"); - } - }; + Module replacements = + new AbstractModule() { + @Override + protected void configure() { + bindConstant().annotatedWith(named("Test")).to("B"); + } + }; Injector injector = createInjector(Modules.override(original).with(replacements)); assertEquals("B", injector.getInstance(Key.get(String.class, named("Test")))); } public void testGetProviderInModule() { - Module original = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("A"); - bind(key2).toProvider(getProvider(String.class)); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("A"); + bind(key2).toProvider(getProvider(String.class)); + } + }; Injector injector = createInjector(Modules.override(original).with(EMPTY_MODULE)); assertEquals("A", injector.getInstance(String.class)); @@ -121,12 +124,14 @@ public void testGetProviderInModule() { } public void testOverrideWhatGetProviderProvided() { - Module original = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("A"); - bind(key2).toProvider(getProvider(String.class)); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("A"); + bind(key2).toProvider(getProvider(String.class)); + } + }; Module replacements = newModule("B"); @@ -136,18 +141,22 @@ public void testOverrideWhatGetProviderProvided() { } public void testOverrideUsingOriginalsGetProvider() { - Module original = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("A"); - bind(key2).toInstance("B"); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("A"); + bind(key2).toInstance("B"); + } + }; - Module replacements = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toProvider(getProvider(key2)); - } - }; + Module replacements = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toProvider(getProvider(key2)); + } + }; Injector injector = createInjector(Modules.override(original).with(replacements)); assertEquals("B", injector.getInstance(String.class)); @@ -155,29 +164,35 @@ public void testOverrideUsingOriginalsGetProvider() { } public void testOverrideOfOverride() { - Module original = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("A1"); - bind(key2).toInstance("A2"); - bind(key3).toInstance("A3"); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("A1"); + bind(key2).toInstance("A2"); + bind(key3).toInstance("A3"); + } + }; - Module replacements1 = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("B1"); - bind(key2).toInstance("B2"); - } - }; + Module replacements1 = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("B1"); + bind(key2).toInstance("B2"); + } + }; Module overrides = Modules.override(original).with(replacements1); - Module replacements2 = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("C1"); - bind(key3).toInstance("C3"); - } - }; + Module replacements2 = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("C1"); + bind(key3).toInstance("C3"); + } + }; Injector injector = createInjector(Modules.override(overrides).with(replacements2)); assertEquals("C1", injector.getInstance(String.class)); @@ -186,16 +201,20 @@ public void testOverrideOfOverride() { } static class OuterReplacementsModule extends AbstractModule { - @Override protected void configure() { + @Override + protected void configure() { install(new InnerReplacementsModule()); } } + static class InnerReplacementsModule extends AbstractModule { - @Override protected void configure() { + @Override + protected void configure() { bind(String.class).toInstance("B"); bind(String.class).toInstance("C"); } } + public void testOverridesTwiceFails() { Module original = newModule("A"); Module replacements = new OuterReplacementsModule(); @@ -204,30 +223,39 @@ public void testOverridesTwiceFails() { createInjector(module); fail(); } catch (CreationException expected) { - assertContains(expected.getMessage(), + assertContains( + expected.getMessage(), "A binding to java.lang.String was already configured at " + InnerReplacementsModule.class.getName(), - asModuleChain(Modules.OverrideModule.class, - OuterReplacementsModule.class, InnerReplacementsModule.class), + asModuleChain( + Modules.OverrideModule.class, + OuterReplacementsModule.class, + InnerReplacementsModule.class), "at " + InnerReplacementsModule.class.getName(), - asModuleChain(Modules.OverrideModule.class, - OuterReplacementsModule.class, InnerReplacementsModule.class)); + asModuleChain( + Modules.OverrideModule.class, + OuterReplacementsModule.class, + InnerReplacementsModule.class)); } } public void testOverridesDoesntFixTwiceBoundInOriginal() { - Module original = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("A"); - bind(String.class).toInstance("B"); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("A"); + bind(String.class).toInstance("B"); + } + }; - Module replacements = new AbstractModule() { - @Override protected void configure() { - bind(String.class).toInstance("C"); - } - }; + Module replacements = + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).toInstance("C"); + } + }; Module module = Modules.override(original).with(replacements); try { @@ -236,7 +264,8 @@ public void testOverridesDoesntFixTwiceBoundInOriginal() { } catch (CreationException expected) { // The replacement comes first because we replace A with C, // then we encounter B and freak out. - assertContains(expected.getMessage(), + assertContains( + expected.getMessage(), "1) A binding to java.lang.String was already configured at " + replacements.getClass().getName(), asModuleChain(Modules.OverrideModule.class, replacements.getClass()), @@ -248,12 +277,14 @@ public void testOverridesDoesntFixTwiceBoundInOriginal() { public void testStandardScopeAnnotation() { final SingleUseScope scope = new SingleUseScope(); - Module module = new AbstractModule() { - @Override protected void configure() { - bindScope(TestScopeAnnotation.class, scope); - bind(String.class).in(TestScopeAnnotation.class); - } - }; + Module module = + new AbstractModule() { + @Override + protected void configure() { + bindScope(TestScopeAnnotation.class, scope); + bind(String.class).in(TestScopeAnnotation.class); + } + }; assertFalse(scope.used); Guice.createInjector(module); @@ -261,17 +292,21 @@ public void testStandardScopeAnnotation() { } public void testOverrideUntargettedBinding() { - Module original = new AbstractModule() { - @Override protected void configure() { - bind(Date.class); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bind(Date.class); + } + }; - Module replacements = new AbstractModule() { - @Override protected void configure() { - bind(Date.class).toInstance(new Date(0)); - } - }; + Module replacements = + new AbstractModule() { + @Override + protected void configure() { + bind(Date.class).toInstance(new Date(0)); + } + }; Injector injector = createInjector(Modules.override(original).with(replacements)); assertEquals(0, injector.getInstance(Date.class).getTime()); @@ -288,18 +323,22 @@ public Provider scope(Key key, Provider unscoped) { final SingleUseScope replacementScope = new SingleUseScope(); - Module original = new AbstractModule() { - @Override protected void configure() { - bindScope(TestScopeAnnotation.class, scope); - bind(Date.class).in(TestScopeAnnotation.class); - } - }; + Module original = + new AbstractModule() { + @Override + protected void configure() { + bindScope(TestScopeAnnotation.class, scope); + bind(Date.class).in(TestScopeAnnotation.class); + } + }; - Module replacements = new AbstractModule() { - @Override protected void configure() { - bindScope(TestScopeAnnotation.class, replacementScope); - } - }; + Module replacements = + new AbstractModule() { + @Override + protected void configure() { + bindScope(TestScopeAnnotation.class, replacementScope); + } + }; Injector injector = createInjector(Modules.override(original).with(replacements)); injector.getInstance(Date.class); @@ -320,53 +359,67 @@ public String toString() { } }; - final Module original = new AbstractModule() { - @Override protected void configure() { - bindScope(TestScopeAnnotation.class, scope); - bind(Date.class).in(scope); - bind(String.class).in(scope); - } - }; - Module originalWrapper = new AbstractModule() { - @Override protected void configure() { - install(original); - } - }; + final Module original = + new AbstractModule() { + @Override + protected void configure() { + bindScope(TestScopeAnnotation.class, scope); + bind(Date.class).in(scope); + bind(String.class).in(scope); + } + }; + Module originalWrapper = + new AbstractModule() { + @Override + protected void configure() { + install(original); + } + }; - Module replacements = new AbstractModule() { - @Override protected void configure() { - bindScope(TestScopeAnnotation.class, new SingleUseScope()); - } - }; + Module replacements = + new AbstractModule() { + @Override + protected void configure() { + bindScope(TestScopeAnnotation.class, new SingleUseScope()); + } + }; try { createInjector(Modules.override(originalWrapper).with(replacements)); fail("Exception expected"); } catch (CreationException e) { - assertContains(e.getMessage(), + assertContains( + e.getMessage(), "1) The scope for @TestScopeAnnotation is bound directly and cannot be overridden.", "original binding at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", asModuleChain(originalWrapper.getClass(), original.getClass()), "bound directly at " + original.getClass().getName() + ".configure(", - asModuleChain(originalWrapper.getClass(), original.getClass()), - "at ", replacements.getClass().getName() + ".configure(", + asModuleChain(originalWrapper.getClass(), original.getClass()), + "at ", + replacements.getClass().getName() + ".configure(", asModuleChain(Modules.OverrideModule.class, replacements.getClass())); } } public void testOverrideIsLazy() { final AtomicReference value = new AtomicReference("A"); - Module overridden = Modules.override(new AbstractModule() { - @Override protected void configure() { - bind(String.class).annotatedWith(named("original")).toInstance(value.get()); - } - }).with(new AbstractModule() { - @Override protected void configure() { - bind(String.class).annotatedWith(named("override")).toInstance(value.get()); - } - }); + Module overridden = + Modules.override( + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).annotatedWith(named("original")).toInstance(value.get()); + } + }) + .with( + new AbstractModule() { + @Override + protected void configure() { + bind(String.class).annotatedWith(named("override")).toInstance(value.get()); + } + }); // the value.get() call should be deferred until Guice.createInjector value.set("B"); @@ -376,46 +429,58 @@ public void testOverrideIsLazy() { } public void testOverridePrivateModuleOverPrivateModule() { - Module exposes5and6 = new AbstractModule() { - @Override protected void configure() { - install(new PrivateModule() { - @Override protected void configure() { - bind(Integer.class).toInstance(5); - expose(Integer.class); - - bind(Character.class).toInstance('E'); - } - }); - - install(new PrivateModule() { - @Override protected void configure() { - bind(Long.class).toInstance(6L); - expose(Long.class); - - bind(Character.class).toInstance('F'); - } - }); - } - }; - - AbstractModule exposes15 = new AbstractModule() { - @Override protected void configure() { - install(new PrivateModule() { - @Override protected void configure() { - bind(Integer.class).toInstance(15); - expose(Integer.class); - - bind(Character.class).toInstance('G'); + Module exposes5and6 = + new AbstractModule() { + @Override + protected void configure() { + install( + new PrivateModule() { + @Override + protected void configure() { + bind(Integer.class).toInstance(5); + expose(Integer.class); + + bind(Character.class).toInstance('E'); + } + }); + + install( + new PrivateModule() { + @Override + protected void configure() { + bind(Long.class).toInstance(6L); + expose(Long.class); + + bind(Character.class).toInstance('F'); + } + }); } - }); + }; - install(new PrivateModule() { - @Override protected void configure() { - bind(Character.class).toInstance('H'); + AbstractModule exposes15 = + new AbstractModule() { + @Override + protected void configure() { + install( + new PrivateModule() { + @Override + protected void configure() { + bind(Integer.class).toInstance(15); + expose(Integer.class); + + bind(Character.class).toInstance('G'); + } + }); + + install( + new PrivateModule() { + @Override + protected void configure() { + bind(Character.class).toInstance('H'); + } + }); } - }); - } - }; + }; // override forwards Injector injector = Guice.createInjector(Modules.override(exposes5and6).with(exposes15)); @@ -429,18 +494,22 @@ public void testOverridePrivateModuleOverPrivateModule() { } public void testOverrideModuleAndPrivateModule() { - Module exposes5 = new PrivateModule() { - @Override protected void configure() { - bind(Integer.class).toInstance(5); - expose(Integer.class); - } - }; + Module exposes5 = + new PrivateModule() { + @Override + protected void configure() { + bind(Integer.class).toInstance(5); + expose(Integer.class); + } + }; - Module binds15 = new AbstractModule() { - @Override protected void configure() { - bind(Integer.class).toInstance(15); - } - }; + Module binds15 = + new AbstractModule() { + @Override + protected void configure() { + bind(Integer.class).toInstance(15); + } + }; Injector injector = Guice.createInjector(Modules.override(exposes5).with(binds15)); assertEquals(15, injector.getInstance(Integer.class).intValue()); @@ -450,22 +519,26 @@ public void testOverrideModuleAndPrivateModule() { } public void testOverrideDeepExpose() { - final AtomicReference> charAProvider - = new AtomicReference>(); + final AtomicReference> charAProvider = + new AtomicReference>(); - Module exposes5 = new PrivateModule() { - @Override protected void configure() { - install(new PrivateModule() { - @Override protected void configure() { - bind(Integer.class).toInstance(5); + Module exposes5 = + new PrivateModule() { + @Override + protected void configure() { + install( + new PrivateModule() { + @Override + protected void configure() { + bind(Integer.class).toInstance(5); + expose(Integer.class); + charAProvider.set(getProvider(Character.class)); + bind(Character.class).toInstance('A'); + } + }); expose(Integer.class); - charAProvider.set(getProvider(Character.class)); - bind(Character.class).toInstance('A'); } - }); - expose(Integer.class); - } - }; + }; Injector injector = Guice.createInjector(Modules.override(exposes5).with(EMPTY_MODULE)); assertEquals(5, injector.getInstance(Integer.class).intValue()); @@ -475,21 +548,25 @@ public void testOverrideDeepExpose() { assertEquals(5, injector.getInstance(Integer.class).intValue()); assertEquals('A', charAProvider.getAndSet(null).get().charValue()); - final AtomicReference> charBProvider - = new AtomicReference>(); + final AtomicReference> charBProvider = + new AtomicReference>(); - Module binds15 = new AbstractModule() { - @Override protected void configure() { - bind(Integer.class).toInstance(15); + Module binds15 = + new AbstractModule() { + @Override + protected void configure() { + bind(Integer.class).toInstance(15); - install(new PrivateModule() { - @Override protected void configure() { - charBProvider.set(getProvider(Character.class)); - bind(Character.class).toInstance('B'); + install( + new PrivateModule() { + @Override + protected void configure() { + charBProvider.set(getProvider(Character.class)); + bind(Character.class).toInstance('B'); + } + }); } - }); - } - }; + }; injector = Guice.createInjector(Modules.override(binds15).with(exposes5)); assertEquals(5, injector.getInstance(Integer.class).intValue()); @@ -509,6 +586,7 @@ public void testOverrideDeepExpose() { private static class SingleUseScope implements Scope { boolean used = false; + @Override public Provider scope(Key key, Provider unscoped) { assertFalse(used); @@ -516,15 +594,18 @@ public Provider scope(Key key, Provider unscoped) { return unscoped; } } - + static class NewModule extends AbstractModule { private final T bound; + NewModule(T bound) { this.bound = bound; } - @Override protected void configure() { + + @Override + protected void configure() { @SuppressWarnings("unchecked") - Class type = (Class)bound.getClass(); + Class type = (Class) bound.getClass(); bind(type).toInstance(bound); } } @@ -532,7 +613,7 @@ static class NewModule extends AbstractModule { private static Module newModule(final T bound) { return new NewModule(bound); } - + private static final String RESULT = "RESULT"; private static final String PRIVATE_INPUT = "PRIVATE_INPUT"; private static final String OVERRIDDEN_INPUT = "FOO"; @@ -541,117 +622,138 @@ private static Module newModule(final T bound) { private static final Key INPUT_KEY = Key.get(String.class, named(PRIVATE_INPUT)); public void testExposedBindingOverride() throws Exception { - Injector inj = Guice.createInjector( - Modules.override(new ExampleModule()).with( - new AbstractModule() { - @Override protected void configure() { - bind(RESULT_KEY).toInstance(OVERRIDDEN_RESULT); - } - })); + Injector inj = + Guice.createInjector( + Modules.override(new ExampleModule()) + .with( + new AbstractModule() { + @Override + protected void configure() { + bind(RESULT_KEY).toInstance(OVERRIDDEN_RESULT); + } + })); assertEquals(inj.getInstance(RESULT_KEY), OVERRIDDEN_RESULT); } public void testPrivateBindingOverride() throws Exception { - Injector inj = Guice.createInjector( - Modules.override(new ExampleModule()).with( - new AbstractModule() { - @Override protected void configure() { - bind(INPUT_KEY).toInstance(OVERRIDDEN_INPUT); - } - })); + Injector inj = + Guice.createInjector( + Modules.override(new ExampleModule()) + .with( + new AbstractModule() { + @Override + protected void configure() { + bind(INPUT_KEY).toInstance(OVERRIDDEN_INPUT); + } + })); assertEquals(inj.getInstance(RESULT_KEY), OVERRIDDEN_RESULT); } public static class ExampleModule extends PrivateModule { - @Provides @Exposed @Named(RESULT) + @Provides + @Exposed + @Named(RESULT) public String provideResult(@Named(PRIVATE_INPUT) String input) { return "Size: " + input.length(); } - @Provides @Named(PRIVATE_INPUT) + @Provides + @Named(PRIVATE_INPUT) public String provideInput() { return "Hello World"; } - @Override protected void configure() { - } - } - + @Override + protected void configure() {} + } + public void testEqualsNotCalledByDefaultOnInstance() { final HashEqualsTester a = new HashEqualsTester(); a.throwOnEquals = true; - Guice.createInjector(Modules.override(new AbstractModule() { - @Override - protected void configure() { - bind(String.class); - bind(HashEqualsTester.class).toInstance(a); - } - }).with()); + Guice.createInjector( + Modules.override( + new AbstractModule() { + @Override + protected void configure() { + bind(String.class); + bind(HashEqualsTester.class).toInstance(a); + } + }) + .with()); } - + public void testEqualsNotCalledByDefaultOnProvider() { final HashEqualsTester a = new HashEqualsTester(); a.throwOnEquals = true; - Guice.createInjector(Modules.override(new AbstractModule() { - @Override - protected void configure() { - bind(String.class); - bind(Object.class).toProvider(a); - } - }).with()); + Guice.createInjector( + Modules.override( + new AbstractModule() { + @Override + protected void configure() { + bind(String.class); + bind(Object.class).toProvider(a); + } + }) + .with()); } - + public void testHashcodeNeverCalledOnInstance() { final HashEqualsTester a = new HashEqualsTester(); a.throwOnHashcode = true; a.equality = "test"; - + final HashEqualsTester b = new HashEqualsTester(); b.throwOnHashcode = true; b.equality = "test"; - Guice.createInjector(Modules.override(new AbstractModule() { - @Override - protected void configure() { - bind(String.class); - bind(HashEqualsTester.class).toInstance(a); - bind(HashEqualsTester.class).toInstance(b); - } - }).with()); + Guice.createInjector( + Modules.override( + new AbstractModule() { + @Override + protected void configure() { + bind(String.class); + bind(HashEqualsTester.class).toInstance(a); + bind(HashEqualsTester.class).toInstance(b); + } + }) + .with()); } - + public void testHashcodeNeverCalledOnProviderInstance() { final HashEqualsTester a = new HashEqualsTester(); a.throwOnHashcode = true; a.equality = "test"; - + final HashEqualsTester b = new HashEqualsTester(); b.throwOnHashcode = true; b.equality = "test"; - Guice.createInjector(Modules.override(new AbstractModule() { - @Override - protected void configure() { - bind(String.class); - bind(Object.class).toProvider(a); - bind(Object.class).toProvider(b); - } - }).with()); + Guice.createInjector( + Modules.override( + new AbstractModule() { + @Override + protected void configure() { + bind(String.class); + bind(Object.class).toProvider(a); + bind(Object.class).toProvider(b); + } + }) + .with()); } - + private static class HashEqualsTester implements Provider { private String equality; private boolean throwOnEquals; private boolean throwOnHashcode; - + @Override public boolean equals(Object obj) { if (throwOnEquals) { throw new RuntimeException(); } else if (obj instanceof HashEqualsTester) { - HashEqualsTester o = (HashEqualsTester)obj; - if(o.throwOnEquals) { + HashEqualsTester o = (HashEqualsTester) obj; + if (o.throwOnEquals) { throw new RuntimeException(); } - if(equality == null && o.equality == null) { + if (equality == null && o.equality == null) { return this == o; } else { return Objects.equal(equality, o.equality); @@ -660,16 +762,16 @@ public boolean equals(Object obj) { return false; } } - + @Override public int hashCode() { - if(throwOnHashcode) { + if (throwOnHashcode) { throw new RuntimeException(); } else { return super.hashCode(); } } - + @Override public Object get() { return new Object(); @@ -678,43 +780,59 @@ public Object get() { public void testCorrectStage() { final Stage stage = Stage.PRODUCTION; - Module module = Modules.override(new AbstractModule() { - @Override - protected void configure() { - if (currentStage() != Stage.PRODUCTION) { - addError("Wronge stage in overridden module:" + currentStage()); - } - } - }).with(new AbstractModule() { - @Override - protected void configure() { - if (currentStage() != Stage.PRODUCTION) { - addError("Wronge stage in overriding module:" + currentStage()); - } - } - }); + Module module = + Modules.override( + new AbstractModule() { + @Override + protected void configure() { + if (currentStage() != Stage.PRODUCTION) { + addError("Wronge stage in overridden module:" + currentStage()); + } + } + }) + .with( + new AbstractModule() { + @Override + protected void configure() { + if (currentStage() != Stage.PRODUCTION) { + addError("Wronge stage in overriding module:" + currentStage()); + } + } + }); Guice.createInjector(stage, module); } public void testOverridesApplyOriginalScanners() { Injector injector = - Guice.createInjector(Modules.override(NamedMunger.module()).with(new AbstractModule() { - @Override protected void configure() {} - @TestProvides @Named("test") String provideString() { return "foo"; } - })); + Guice.createInjector( + Modules.override(NamedMunger.module()) + .with( + new AbstractModule() { + @Override + protected void configure() {} + + @TestProvides + @Named("test") + String provideString() { + return "foo"; + } + })); assertNull(injector.getExistingBinding(Key.get(String.class, named("test")))); Binding binding = injector.getBinding(Key.get(String.class, named("test-munged"))); assertEquals("foo", binding.getProvider().get()); } - @Documented @Target(METHOD) @Retention(RUNTIME) + @Documented + @Target(METHOD) + @Retention(RUNTIME) private @interface TestProvides {} private static class NamedMunger extends ModuleAnnotatedMethodScanner { static Module module() { return new AbstractModule() { - @Override protected void configure() { + @Override + protected void configure() { binder().scanModulesForAnnotatedMethods(new NamedMunger()); } }; @@ -731,10 +849,10 @@ public Set> annotationClasses() { } @Override - public Key prepareMethod(Binder binder, Annotation annotation, Key key, - InjectionPoint injectionPoint) { - return Key.get(key.getTypeLiteral(), - Names.named(((Named) key.getAnnotation()).value() + "-munged")); + public Key prepareMethod( + Binder binder, Annotation annotation, Key key, InjectionPoint injectionPoint) { + return Key.get( + key.getTypeLiteral(), Names.named(((Named) key.getAnnotation()).value() + "-munged")); } } } diff --git a/core/test/com/google/inject/util/ProvidersTest.java b/core/test/com/google/inject/util/ProvidersTest.java index fd20f75a0b..7c3c8b7b2e 100644 --- a/core/test/com/google/inject/util/ProvidersTest.java +++ b/core/test/com/google/inject/util/ProvidersTest.java @@ -19,10 +19,8 @@ import com.google.common.base.Objects; import com.google.common.testing.EqualsTester; import com.google.inject.Provider; - -import junit.framework.TestCase; - import javax.inject.Inject; +import junit.framework.TestCase; /** * Unit tests for {@link Providers}. @@ -42,70 +40,69 @@ public void testOfNull() { Provider p = Providers.of(null); assertNull(p.get()); } - + public void testOfEquality() { new EqualsTester() - .addEqualityGroup( - Providers.of(null), - Providers.of(null)) - .addEqualityGroup( - Providers.of("Hello"), - Providers.of("Hello")) + .addEqualityGroup(Providers.of(null), Providers.of(null)) + .addEqualityGroup(Providers.of("Hello"), Providers.of("Hello")) .testEquals(); } - + public void testGuicifyEquality() { new EqualsTester() .addEqualityGroup( - Providers.guicify(new JavaxProvider(10)), - Providers.guicify(new JavaxProvider(10))) + Providers.guicify(new JavaxProvider(10)), Providers.guicify(new JavaxProvider(10))) .addEqualityGroup( - Providers.guicify(new JavaxProvider(11)), - Providers.guicify(new JavaxProvider(11))) + Providers.guicify(new JavaxProvider(11)), Providers.guicify(new JavaxProvider(11))) .addEqualityGroup( Providers.guicify(new JavaxProviderWithDependencies()), Providers.guicify(new JavaxProviderWithDependencies())) .testEquals(); } - + private static class JavaxProvider implements javax.inject.Provider { private final int value; public JavaxProvider(int value) { this.value = value; } - + @Override public Integer get() { return value; } - @Override public int hashCode() { + @Override + public int hashCode() { return Objects.hashCode(value); } - @Override public boolean equals(Object obj) { + @Override + public boolean equals(Object obj) { return (obj instanceof JavaxProvider) && (value == ((JavaxProvider) obj).value); } } - + private static class JavaxProviderWithDependencies implements javax.inject.Provider { private int value; - - @Inject void setValue(int value) { + + @Inject + void setValue(int value) { this.value = value; } - + @Override public Integer get() { return value; } - @Override public int hashCode() { + @Override + public int hashCode() { return 42; } - @Override public boolean equals(Object obj) { + @Override + public boolean equals(Object obj) { return (obj instanceof JavaxProviderWithDependencies); } } diff --git a/core/test/com/google/inject/util/TypesTest.java b/core/test/com/google/inject/util/TypesTest.java index d4a0e6d24d..4cdb78acb3 100644 --- a/core/test/com/google/inject/util/TypesTest.java +++ b/core/test/com/google/inject/util/TypesTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,7 +14,6 @@ * limitations under the License. */ - package com.google.inject.util; import static com.google.inject.Asserts.assertContains; @@ -36,9 +35,7 @@ import junit.framework.Assert; import junit.framework.TestCase; -/** - * @author jessewilson@google.com (Jesse Wilson) - */ +/** @author jessewilson@google.com (Jesse Wilson) */ public class TypesTest extends TestCase { // generic types for comparison @@ -76,7 +73,7 @@ public void testListSetMap() { } public void testDefensiveCopies() { - Type[] arguments = new Type[] { String.class, Integer.class }; + Type[] arguments = new Type[] {String.class, Integer.class}; ParameterizedType parameterizedType = Types.newParameterizedType(Map.class, arguments); arguments[0] = null; assertEquals(String.class, parameterizedType.getActualTypeArguments()[0]); @@ -85,17 +82,20 @@ public void testDefensiveCopies() { } public void testTypeWithOwnerType() { - ParameterizedType actual = Types.newParameterizedTypeWithOwner( - TypesTest.class, Inner.class, Float.class, Double.class); + ParameterizedType actual = + Types.newParameterizedTypeWithOwner( + TypesTest.class, Inner.class, Float.class, Double.class); assertEquals(TypesTest.class, actual.getOwnerType()); assertEqualsBothWays(innerFloatDouble, actual); // The JDK prints this out as: // com.google.inject.util.TypesTest.com.google.inject.util.TypesTest$Inner // and we think that's wrong, so the assertEquals comparison is worthless. :-( -// assertEquals(innerFloatDouble.toString(), actual.toString()); - + // assertEquals(innerFloatDouble.toString(), actual.toString()); + // We think the correct comparison is: - assertEquals("com.google.inject.util.TypesTest$Inner", actual.toString()); + assertEquals( + "com.google.inject.util.TypesTest$Inner", + actual.toString()); } public void testTypeParametersMustNotBePrimitives() { @@ -103,8 +103,8 @@ public void testTypeParametersMustNotBePrimitives() { Types.newParameterizedType(Map.class, String.class, int.class); fail(); } catch (IllegalArgumentException expected) { - assertContains(expected.getMessage(), - "Primitive types are not allowed in type parameters: int"); + assertContains( + expected.getMessage(), "Primitive types are not allowed in type parameters: int"); } } @@ -124,22 +124,22 @@ public void testWildcardTypes() throws NoSuchFieldException, IOException { assertEqualWhenReserialized(supertypeOf(CharSequence.class)); assertEqualWhenReserialized(subtypeOf(CharSequence.class)); } - + public void testWildcardBoundsMustNotBePrimitives() { try { supertypeOf(int.class); fail(); } catch (IllegalArgumentException expected) { - assertContains(expected.getMessage(), - "Primitive types are not allowed in wildcard bounds: int"); + assertContains( + expected.getMessage(), "Primitive types are not allowed in wildcard bounds: int"); } try { subtypeOf(int.class); fail(); } catch (IllegalArgumentException expected) { - assertContains(expected.getMessage(), - "Primitive types are not allowed in wildcard bounds: int"); + assertContains( + expected.getMessage(), "Primitive types are not allowed in wildcard bounds: int"); } } @@ -149,13 +149,13 @@ private WildcardType getWildcard(String fieldName) throws NoSuchFieldException { } public void testEqualsAndHashcode() { - ParameterizedType parameterizedType - = Types.newParameterizedType(Map.class, String.class, Integer.class); + ParameterizedType parameterizedType = + Types.newParameterizedType(Map.class, String.class, Integer.class); assertEqualsBothWays(mapStringInteger, parameterizedType); assertEquals(mapStringInteger.toString(), parameterizedType.toString()); - GenericArrayType genericArrayType = Types.arrayOf(Types.arrayOf( - Types.newParameterizedType(Set.class, String.class))); + GenericArrayType genericArrayType = + Types.arrayOf(Types.arrayOf(Types.newParameterizedType(Set.class, String.class))); assertEqualsBothWays(setStringArray, genericArrayType); assertEquals(setStringArray.toString(), genericArrayType.toString()); } @@ -163,41 +163,37 @@ public void testEqualsAndHashcode() { public void testToString() { Assert.assertEquals("java.lang.String", MoreTypes.typeToString(String.class)); assertEquals("java.util.Set[][]", MoreTypes.typeToString(setStringArray)); - assertEquals("java.util.Map", + assertEquals( + "java.util.Map", MoreTypes.typeToString(mapStringInteger)); - assertEquals("java.util.List[][]>", + assertEquals( + "java.util.List[][]>", MoreTypes.typeToString(listSetStringArray)); - assertEquals(innerFloatDouble.toString(), - MoreTypes.typeToString(innerFloatDouble)); + assertEquals(innerFloatDouble.toString(), MoreTypes.typeToString(innerFloatDouble)); } static class Owning {} - /** - * Ensure that owning types are required when necessary, and forbidden - * otherwise. - */ + /** Ensure that owning types are required when necessary, and forbidden otherwise. */ public void testCanonicalizeRequiresOwnerTypes() { try { Types.newParameterizedType(Owning.class, String.class); fail(); } catch (IllegalArgumentException expected) { - assertContains(expected.getMessage(), - "No owner type for enclosed " + Owning.class); + assertContains(expected.getMessage(), "No owner type for enclosed " + Owning.class); } try { Types.newParameterizedTypeWithOwner(Object.class, Set.class, String.class); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { - assertContains(expected.getMessage(), - "Owner type for unenclosed " + Set.class); + assertContains(expected.getMessage(), "Owner type for unenclosed " + Set.class); } } @SuppressWarnings("UnusedDeclaration") class Inner {} - + public void testInnerParameterizedEvenWithZeroArgs() { TypeLiteral.Inner> type = new TypeLiteral.Inner>() {}; assertEqualsBothWays(outerInner, type.getType()); diff --git a/extensions/grapher/test/com/google/inject/grapher/AbstractInjectorGrapherTest.java b/extensions/grapher/test/com/google/inject/grapher/AbstractInjectorGrapherTest.java index a8a35e3e76..1ab7335270 100644 --- a/extensions/grapher/test/com/google/inject/grapher/AbstractInjectorGrapherTest.java +++ b/extensions/grapher/test/com/google/inject/grapher/AbstractInjectorGrapherTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,13 +26,11 @@ import com.google.inject.Key; import com.google.inject.Provider; import com.google.inject.spi.InjectionPoint; - -import junit.framework.TestCase; - import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Member; import java.util.Set; +import junit.framework.TestCase; /** * Test cases for {@link AbstractInjectorGrapher}. This indirectly tests most classes in this @@ -48,37 +46,44 @@ private static class FakeGrapher extends AbstractInjectorGrapher { final Set nodes = Sets.newHashSet(); final Set edges = Sets.newHashSet(); - @Override protected void reset() { + @Override + protected void reset() { nodes.clear(); edges.clear(); } - @Override protected void newInterfaceNode(InterfaceNode node) { + @Override + protected void newInterfaceNode(InterfaceNode node) { assertFalse(nodes.contains(node)); nodes.add(node); } - @Override protected void newImplementationNode(ImplementationNode node) { + @Override + protected void newImplementationNode(ImplementationNode node) { assertFalse(nodes.contains(node)); nodes.add(node); } - @Override protected void newInstanceNode(InstanceNode node) { + @Override + protected void newInstanceNode(InstanceNode node) { assertFalse(nodes.contains(node)); nodes.add(node); } - @Override protected void newDependencyEdge(DependencyEdge edge) { + @Override + protected void newDependencyEdge(DependencyEdge edge) { assertFalse(edges.contains(edge)); edges.add(edge); } - @Override protected void newBindingEdge(BindingEdge edge) { + @Override + protected void newBindingEdge(BindingEdge edge) { assertFalse(edges.contains(edge)); edges.add(edge); } - @Override protected void postProcess() {} + @Override + protected void postProcess() {} } private static final class Wrapper { @@ -88,12 +93,17 @@ private static final class Wrapper { @BindingAnnotation @Retention(RetentionPolicy.RUNTIME) private static @interface Ann {} + private static interface IA {} + private static class A implements IA { - @Inject public A(String str) {} + @Inject + public A(String str) {} } + private static class A2 implements IA { - @Inject public A2(Provider strProvider) {} + @Inject + public A2(Provider strProvider) {} } private Node aNode; @@ -105,79 +115,114 @@ private static class A2 implements IA { private FakeGrapher grapher; - @Override protected void setUp() throws Exception { + @Override + protected void setUp() throws Exception { super.setUp(); grapher = new FakeGrapher(); Node.ignoreSourceInComparisons = true; - aNode = new ImplementationNode(NodeId.newTypeId(Key.get(A.class)), null, - ImmutableList.of(A.class.getConstructor(String.class))); - a2Node = new ImplementationNode(NodeId.newTypeId(Key.get(A2.class)), null, - ImmutableList.of(A2.class.getConstructor(Provider.class))); + aNode = + new ImplementationNode( + NodeId.newTypeId(Key.get(A.class)), + null, + ImmutableList.of(A.class.getConstructor(String.class))); + a2Node = + new ImplementationNode( + NodeId.newTypeId(Key.get(A2.class)), + null, + ImmutableList.of(A2.class.getConstructor(Provider.class))); iaNode = new InterfaceNode(NodeId.newTypeId(Key.get(IA.class)), null); iaAnnNode = new InterfaceNode(NodeId.newTypeId(Key.get(IA.class, Ann.class)), null); stringNode = new InterfaceNode(NodeId.newTypeId(Key.get(String.class)), null); - stringInstanceNode = new InstanceNode(NodeId.newInstanceId(Key.get(String.class)), null, - TEST_STRING, ImmutableList.of()); + stringInstanceNode = + new InstanceNode( + NodeId.newInstanceId(Key.get(String.class)), + null, + TEST_STRING, + ImmutableList.of()); } public void testLinkedAndInstanceBindings() throws Exception { - grapher.graph(Guice.createInjector(new AbstractModule() { - @Override protected void configure() { - bind(IA.class).to(A.class); - bind(IA.class).annotatedWith(Ann.class).to(A.class); - bind(String.class).toInstance(TEST_STRING); - } - })); + grapher.graph( + Guice.createInjector( + new AbstractModule() { + @Override + protected void configure() { + bind(IA.class).to(A.class); + bind(IA.class).annotatedWith(Ann.class).to(A.class); + bind(String.class).toInstance(TEST_STRING); + } + })); Set expectedNodes = ImmutableSet.of(iaNode, iaAnnNode, aNode, stringNode, stringInstanceNode); - Set expectedEdges = ImmutableSet.of( - new BindingEdge(iaNode.getId(), aNode.getId(), BindingEdge.Type.NORMAL), - new BindingEdge(iaAnnNode.getId(), aNode.getId(), BindingEdge.Type.NORMAL), - new BindingEdge(stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL), - new DependencyEdge(aNode.getId(), stringNode.getId(), - InjectionPoint.forConstructor(A.class.getConstructor(String.class)))); + Set expectedEdges = + ImmutableSet.of( + new BindingEdge(iaNode.getId(), aNode.getId(), BindingEdge.Type.NORMAL), + new BindingEdge(iaAnnNode.getId(), aNode.getId(), BindingEdge.Type.NORMAL), + new BindingEdge( + stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL), + new DependencyEdge( + aNode.getId(), + stringNode.getId(), + InjectionPoint.forConstructor(A.class.getConstructor(String.class)))); assertEquals(expectedNodes, grapher.nodes); assertEquals(expectedEdges, grapher.edges); } public void testProviderBindings() throws Exception { final Wrapper> wrapper = new Wrapper>(); - grapher.graph(Guice.createInjector(new AbstractModule() { - @Override protected void configure() { - wrapper.value = getProvider(A2.class); - bind(IA.class).toProvider(wrapper.value); - bind(A2.class); - bind(String.class).toInstance(TEST_STRING); - } - })); - - Node a2ProviderNode = new InstanceNode(NodeId.newInstanceId(Key.get(IA.class)), null, - wrapper.value, ImmutableList.of()); + grapher.graph( + Guice.createInjector( + new AbstractModule() { + @Override + protected void configure() { + wrapper.value = getProvider(A2.class); + bind(IA.class).toProvider(wrapper.value); + bind(A2.class); + bind(String.class).toInstance(TEST_STRING); + } + })); + + Node a2ProviderNode = + new InstanceNode( + NodeId.newInstanceId(Key.get(IA.class)), + null, + wrapper.value, + ImmutableList.of()); Set expectedNodes = ImmutableSet.of(iaNode, stringNode, a2Node, stringInstanceNode, a2ProviderNode); - Set expectedEdges = ImmutableSet.of( - new BindingEdge(stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL), - new BindingEdge(iaNode.getId(), a2ProviderNode.getId(), BindingEdge.Type.PROVIDER), - new DependencyEdge(a2Node.getId(), stringNode.getId(), - InjectionPoint.forConstructor(A2.class.getConstructor(Provider.class))), - new DependencyEdge(a2ProviderNode.getId(), a2Node.getId(), null)); + Set expectedEdges = + ImmutableSet.of( + new BindingEdge( + stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL), + new BindingEdge(iaNode.getId(), a2ProviderNode.getId(), BindingEdge.Type.PROVIDER), + new DependencyEdge( + a2Node.getId(), + stringNode.getId(), + InjectionPoint.forConstructor(A2.class.getConstructor(Provider.class))), + new DependencyEdge(a2ProviderNode.getId(), a2Node.getId(), null)); assertEquals("wrong nodes", expectedNodes, grapher.nodes); assertEquals("wrong edges", expectedEdges, grapher.edges); } public void testGraphWithGivenRoot() throws Exception { - grapher.graph(Guice.createInjector(new AbstractModule() { - @Override protected void configure() { - bind(IA.class).to(A.class); - bind(IA.class).annotatedWith(Ann.class).to(A.class); - bind(String.class).toInstance(TEST_STRING); - } - }), ImmutableSet.>of(Key.get(String.class))); + grapher.graph( + Guice.createInjector( + new AbstractModule() { + @Override + protected void configure() { + bind(IA.class).to(A.class); + bind(IA.class).annotatedWith(Ann.class).to(A.class); + bind(String.class).toInstance(TEST_STRING); + } + }), + ImmutableSet.>of(Key.get(String.class))); Set expectedNodes = ImmutableSet.of(stringNode, stringInstanceNode); - Set expectedEdges = ImmutableSet.of( - new BindingEdge(stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL)); + Set expectedEdges = + ImmutableSet.of( + new BindingEdge( + stringNode.getId(), stringInstanceNode.getId(), BindingEdge.Type.NORMAL)); assertEquals(expectedNodes, grapher.nodes); assertEquals(expectedEdges, grapher.edges); } diff --git a/extensions/grapher/test/com/google/inject/grapher/AllTests.java b/extensions/grapher/test/com/google/inject/grapher/AllTests.java index af81398a28..fec5cc0d81 100644 --- a/extensions/grapher/test/com/google/inject/grapher/AllTests.java +++ b/extensions/grapher/test/com/google/inject/grapher/AllTests.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,9 +19,7 @@ import junit.framework.Test; import junit.framework.TestSuite; -/** - * @author phopkins@gmail.com (Pete Hopkins) - */ +/** @author phopkins@gmail.com (Pete Hopkins) */ public class AllTests { public static Test suite() { diff --git a/extensions/grapher/test/com/google/inject/grapher/ShortNameFactoryTest.java b/extensions/grapher/test/com/google/inject/grapher/ShortNameFactoryTest.java index 867a5a86c9..8f0cfa2ff0 100644 --- a/extensions/grapher/test/com/google/inject/grapher/ShortNameFactoryTest.java +++ b/extensions/grapher/test/com/google/inject/grapher/ShortNameFactoryTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -31,13 +31,11 @@ import com.google.inject.name.Names; import com.google.inject.spi.DefaultBindingTargetVisitor; import com.google.inject.spi.ProviderInstanceBinding; - -import junit.framework.TestCase; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.lang.reflect.Member; +import junit.framework.TestCase; /** * Tests for {@link ShortNameFactory}. @@ -47,9 +45,10 @@ public class ShortNameFactoryTest extends TestCase { // Helper objects are up here because their line numbers are tested below. private static class Obj { - @Annotated - public String field; + @Annotated public String field; + Obj() {} + void method(String parameter) {} } @@ -59,9 +58,9 @@ public String toString() { return "I'm a ToStringObj"; } } - + @Retention(RUNTIME) - @Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) + @Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD}) @BindingAnnotation private @interface Annotated {} @@ -95,8 +94,8 @@ public void testGetAnnotationName_annotationType() throws Exception { } public void testGetAnnotationName_annotationInstance() throws Exception { - Key key = Key.get(String.class, - Obj.class.getDeclaredField("field").getDeclaredAnnotations()[0]); + Key key = + Key.get(String.class, Obj.class.getDeclaredField("field").getDeclaredAnnotations()[0]); assertEquals("@Annotated", nameFactory.getAnnotationName(key)); } @@ -107,69 +106,89 @@ public void testGetAnnotationName_annotationInstanceWithParameters() throws Exce public void testGetClassName_key() throws Exception { Key key = Key.get(Obj.class); - assertEquals("Class name should not have the package", - "ShortNameFactoryTest$Obj", nameFactory.getClassName(key)); + assertEquals( + "Class name should not have the package", + "ShortNameFactoryTest$Obj", + nameFactory.getClassName(key)); } - + public void testGetClassName_keyWithTypeParameters() throws Exception { Key key = Key.get(new TypeLiteral>() {}); - assertEquals("Class name and type values should not have packages", - "Provider", nameFactory.getClassName(key)); + assertEquals( + "Class name and type values should not have packages", + "Provider", + nameFactory.getClassName(key)); } /** - * Tests the case where a provider method is the source of the + * Tests the case where a provider method is the source of the + * * @throws Exception */ public void testGetSourceName_method() throws Exception { Member method = Obj.class.getDeclaredMethod("method", String.class); - assertEquals("Method should be identified by its file name and line number", - "ShortNameFactoryTest.java:53", nameFactory.getSourceName(method)); + assertEquals( + "Method should be identified by its file name and line number", + "ShortNameFactoryTest.java:52", + nameFactory.getSourceName(method)); } public void testGetSourceName_stackTraceElement() throws Exception { - StackTraceElement element = + StackTraceElement element = (StackTraceElement) StackTraceElements.forMember(Obj.class.getField("field")); - assertEquals("Stack trace element should be identified by its file name and line number", - "ShortNameFactoryTest.java:52", nameFactory.getSourceName(element)); + assertEquals( + "Stack trace element should be identified by its file name and line number", + "ShortNameFactoryTest.java:50", + nameFactory.getSourceName(element)); } public void testGetInstanceName_defaultToString() throws Exception { - assertEquals("Should use class name instead of Object#toString()", - "ShortNameFactoryTest$Obj", nameFactory.getInstanceName(new Obj())); + assertEquals( + "Should use class name instead of Object#toString()", + "ShortNameFactoryTest$Obj", + nameFactory.getInstanceName(new Obj())); } - + public void testGetInstanceName_customToString() throws Exception { - assertEquals("Should use class's toString() method since it's defined", - "I'm a ToStringObj", nameFactory.getInstanceName(new ToStringObj())); + assertEquals( + "Should use class's toString() method since it's defined", + "I'm a ToStringObj", + nameFactory.getInstanceName(new ToStringObj())); } - + public void testGetInstanceName_string() throws Exception { - assertEquals("String should have quotes to evoke a string literal", - "\"My String Instance\"", nameFactory.getInstanceName("My String Instance")); + assertEquals( + "String should have quotes to evoke a string literal", + "\"My String Instance\"", + nameFactory.getInstanceName("My String Instance")); } - + public void testGetInstanceName_providerMethod() throws Exception { final ProviderMethod[] methodHolder = new ProviderMethod[1]; - + Injector injector = Guice.createInjector(new ProvidingModule()); - injector.getBinding(Integer.class).acceptTargetVisitor( - new DefaultBindingTargetVisitor() { - @SuppressWarnings("unchecked") @Override - public Void visit(ProviderInstanceBinding binding) { - methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider(); - return null; - } - }); - - assertEquals("Method provider should pretty print as the method signature", - "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0])); - } - + injector + .getBinding(Integer.class) + .acceptTargetVisitor( + new DefaultBindingTargetVisitor() { + @SuppressWarnings("unchecked") + @Override + public Void visit(ProviderInstanceBinding binding) { + methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider(); + return null; + } + }); + + assertEquals( + "Method provider should pretty print as the method signature", + "#provideInteger(String)", + nameFactory.getInstanceName(methodHolder[0])); + } + private static class ProvidingModule extends AbstractModule { @Override protected void configure() {} - + @Provides public Integer provideInteger(String string) { return Integer.valueOf(string); diff --git a/extensions/grapher/test/com/google/inject/grapher/TransitiveDependencyVisitorTest.java b/extensions/grapher/test/com/google/inject/grapher/TransitiveDependencyVisitorTest.java index 663c3373b7..39ea491408 100644 --- a/extensions/grapher/test/com/google/inject/grapher/TransitiveDependencyVisitorTest.java +++ b/extensions/grapher/test/com/google/inject/grapher/TransitiveDependencyVisitorTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2008 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -35,11 +35,9 @@ import com.google.inject.spi.ProviderBinding; import com.google.inject.spi.ProviderInstanceBinding; import com.google.inject.spi.ProviderKeyBinding; - -import junit.framework.TestCase; - import java.util.Collection; import java.util.Set; +import junit.framework.TestCase; /** * Tests for {@link TransitiveDependencyVisitor}. @@ -48,45 +46,42 @@ */ public class TransitiveDependencyVisitorTest extends TestCase { private TransitiveDependencyVisitor visitor; - + @Override protected void setUp() throws Exception { super.setUp(); - + visitor = new TransitiveDependencyVisitor(); } public void testVisitConstructor() { Binding binding = getBinding(Key.get(ConstructedClass.class)); Collection> dependencies = visitor.visit((ConstructorBinding) binding); - - assertDependencies(dependencies, Key.get(A.class), Key.get(B.class), Key.get(C.class), - Key.get(D.class)); + + assertDependencies( + dependencies, Key.get(A.class), Key.get(B.class), Key.get(C.class), Key.get(D.class)); } public void testVisitConvertedConstant() { - Binding binding = getBinding(Key.get(Integer.class, Names.named("number")), - new ConvertedConstantModule()); - Collection> dependencies = visitor.visit( - (ConvertedConstantBinding) binding); - + Binding binding = + getBinding(Key.get(Integer.class, Names.named("number")), new ConvertedConstantModule()); + Collection> dependencies = visitor.visit((ConvertedConstantBinding) binding); + assertDependencies(dependencies, Key.get(String.class, Names.named("number"))); } public void testVisitInstance() { Binding binding = getBinding(Key.get(ConstructedClass.class), new InstanceModule()); - Collection> dependencies = visitor.visit( - (InstanceBinding) binding); - + Collection> dependencies = visitor.visit((InstanceBinding) binding); + // Dependencies will only be on the field- and method-injected classes. assertDependencies(dependencies, Key.get(A.class), Key.get(D.class)); } public void testVisitInstance_instanceHasDependencies() { Binding binding = getBinding(Key.get(Interface.class), new HasDependenciesModule()); - Collection> dependencies = visitor.visit( - (InstanceBinding) binding); - + Collection> dependencies = visitor.visit((InstanceBinding) binding); + // Dependencies should only be on the stated // HasDependencies#getDependencies() values assertDependencies(dependencies, Key.get(G.class)); @@ -103,16 +98,14 @@ public void testVisitLinkedKey() { public void testVisitProviderBinding() { Binding binding = getBinding(Key.get(new TypeLiteral>() {})); Collection> dependencies = visitor.visit((ProviderBinding) binding); - + assertDependencies(dependencies, Key.get(ConstructedClass.class)); } public void testVisitProviderInstance() { - Binding binding = getBinding(Key.get(ConstructedClass.class), - new ProviderInstanceModule()); - Collection> dependencies = visitor.visit( - (ProviderInstanceBinding) binding); - + Binding binding = getBinding(Key.get(ConstructedClass.class), new ProviderInstanceModule()); + Collection> dependencies = visitor.visit((ProviderInstanceBinding) binding); + // Dependencies will only be on the field- and method-injected classes. assertDependencies(dependencies, Key.get(E.class), Key.get(F.class)); } @@ -124,15 +117,15 @@ public void testVisitProviderKey() { // Dependency should be to the class that provides this one. assertDependencies(dependencies, Key.get(ConstructedClassProvider.class)); } - + private Binding getBinding(Key key, Module... modules) { return Guice.createInjector(modules).getBinding(key); } - + private void assertDependencies(Collection> dependencies, Key... keys) { assertNotNull("Dependencies should not be null", dependencies); - assertEquals("There should be " + keys.length + " dependencies", - keys.length, dependencies.size()); + assertEquals( + "There should be " + keys.length + " dependencies", keys.length, dependencies.size()); for (Key key : keys) { assertTrue("Dependencies should contain " + key, dependencies.contains(key)); @@ -140,28 +133,44 @@ private void assertDependencies(Collection> dependencies, Key... keys) } private static class A {} + private static class B {} + private static class C {} + private static class D {} + private static class E {} + private static class F {} + private static class G {} - + private static interface Interface {} - + private static class ConstructedClass implements Interface { @Inject A a; + ConstructedClass() {} - @Inject ConstructedClass(B b, C c) {} - @Inject void setD(D d) {} + + @Inject + ConstructedClass(B b, C c) {} + + @Inject + void setD(D d) {} } private static class ConstructedClassProvider implements Provider { @Inject E e; + ConstructedClassProvider() {} - @Inject ConstructedClassProvider(A a, B b, C c) {} - @Inject void setF(F f) {} - + + @Inject + ConstructedClassProvider(A a, B b, C c) {} + + @Inject + void setF(F f) {} + @Override public ConstructedClass get() { return null; @@ -171,7 +180,7 @@ public ConstructedClass get() { private static class HasDependenciesClass implements Interface, HasDependencies { @Inject A a; @Inject B b; - + @Override public Set> getDependencies() { return ImmutableSet.>of(Dependency.get(Key.get(G.class))); @@ -198,7 +207,7 @@ protected void configure() { bind(Interface.class).to(ConstructedClass.class); } } - + private static class ProviderInstanceModule extends AbstractModule { @Override protected void configure() { @@ -212,7 +221,7 @@ protected void configure() { bind(Interface.class).toInstance(new HasDependenciesClass()); } } - + private static class ProviderKeyModule extends AbstractModule { @Override protected void configure() { diff --git a/extensions/persist/src/com/google/inject/persist/finder/DynamicFinder.java b/extensions/persist/src/com/google/inject/persist/finder/DynamicFinder.java index 3bcd931552..6be2c11785 100644 --- a/extensions/persist/src/com/google/inject/persist/finder/DynamicFinder.java +++ b/extensions/persist/src/com/google/inject/persist/finder/DynamicFinder.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2010 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/extensions/persist/src/com/google/inject/persist/finder/Finder.java b/extensions/persist/src/com/google/inject/persist/finder/Finder.java index 7bfbc92761..d0c669e065 100644 --- a/extensions/persist/src/com/google/inject/persist/finder/Finder.java +++ b/extensions/persist/src/com/google/inject/persist/finder/Finder.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2010 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -32,20 +32,18 @@ @Retention(RetentionPolicy.RUNTIME) public @interface Finder { /** - * Returns the configured named query's name. Specify a named query's name - * here. This name is typically specified in your JPA configuration. + * Returns the configured named query's name. Specify a named query's name here. This name is + * typically specified in your JPA configuration. */ String namedQuery() default ""; - /** - * Returns the configured query string. Directly specify a JPAQL query here. - */ + /** Returns the configured query string. Directly specify a JPAQL query here. */ String query() default ""; /** - * Returns the configured autoboxing collection class. - * Use this clause to specify a collection impl to autobox result lists into. The impl must - * have a default no-arg constructor and be a subclass of {@code java.util.Collection}. + * Returns the configured autoboxing collection class. Use this clause to specify a collection + * impl to autobox result lists into. The impl must have a default no-arg constructor and be a + * subclass of {@code java.util.Collection}. */ Class returnAs() default Collection.class; -} \ No newline at end of file +} diff --git a/extensions/persist/src/com/google/inject/persist/finder/FirstResult.java b/extensions/persist/src/com/google/inject/persist/finder/FirstResult.java index 0feed7e3c2..7d9a21438d 100644 --- a/extensions/persist/src/com/google/inject/persist/finder/FirstResult.java +++ b/extensions/persist/src/com/google/inject/persist/finder/FirstResult.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2010 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,13 +22,12 @@ import java.lang.annotation.Target; /** - * Annotate any dynamic finder method's integer argument with this to pass in - * the index of the first result in the result set you are interested in. - * Useful for paging result sets. Complemented by {@link MaxResults}. + * Annotate any dynamic finder method's integer argument with this to pass in the index of the first + * result in the result set you are interested in. Useful for paging result sets. Complemented by + * {@link MaxResults}. * * @author Dhanji R. Prasanna (dhanji@gmail.com) */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) -public @interface FirstResult { -} +public @interface FirstResult {} diff --git a/extensions/persist/src/com/google/inject/persist/finder/MaxResults.java b/extensions/persist/src/com/google/inject/persist/finder/MaxResults.java index 69223d0334..3f9dd433a3 100644 --- a/extensions/persist/src/com/google/inject/persist/finder/MaxResults.java +++ b/extensions/persist/src/com/google/inject/persist/finder/MaxResults.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2010 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,13 +22,11 @@ import java.lang.annotation.Target; /** - * Annotate any dynamic finder method's integer argument with this to pass in - * the maximum size of returned result window. Usefule for paging result sets. - * Complement of {@link FirstResult}. + * Annotate any dynamic finder method's integer argument with this to pass in the maximum size of + * returned result window. Usefule for paging result sets. Complement of {@link FirstResult}. * * @author Dhanji R. Prasanna (dhanji@gmail.com) */ @Target(ElementType.PARAMETER) @Retention(RetentionPolicy.RUNTIME) -public @interface MaxResults { -} +public @interface MaxResults {} diff --git a/extensions/persist/src/com/google/inject/persist/finder/package-info.java b/extensions/persist/src/com/google/inject/persist/finder/package-info.java index 279929cfd0..bc4eb6e45d 100644 --- a/extensions/persist/src/com/google/inject/persist/finder/package-info.java +++ b/extensions/persist/src/com/google/inject/persist/finder/package-info.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2010 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,7 +14,5 @@ * limitations under the License. */ -/** - * Dynamic Finder API for Guice Persist. - */ +/** Dynamic Finder API for Guice Persist. */ package com.google.inject.persist.finder; diff --git a/extensions/struts2/example/src/com/google/inject/struts2/example/Count.java b/extensions/struts2/example/src/com/google/inject/struts2/example/Count.java index 76e7ec957c..8379cdf472 100644 --- a/extensions/struts2/example/src/com/google/inject/struts2/example/Count.java +++ b/extensions/struts2/example/src/com/google/inject/struts2/example/Count.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,9 +16,10 @@ package com.google.inject.struts2.example; -import com.google.inject.Inject; import static com.opensymphony.xwork2.Action.SUCCESS; +import com.google.inject.Inject; + public class Count { final Counter counter; diff --git a/extensions/struts2/example/src/com/google/inject/struts2/example/Counter.java b/extensions/struts2/example/src/com/google/inject/struts2/example/Counter.java index c375b9815d..251bc09b48 100644 --- a/extensions/struts2/example/src/com/google/inject/struts2/example/Counter.java +++ b/extensions/struts2/example/src/com/google/inject/struts2/example/Counter.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,9 +18,7 @@ import com.google.inject.servlet.SessionScoped; -/** - * Session-scoped counter. - */ +/** Session-scoped counter. */ @SessionScoped public class Counter { diff --git a/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java b/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java index 244d34b9c1..c865be9833 100644 --- a/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java +++ b/extensions/struts2/example/src/com/google/inject/struts2/example/ExampleListener.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,14 +16,13 @@ package com.google.inject.struts2.example; -import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter; - import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Singleton; import com.google.inject.servlet.GuiceServletContextListener; import com.google.inject.servlet.ServletModule; import com.google.inject.struts2.Struts2GuicePluginModule; +import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter; /** * Example application module. @@ -34,18 +33,17 @@ public class ExampleListener extends GuiceServletContextListener { public Injector getInjector() { return Guice.createInjector( - new Struts2GuicePluginModule(), - new ServletModule() { - @Override - protected void configureServlets() { - // Struts 2 setup - bind(StrutsPrepareAndExecuteFilter.class).in(Singleton.class); - filter("/*").through(StrutsPrepareAndExecuteFilter.class); + new Struts2GuicePluginModule(), + new ServletModule() { + @Override + protected void configureServlets() { + // Struts 2 setup + bind(StrutsPrepareAndExecuteFilter.class).in(Singleton.class); + filter("/*").through(StrutsPrepareAndExecuteFilter.class); - // Our app-specific code - bind(Service.class).to(ServiceImpl.class); - } - }); + // Our app-specific code + bind(Service.class).to(ServiceImpl.class); + } + }); } - } diff --git a/extensions/struts2/example/src/com/google/inject/struts2/example/Main.java b/extensions/struts2/example/src/com/google/inject/struts2/example/Main.java index eb4f0e224d..fb2c4a4d40 100644 --- a/extensions/struts2/example/src/com/google/inject/struts2/example/Main.java +++ b/extensions/struts2/example/src/com/google/inject/struts2/example/Main.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -21,9 +21,7 @@ import org.mortbay.jetty.nio.SelectChannelConnector; import org.mortbay.jetty.webapp.WebAppContext; -/** - * Starts the example web server on port 8080. Run from "./struts2/example". - */ +/** Starts the example web server on port 8080. Run from "./struts2/example". */ public class Main { public static void main(String[] args) throws Exception { @@ -31,7 +29,7 @@ public static void main(String[] args) throws Exception { Connector connector = new SelectChannelConnector(); connector.setPort(8080); - server.setConnectors(new Connector[] { connector }); + server.setConnectors(new Connector[] {connector}); WebAppContext webapp = new WebAppContext("./root", "/example"); server.addHandler(webapp);