Skip to content

Commit

Permalink
Implement getPermittedSubclasses
Browse files Browse the repository at this point in the history
Startblock:
  * unknown commit is submitted
PiperOrigin-RevId: 673998213
  • Loading branch information
cushon authored and Javac Team committed Sep 12, 2024
1 parent 6574ba5 commit 171153d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
18 changes: 18 additions & 0 deletions java/com/google/turbine/processing/TurbineElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,24 @@ public Element getEnclosingElement() {
return enclosing.get();
}

private final Supplier<ImmutableList<TypeMirror>> permits =
memoize(
new Supplier<>() {
@Override
public ImmutableList<TypeMirror> get() {
ImmutableList.Builder<TypeMirror> result = ImmutableList.builder();
for (ClassSymbol p : infoNonNull().permits()) {
result.add(factory.asTypeMirror(ClassTy.asNonParametricClassTy(p)));
}
return result.build();
}
});

@Override
public List<? extends TypeMirror> getPermittedSubclasses() {
return permits.get();
}

private final Supplier<ImmutableList<Element>> enclosed =
memoize(
new Supplier<ImmutableList<Element>>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.ExecutableType;
import javax.lang.model.util.ElementFilter;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.JavaFileObject;
Expand Down Expand Up @@ -899,6 +900,39 @@ public void modifiers() {
.containsExactly("I [abstract, sealed]", "J [abstract, non-sealed]");
}

@SupportedAnnotationTypes("*")
public static class PermitsProcessor extends AbstractProcessor {
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement e : ElementFilter.typesIn(roundEnv.getRootElements())) {
processingEnv
.getMessager()
.printMessage(
Diagnostic.Kind.ERROR, String.format("%s %s", e, e.getPermittedSubclasses()), e);
}
return false;
}
}

@Test
public void permits() {
ImmutableList<Tree.CompUnit> units =
parseUnit(
"=== I.java ===", //
"interface I permits J, K {}",
"interface J {}",
"interface K {}");
TurbineError e1 = runProcessors(units, new PermitsProcessor());
TurbineError e = e1;
assertThat(e.diagnostics().stream().map(d -> d.message()))
.containsExactly("I [J, K]", "J []", "K []");
}

private TurbineError runProcessors(ImmutableList<Tree.CompUnit> units, Processor... processors) {
return assertThrows(
TurbineError.class,
Expand Down

0 comments on commit 171153d

Please sign in to comment.