8284220: TypeMirror#toString omits enclosing class names after JDK-8281238

Reviewed-by: darcy
This commit is contained in:
Liam Miller-Cushon 2022-05-04 20:25:36 +00:00
parent 28e6d805f4
commit 4d30a1e8d1
4 changed files with 165 additions and 0 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/code
test/langtools/tools/javac/processing/model/type/AnnotatedTypeToString

@ -1043,6 +1043,14 @@ public abstract class Type extends AnnoConstruct implements TypeMirror, PoolCons
buf.append(tsym.packge());
buf.append(".");
}
ListBuffer<Name> names = new ListBuffer<>();
for (Symbol sym = tsym.owner; sym != null && sym.kind == TYP; sym = sym.owner) {
names.prepend(sym.name);
}
for (Name name : names) {
buf.append(name);
buf.append(".");
}
appendAnnotationsString(buf);
buf.append(tsym.name);
} else {

@ -0,0 +1,67 @@
/*
* Copyright (c) 2022, Google LLC. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8284220
* @summary Tests DeclaredType.toString with type annotations present, for example that '@A
* Map.Entry' is printed as 'java.util.@A Map.Entry' (and not '@A java.util.Map.Entry' or
* 'java.util.@A Entry').
* @library /tools/javac/lib
* @build AnnotatedTypeToString JavacTestingAbstractProcessor ExpectedToString
* @compile -processor AnnotatedTypeToString -proc:only Test.java
*/
import p.ExpectedToString;
import java.util.Set;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
/**
* Verify that the toString representation of the types of elements annotated with {@code
* ExpectedToString} matches the expected string representation in the annotation.
*/
@SupportedAnnotationTypes("p.ExpectedToString")
public class AnnotatedTypeToString extends JavacTestingAbstractProcessor {
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (roundEnv.processingOver()) {
return false;
}
for (Element e : roundEnv.getElementsAnnotatedWith(ExpectedToString.class)) {
String expected = e.getAnnotation(ExpectedToString.class).value();
String actual = e.asType().toString();
if (!expected.equals(actual)) {
processingEnv
.getMessager()
.printError(String.format("expected: %s, was: %s", expected, actual), e);
}
}
return false;
}
}

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022, Google LLC. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package p;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface ExpectedToString {
String value();
}

@ -0,0 +1,58 @@
/*
* Copyright (c) 2022, Google LLC. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package p;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface A {}
public class Test {
static class StaticNested {
static class InnerMostStaticNested {}
}
class Inner {
class InnerMost {}
}
@ExpectedToString("p.Test.@p.A StaticNested")
@A StaticNested i;
@ExpectedToString("p.Test.StaticNested.@p.A InnerMostStaticNested")
StaticNested.@A InnerMostStaticNested j;
@ExpectedToString("p.Test.@p.A Inner")
@A Inner k;
@ExpectedToString("p.Test.Inner.@p.A InnerMost")
Inner.@A InnerMost l;
@ExpectedToString("p.Test.@p.A Inner.InnerMost")
@A Inner.InnerMost m;
}