Browse Source

Consistently use canonical annotation names in string representations

pull/32408/head
Sam Brannen 2 years ago
parent
commit
4c246b7c96
  1. 7
      spring-core/src/main/java/org/springframework/core/annotation/AttributeMethods.java
  2. 4
      spring-core/src/main/java/org/springframework/core/annotation/SynthesizedMergedAnnotationInvocationHandler.java
  3. 6
      spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java
  4. 2
      spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java
  5. 22
      spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java

7
spring-core/src/main/java/org/springframework/core/annotation/AttributeMethods.java

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

4
spring-core/src/main/java/org/springframework/core/annotation/SynthesizedMergedAnnotationInvocationHandler.java

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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()); Class<?> type = ClassUtils.resolvePrimitiveIfNecessary(method.getReturnType());
return this.annotation.getValue(attributeName, type).orElseThrow( return this.annotation.getValue(attributeName, type).orElseThrow(
() -> new NoSuchElementException("No value found for attribute named '" + attributeName + () -> 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. // Clone non-empty arrays so that users cannot alter the contents of values in our cache.

6
spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

@ -546,12 +546,16 @@ public class TypeDescriptor implements Serializable {
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (Annotation ann : getAnnotations()) { for (Annotation ann : getAnnotations()) {
builder.append('@').append(ann.annotationType().getName()).append(' '); builder.append('@').append(getName(ann.annotationType())).append(' ');
} }
builder.append(getResolvableType()); builder.append(getResolvableType());
return builder.toString(); 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. * Create a new type descriptor for an object.

2
spring-core/src/test/java/org/springframework/core/annotation/MergedAnnotationsTests.java

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

22
spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java

@ -71,7 +71,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.HashMap<?, ?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.HashMap<?, ?>", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isEqualTo(property); assertThat(expression.getValue(this)).isEqualTo(property);
assertThat(expression.getValue(this, Map.class)).isEqualTo(property); assertThat(expression.getValue(this, Map.class)).isEqualTo(property);
expression = parser.parseExpression("property['foo']"); expression = parser.parseExpression("property['foo']");
@ -106,7 +106,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.HashMap<?, ?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.HashMap<?, ?>", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isEqualTo(property); assertThat(expression.getValue(this)).isEqualTo(property);
expression = parser.parseExpression("property['foo']"); expression = parser.parseExpression("property['foo']");
assertThat(expression.getValue(this)).isEqualTo("bar"); assertThat(expression.getValue(this)).isEqualTo("bar");
@ -151,7 +151,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isEqualTo(property); assertThat(expression.getValue(this)).isEqualTo(property);
expression = parser.parseExpression("property[0]"); expression = parser.parseExpression("property[0]");
assertThat(expression.getValue(this)).isEqualTo("bar"); assertThat(expression.getValue(this)).isEqualTo("bar");
@ -165,7 +165,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isEqualTo(property); assertThat(expression.getValue(this)).isEqualTo(property);
expression = parser.parseExpression("property[0]"); expression = parser.parseExpression("property[0]");
assertThat(expression.getValue(this)).isEqualTo(3); assertThat(expression.getValue(this)).isEqualTo(3);
@ -180,7 +180,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true)); SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isEqualTo(property); assertThat(expression.getValue(this)).isEqualTo(property);
Expression indexExpression = parser.parseExpression("property[0]"); Expression indexExpression = parser.parseExpression("property[0]");
@ -257,7 +257,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(configuration); SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.lang.Object", FieldAnnotation.class.getName()); .isEqualTo("@%s java.lang.Object", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isNull(); assertThat(expression.getValue(this)).isNull();
Expression indexExpression = parser.parseExpression("property[0]"); Expression indexExpression = parser.parseExpression("property[0]");
@ -274,7 +274,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(configuration); SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isEqualTo(property); assertThat(expression.getValue(this)).isEqualTo(property);
Expression indexExpression = parser.parseExpression("property[0]"); Expression indexExpression = parser.parseExpression("property[0]");
@ -306,7 +306,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("property"); Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.lang.String[]", FieldAnnotation.class.getName()); .isEqualTo("@%s java.lang.String[]", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this)).isEqualTo(property); assertThat(expression.getValue(this)).isEqualTo(property);
expression = parser.parseExpression("property[0]"); expression = parser.parseExpression("property[0]");
assertThat(expression.getValue(this)).isEqualTo("bar"); assertThat(expression.getValue(this)).isEqualTo("bar");
@ -330,7 +330,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("listNotGeneric"); Expression expression = parser.parseExpression("listNotGeneric");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.ArrayList<?>", FieldAnnotation.class.getCanonicalName());
assertThat(expression.getValue(this, String.class)).isEqualTo("5,6"); assertThat(expression.getValue(this, String.class)).isEqualTo("5,6");
} }
@ -339,7 +339,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("listNotGeneric"); Expression expression = parser.parseExpression("listNotGeneric");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.List<?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.List<?>", FieldAnnotation.class.getCanonicalName());
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -351,7 +351,7 @@ class IndexingTests {
SpelExpressionParser parser = new SpelExpressionParser(); SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("mapNotGeneric"); Expression expression = parser.parseExpression("mapNotGeneric");
assertThat(expression.getValueTypeDescriptor(this)).asString() assertThat(expression.getValueTypeDescriptor(this)).asString()
.isEqualTo("@%s java.util.HashMap<?, ?>", FieldAnnotation.class.getName()); .isEqualTo("@%s java.util.HashMap<?, ?>", FieldAnnotation.class.getCanonicalName());
} }
@Test @Test

Loading…
Cancel
Save