Author: limpbizkit
Date: Sun Nov  2 01:37:28 2008
New Revision: 658

Modified:
    trunk/src/com/google/inject/internal/ModuleBinding.java
    trunk/test/com/google/inject/BinderTest.java

Log:
Tests and fix for bug 256.

Modified: trunk/src/com/google/inject/internal/ModuleBinding.java
==============================================================================
--- trunk/src/com/google/inject/internal/ModuleBinding.java     (original)
+++ trunk/src/com/google/inject/internal/ModuleBinding.java     Sun Nov  2  
01:37:28 2008
@@ -197,8 +197,7 @@
        checkNotTargetted();

        if (instance == null) {
-        binder.addError("Binding to null instances is not allowed. "
-            + "Use toProvider(Providers.of(null)) if this is your intended  
behaviour.");
+        binder.addError(BINDING_TO_NULL);
          // we finish the binding to prevent additional errors
          toProvider(Providers.<T>of(null));
          return;
@@ -421,14 +420,7 @@
        to(value.getDeclaringClass(), value);
      }

-    static final String CONSTANT_VALUE_ALREADY_SET = "Constant value is  
set more"
-        + " than once.";
-    static final String ANNOTATION_ALREADY_SPECIFIED = "More than one  
annotation"
-        + " is specified for this binding.";
-
      private void to(Class<?> type, Object instance) {
-      checkNotNull(instance, "instance");
-
        // this type will define T, so these assignments are safe
        @SuppressWarnings("unchecked")
        Class<T> typeAsClassT = (Class<T>) type;
@@ -450,6 +442,10 @@

        ModuleBinding.this.target = new InstanceTarget<T>(instanceAsT,
            ImmutableSet.<InjectionPoint>of());
+
+      if (instanceAsT == null) {
+        binder.addError(BINDING_TO_NULL);
+      }
      }

      @Override public String toString() {
@@ -482,4 +478,11 @@
        return visitor.visitInstance(instance, injectionPoints);
      }
    }
+
+  static final String BINDING_TO_NULL = "Binding to null instances is not  
allowed. "
+      + "Use toProvider(Providers.of(null)) if this is your intended  
behaviour.";
+  static final String CONSTANT_VALUE_ALREADY_SET = "Constant value is set  
more"
+      + " than once.";
+  static final String ANNOTATION_ALREADY_SPECIFIED = "More than one  
annotation"
+      + " is specified for this binding.";
  }

Modified: trunk/test/com/google/inject/BinderTest.java
==============================================================================
--- trunk/test/com/google/inject/BinderTest.java        (original)
+++ trunk/test/com/google/inject/BinderTest.java        Sun Nov  2 01:37:28 2008
@@ -149,6 +149,23 @@
      }
    }

+  public void testBindingNullConstant() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+        @Override public void configure() {
+          String none = null;
+          bindConstant().annotatedWith(Names.named("nullOne")).to(none);
+           
bind(String.class).annotatedWith(Names.named("nullTwo")).toInstance(none);
+        }
+      });
+      fail();
+    } catch (CreationException expected) {
+      assertContains(expected.getMessage(),
+          "1) Binding to null instances is not allowed. Use  
toProvider(Providers.of(null))",
+          "2) Binding to null instances is not allowed. Use  
toProvider(Providers.of(null))");
+    }
+  }
+
    public void testToStringOnBinderApi() {
      try {
        Guice.createInjector(new AbstractModule() {

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