Browse Source

Obtain merged annotation for annotation validation.

We now validate declared annotations by pre-processing these through AnnotatedElementUtils to ensure a proper comparison. Previously, we compared annotation in their declared form (AnnotatedElement.getAnnotations()) with merged annotations which could fail due to aliasing effects of merged annotations.

Closes #2368.
2.4.x
Mark Paluch 5 years ago
parent
commit
2bb21181e8
No known key found for this signature in database
GPG Key ID: 4406B84C1661DCD1
  1. 11
      src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java
  2. 1
      src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

11
src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java

@ -107,13 +107,15 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp @@ -107,13 +107,15 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
Class<? extends Annotation> annotationType = annotation.annotationType();
validateAnnotation(annotation,
Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(it, annotationType);
validateAnnotation(mergedAnnotation,
"Ambiguous mapping! Annotation %s configured "
+ "multiple times on accessor methods of property %s in class %s!",
annotationType.getSimpleName(), getName(), getOwner().getType().getSimpleName());
annotationCache.put(annotationType,
Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType)));
Optional.of(mergedAnnotation));
}
});
@ -122,13 +124,14 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp @@ -122,13 +124,14 @@ public abstract class AnnotationBasedPersistentProperty<P extends PersistentProp
for (Annotation annotation : it.getAnnotations()) {
Class<? extends Annotation> annotationType = annotation.annotationType();
Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(it, annotationType);
validateAnnotation(annotation,
validateAnnotation(mergedAnnotation,
"Ambiguous mapping! Annotation %s configured " + "on field %s and one of its accessor methods in class %s!",
annotationType.getSimpleName(), it.getName(), getOwner().getType().getSimpleName());
annotationCache.put(annotationType,
Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType)));
Optional.of(mergedAnnotation));
}
});
}

1
src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

@ -331,6 +331,7 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase @@ -331,6 +331,7 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
}
@MyAnnotation
@RevisedAnnnotationWithAliasFor(value = "my-value")
public void setSetter(String setter) {
this.setter = setter;
}

Loading…
Cancel
Save