Author: limpbizkit
Date: Sat Nov 29 14:01:13 2008
New Revision: 707
Modified:
trunk/src/com/google/inject/spi/InjectionPoint.java
trunk/test/com/google/inject/ProvisionExceptionTest.java
Log:
Fix for bug 242.
We warn if the user misplaces a binding annotation. In Scala, binding
annotations are applied to accessor methods automatically, and there's
nothing the user can do (to suppress the warning). So we special case it
and don't warn. There might be a tiny number of false negatives; this
shouldn't really be a problem.
Modified: trunk/src/com/google/inject/spi/InjectionPoint.java
==============================================================================
--- trunk/src/com/google/inject/spi/InjectionPoint.java (original)
+++ trunk/src/com/google/inject/spi/InjectionPoint.java Sat Nov 29 14:01:13
2008
@@ -322,9 +322,22 @@
private static void checkForMisplacedBindingAnnotations(Member member,
Errors errors) {
Annotation misplacedBindingAnnotation =
Annotations.findBindingAnnotation(
errors, member, ((AnnotatedElement) member).getAnnotations());
- if (misplacedBindingAnnotation != null) {
- errors.misplacedBindingAnnotation(member,
misplacedBindingAnnotation);
+ if (misplacedBindingAnnotation == null) {
+ return;
}
+
+ // don't warn about misplaced binding annotations on methods when
there's a field with the same
+ // name. In Scala, fields always get accessor methods (that we need to
ignore). See bug 242.
+ if (member instanceof Method) {
+ try {
+ if
(member.getDeclaringClass().getDeclaredField(member.getName()) != null) {
+ return;
+ }
+ } catch (NoSuchFieldException ignore) {
+ }
+ }
+
+ errors.misplacedBindingAnnotation(member, misplacedBindingAnnotation);
}
private static <M extends Member & AnnotatedElement> void
addInjectionPoints(TypeLiteral<?> type,
Modified: trunk/test/com/google/inject/ProvisionExceptionTest.java
==============================================================================
--- trunk/test/com/google/inject/ProvisionExceptionTest.java (original)
+++ trunk/test/com/google/inject/ProvisionExceptionTest.java Sat Nov 29
14:01:13 2008
@@ -19,13 +19,13 @@
import static com.google.inject.Asserts.assertContains;
import static com.google.inject.Asserts.assertSimilarWhenReserialized;
import java.io.IOException;
-import java.lang.annotation.Retention;
-import java.lang.annotation.Target;
-import static java.lang.annotation.ElementType.FIELD;
-import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
+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;
/**
@@ -212,6 +212,15 @@
}
}
+ public void testBindingAnnotationWarningForScala() {
+ Injector injector = Guice.createInjector(new AbstractModule() {
+ protected void configure() {
+ bind(String.class).annotatedWith(Green.class).toInstance("lime!");
+ }
+ });
+ injector.getInstance(LikeScala.class);
+ }
+
public void testLinkedBindings() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
@@ -272,6 +281,15 @@
static class ConstructorWithBindingAnnotation {
@Inject @Green ConstructorWithBindingAnnotation(String greenString) {}
+ }
+
+ /**
+ * In Scala, fields automatically get accessor methods with the same
name. So we don't do
+ * misplaced-binding annotation detection if the offending method has a
matching field.
+ */
+ static class LikeScala {
+ @Inject @Green String green;
+ @Inject @Green String green() { return green; }
}
@Retention(RUNTIME)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---