diff --git a/java/com/google/turbine/processing/TurbineTypeMirror.java b/java/com/google/turbine/processing/TurbineTypeMirror.java index a0ee3ff..60ca690 100644 --- a/java/com/google/turbine/processing/TurbineTypeMirror.java +++ b/java/com/google/turbine/processing/TurbineTypeMirror.java @@ -19,7 +19,6 @@ import static com.google.common.collect.Iterables.getLast; import static java.util.Objects.requireNonNull; -import com.google.common.base.Ascii; import com.google.common.base.Joiner; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableList; @@ -100,11 +99,6 @@ public String toString() { /** A {@link PrimitiveType} implementation backed by a {@link PrimTy}. */ static class TurbinePrimitiveType extends TurbineTypeMirror implements PrimitiveType { - @Override - public String toString() { - return Ascii.toLowerCase(type.primkind().toString()); - } - @Override public Type asTurbineType() { return type; @@ -182,11 +176,6 @@ public ClassTy asTurbineType() { this.type = type; } - @Override - public String toString() { - return type.toString(); - } - final Supplier element = factory.memoize( new Supplier() { @@ -340,11 +329,6 @@ public TypeMirror getEnclosingType() { public List getTypeArguments() { return factory.asTypeMirrors(type.targs()); } - - @Override - public String toString() { - return type.toString(); - } } /** A 'package type' implementation backed by a {@link PackageSymbol}. */ @@ -416,11 +400,6 @@ public R accept(TypeVisitor v, P p) { return v.visitNoType(this, p); } - @Override - public String toString() { - return "none"; - } - @Override public boolean equals(@Nullable Object other) { return other instanceof TurbineNoType; @@ -530,11 +509,6 @@ public TypeMirror getLowerBound() { : factory.nullType(); } - @Override - public String toString() { - return type.toString(); - } - @Override protected ImmutableList annos() { return type.annos(); @@ -694,11 +668,6 @@ public R accept(TypeVisitor v, P p) { /** An {@link ExecutableType} implementation backed by a {@link MethodTy}. */ public static class TurbineExecutableType extends TurbineTypeMirror implements ExecutableType { - @Override - public String toString() { - return type.toString(); - } - @Override public MethodTy asTurbineType() { return type; diff --git a/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java b/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java index 822283b..504001b 100644 --- a/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java +++ b/javatests/com/google/turbine/processing/AbstractTurbineTypesTest.java @@ -238,6 +238,7 @@ protected static TypeParameters typeParameters() throws Exception { // type annotations List annotatedTypes = new ArrayList<>(); annotatedTypes.add("@A int @B []"); + annotatedTypes.add("@A int"); // The string representation of these types changed in JDK 19, see JDK-8281238 if (Runtime.version().feature() >= 19) { annotatedTypes.add("@A List<@B Integer>"); diff --git a/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java b/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java index 097e59a..8be0cc3 100644 --- a/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java +++ b/javatests/com/google/turbine/processing/ProcessingIntegrationTest.java @@ -71,6 +71,7 @@ import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.RecordComponentElement; import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.ErrorType; import javax.lang.model.type.ExecutableType; @@ -1098,6 +1099,70 @@ class T extends M { "c supertype: B.E, arguments: [F], enclosing: none"); } + @SupportedAnnotationTypes("*") + public static class TypeAnnotationFieldType extends AbstractProcessor { + @Override + public SourceVersion getSupportedSourceVersion() { + return SourceVersion.latestSupported(); + } + + private boolean first = true; + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + if (!first) { + return false; + } + first = false; + for (Element root : roundEnv.getRootElements()) { + for (VariableElement field : ElementFilter.fieldsIn(root.getEnclosedElements())) { + TypeMirror type = field.asType(); + processingEnv + .getMessager() + .printMessage( + Diagnostic.Kind.ERROR, + String.format( + "field %s with annotations %s, type '%s' with annotations %s", + field, field.getAnnotationMirrors(), type, type.getAnnotationMirrors())); + } + } + return false; + } + } + + // Ensure that type annotations are included in the string representation of primtive types + @Test + public void fieldTypeToString() { + ImmutableList units = + parseUnit( + """ + === T.java === + class T { + private final @A int f; + private final @A Object g; + private final @A int[] h; + } + === A.java === + import java.lang.annotation.ElementType; + import java.lang.annotation.Retention; + import java.lang.annotation.RetentionPolicy; + import java.lang.annotation.Target; + @Retention(RetentionPolicy.SOURCE) + @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE}) + public @interface A { + } + """); + TurbineError e = runProcessors(units, new TypeAnnotationFieldType()); + assertThat( + e.diagnostics().stream() + .filter(d -> d.severity().equals(Diagnostic.Kind.ERROR)) + .map(d -> d.message())) + .containsExactly( + "field f with annotations [], type '@A int' with annotations [@A]", + "field g with annotations [], type 'java.lang.@A Object' with annotations [@A]", + "field h with annotations [], type '@A int[]' with annotations []"); + } + private TurbineError runProcessors(ImmutableList units, Processor... processors) { return assertThrows( TurbineError.class, diff --git a/javatests/com/google/turbine/processing/TurbineTypesUnaryTest.java b/javatests/com/google/turbine/processing/TurbineTypesUnaryTest.java index 2fd5d4c..eaa7887 100644 --- a/javatests/com/google/turbine/processing/TurbineTypesUnaryTest.java +++ b/javatests/com/google/turbine/processing/TurbineTypesUnaryTest.java @@ -105,6 +105,9 @@ public void erasure() { case "@p.Test0.A int @p.Test0.B []": expected = "int[]"; break; + case "@p.Test0.A int": + expected = "int"; + break; default: // fall out } assertWithMessage("erasure(`%s`) = erasure(`%s`)", javacA, turbineA)