diff options
author | bhawkes <bhawkes@google.com> | 2016-04-25 09:46:26 -0700 |
---|---|---|
committer | Éamonn McManus <eamonn@mcmanus.net> | 2016-05-10 08:35:15 -0700 |
commit | 9b20c63e528f6fbed736057d1d90ce466b37d208 (patch) | |
tree | a62abad56a90f596972aa4c910e1d27e0c799435 | |
parent | 53b37e85e7242f3d245a31777c6702c8ad052f12 (diff) | |
download | auto-9b20c63e528f6fbed736057d1d90ce466b37d208.tar.gz |
Have AutoAnnotation factor in package names when detecting overloads. Previously it treated all annotations with the same SimpleName as being overload attempts.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=120715929
-rw-r--r-- | value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java | 5 | ||||
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/AutoAnnotationErrorsTest.java | 36 |
2 files changed, 40 insertions, 1 deletions
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java index a044ed0b..252758b6 100644 --- a/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java +++ b/value/src/main/java/com/google/auto/value/processor/AutoAnnotationProcessor.java @@ -15,6 +15,7 @@ */ package com.google.auto.value.processor; +import com.google.auto.common.MoreElements; import com.google.auto.common.SuperficialValidation; import com.google.auto.service.AutoService; import com.google.auto.value.AutoAnnotation; @@ -186,7 +187,9 @@ public class AutoAnnotationProcessor extends AbstractProcessor { boolean overloaded = false; Set<String> classNames = new HashSet<String>(); for (ExecutableElement method : methods) { - if (!classNames.add(generatedClassName(method))) { + String qualifiedClassName = MoreElements.getPackage(method).getQualifiedName() + "." + + generatedClassName(method); + if (!classNames.add(qualifiedClassName)) { overloaded = true; reportError(method, "@AutoAnnotation methods cannot be overloaded"); } diff --git a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationErrorsTest.java b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationErrorsTest.java index b3138591..1f4f3fd0 100644 --- a/value/src/test/java/com/google/auto/value/processor/AutoAnnotationErrorsTest.java +++ b/value/src/test/java/com/google/auto/value/processor/AutoAnnotationErrorsTest.java @@ -112,6 +112,42 @@ public class AutoAnnotationErrorsTest extends TestCase { .withErrorContaining("@AutoAnnotation methods cannot be overloaded") .in(testSource).onLine(11); } + + // Overload detection used to detect all @AutoAnnotation methods that resulted in + // annotation class of the same SimpleName as being an overload. + // This verifies that implementations in different packages work correctly. + public void testSameNameDifferentPackagesDoesNotTriggerOverload() { + + JavaFileObject fooTestSource = JavaFileObjects.forSourceLines( + "com.foo.Test", + "package com.foo;", + "", + "import com.example.TestAnnotation;", + "import com.google.auto.value.AutoAnnotation;", + "", + "class Test {", + " @AutoAnnotation static TestAnnotation newTestAnnotation(int value) {", + " return new AutoAnnotation_Test_newTestAnnotation(value);", + " }", + "}"); + JavaFileObject barTestSource = JavaFileObjects.forSourceLines( + "com.bar.Test", + "package com.bar;", + "", + "import com.example.TestAnnotation;", + "import com.google.auto.value.AutoAnnotation;", + "", + "class Test {", + " @AutoAnnotation static TestAnnotation newTestAnnotation(int value) {", + " return new AutoAnnotation_Test_newTestAnnotation(value);", + " }", + "}"); + + assert_().about(javaSources()) + .that(ImmutableList.of(TEST_ANNOTATION, fooTestSource, barTestSource)) + .processedWith(new AutoAnnotationProcessor()) + .compilesWithoutError(); + } public void testWrongName() { JavaFileObject testSource = JavaFileObjects.forSourceLines( |