Skip to content

Commit 13ebc50

Browse files
committed
change from java's isAnnotationPresent to spring's findAnnotation, for MergedAnnotations can be used.
1 parent 788457c commit 13ebc50

File tree

7 files changed

+26
-14
lines changed

7 files changed

+26
-14
lines changed

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

+4-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;
@@ -757,8 +759,8 @@ public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
757759
Assert.notNull(converter, "Converter must not be null!");
758760

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

763765
if (converter instanceof ConverterAware) {
764766

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

+3-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;
@@ -110,7 +112,7 @@ public boolean isNoArgConstructor() {
110112
* @return
111113
*/
112114
public boolean isExplicitlyAnnotated() {
113-
return constructor.isAnnotationPresent(PersistenceConstructor.class);
115+
return AnnotationUtils.findAnnotation(constructor, PersistenceConstructor.class) != null;
114116
}
115117

116118
/**

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

+6-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;
@@ -115,7 +119,7 @@ <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover(TypeInf
115119
continue;
116120
}
117121

118-
if (candidate.isAnnotationPresent(PersistenceConstructor.class)) {
122+
if (AnnotationUtils.findAnnotation(candidate, PersistenceConstructor.class) != null) {
119123
return buildPreferredConstructor(candidate, type, entity);
120124
}
121125

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

154158
return Arrays.stream(rawOwningType.getDeclaredConstructors()) //
155159
.filter(it -> !it.isSynthetic()) // Synthetic constructors should not be considered
156-
.filter(it -> it.isAnnotationPresent(PersistenceConstructor.class)) // Explicitly defined constructor trumps
160+
.filter(it -> AnnotationUtils.findAnnotation(it, PersistenceConstructor.class) != null) // Explicitly defined constructor trumps
157161
// all
158162
.map(it -> buildPreferredConstructor(it, type, entity)) //
159163
.findFirst() //

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

+4-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;
@@ -108,12 +110,11 @@ private static Map<Integer, Expression> potentiallyCreateExpressionsForMethodsOn
108110

109111
for (Method method : targetInterface.getMethods()) {
110112

111-
if (!method.isAnnotationPresent(Value.class)) {
113+
Value value = AnnotationUtils.findAnnotation(method, Value.class);
114+
if (value == null) {
112115
continue;
113116
}
114117

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

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

+4-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;
@@ -261,7 +263,7 @@ public Set<Class<? extends Annotation>> getStereotypes() {
261263

262264
return Arrays.stream(repositoryType.getAnnotations())//
263265
.map(Annotation::annotationType)//
264-
.filter(it -> it.isAnnotationPresent(Stereotype.class))//
266+
.filter(it -> AnnotationUtils.findAnnotation(it, Stereotype.class) != null)//
265267
.collect(Collectors.toSet());
266268
}
267269

@@ -278,7 +280,7 @@ public Class<?> getBeanClass() {
278280
* @see javax.enterprise.inject.spi.Bean#isAlternative()
279281
*/
280282
public boolean isAlternative() {
281-
return repositoryType.isAnnotationPresent(Alternative.class);
283+
return AnnotationUtils.findAnnotation(repositoryType, Alternative.class) != null;
282284
}
283285

284286
/*

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ private boolean isRepository(Class<?> type) {
9898

9999
boolean isInterface = type.isInterface();
100100
boolean extendsRepository = Repository.class.isAssignableFrom(type);
101-
boolean isAnnotated = type.isAnnotationPresent(RepositoryDefinition.class);
102-
boolean excludedByAnnotation = type.isAnnotationPresent(NoRepositoryBean.class);
101+
boolean isAnnotated = AnnotationUtils.findAnnotation(type, RepositoryDefinition.class) != null;
102+
boolean excludedByAnnotation = AnnotationUtils.findAnnotation(type, NoRepositoryBean.class) != null;
103103

104104
return isInterface && (extendsRepository || isAnnotated) && !excludedByAnnotation;
105105
}
@@ -113,7 +113,7 @@ private Set<Annotation> getQualifiers(final Class<?> type) {
113113
Annotation[] annotations = type.getAnnotations();
114114
for (Annotation annotation : annotations) {
115115
Class<? extends Annotation> annotationType = annotation.annotationType();
116-
if (annotationType.isAnnotationPresent(Qualifier.class)) {
116+
if (AnnotationUtils.findAnnotation(annotationType, Qualifier.class) != null) {
117117
qualifiers.add(annotation);
118118
}
119119
}

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

+2-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;
@@ -44,7 +45,7 @@ public AnnotationRepositoryMetadata(Class<?> repositoryInterface) {
4445

4546
super(repositoryInterface);
4647

47-
Assert.isTrue(repositoryInterface.isAnnotationPresent(RepositoryDefinition.class),
48+
Assert.isTrue(AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class) != null,
4849
String.format(NO_ANNOTATION_FOUND, repositoryInterface.getName()));
4950

5051
this.idType = resolveIdType(repositoryInterface);

0 commit comments

Comments
 (0)