Skip to content

Commit ef522f8

Browse files
committed
Polishing.
Extract duplicate calls to findAnnotation(…) into static helper methods. See #2500
1 parent ca723d1 commit ef522f8

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

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

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

18+
import java.lang.annotation.Annotation;
1819
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collection;
@@ -43,7 +44,6 @@
4344
import org.springframework.core.convert.support.GenericConversionService;
4445
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
4546
import org.springframework.data.mapping.model.SimpleTypeHolder;
46-
import org.springframework.data.repository.RepositoryDefinition;
4747
import org.springframework.data.util.Streamable;
4848
import org.springframework.lang.Nullable;
4949
import org.springframework.util.Assert;
@@ -760,8 +760,8 @@ public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
760760
Assert.notNull(converter, "Converter must not be null!");
761761

762762
Class<?> type = converter.getClass();
763-
boolean isWriting = AnnotationUtils.findAnnotation(type, WritingConverter.class) != null;
764-
boolean isReading = AnnotationUtils.findAnnotation(type, ReadingConverter.class) != null;
763+
boolean isWriting = isAnnotatedWith(type, WritingConverter.class);
764+
boolean isReading = isAnnotatedWith(type, ReadingConverter.class);
765765

766766
if (converter instanceof ConverterAware) {
767767

@@ -789,6 +789,10 @@ public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
789789
}
790790
}
791791

792+
private static boolean isAnnotatedWith(Class<?> type, Class<? extends Annotation> annotationType) {
793+
return AnnotationUtils.findAnnotation(type, annotationType) != null;
794+
}
795+
792796
private Streamable<ConverterRegistration> getRegistrationFor(Object converter, Class<?> type, boolean isReading,
793797
boolean isWriting) {
794798

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242

4343
import org.springframework.core.annotation.AnnotationUtils;
4444
import org.springframework.core.log.LogMessage;
45-
import org.springframework.data.annotation.PersistenceConstructor;
4645
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
4746
import org.springframework.data.repository.config.RepositoryFragmentConfiguration;
4847
import org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments;
@@ -264,7 +263,7 @@ public Set<Class<? extends Annotation>> getStereotypes() {
264263

265264
return Arrays.stream(repositoryType.getAnnotations())//
266265
.map(Annotation::annotationType)//
267-
.filter(it -> AnnotationUtils.findAnnotation(it, Stereotype.class) != null)//
266+
.filter(it -> isAnnotatedWith(it, Stereotype.class))//
268267
.collect(Collectors.toSet());
269268
}
270269

@@ -281,7 +280,7 @@ public Class<?> getBeanClass() {
281280
* @see javax.enterprise.inject.spi.Bean#isAlternative()
282281
*/
283282
public boolean isAlternative() {
284-
return AnnotationUtils.findAnnotation(repositoryType, Alternative.class) != null;
283+
return isAnnotatedWith(repositoryType, Alternative.class);
285284
}
286285

287286
/*
@@ -524,6 +523,10 @@ public String toString() {
524523
qualifiers.toString());
525524
}
526525

526+
private static boolean isAnnotatedWith(Class<?> type, Class<? extends Annotation> annotationType) {
527+
return AnnotationUtils.findAnnotation(type, annotationType) != null;
528+
}
529+
527530
enum DefaultCdiRepositoryConfiguration implements CdiRepositoryConfiguration {
528531
INSTANCE;
529532
}

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

+12-4
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ private boolean isRepository(Class<?> type) {
9999

100100
boolean isInterface = type.isInterface();
101101
boolean extendsRepository = Repository.class.isAssignableFrom(type);
102-
boolean isAnnotated = AnnotationUtils.findAnnotation(type, RepositoryDefinition.class) != null;
103-
boolean excludedByAnnotation = AnnotationUtils.findAnnotation(type, NoRepositoryBean.class) != null;
102+
boolean isAnnotated = isAnnotatedWith(type, RepositoryDefinition.class);
103+
boolean excludedByAnnotation = isAnnotatedDirectlyWith(type, NoRepositoryBean.class);
104104

105105
return isInterface && (extendsRepository || isAnnotated) && !excludedByAnnotation;
106106
}
@@ -114,7 +114,7 @@ private Set<Annotation> getQualifiers(final Class<?> type) {
114114
Annotation[] annotations = type.getAnnotations();
115115
for (Annotation annotation : annotations) {
116116
Class<? extends Annotation> annotationType = annotation.annotationType();
117-
if (AnnotationUtils.findAnnotation(annotationType, Qualifier.class) != null) {
117+
if (isAnnotatedWith(annotationType, Qualifier.class)) {
118118
qualifiers.add(annotation);
119119
}
120120
}
@@ -165,7 +165,7 @@ protected void registerBean(CdiRepositoryBean<?> bean) {
165165

166166
Class<?> repositoryInterface = bean.getBeanClass();
167167

168-
if (AnnotationUtils.findAnnotation(repositoryInterface, Eager.class) != null) {
168+
if (isAnnotatedWith(repositoryInterface, Eager.class)) {
169169
this.eagerRepositories.add(bean);
170170
}
171171
}
@@ -185,6 +185,14 @@ protected CdiRepositoryContext getRepositoryContext() {
185185
return context;
186186
}
187187

188+
private static boolean isAnnotatedWith(Class<?> type, Class<? extends Annotation> annotationType) {
189+
return AnnotationUtils.findAnnotation(type, annotationType) != null;
190+
}
191+
192+
private static boolean isAnnotatedDirectlyWith(Class<?> type, Class<? extends Annotation> annotationType) {
193+
return AnnotationUtils.isAnnotationDeclaredLocally(annotationType, type);
194+
}
195+
188196
@SuppressWarnings("all")
189197
static class DefaultAnnotationLiteral extends AnnotationLiteral<Default> implements Default {
190198

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public AnnotationRepositoryMetadata(Class<?> repositoryInterface) {
4747
super(repositoryInterface);
4848

4949
Assert.isTrue(AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class) != null,
50-
String.format(NO_ANNOTATION_FOUND, repositoryInterface.getName()));
50+
() -> String.format(NO_ANNOTATION_FOUND, repositoryInterface.getName()));
5151

5252
this.idType = resolveIdType(repositoryInterface);
5353
this.domainType = resolveDomainType(repositoryInterface);
@@ -73,7 +73,7 @@ public Class<?> getDomainType() {
7373

7474
private Class<?> resolveIdType(Class<?> repositoryInterface) {
7575

76-
RepositoryDefinition annotation = repositoryInterface.getAnnotation(RepositoryDefinition.class);
76+
RepositoryDefinition annotation = AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class);
7777

7878
if (annotation == null || annotation.idClass() == null) {
7979
throw new IllegalArgumentException(String.format("Could not resolve id type of %s!", repositoryInterface));
@@ -84,7 +84,7 @@ private Class<?> resolveIdType(Class<?> repositoryInterface) {
8484

8585
private Class<?> resolveDomainType(Class<?> repositoryInterface) {
8686

87-
RepositoryDefinition annotation = repositoryInterface.getAnnotation(RepositoryDefinition.class);
87+
RepositoryDefinition annotation = AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class);
8888

8989
if (annotation == null || annotation.domainClass() == null) {
9090
throw new IllegalArgumentException(String.format("Could not resolve domain type of %s!", repositoryInterface));

0 commit comments

Comments
 (0)