Author: limpbizkit
Date: Sat Jun  6 11:32:54 2009
New Revision: 1004

Modified:
    trunk/src/com/google/inject/internal/ExposedBindingImpl.java
    trunk/src/com/google/inject/internal/InjectorShell.java
    trunk/src/com/google/inject/internal/MoreTypes.java
    trunk/src/com/google/inject/internal/ToStringBuilder.java
    trunk/test/com/google/inject/BinderTest.java
    trunk/test/com/google/inject/BoundInstanceInjectionTest.java
    trunk/test/com/google/inject/TypeListenerTest.java

Log:
I did some cleanup after the previous refactoring round, driven by a run of  
coverage.

Modified: trunk/src/com/google/inject/internal/ExposedBindingImpl.java
==============================================================================
--- trunk/src/com/google/inject/internal/ExposedBindingImpl.java        
(original)
+++ trunk/src/com/google/inject/internal/ExposedBindingImpl.java        Sat Jun 
 6  
11:32:54 2009
@@ -35,12 +35,6 @@
      this.privateElements = privateElements;
    }

-  public ExposedBindingImpl(Object source, Key<T> key, Scoping scoping,
-      PrivateElements privateElements) {
-    super(source, key, scoping);
-    this.privateElements = privateElements;
-  }
-
    public <V> V acceptTargetVisitor(BindingTargetVisitor<? super T, V>  
visitor) {
      return visitor.visit(this);
    }
@@ -51,14 +45,6 @@

    public PrivateElements getPrivateElements() {
      return privateElements;
-  }
-
-  public BindingImpl<T> withScoping(Scoping scoping) {
-    return new ExposedBindingImpl<T>(getSource(), getKey(), scoping,  
privateElements);
-  }
-
-  public ExposedBindingImpl<T> withKey(Key<T> key) {
-    return new ExposedBindingImpl<T>(getSource(), key, getScoping(),  
privateElements);
    }

    @Override public String toString() {

Modified: trunk/src/com/google/inject/internal/InjectorShell.java
==============================================================================
--- trunk/src/com/google/inject/internal/InjectorShell.java     (original)
+++ trunk/src/com/google/inject/internal/InjectorShell.java     Sat Jun  6  
11:32:54 2009
@@ -53,10 +53,6 @@
      this.injector = injector;
    }

-  private PrivateElements getPrivateElements() {
-    return privateElements;
-  }
-
    InjectorImpl getInjector() {
      return injector;
    }

Modified: trunk/src/com/google/inject/internal/MoreTypes.java
==============================================================================
--- trunk/src/com/google/inject/internal/MoreTypes.java (original)
+++ trunk/src/com/google/inject/internal/MoreTypes.java Sat Jun  6 11:32:54  
2009
@@ -133,17 +133,6 @@
      }
    }

-  /**
-   * Returns a type that's functionally equal but not necessarily equal
-   * according to {...@link Object#equals(Object) Object.equals}. The returned
-   * member is {...@link Serializable}.
-   */
-  public static Member serializableCopy(Member member) {
-    return member instanceof MemberImpl
-        ? member
-        : new MemberImpl(member);
-  }
-
    public static Class<?> getRawType(Type type) {
      if (type instanceof Class<?>) {
        // type is a normal class.
@@ -321,10 +310,7 @@
    public static Class<? extends Member> memberType(Member member) {
      checkNotNull(member, "member");

-    if (member instanceof MemberImpl) {
-      return ((MemberImpl) member).memberType;
-
-    } else if (member instanceof Field) {
+    if (member instanceof Field) {
        return Field.class;

      } else if (member instanceof Method) {
@@ -361,10 +347,7 @@
      checkNotNull(member, "member");

      /*if[AOP]*/
-    if (member instanceof MemberImpl) {
-      return ((MemberImpl) member).memberKey;
-
-    } else if (member instanceof Field) {
+    if (member instanceof Field) {
        return member.getName();

      } else if (member instanceof Method) {
@@ -627,49 +610,6 @@
    private static void checkNotPrimitive(Type type, String use) {
      checkArgument(!(type instanceof Class<?>) || !((Class)  
type).isPrimitive(),
          "Primitive types are not allowed in %s: %s", use, type);
-  }
-
-  /**
-   * We cannot serialize the built-in Java member classes, which prevents  
us from using Members in
-   * our exception types. We workaround this with this serializable  
implementation. It includes all
-   * of the API methods, plus everything we use for line numbers and  
messaging.
-   */
-  public static class MemberImpl implements Member, Serializable {
-    private final Class<?> declaringClass;
-    private final String name;
-    private final int modifiers;
-    private final boolean synthetic;
-    private final Class<? extends Member> memberType;
-    private final String memberKey;
-
-    private MemberImpl(Member member) {
-      this.declaringClass = member.getDeclaringClass();
-      this.name = member.getName();
-      this.modifiers = member.getModifiers();
-      this.synthetic = member.isSynthetic();
-      this.memberType = memberType(member);
-      this.memberKey = memberKey(member);
-    }
-
-    public Class getDeclaringClass() {
-      return declaringClass;
-    }
-
-    public String getName() {
-      return name;
-    }
-
-    public int getModifiers() {
-      return modifiers;
-    }
-
-    public boolean isSynthetic() {
-      return synthetic;
-    }
-
-    @Override public String toString() {
-      return MoreTypes.toString(this);
-    }
    }

    /** A type formed from other types, such as arrays, parameterized types  
or wildcard types */

Modified: trunk/src/com/google/inject/internal/ToStringBuilder.java
==============================================================================
--- trunk/src/com/google/inject/internal/ToStringBuilder.java   (original)
+++ trunk/src/com/google/inject/internal/ToStringBuilder.java   Sat Jun  6  
11:32:54 2009
@@ -31,10 +31,6 @@

    final String name;

-  public ToStringBuilder(String name) {
-    this.name = name;
-  }
-
    public ToStringBuilder(Class type) {
      this.name = type.getSimpleName();
    }

Modified: trunk/test/com/google/inject/BinderTest.java
==============================================================================
--- trunk/test/com/google/inject/BinderTest.java        (original)
+++ trunk/test/com/google/inject/BinderTest.java        Sat Jun  6 11:32:54 2009
@@ -18,7 +18,9 @@

  import static com.google.inject.Asserts.assertContains;
  import static com.google.inject.Asserts.assertNotSerializable;
+import com.google.inject.internal.ImmutableList;
  import com.google.inject.internal.Iterables;
+import com.google.inject.internal.Lists;
  import com.google.inject.name.Named;
  import com.google.inject.name.Names;
  import com.google.inject.spi.Message;
@@ -28,6 +30,9 @@
  import java.util.Date;
  import java.util.List;
  import java.util.concurrent.Callable;
+import java.util.logging.Handler;
+import java.util.logging.LogRecord;
+import java.util.logging.Logger;
  import junit.framework.TestCase;

  /**
@@ -35,8 +40,30 @@
   */
  public class BinderTest extends TestCase {

+  private final Logger loggerToWatch =  
Logger.getLogger(Guice.class.getName());
+
+  private final List<LogRecord> logRecords = Lists.newArrayList();
+  private final Handler fakeHandler = new Handler() {
+    public void publish(LogRecord logRecord) {
+      logRecords.add(logRecord);
+    }
+
+    public void flush() {}
+    public void close() throws SecurityException {}
+  };
+
    Provider<Foo> fooProvider;

+  @Override protected void setUp() throws Exception {
+    super.setUp();
+    loggerToWatch.addHandler(fakeHandler);
+  }
+
+  @Override protected void tearDown() throws Exception {
+    loggerToWatch.removeHandler(fakeHandler);
+    super.tearDown();
+  }
+
    public void testProviderFromBinder() {
      Guice.createInjector(new Module() {
        public void configure(Binder binder) {
@@ -296,6 +323,22 @@
      } catch (CreationException expected) {
        assertSame(message,  
Iterables.getOnlyElement(expected.getErrorMessages()));
      }
+  }
+
+  public void testUserReportedErrorsAreAlsoLogged() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+        protected void configure() {
+          addError(new Message(ImmutableList.of(), "Whoops!", new  
IllegalArgumentException()));
+        }
+      });
+      fail();
+    } catch (CreationException expected) {
+    }
+
+    LogRecord logRecord = Iterables.getOnlyElement(this.logRecords);
+    assertContains(logRecord.getMessage(),
+        "An exception was caught and reported. Message:  
java.lang.IllegalArgumentException");
    }

    public void testBindingToProvider() {

Modified: trunk/test/com/google/inject/BoundInstanceInjectionTest.java
==============================================================================
--- trunk/test/com/google/inject/BoundInstanceInjectionTest.java        
(original)
+++ trunk/test/com/google/inject/BoundInstanceInjectionTest.java        Sat Jun 
 6  
11:32:54 2009
@@ -16,6 +16,13 @@

  package com.google.inject;

+import com.google.inject.name.Named;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+import static java.lang.annotation.ElementType.PARAMETER;
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import java.lang.annotation.Target;
  import junit.framework.TestCase;

  /**
@@ -62,4 +69,48 @@
      assertEquals(5, injector.getInstance(O.class).fromMethod);
    }

+  public void testMalformedInstance() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+        protected void configure() {
+          bind(Object.class).toInstance(new MalformedInjectable());
+        }
+      });
+      fail();
+    } catch (CreationException expected) {
+      Asserts.assertContains(expected.getMessage(),  
MalformedInjectable.class.getName(),
+          ".doublyAnnotated() has more than one ", "annotation annotated  
with @BindingAnnotation: ",
+          Named.class.getName() + " and " + Another.class.getName());
+    }
+  }
+
+  public void testMalformedProvider() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+        protected void configure() {
+          bind(String.class).toProvider(new MalformedProvider());
+        }
+      });
+      fail();
+    } catch (CreationException expected) {
+      Asserts.assertContains(expected.getMessage(),  
MalformedProvider.class.getName(),
+          ".doublyAnnotated() has more than one ", "annotation annotated  
with @BindingAnnotation: ",
+          Named.class.getName() + " and " + Another.class.getName());
+    }
+  }
+
+  static class MalformedInjectable {
+    @Inject void doublyAnnotated(@Named("a") @Another String unused) {}
+  }
+
+  static class MalformedProvider implements Provider<String> {
+    @Inject void doublyAnnotated(@Named("a") @Another String s) {}
+
+    public String get() {
+      return "a";
+    }
+  }
+
+  @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD })  
@Retention(RUNTIME)
+  public @interface Another {}
  }

Modified: trunk/test/com/google/inject/TypeListenerTest.java
==============================================================================
--- trunk/test/com/google/inject/TypeListenerTest.java  (original)
+++ trunk/test/com/google/inject/TypeListenerTest.java  Sat Jun  6 11:32:54  
2009
@@ -25,6 +25,7 @@
  import static com.google.inject.matcher.Matchers.only;
  import static com.google.inject.name.Names.named;
  import com.google.inject.spi.InjectionListener;
+import com.google.inject.spi.Message;
  import com.google.inject.spi.TypeEncounter;
  import com.google.inject.spi.TypeListener;
  import java.util.List;
@@ -585,6 +586,29 @@
        encounter.getProvider(B.class);
        fail();
      } catch (IllegalStateException expected) {
+    }
+  }
+
+  public void testAddErrors() {
+    try {
+      Guice.createInjector(new AbstractModule() {
+        protected void configure() {
+          bindListener(Matchers.only(new TypeLiteral<Stage>() {}), new  
TypeListener() {
+            public <I> void hear(TypeLiteral<I> type, TypeEncounter<I>  
encounter) {
+              encounter.addError("There was an error on %s", type);
+              encounter.addError(new IllegalArgumentException("whoops!"));
+              encounter.addError(new Message("And another problem"));
+            }
+          });
+        }
+      });
+      fail();
+    } catch (CreationException expected) {
+      assertContains(expected.getMessage(),
+          "1) There was an error on com.google.inject.Stage",
+          "2) An exception was caught and reported. Message: whoops!",
+          "3) And another problem",
+          "3 errors");
      }
    }


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