Skip to content

Commit

Permalink
Merge pull request #312 from google/explicit_final
Browse files Browse the repository at this point in the history
Add an explicit check for @autovalue class being private
  • Loading branch information
cgruber committed Mar 21, 2016
2 parents 8b565b8 + 2627d42 commit 605b912
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Expand Up @@ -357,7 +357,7 @@ private void processType(TypeElement type) {
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());
Expand Down Expand Up @@ -565,11 +565,15 @@ private String nameWithoutPrefix(String name) {
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
Expand Down
Expand Up @@ -242,6 +242,31 @@ public void autoValueMustBeStatic() {
.in(javaFileObject).onLine(7);
}

@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(
Expand Down

0 comments on commit 605b912

Please sign in to comment.