diff options
author | Christian Edward Gruber <christianedwardgruber@gmail.com> | 2016-03-21 11:24:43 -0700 |
---|---|---|
committer | Christian Edward Gruber <christianedwardgruber@gmail.com> | 2016-03-21 11:24:43 -0700 |
commit | 605b91243f65c3c2732973eb3146dbb58b7f6e70 (patch) | |
tree | 603ef5be9ebdd3d8aa70d89fe9ec181a11540e42 | |
parent | 8b565b8ddbc42af437a596533298dc9223d0ad20 (diff) | |
parent | 2627d42a65854daa87461a34ad1709136b67e410 (diff) | |
download | auto-605b91243f65c3c2732973eb3146dbb58b7f6e70.tar.gz |
Merge pull request #312 from google/explicit_final
Add an explicit check for @AutoValue class being private
-rw-r--r-- | value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java | 14 | ||||
-rw-r--r-- | value/src/test/java/com/google/auto/value/processor/CompilationTest.java | 25 |
2 files changed, 34 insertions, 5 deletions
diff --git a/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java b/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java index 7d717698..b527b739 100644 --- a/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java +++ b/value/src/main/java/com/google/auto/value/processor/AutoValueProcessor.java @@ -357,7 +357,7 @@ public class AutoValueProcessor extends AbstractProcessor { errorReporter.abortWithError("@AutoValue may not be used to implement an annotation" + " interface; try using @AutoAnnotation instead", type); } - checkTopLevelOrStatic(type); + checkModifiersIfNested(type); ImmutableSet<ExecutableElement> methods = getLocalAndInheritedMethods(type, processingEnv.getElementUtils()); @@ -565,11 +565,15 @@ public class AutoValueProcessor extends AbstractProcessor { return Introspector.decapitalize(name); } - private void checkTopLevelOrStatic(TypeElement type) { + private void checkModifiersIfNested(TypeElement type) { ElementKind enclosingKind = type.getEnclosingElement().getKind(); - if ((enclosingKind.isClass() || enclosingKind.isInterface()) - && !type.getModifiers().contains(Modifier.STATIC)) { - errorReporter.abortWithError("Nested @AutoValue class must be static", type); + if (enclosingKind.isClass() || enclosingKind.isInterface()) { + if (type.getModifiers().contains(Modifier.PRIVATE)) { + errorReporter.abortWithError("@AutoValue class must not be private", type); + } + if (!type.getModifiers().contains(Modifier.STATIC)) { + errorReporter.abortWithError("Nested @AutoValue class must be static", type); + } } // In principle type.getEnclosingElement() could be an ExecutableElement (for a class // declared inside a method), but since RoundEnvironment.getElementsAnnotatedWith doesn't diff --git a/value/src/test/java/com/google/auto/value/processor/CompilationTest.java b/value/src/test/java/com/google/auto/value/processor/CompilationTest.java index c5a33be7..ea9f37c9 100644 --- a/value/src/test/java/com/google/auto/value/processor/CompilationTest.java +++ b/value/src/test/java/com/google/auto/value/processor/CompilationTest.java @@ -243,6 +243,31 @@ public class CompilationTest { } @Test + public void autoValueMustBeNotBePrivate() { + JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( + "foo.bar.Baz", + "package foo.bar;", + "", + "import com.google.auto.value.AutoValue;", + "", + "public class Baz {", + " @AutoValue", + " private abstract static class Private {", + " public abstract String buh();", + " public Private create(String buh) {", + " return new AutoValue_Baz_Private(buh);", + " }", + " }", + "}"); + assertAbout(javaSource()) + .that(javaFileObject) + .processedWith(new AutoValueProcessor()) + .failsToCompile() + .withErrorContaining("@AutoValue class must not be private") + .in(javaFileObject).onLine(7); + } + + @Test public void noMultidimensionalPrimitiveArrays() throws Exception { JavaFileObject javaFileObject = JavaFileObjects.forSourceLines( "foo.bar.Baz", |