Polishing.

Extract duplicate calls to findAnnotation(…) into static helper methods.

See #2500
This commit is contained in:
Mark Paluch
2021-12-08 10:14:58 +01:00
parent ca723d11c7
commit ef522f8e9c
4 changed files with 28 additions and 13 deletions

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.convert;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -43,7 +44,6 @@ import org.springframework.core.convert.converter.GenericConverter.ConvertiblePa
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -760,8 +760,8 @@ public class CustomConversions {
Assert.notNull(converter, "Converter must not be null!");
Class<?> type = converter.getClass();
boolean isWriting = AnnotationUtils.findAnnotation(type, WritingConverter.class) != null;
boolean isReading = AnnotationUtils.findAnnotation(type, ReadingConverter.class) != null;
boolean isWriting = isAnnotatedWith(type, WritingConverter.class);
boolean isReading = isAnnotatedWith(type, ReadingConverter.class);
if (converter instanceof ConverterAware) {
@@ -789,6 +789,10 @@ public class CustomConversions {
}
}
private static boolean isAnnotatedWith(Class<?> type, Class<? extends Annotation> annotationType) {
return AnnotationUtils.findAnnotation(type, annotationType) != null;
}
private Streamable<ConverterRegistration> getRegistrationFor(Object converter, Class<?> type, boolean isReading,
boolean isWriting) {

View File

@@ -42,7 +42,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.log.LogMessage;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.repository.config.CustomRepositoryImplementationDetector;
import org.springframework.data.repository.config.RepositoryFragmentConfiguration;
import org.springframework.data.repository.core.support.RepositoryComposition.RepositoryFragments;
@@ -264,7 +263,7 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
return Arrays.stream(repositoryType.getAnnotations())//
.map(Annotation::annotationType)//
.filter(it -> AnnotationUtils.findAnnotation(it, Stereotype.class) != null)//
.filter(it -> isAnnotatedWith(it, Stereotype.class))//
.collect(Collectors.toSet());
}
@@ -281,7 +280,7 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
* @see javax.enterprise.inject.spi.Bean#isAlternative()
*/
public boolean isAlternative() {
return AnnotationUtils.findAnnotation(repositoryType, Alternative.class) != null;
return isAnnotatedWith(repositoryType, Alternative.class);
}
/*
@@ -524,6 +523,10 @@ public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapabl
qualifiers.toString());
}
private static boolean isAnnotatedWith(Class<?> type, Class<? extends Annotation> annotationType) {
return AnnotationUtils.findAnnotation(type, annotationType) != null;
}
enum DefaultCdiRepositoryConfiguration implements CdiRepositoryConfiguration {
INSTANCE;
}

View File

@@ -99,8 +99,8 @@ public abstract class CdiRepositoryExtensionSupport implements Extension {
boolean isInterface = type.isInterface();
boolean extendsRepository = Repository.class.isAssignableFrom(type);
boolean isAnnotated = AnnotationUtils.findAnnotation(type, RepositoryDefinition.class) != null;
boolean excludedByAnnotation = AnnotationUtils.findAnnotation(type, NoRepositoryBean.class) != null;
boolean isAnnotated = isAnnotatedWith(type, RepositoryDefinition.class);
boolean excludedByAnnotation = isAnnotatedDirectlyWith(type, NoRepositoryBean.class);
return isInterface && (extendsRepository || isAnnotated) && !excludedByAnnotation;
}
@@ -114,7 +114,7 @@ public abstract class CdiRepositoryExtensionSupport implements Extension {
Annotation[] annotations = type.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (AnnotationUtils.findAnnotation(annotationType, Qualifier.class) != null) {
if (isAnnotatedWith(annotationType, Qualifier.class)) {
qualifiers.add(annotation);
}
}
@@ -165,7 +165,7 @@ public abstract class CdiRepositoryExtensionSupport implements Extension {
Class<?> repositoryInterface = bean.getBeanClass();
if (AnnotationUtils.findAnnotation(repositoryInterface, Eager.class) != null) {
if (isAnnotatedWith(repositoryInterface, Eager.class)) {
this.eagerRepositories.add(bean);
}
}
@@ -185,6 +185,14 @@ public abstract class CdiRepositoryExtensionSupport implements Extension {
return context;
}
private static boolean isAnnotatedWith(Class<?> type, Class<? extends Annotation> annotationType) {
return AnnotationUtils.findAnnotation(type, annotationType) != null;
}
private static boolean isAnnotatedDirectlyWith(Class<?> type, Class<? extends Annotation> annotationType) {
return AnnotationUtils.isAnnotationDeclaredLocally(annotationType, type);
}
@SuppressWarnings("all")
static class DefaultAnnotationLiteral extends AnnotationLiteral<Default> implements Default {

View File

@@ -47,7 +47,7 @@ public class AnnotationRepositoryMetadata extends AbstractRepositoryMetadata {
super(repositoryInterface);
Assert.isTrue(AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class) != null,
String.format(NO_ANNOTATION_FOUND, repositoryInterface.getName()));
() -> String.format(NO_ANNOTATION_FOUND, repositoryInterface.getName()));
this.idType = resolveIdType(repositoryInterface);
this.domainType = resolveDomainType(repositoryInterface);
@@ -73,7 +73,7 @@ public class AnnotationRepositoryMetadata extends AbstractRepositoryMetadata {
private Class<?> resolveIdType(Class<?> repositoryInterface) {
RepositoryDefinition annotation = repositoryInterface.getAnnotation(RepositoryDefinition.class);
RepositoryDefinition annotation = AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class);
if (annotation == null || annotation.idClass() == null) {
throw new IllegalArgumentException(String.format("Could not resolve id type of %s!", repositoryInterface));
@@ -84,7 +84,7 @@ public class AnnotationRepositoryMetadata extends AbstractRepositoryMetadata {
private Class<?> resolveDomainType(Class<?> repositoryInterface) {
RepositoryDefinition annotation = repositoryInterface.getAnnotation(RepositoryDefinition.class);
RepositoryDefinition annotation = AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class);
if (annotation == null || annotation.domainClass() == null) {
throw new IllegalArgumentException(String.format("Could not resolve domain type of %s!", repositoryInterface));