Use AnnotationUtils.findAnnotation(…) instead of AnnotatedElement.isAnnotationPresent(…).

Enable use of meta annotations by leveraging MergedAnnotations.

Closes #2500
This commit is contained in:
XenoAmess
2021-11-23 04:45:56 +08:00
committed by Mark Paluch
parent 8bd81a796d
commit ca723d11c7
7 changed files with 33 additions and 14 deletions

View File

@@ -23,8 +23,10 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.util.Lazy;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
@@ -42,6 +44,7 @@ import org.springframework.util.StringUtils;
* @author Christoph Strobl
* @author Mark Paluch
* @author Myeonghyeon Lee
* @author Xeno Amess
*/
public class PreferredConstructor<T, P extends PersistentProperty<P>> {
@@ -110,7 +113,7 @@ public class PreferredConstructor<T, P extends PersistentProperty<P>> {
* @return
*/
public boolean isExplicitlyAnnotated() {
return constructor.isAnnotationPresent(PersistenceConstructor.class);
return AnnotationUtils.findAnnotation(constructor, PersistenceConstructor.class) != null;
}
/**

View File

@@ -26,8 +26,12 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.inject.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
@@ -46,6 +50,7 @@ import org.springframework.util.Assert;
* @author Christoph Strobl
* @author Roman Rodov
* @author Mark Paluch
* @author Xeno Amess
*/
public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {
@@ -115,7 +120,7 @@ public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty<
continue;
}
if (candidate.isAnnotationPresent(PersistenceConstructor.class)) {
if (AnnotationUtils.findAnnotation(candidate, PersistenceConstructor.class) != null) {
return buildPreferredConstructor(candidate, type, entity);
}
@@ -153,7 +158,7 @@ public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty<
return Arrays.stream(rawOwningType.getDeclaredConstructors()) //
.filter(it -> !it.isSynthetic()) // Synthetic constructors should not be considered
.filter(it -> it.isAnnotationPresent(PersistenceConstructor.class)) // Explicitly defined constructor trumps
.filter(it -> AnnotationUtils.findAnnotation(it, PersistenceConstructor.class) != null) // Explicitly defined constructor trumps
// all
.map(it -> buildPreferredConstructor(it, type, entity)) //
.findFirst() //