Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement getPermittedSubclasses #338

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading