Skip to content

Commit ca723d1

Browse files
XenoAmessmp911de
authored andcommitted
Use AnnotationUtils.findAnnotation(…) instead of AnnotatedElement.isAnnotationPresent(…).
Enable use of meta annotations by leveraging MergedAnnotations. Closes #2500
1 parent 8bd81a7 commit ca723d1

File tree

7 files changed

+33
-14
lines changed

7 files changed

+33
-14
lines changed

src/main/java/org/springframework/data/convert/CustomConversions.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.apache.commons.logging.LogFactory;
3535

3636
import org.springframework.core.GenericTypeResolver;
37+
import org.springframework.core.annotation.AnnotationUtils;
3738
import org.springframework.core.convert.converter.Converter;
3839
import org.springframework.core.convert.converter.ConverterFactory;
3940
import org.springframework.core.convert.converter.ConverterRegistry;
@@ -42,6 +43,7 @@
4243
import org.springframework.core.convert.support.GenericConversionService;
4344
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
4445
import org.springframework.data.mapping.model.SimpleTypeHolder;
46+
import org.springframework.data.repository.RepositoryDefinition;
4547
import org.springframework.data.util.Streamable;
4648
import org.springframework.lang.Nullable;
4749
import org.springframework.util.Assert;
@@ -57,6 +59,7 @@
5759
* @author Thomas Darimont
5860
* @author Christoph Strobl
5961
* @author Mark Paluch
62+
* @author Xeno Amess
6063
* @since 2.0
6164
*/
6265
public class CustomConversions {
@@ -757,8 +760,8 @@ public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
757760
Assert.notNull(converter, "Converter must not be null!");
758761

759762
Class<?> type = converter.getClass();
760-
boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
761-
boolean isReading = type.isAnnotationPresent(ReadingConverter.class);
763+
boolean isWriting = AnnotationUtils.findAnnotation(type, WritingConverter.class) != null;
764+
boolean isReading = AnnotationUtils.findAnnotation(type, ReadingConverter.class) != null;
762765

763766
if (converter instanceof ConverterAware) {
764767

src/main/java/org/springframework/data/mapping/PreferredConstructor.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import java.util.concurrent.ConcurrentHashMap;
2424

2525
import org.springframework.beans.factory.annotation.Value;
26+
import org.springframework.core.annotation.AnnotationUtils;
2627
import org.springframework.core.annotation.MergedAnnotations;
2728
import org.springframework.data.annotation.PersistenceConstructor;
29+
import org.springframework.data.convert.WritingConverter;
2830
import org.springframework.data.util.Lazy;
2931
import org.springframework.data.util.TypeInformation;
3032
import org.springframework.lang.Nullable;
@@ -42,6 +44,7 @@
4244
* @author Christoph Strobl
4345
* @author Mark Paluch
4446
* @author Myeonghyeon Lee
47+
* @author Xeno Amess
4548
*/
4649
public class PreferredConstructor<T, P extends PersistentProperty<P>> {
4750

@@ -110,7 +113,7 @@ public boolean isNoArgConstructor() {
110113
* @return
111114
*/
112115
public boolean isExplicitlyAnnotated() {
113-
return constructor.isAnnotationPresent(PersistenceConstructor.class);
116+
return AnnotationUtils.findAnnotation(constructor, PersistenceConstructor.class) != null;
114117
}
115118

116119
/**

src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@
2626
import java.util.Arrays;
2727
import java.util.List;
2828

29+
import javax.inject.Qualifier;
30+
31+
import org.springframework.beans.factory.annotation.Value;
2932
import org.springframework.core.DefaultParameterNameDiscoverer;
3033
import org.springframework.core.ParameterNameDiscoverer;
34+
import org.springframework.core.annotation.AnnotationUtils;
3135
import org.springframework.data.annotation.PersistenceConstructor;
3236
import org.springframework.data.mapping.PersistentEntity;
3337
import org.springframework.data.mapping.PersistentProperty;
@@ -46,6 +50,7 @@
4650
* @author Christoph Strobl
4751
* @author Roman Rodov
4852
* @author Mark Paluch
53+
* @author Xeno Amess
4954
*/
5055
public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {
5156

@@ -115,7 +120,7 @@ <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover(TypeInf
115120
continue;
116121
}
117122

118-
if (candidate.isAnnotationPresent(PersistenceConstructor.class)) {
123+
if (AnnotationUtils.findAnnotation(candidate, PersistenceConstructor.class) != null) {
119124
return buildPreferredConstructor(candidate, type, entity);
120125
}
121126

@@ -153,7 +158,7 @@ <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover(TypeInf
153158

154159
return Arrays.stream(rawOwningType.getDeclaredConstructors()) //
155160
.filter(it -> !it.isSynthetic()) // Synthetic constructors should not be considered
156-
.filter(it -> it.isAnnotationPresent(PersistenceConstructor.class)) // Explicitly defined constructor trumps
161+
.filter(it -> AnnotationUtils.findAnnotation(it, PersistenceConstructor.class) != null) // Explicitly defined constructor trumps
157162
// all
158163
.map(it -> buildPreferredConstructor(it, type, entity)) //
159164
.findFirst() //

src/main/java/org/springframework/data/projection/SpelEvaluatingMethodInterceptor.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.springframework.beans.factory.annotation.Value;
2828
import org.springframework.context.expression.BeanFactoryResolver;
2929
import org.springframework.context.expression.MapAccessor;
30+
import org.springframework.core.annotation.AnnotationUtils;
31+
import org.springframework.data.annotation.PersistenceConstructor;
3032
import org.springframework.expression.EvaluationContext;
3133
import org.springframework.expression.Expression;
3234
import org.springframework.expression.ParserContext;
@@ -45,6 +47,7 @@
4547
* @author Oliver Gierke
4648
* @author Thomas Darimont
4749
* @author Christoph Strobl
50+
* @author Xeno Amess
4851
* @see 1.10
4952
*/
5053
class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
@@ -108,12 +111,11 @@ private static Map<Integer, Expression> potentiallyCreateExpressionsForMethodsOn
108111

109112
for (Method method : targetInterface.getMethods()) {
110113

111-
if (!method.isAnnotationPresent(Value.class)) {
114+
Value value = AnnotationUtils.findAnnotation(method, Value.class);
115+
if (value == null) {
112116
continue;
113117
}
114118

115-
Value value = method.getAnnotation(Value.class);
116-
117119
if (!StringUtils.hasText(value.value())) {
118120
throw new IllegalStateException(String.format("@Value annotation on %s contains empty expression!", method));
119121
}

src/main/java/org/springframework/data/repository/cdi/CdiRepositoryBean.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.apache.commons.logging.Log;
4141
import org.apache.commons.logging.LogFactory;
4242

43+
import org.springframework.core.annotation.AnnotationUtils;
4344
import org.springframework.core.log.LogMessage;
45+
import org.springframework.data.annotation.PersistenceConstructor;
4446
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
4547
import org.springframework.data.repository.config.RepositoryFragmentConfiguration;
4648
import org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments;
@@ -61,6 +63,7 @@
6163
* @author Jens Schauder
6264
* @author Christoph Strobl
6365
* @author Ariel Carrera
66+
* @author Xeno Amess
6467
*/
6568
public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapable {
6669

@@ -261,7 +264,7 @@ public Set<Class<? extends Annotation>> getStereotypes() {
261264

262265
return Arrays.stream(repositoryType.getAnnotations())//
263266
.map(Annotation::annotationType)//
264-
.filter(it -> it.isAnnotationPresent(Stereotype.class))//
267+
.filter(it -> AnnotationUtils.findAnnotation(it, Stereotype.class) != null)//
265268
.collect(Collectors.toSet());
266269
}
267270

@@ -278,7 +281,7 @@ public Class<?> getBeanClass() {
278281
* @see javax.enterprise.inject.spi.Bean#isAlternative()
279282
*/
280283
public boolean isAlternative() {
281-
return repositoryType.isAnnotationPresent(Alternative.class);
284+
return AnnotationUtils.findAnnotation(repositoryType, Alternative.class) != null;
282285
}
283286

284287
/*

src/main/java/org/springframework/data/repository/cdi/CdiRepositoryExtensionSupport.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Oliver Gierke
5050
* @author Mark Paluch
5151
* @author Christoph Strobl
52+
* @author Xeno Amess
5253
*/
5354
public abstract class CdiRepositoryExtensionSupport implements Extension {
5455

@@ -98,8 +99,8 @@ private boolean isRepository(Class<?> type) {
9899

99100
boolean isInterface = type.isInterface();
100101
boolean extendsRepository = Repository.class.isAssignableFrom(type);
101-
boolean isAnnotated = type.isAnnotationPresent(RepositoryDefinition.class);
102-
boolean excludedByAnnotation = type.isAnnotationPresent(NoRepositoryBean.class);
102+
boolean isAnnotated = AnnotationUtils.findAnnotation(type, RepositoryDefinition.class) != null;
103+
boolean excludedByAnnotation = AnnotationUtils.findAnnotation(type, NoRepositoryBean.class) != null;
103104

104105
return isInterface && (extendsRepository || isAnnotated) && !excludedByAnnotation;
105106
}
@@ -113,7 +114,7 @@ private Set<Annotation> getQualifiers(final Class<?> type) {
113114
Annotation[] annotations = type.getAnnotations();
114115
for (Annotation annotation : annotations) {
115116
Class<? extends Annotation> annotationType = annotation.annotationType();
116-
if (annotationType.isAnnotationPresent(Qualifier.class)) {
117+
if (AnnotationUtils.findAnnotation(annotationType, Qualifier.class) != null) {
117118
qualifiers.add(annotation);
118119
}
119120
}

src/main/java/org/springframework/data/repository/core/support/AnnotationRepositoryMetadata.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.data.repository.core.support;
1717

18+
import org.springframework.core.annotation.AnnotationUtils;
1819
import org.springframework.data.repository.RepositoryDefinition;
1920
import org.springframework.data.repository.core.RepositoryMetadata;
2021
import org.springframework.util.Assert;
@@ -25,6 +26,7 @@
2526
*
2627
* @author Oliver Gierke
2728
* @author Thomas Darimont
29+
* @author Xeno Amess
2830
*/
2931
public class AnnotationRepositoryMetadata extends AbstractRepositoryMetadata {
3032

@@ -44,7 +46,7 @@ public AnnotationRepositoryMetadata(Class<?> repositoryInterface) {
4446

4547
super(repositoryInterface);
4648

47-
Assert.isTrue(repositoryInterface.isAnnotationPresent(RepositoryDefinition.class),
49+
Assert.isTrue(AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class) != null,
4850
String.format(NO_ANNOTATION_FOUND, repositoryInterface.getName()));
4951

5052
this.idType = resolveIdType(repositoryInterface);

0 commit comments

Comments
 (0)