Skip to content

Commit

Permalink
Update SuperficialValidation to validate supertypes and superinterfac…
Browse files Browse the repository at this point in the history
…es recursively.
  • Loading branch information
Leland Takamine committed May 14, 2019
1 parent 9f36c57 commit 0dbfc0d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
Expand Down Expand Up @@ -63,10 +64,13 @@ public static boolean validateElements(Iterable<? extends Element> elements) {
}

@Override public Boolean visitType(TypeElement e, Void p) {
TypeMirror superclass = e.getSuperclass();
return isValidBaseElement(e)
&& validateElements(e.getTypeParameters())
&& validateTypes(e.getInterfaces())
&& validateType(e.getSuperclass());
&& validateType(superclass)
&& validateElements(e.getInterfaces().stream().map(MoreTypes::asElement).collect(Collectors.toList()))
&& (superclass.getKind() == TypeKind.NONE || validateElement(MoreTypes.asElement(superclass)));
}

@Override public Boolean visitVariable(VariableElement e, Void p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,90 @@ void runAssertions() {
.failsToCompile();
}

@Test
public void missingSuperclass() {
JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
"test.TestClass",
"package test;",
"",
"class TestClass extends Missing {}");
assertAbout(javaSource())
.that(javaFileObject)
.processedWith(new AssertingProcessor() {
@Override
void runAssertions() {
TypeElement testClassElement =
processingEnv.getElementUtils().getTypeElement("test.TestClass");
assertThat(SuperficialValidation.validateElement(testClassElement)).isFalse();
}
})
.failsToCompile();
}

@Test
public void missingSuperinterface() {
JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
"test.TestClass",
"package test;",
"",
"class TestClass implements Missing {}");
assertAbout(javaSource())
.that(javaFileObject)
.processedWith(new AssertingProcessor() {
@Override
void runAssertions() {
TypeElement testClassElement =
processingEnv.getElementUtils().getTypeElement("test.TestClass");
assertThat(SuperficialValidation.validateElement(testClassElement)).isFalse();
}
})
.failsToCompile();
}

@Test
public void missingGrandparentSuperclass() {
JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
"test.TestClass",
"package test;",
"",
"class Parent extends Missing {}",
"",
"class TestClass extends Parent {}");
assertAbout(javaSource())
.that(javaFileObject)
.processedWith(new AssertingProcessor() {
@Override
void runAssertions() {
TypeElement testClassElement =
processingEnv.getElementUtils().getTypeElement("test.TestClass");
assertThat(SuperficialValidation.validateElement(testClassElement)).isFalse();
}
})
.failsToCompile();
}

@Test
public void missingGrandparentSuperinterface() {
JavaFileObject javaFileObject = JavaFileObjects.forSourceLines(
"test.TestClass",
"package test;",
"",
"interface Parent extends Missing {}",
"",
"class TestClass implements Parent {}");
assertAbout(javaSource())
.that(javaFileObject)
.processedWith(new AssertingProcessor() {
@Override
void runAssertions() {
TypeElement testClassElement =
processingEnv.getElementUtils().getTypeElement("test.TestClass");
assertThat(SuperficialValidation.validateElement(testClassElement)).isFalse();
}
})
.failsToCompile();
}

private abstract static class AssertingProcessor extends AbstractProcessor {
@Override
public Set<String> getSupportedAnnotationTypes() {
Expand Down

0 comments on commit 0dbfc0d

Please sign in to comment.