Revision: 1192
Author: sberlin
Date: Sat Jul 31 09:08:27 2010
Log: issue 506 - fail fast & with a useful error message when an AssistedInject factory has Provider<T> as a factory type. patch ( slightly modified) by ffaber.
http://code.google.com/p/google-guice/source/detail?r=1192

Modified:
/trunk/extensions/assistedinject/src/com/google/inject/assistedinject/FactoryModuleBuilder.java /trunk/extensions/assistedinject/src/com/google/inject/assistedinject/FactoryProvider2.java /trunk/extensions/assistedinject/test/com/google/inject/assistedinject/FactoryProvider2Test.java

=======================================
--- /trunk/extensions/assistedinject/src/com/google/inject/assistedinject/FactoryModuleBuilder.java Sat Jul 17 09:39:00 2010 +++ /trunk/extensions/assistedinject/src/com/google/inject/assistedinject/FactoryModuleBuilder.java Sat Jul 31 09:08:27 2010
@@ -173,6 +173,10 @@
  *       .implement(Car.class, Names.named("clean"), Prius.class)
  *       .build(CarFactory.class));
  * }</pre>
+ *
+ * <h3>Implementation limitations</h3>
+ * As a limitation of the implementation, it is prohibited to declare a factory method that
+ * accepts a {...@code Provider} as one of its arguments.
  *
  * @author [email protected] (Peter Schmitt)
  */
=======================================
--- /trunk/extensions/assistedinject/src/com/google/inject/assistedinject/FactoryProvider2.java Sat Jul 17 09:39:00 2010 +++ /trunk/extensions/assistedinject/src/com/google/inject/assistedinject/FactoryProvider2.java Sat Jul 31 09:08:27 2010
@@ -179,6 +179,13 @@
         List<Key<?>> keys = Lists.newArrayList();
         for (TypeLiteral<?> param : params) {
Key<?> paramKey = getKey(param, method, paramAnnotations[p++], errors); + Class<?> underlylingType = paramKey.getTypeLiteral().getRawType();
+          if (underlylingType.equals(Provider.class)
+              || underlylingType.equals(javax.inject.Provider.class)) {
+ errors.addMessage("A Provider may not be a type in a factory method of an AssistedInject." + + "\n Offending instance is parameter [%s] with key [%s] on method [%s]",
+                    p, paramKey, method);
+          }
           keys.add(assistKey(method, paramKey, errors));
         }
ImmutableList<Key<?>> immutableParamList = ImmutableList.copyOf(keys);
@@ -407,7 +414,7 @@
     }
     return false;
   }
-
+
   /**
* Returns a key similar to {...@code key}, but with an {...@literal @}Assisted binding annotation. * This fails if another binding annotation is clobbered in the process. If the key already has
@@ -466,7 +473,7 @@
final Key<?> assistedReturnType = Key.get(returnType.getTypeLiteral(), Assisted.class);

     Module assistedModule = new AbstractModule() {
- @SuppressWarnings("unchecked") // raw keys are necessary for the args array and return value + @Override @SuppressWarnings("unchecked") // raw keys are necessary for the args array and return value
       protected void configure() {
         Binder binder = binder().withSource(method);

=======================================
--- /trunk/extensions/assistedinject/test/com/google/inject/assistedinject/FactoryProvider2Test.java Wed Mar 24 20:37:35 2010 +++ /trunk/extensions/assistedinject/test/com/google/inject/assistedinject/FactoryProvider2Test.java Sat Jul 31 09:08:27 2010
@@ -559,6 +559,73 @@
     assertNull(subaru.colorProvider.get());
     assertNull(subaru.colorProvider.get());
   }
+
+  interface ProviderBasedColoredCarFactory {
+ Car createCar(Provider<Color> colorProvider, Provider<String> stringProvider); + Mustang createMustang(@Assisted("color") Provider<Color> colorProvider);
+  }
+
+  public void testAssistedProviderIsDisallowed() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+          @Override protected void configure() {
+            bind(ProviderBasedColoredCarFactory.class).toProvider(
+ FactoryProvider.newFactory(ProviderBasedColoredCarFactory.class, Subaru.class));
+          }
+      });
+      fail();
+    } catch (CreationException expected) {
+      assertContains(expected.getMessage(),
+ "1) A Provider may not be a type in a factory method of an AssistedInject."
+            + "\n  Offending instance is parameter [1] with key"
+            + " [com.google.inject.Provider<java.awt.Color>] on method ["
+ + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]", + "2) A Provider may not be a type in a factory method of an AssistedInject."
+            + "\n  Offending instance is parameter [2] with key"
+            + " [com.google.inject.Provider<java.lang.String>] on method ["
+ + ProviderBasedColoredCarFactory.class.getName() + ".createCar()]", + "3) A Provider may not be a type in a factory method of an AssistedInject."
+            + "\n  Offending instance is parameter [1] with key"
+            + " [com.google.inject.Provider<java.awt.Color>"
+ + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + + " on method [" + ProviderBasedColoredCarFactory.class.getName() + ".createMustang()]"
+      );
+
+    }
+  }
+
+  interface JavaxProviderBasedColoredCarFactory {
+ Car createCar(javax.inject.Provider<Color> colorProvider, javax.inject.Provider<String> stringProvider); + Mustang createMustang(@Assisted("color") javax.inject.Provider<Color> colorProvider);
+  }
+
+  public void testAssistedJavaxProviderIsDisallowed() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+          @Override protected void configure() {
+            bind(JavaxProviderBasedColoredCarFactory.class).toProvider(
+ FactoryProvider.newFactory(JavaxProviderBasedColoredCarFactory.class, Subaru.class));
+          }
+      });
+      fail();
+    } catch (CreationException expected) {
+      assertContains(expected.getMessage(),
+ "1) A Provider may not be a type in a factory method of an AssistedInject."
+            + "\n  Offending instance is parameter [1] with key"
+            + " [com.google.inject.Provider<java.awt.Color>] on method ["
+ + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]", + "2) A Provider may not be a type in a factory method of an AssistedInject."
+            + "\n  Offending instance is parameter [2] with key"
+            + " [com.google.inject.Provider<java.lang.String>] on method ["
+ + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]", + "3) A Provider may not be a type in a factory method of an AssistedInject."
+            + "\n  Offending instance is parameter [1] with key"
+            + " [com.google.inject.Provider<java.awt.Color>"
+ + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + + " on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createMustang()]"
+      );
+    }
+  }

   public void testFactoryUseBeforeInitialization() {
ColoredCarFactory carFactory = FactoryProvider.newFactory(ColoredCarFactory.class, Subaru.class)

--
You received this message because you are subscribed to the Google Groups 
"google-guice-dev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice-dev?hl=en.

Reply via email to