diff options
author | Liam Miller-Cushon <cushon@google.com> | 2021-02-02 14:09:44 -0800 |
---|---|---|
committer | Javac Team <javac-team+copybara@google.com> | 2021-02-02 14:10:18 -0800 |
commit | 1bbb1366145029c39bf8174de2516a0d6511a859 (patch) | |
tree | 4347b3cec0cd0279a5b7094c05b7ac45bc0461b9 | |
parent | 622b59136c480a9aeca3fd6b4cc433c87777f8bd (diff) | |
download | turbine-1bbb1366145029c39bf8174de2516a0d6511a859.tar.gz |
Include all simple names present in source for error types
When creating error types to model invalid elements during annotation processing,
include all simple names present in source for qualified names, instead of
just the last one.
This fixes https://github.com/bazelbuild/bazel/issues/12926
PiperOrigin-RevId: 355249091
-rw-r--r-- | java/com/google/turbine/processing/TurbineElement.java | 12 | ||||
-rw-r--r-- | javatests/com/google/turbine/processing/ProcessingIntegrationTest.java | 46 |
2 files changed, 55 insertions, 3 deletions
diff --git a/java/com/google/turbine/processing/TurbineElement.java b/java/com/google/turbine/processing/TurbineElement.java index a9e328a..863f8c9 100644 --- a/java/com/google/turbine/processing/TurbineElement.java +++ b/java/com/google/turbine/processing/TurbineElement.java @@ -47,6 +47,7 @@ import com.google.turbine.model.Const; import com.google.turbine.model.Const.ArrayInitValue; import com.google.turbine.model.TurbineFlag; import com.google.turbine.model.TurbineTyKind; +import com.google.turbine.tree.Tree; import com.google.turbine.tree.Tree.MethDecl; import com.google.turbine.tree.Tree.TyDecl; import com.google.turbine.tree.Tree.VarDecl; @@ -262,11 +263,16 @@ public abstract class TurbineElement implements Element { return factory.asTypeMirror(info.superClassType()); } if (info instanceof SourceTypeBoundClass) { - // support simple name for stuff that doesn't exist + // support simple names for stuff that doesn't exist TyDecl decl = ((SourceTypeBoundClass) info).decl(); if (decl.xtnds().isPresent()) { - return factory.asTypeMirror( - ErrorTy.create(decl.xtnds().get().name().value())); + ArrayDeque<Tree.Ident> flat = new ArrayDeque<>(); + for (Tree.ClassTy curr = decl.xtnds().get(); + curr != null; + curr = curr.base().orElse(null)) { + flat.addFirst(curr.name()); + } + return factory.asTypeMirror(ErrorTy.create(flat)); } } return factory.noType(); diff --git a/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java b/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java index f6a00d2..0774bf4 100644 --- a/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java +++ b/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java @@ -19,6 +19,7 @@ package com.google.turbine.processing; import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.MoreCollectors.onlyElement; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth8.assertThat; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.joining; import static org.junit.Assert.fail; @@ -509,6 +510,51 @@ public class ProcessingIntegrationTest { assertThat(bound.generatedSources()).containsKey("A.java"); } + @SupportedAnnotationTypes("*") + public static class GenerateQualifiedProcessor extends AbstractProcessor { + + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + @Override + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + String superType = + processingEnv.getElementUtils().getTypeElement("T").getSuperclass().toString(); + processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, superType); + return false; + } + } + + @Test + public void qualifiedErrorType() throws IOException { + ImmutableList<Tree.CompUnit> units = + parseUnit( + "=== T.java ===", // + "class T extends G.I {", + "}"); + try { + Binder.bind( + units, + ClassPathBinder.bindClasspath(ImmutableList.of()), + ProcessorInfo.create( + ImmutableList.of(new GenerateQualifiedProcessor()), + getClass().getClassLoader(), + ImmutableMap.of(), + SourceVersion.latestSupported()), + TestClassPaths.TURBINE_BOOTCLASSPATH, + Optional.empty()); + fail(); + } catch (TurbineError e) { + assertThat( + e.diagnostics().stream() + .filter(d -> d.severity().equals(Diagnostic.Kind.NOTE)) + .map(d -> d.message())) + .containsExactly("G.I"); + } + } + private static ImmutableList<Tree.CompUnit> parseUnit(String... lines) { return IntegrationTestSupport.TestInput.parse(Joiner.on('\n').join(lines)) .sources |