Revision: 8b7aaecd6164
Author:   Sam Berlin <[email protected]>
Date:     Sun May 27 10:42:13 2012
Log: Update ThrowingProviderBinder to have a better exception in case of ProvisionExceptions from more than one dependency.

Revision created by MOE tool push_codebase.
MOE_MIGRATION=4877

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

Modified:
/extensions/throwingproviders/src/com/google/inject/throwingproviders/ThrowingProviderBinder.java /extensions/throwingproviders/test/com/google/inject/throwingproviders/CheckedProviderTest.java

=======================================
--- /extensions/throwingproviders/src/com/google/inject/throwingproviders/ThrowingProviderBinder.java Sun May 27 10:40:27 2012 +++ /extensions/throwingproviders/src/com/google/inject/throwingproviders/ThrowingProviderBinder.java Sun May 27 10:42:13 2012
@@ -222,7 +222,11 @@
             } else if (pe.getCause() instanceof Error) {
               throw (Error) pe.getCause();
             } else {
-              throw new AssertionError(pe); // Impossible!
+              // If this failed because of multiple reasons (ie, more than
+              // one dependency failed due to scoping errors), then
+              // the ProvisionException won't have a cause, so we need
+              // to rethrow it as-is.
+              throw pe;
             }
           }
         }
@@ -265,6 +269,10 @@
               new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args)
                     throws Throwable {
+ // Allow methods like .equals(..), .hashcode(..), .toString(..) to work.
+                  if (method.getDeclaringClass() == Object.class) {
+                    return method.invoke(this, args);
+                  }
                   return resultProvider.get().getOrThrow();
                 }
               }));
=======================================
--- /extensions/throwingproviders/test/com/google/inject/throwingproviders/CheckedProviderTest.java Sun May 27 10:39:59 2012 +++ /extensions/throwingproviders/test/com/google/inject/throwingproviders/CheckedProviderTest.java Sun May 27 10:42:13 2012
@@ -20,6 +20,7 @@
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import com.google.inject.AbstractModule;
 import com.google.inject.Asserts;
 import com.google.inject.CreationException;
@@ -27,6 +28,11 @@
 import com.google.inject.Inject;
 import com.google.inject.Injector;
 import com.google.inject.Key;
+import com.google.inject.OutOfScopeException;
+import com.google.inject.Provider;
+import com.google.inject.ProvisionException;
+import com.google.inject.Scope;
+import com.google.inject.ScopeAnnotation;
 import com.google.inject.TypeLiteral;
 import com.google.inject.internal.util.Classes;
 import com.google.inject.name.Named;
@@ -39,6 +45,10 @@
 import junit.framework.TestCase;

 import java.io.IOException;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
 import java.net.BindException;
 import java.rmi.AccessException;
 import java.rmi.RemoteException;
@@ -1379,6 +1389,56 @@
     @Inject
     public NormalInjectableFoo() {
     }
+
+    @Override public String s() { return null; }
+  }
+
+ public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
+    Injector injector = Guice.createInjector(new AbstractModule() {
+        @Override
+        protected void configure() {
+          ThrowingProviderBinder.create(binder())
+              .bind(RemoteProvider.class, Foo.class)
+              .providing(ProvisionExceptionFoo.class);
+          bindScope(BadScope.class, new Scope() {
+            @Override
+ public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
+              return new Provider<T>() {
+                @Override
+                public T get() {
+                  throw new OutOfScopeException("failure");
+                }
+              };
+            }
+          });
+        }
+      });
+
+    try {
+      injector.getInstance(Key.get(remoteProviderOfFoo)).get();
+      fail();
+    } catch(ProvisionException pe) {
+      assertEquals(2, pe.getErrorMessages().size());
+      List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
+ assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure",
+          messages.get(0).getMessage());
+ assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure",
+          messages.get(1).getMessage());
+    }
+  }
+
+  @ScopeAnnotation
+  @Target(ElementType.TYPE)
+  @Retention(RetentionPolicy.RUNTIME)
+  private @interface BadScope { }
+
+  @BadScope private static class Unscoped1 {}
+  @BadScope private static class Unscoped2 {}
+
+  static class ProvisionExceptionFoo implements Foo {
+    @ThrowingInject
+    public ProvisionExceptionFoo(Unscoped1 a, Unscoped2 b) {
+    }

     @Override public String s() { return null; }
   }

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