Consistently use canonical annotation names in string representations

This commit is contained in:
Sam Brannen
2024-03-10 15:54:53 +01:00
parent 5345a13918
commit 4c246b7c96
5 changed files with 25 additions and 16 deletions

View File

@@ -136,7 +136,7 @@ final class AttributeMethods {
}
catch (Throwable ex) {
throw new IllegalStateException("Could not obtain annotation attribute value for " +
get(i).getName() + " declared on " + annotation.annotationType(), ex);
get(i).getName() + " declared on @" + getName(annotation.annotationType()), ex);
}
}
}
@@ -300,4 +300,9 @@ final class AttributeMethods {
return "attribute '" + attributeName + "'" + in;
}
private static String getName(Class<?> clazz) {
String canonicalName = clazz.getCanonicalName();
return (canonicalName != null ? canonicalName : clazz.getName());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -211,7 +211,7 @@ final class SynthesizedMergedAnnotationInvocationHandler<A extends Annotation> i
Class<?> type = ClassUtils.resolvePrimitiveIfNecessary(method.getReturnType());
return this.annotation.getValue(attributeName, type).orElseThrow(
() -> new NoSuchElementException("No value found for attribute named '" + attributeName +
"' in merged annotation " + this.annotation.getType().getName()));
"' in merged annotation " + getName(this.annotation.getType())));
});
// Clone non-empty arrays so that users cannot alter the contents of values in our cache.

View File

@@ -546,12 +546,16 @@ public class TypeDescriptor implements Serializable {
public String toString() {
StringBuilder builder = new StringBuilder();
for (Annotation ann : getAnnotations()) {
builder.append('@').append(ann.annotationType().getName()).append(' ');
builder.append('@').append(getName(ann.annotationType())).append(' ');
}
builder.append(getResolvableType());
return builder.toString();
}
private static String getName(Class<?> clazz) {
String canonicalName = clazz.getCanonicalName();
return (canonicalName != null ? canonicalName : clazz.getName());
}
/**
* Create a new type descriptor for an object.

View File

@@ -1960,7 +1960,7 @@ class MergedAnnotationsTests {
assertThatExceptionOfType(NoSuchElementException.class).isThrownBy(() ->
MergedAnnotation.of(AnnotationWithoutDefaults.class, attributes).synthesize().text())
.withMessage("No value found for attribute named 'text' in merged annotation " +
AnnotationWithoutDefaults.class.getName());
AnnotationWithoutDefaults.class.getCanonicalName());
}
@Test