Revision: 1578
Author:   [email protected]
Date:     Fri Sep  9 14:19:11 2011
Log:
Fix issue 644. Print a better error message when requestStaticInjection is called on an interface, fix it so that static injection errors show the source of the static injection.


Revision created by MOE tool push_codebase.
MOE_MIGRATION=3070

http://code.google.com/p/google-guice/source/detail?r=1578

Modified:
 /trunk/core/src/com/google/inject/internal/Errors.java
 /trunk/core/src/com/google/inject/internal/InjectionRequestProcessor.java
 /trunk/core/src/com/google/inject/spi/InjectionPoint.java
 /trunk/core/test/com/google/inject/InjectorTest.java
/trunk/extensions/multibindings/test/com/google/inject/multibindings/MultibinderTest.java

=======================================
--- /trunk/core/src/com/google/inject/internal/Errors.java Thu Jul 7 17:34:16 2011 +++ /trunk/core/src/com/google/inject/internal/Errors.java Fri Sep 9 14:19:11 2011
@@ -267,6 +267,10 @@
return addMessage("%s has more than one annotation annotated with @BindingAnnotation: "
         + "%s and %s", member, a, b);
   }
+
+  public Errors staticInjectionOnInterface(Class<?> clazz) {
+ return addMessage("%s is an interface, but interfaces have no static injection points.", clazz);
+  }

   public Errors cannotInjectFinalField(Field field) {
     return addMessage("Injected field %s cannot be final.", field);
=======================================
--- /trunk/core/src/com/google/inject/internal/InjectionRequestProcessor.java Thu Jul 7 17:34:16 2011 +++ /trunk/core/src/com/google/inject/internal/InjectionRequestProcessor.java Fri Sep 9 14:19:11 2011
@@ -99,11 +99,17 @@
       try {
         injectionPoints = request.getInjectionPoints();
       } catch (ConfigurationException e) {
-        errors.merge(e.getErrorMessages());
+        errorsForMember.merge(e.getErrorMessages());
         injectionPoints = e.getPartialValue();
       }
-      memberInjectors = injector.membersInjectorStore.getInjectors(
-          injectionPoints, errorsForMember);
+      if (injectionPoints != null) {
+        memberInjectors = injector.membersInjectorStore.getInjectors(
+            injectionPoints, errorsForMember);
+      } else {
+        memberInjectors = ImmutableList.of();
+      }
+
+      errors.merge(errorsForMember);
     }

     void injectMembers() {
=======================================
--- /trunk/core/src/com/google/inject/spi/InjectionPoint.java Thu Jul 7 17:34:16 2011 +++ /trunk/core/src/com/google/inject/spi/InjectionPoint.java Fri Sep 9 14:19:11 2011
@@ -319,8 +319,16 @@
    */
public static Set<InjectionPoint> forStaticMethodsAndFields(TypeLiteral<?> type) {
     Errors errors = new Errors();
-
-    Set<InjectionPoint> result = getInjectionPoints(type, true, errors);
+
+    Set<InjectionPoint> result;
+
+    if (type.getRawType().isInterface()) {
+      errors.staticInjectionOnInterface(type.getRawType());
+      result = null;
+    } else {
+      result = getInjectionPoints(type, true, errors);
+    }
+
     if (errors.hasErrors()) {
throw new ConfigurationException(errors.getMessages()).withPartialValue(result);
     }
=======================================
--- /trunk/core/test/com/google/inject/InjectorTest.java Thu Jul 7 17:34:16 2011 +++ /trunk/core/test/com/google/inject/InjectorTest.java Fri Sep 9 14:19:11 2011
@@ -214,6 +214,27 @@
     assertEquals("test", Static.s);
     assertEquals(5, Static.i);
   }
+
+  public void testInjectStaticInterface() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+        protected void configure() {
+          requestStaticInjection(Interface.class);
+        }
+      });
+      fail();
+    } catch(CreationException ce) {
+      assertEquals(1, ce.getErrorMessages().size());
+      Asserts.assertContains(
+          ce.getMessage(),
+          "1) " + Interface.class.getName()
+ + " is an interface, but interfaces have no static injection points.",
+          "at " + InjectorTest.class.getName(),
+          "configure");
+    }
+  }
+
+  private static interface Interface {}

   static class Static {

=======================================
--- /trunk/extensions/multibindings/test/com/google/inject/multibindings/MultibinderTest.java Mon Aug 8 22:44:38 2011 +++ /trunk/extensions/multibindings/test/com/google/inject/multibindings/MultibinderTest.java Fri Sep 9 14:19:11 2011
@@ -351,7 +351,7 @@
       injector.getInstance(Key.get(setOfString));
       fail();
     } catch(ProvisionException expected) {
-      assertContains(expected.getMessage(),
+      assertContains(expected.getMessage(),
           "1) Set injection failed due to null element");
     }
   }
@@ -365,7 +365,7 @@
       });
       fail();
     } catch (CreationException expected) {
- assertContains(expected.getMessage(), "No implementation for java.lang.Integer", + assertContains(expected.getMessage(), "No implementation for java.lang.Integer",
           "at " + getClass().getName());
     }
   }
@@ -393,7 +393,7 @@
     }
     assertEquals(ImmutableSet.of("A", "B"), elements);
   }
-
+
   /**
* We just want to make sure that multibinder's binding depends on each of its values. We don't * really care about the underlying structure of those bindings, which are implementation details.
@@ -404,7 +404,7 @@
Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
           multibinder.addBinding().toInstance("A");
multibinder.addBinding().to(Key.get(String.class, Names.named("b")));
-
+
           bindConstant().annotatedWith(Names.named("b")).to("B");
         }});

@@ -416,8 +416,8 @@
// the right values are returned -- in tool stage we can't do that. It's also a // little difficult to validate the dependencies & bindings, because they're
     // bindings created internally within Multibinder.
- // To workaround this, we just validate that the dependencies lookup to a single - // InstanceBinding whose value is "A" and another LinkedBinding whose target is + // To workaround this, we just validate that the dependencies lookup to a single + // InstanceBinding whose value is "A" and another LinkedBinding whose target is
     // the Key of @Named("b") String=B
     for (Dependency<?> dependency : withDependencies.getDependencies()) {
       Binding<?> b = injector.getBinding(dependency.getKey());
@@ -437,10 +437,10 @@
         fail("Unexpected dependency of: " + dependency);
       }
     }
-
+
     assertNotNull(instanceBinding);
     assertNotNull(linkedBinding);
-
+
     assertEquals("A", instanceBinding.getInstance());
assertEquals(Key.get(String.class, Names.named("b")), linkedBinding.getLinkedKey());
   }
@@ -599,18 +599,4 @@
     expected.add(2);
     assertEquals(expected, s1);
   }
-
-  public void testSetAndMapValueConflict() {
-    Injector injector = Guice.createInjector(new AbstractModule() {
-      @Override protected void configure() {
-        Multibinder.newSetBinder(binder(), String.class)
-            .addBinding().toInstance("A");
-
-        MapBinder.newMapBinder(binder(), String.class, String.class)
-            .addBinding("B").toInstance("b");
-      }
-    });
-
- assertEquals(ImmutableSet.<String>of("A"), injector.getInstance(Key.get(setOfString)));
-  }
-}
+}

--
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