Skip to content

Commit 1bbb136

Browse files
cushonJavac Team
authored andcommitted
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 bazelbuild/bazel#12926 PiperOrigin-RevId: 355249091
1 parent 622b591 commit 1bbb136

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

java/com/google/turbine/processing/TurbineElement.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.google.turbine.model.Const.ArrayInitValue;
4848
import com.google.turbine.model.TurbineFlag;
4949
import com.google.turbine.model.TurbineTyKind;
50+
import com.google.turbine.tree.Tree;
5051
import com.google.turbine.tree.Tree.MethDecl;
5152
import com.google.turbine.tree.Tree.TyDecl;
5253
import com.google.turbine.tree.Tree.VarDecl;
@@ -262,11 +263,16 @@ public TypeMirror get() {
262263
return factory.asTypeMirror(info.superClassType());
263264
}
264265
if (info instanceof SourceTypeBoundClass) {
265-
// support simple name for stuff that doesn't exist
266+
// support simple names for stuff that doesn't exist
266267
TyDecl decl = ((SourceTypeBoundClass) info).decl();
267268
if (decl.xtnds().isPresent()) {
268-
return factory.asTypeMirror(
269-
ErrorTy.create(decl.xtnds().get().name().value()));
269+
ArrayDeque<Tree.Ident> flat = new ArrayDeque<>();
270+
for (Tree.ClassTy curr = decl.xtnds().get();
271+
curr != null;
272+
curr = curr.base().orElse(null)) {
273+
flat.addFirst(curr.name());
274+
}
275+
return factory.asTypeMirror(ErrorTy.create(flat));
270276
}
271277
}
272278
return factory.noType();

javatests/com/google/turbine/processing/ProcessingIntegrationTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.collect.ImmutableList.toImmutableList;
2020
import static com.google.common.collect.MoreCollectors.onlyElement;
2121
import static com.google.common.truth.Truth.assertThat;
22+
import static com.google.common.truth.Truth8.assertThat;
2223
import static java.nio.charset.StandardCharsets.UTF_8;
2324
import static java.util.stream.Collectors.joining;
2425
import static org.junit.Assert.fail;
@@ -509,6 +510,51 @@ public void generatedAnnotationDefinition() throws IOException {
509510
assertThat(bound.generatedSources()).containsKey("A.java");
510511
}
511512

513+
@SupportedAnnotationTypes("*")
514+
public static class GenerateQualifiedProcessor extends AbstractProcessor {
515+
516+
@Override
517+
public SourceVersion getSupportedSourceVersion() {
518+
return SourceVersion.latestSupported();
519+
}
520+
521+
@Override
522+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
523+
String superType =
524+
processingEnv.getElementUtils().getTypeElement("T").getSuperclass().toString();
525+
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, superType);
526+
return false;
527+
}
528+
}
529+
530+
@Test
531+
public void qualifiedErrorType() throws IOException {
532+
ImmutableList<Tree.CompUnit> units =
533+
parseUnit(
534+
"=== T.java ===", //
535+
"class T extends G.I {",
536+
"}");
537+
try {
538+
Binder.bind(
539+
units,
540+
ClassPathBinder.bindClasspath(ImmutableList.of()),
541+
ProcessorInfo.create(
542+
ImmutableList.of(new GenerateQualifiedProcessor()),
543+
getClass().getClassLoader(),
544+
ImmutableMap.of(),
545+
SourceVersion.latestSupported()),
546+
TestClassPaths.TURBINE_BOOTCLASSPATH,
547+
Optional.empty());
548+
fail();
549+
} catch (TurbineError e) {
550+
assertThat(
551+
e.diagnostics().stream()
552+
.filter(d -> d.severity().equals(Diagnostic.Kind.NOTE))
553+
.map(d -> d.message()))
554+
.containsExactly("G.I");
555+
}
556+
}
557+
512558
private static ImmutableList<Tree.CompUnit> parseUnit(String... lines) {
513559
return IntegrationTestSupport.TestInput.parse(Joiner.on('\n').join(lines))
514560
.sources

0 commit comments

Comments
 (0)