Skip to content

Commit

Permalink
Include all simple names present in source for error types
Browse files Browse the repository at this point in the history
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 bazelbuild/bazel#12926

PiperOrigin-RevId: 355065427
  • Loading branch information
cushon authored and Javac Team committed Feb 2, 2021
1 parent 622b591 commit 372393a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
12 changes: 9 additions & 3 deletions java/com/google/turbine/processing/TurbineElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
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;
Expand Down Expand Up @@ -262,11 +263,16 @@ public TypeMirror get() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand Down Expand Up @@ -509,6 +510,51 @@ public void generatedAnnotationDefinition() throws IOException {
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
Expand Down

0 comments on commit 372393a

Please sign in to comment.