Use canonical names for types in synthesized annotation toString

My proposal for the same change in the JDK is currently targeted for
JDK 19.

- https://bugs.openjdk.java.net/browse/JDK-8281462
- https://bugs.openjdk.java.net/browse/JDK-8281568
- https://github.com/openjdk/jdk/pull/7418

See gh-28015
This commit is contained in:
Sam Brannen
2022-02-10 14:16:34 +01:00
parent 97582fd0a1
commit ce87285be5
3 changed files with 24 additions and 11 deletions

View File

@@ -177,7 +177,7 @@ final class SynthesizedMergedAnnotationInvocationHandler<A extends Annotation> i
private String annotationToString() {
String string = this.string;
if (string == null) {
StringBuilder builder = new StringBuilder("@").append(this.type.getName()).append('(');
StringBuilder builder = new StringBuilder("@").append(getName(this.type)).append('(');
for (int i = 0; i < this.attributes.size(); i++) {
Method attribute = this.attributes.get(i);
if (i > 0) {
@@ -202,7 +202,7 @@ final class SynthesizedMergedAnnotationInvocationHandler<A extends Annotation> i
return ((Enum<?>) value).name();
}
if (value instanceof Class) {
return ((Class<?>) value).getName() + ".class";
return getName((Class<?>) value) + ".class";
}
if (value.getClass().isArray()) {
StringBuilder builder = new StringBuilder("{");
@@ -277,6 +277,11 @@ final class SynthesizedMergedAnnotationInvocationHandler<A extends Annotation> i
return (A) Proxy.newProxyInstance(classLoader, interfaces, handler);
}
private static String getName(Class<?> clazz) {
String canonicalName = clazz.getCanonicalName();
return (canonicalName != null ? canonicalName : clazz.getName());
}
private static boolean isVisible(ClassLoader classLoader, Class<?> interfaceClass) {
if (classLoader == interfaceClass.getClassLoader()) {

View File

@@ -1887,15 +1887,22 @@ class MergedAnnotationsTests {
// Formatting common to Spring and JDK 9+
assertThat(string)
.startsWith("@" + RequestMapping.class.getName() + "(")
.contains("value={\"/test\"}", "path={\"/test\"}", "name=\"bar\"", "clazz=java.lang.Object.class")
.contains("value={\"/test\"}", "path={\"/test\"}", "name=\"bar\"")
.endsWith(")");
if (webMapping instanceof SynthesizedAnnotation) {
assertThat(string).as("Spring uses Enum#name()").contains("method={GET, POST}");
assertThat(string).as("Spring formatting")
.startsWith("@org.springframework.core.annotation.MergedAnnotationsTests.RequestMapping(")
.contains("method={GET, POST}",
"clazz=org.springframework.core.annotation.MergedAnnotationsTests.RequestMethod.class",
"classes={org.springframework.core.annotation.MergedAnnotationsTests.RequestMethod.class}");
}
else {
assertThat(string).as("JDK uses Enum#toString()").contains("method={method: get, method: post}");
assertThat(string).as("JDK 9-18 formatting")
.startsWith("@org.springframework.core.annotation.MergedAnnotationsTests$RequestMapping(")
.contains("method={method: get, method: post}",
"clazz=org.springframework.core.annotation.MergedAnnotationsTests$RequestMethod.class",
"classes={org.springframework.core.annotation.MergedAnnotationsTests$RequestMethod.class}");
}
}
@@ -2989,8 +2996,9 @@ class MergedAnnotationsTests {
RequestMethod[] method() default {};
// clazz is only used for testing annotation toString() implementations
Class<?> clazz() default Object.class;
Class<?> clazz() default RequestMethod.class;
Class<?>[] classes() default {RequestMethod.class};
}
@Retention(RetentionPolicy.RUNTIME)