Skip to content

Use AnnotationUtils.findAnnotation(…) instead of AnnotatedElement.isAnnotationPresent(…) to support meta-annotations #2500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.commons.logging.LogFactory;

import org.springframework.core.GenericTypeResolver;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.ConverterRegistry;
Expand All @@ -42,6 +43,7 @@
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;
Expand All @@ -57,6 +59,7 @@
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Xeno Amess
* @since 2.0
*/
public class CustomConversions {
Expand Down Expand Up @@ -757,8 +760,8 @@ public Streamable<ConverterRegistration> getRegistrationsFor(Object converter) {
Assert.notNull(converter, "Converter must not be null!");

Class<?> type = converter.getClass();
boolean isWriting = type.isAnnotationPresent(WritingConverter.class);
boolean isReading = type.isAnnotationPresent(ReadingConverter.class);
boolean isWriting = AnnotationUtils.findAnnotation(type, WritingConverter.class) != null;
boolean isReading = AnnotationUtils.findAnnotation(type, ReadingConverter.class) != null;

if (converter instanceof ConverterAware) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
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;
Expand All @@ -42,6 +44,7 @@
* @author Christoph Strobl
* @author Mark Paluch
* @author Myeonghyeon Lee
* @author Xeno Amess
*/
public class PreferredConstructor<T, P extends PersistentProperty<P>> {

Expand Down Expand Up @@ -110,7 +113,7 @@ public boolean isNoArgConstructor() {
* @return
*/
public boolean isExplicitlyAnnotated() {
return constructor.isAnnotationPresent(PersistenceConstructor.class);
return AnnotationUtils.findAnnotation(constructor, PersistenceConstructor.class) != null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
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;
Expand All @@ -46,6 +50,7 @@
* @author Christoph Strobl
* @author Roman Rodov
* @author Mark Paluch
* @author Xeno Amess
*/
public interface PreferredConstructorDiscoverer<T, P extends PersistentProperty<P>> {

Expand Down Expand Up @@ -115,7 +120,7 @@ <T, P extends PersistentProperty<P>> PreferredConstructor<T, P> discover(TypeInf
continue;
}

if (candidate.isAnnotationPresent(PersistenceConstructor.class)) {
if (AnnotationUtils.findAnnotation(candidate, PersistenceConstructor.class) != null) {
return buildPreferredConstructor(candidate, type, entity);
}

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

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() //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.expression.MapAccessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
Expand All @@ -45,6 +47,7 @@
* @author Oliver Gierke
* @author Thomas Darimont
* @author Christoph Strobl
* @author Xeno Amess
* @see 1.10
*/
class SpelEvaluatingMethodInterceptor implements MethodInterceptor {
Expand Down Expand Up @@ -108,12 +111,11 @@ private static Map<Integer, Expression> potentiallyCreateExpressionsForMethodsOn

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

if (!method.isAnnotationPresent(Value.class)) {
Value value = AnnotationUtils.findAnnotation(method, Value.class);
if (value == null) {
continue;
}

Value value = method.getAnnotation(Value.class);

if (!StringUtils.hasText(value.value())) {
throw new IllegalStateException(String.format("@Value annotation on %s contains empty expression!", method));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
import org.apache.commons.logging.Log;
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;
Expand All @@ -61,6 +63,7 @@
* @author Jens Schauder
* @author Christoph Strobl
* @author Ariel Carrera
* @author Xeno Amess
*/
public abstract class CdiRepositoryBean<T> implements Bean<T>, PassivationCapable {

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

return Arrays.stream(repositoryType.getAnnotations())//
.map(Annotation::annotationType)//
.filter(it -> it.isAnnotationPresent(Stereotype.class))//
.filter(it -> AnnotationUtils.findAnnotation(it, Stereotype.class) != null)//
.collect(Collectors.toSet());
}

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

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
* @author Xeno Amess
*/
public abstract class CdiRepositoryExtensionSupport implements Extension {

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

boolean isInterface = type.isInterface();
boolean extendsRepository = Repository.class.isAssignableFrom(type);
boolean isAnnotated = type.isAnnotationPresent(RepositoryDefinition.class);
boolean excludedByAnnotation = type.isAnnotationPresent(NoRepositoryBean.class);
boolean isAnnotated = AnnotationUtils.findAnnotation(type, RepositoryDefinition.class) != null;
boolean excludedByAnnotation = AnnotationUtils.findAnnotation(type, NoRepositoryBean.class) != null;

return isInterface && (extendsRepository || isAnnotated) && !excludedByAnnotation;
}
Expand All @@ -113,7 +114,7 @@ private Set<Annotation> getQualifiers(final Class<?> type) {
Annotation[] annotations = type.getAnnotations();
for (Annotation annotation : annotations) {
Class<? extends Annotation> annotationType = annotation.annotationType();
if (annotationType.isAnnotationPresent(Qualifier.class)) {
if (AnnotationUtils.findAnnotation(annotationType, Qualifier.class) != null) {
qualifiers.add(annotation);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.repository.core.support;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.util.Assert;
Expand All @@ -25,6 +26,7 @@
*
* @author Oliver Gierke
* @author Thomas Darimont
* @author Xeno Amess
*/
public class AnnotationRepositoryMetadata extends AbstractRepositoryMetadata {

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

super(repositoryInterface);

Assert.isTrue(repositoryInterface.isAnnotationPresent(RepositoryDefinition.class),
Assert.isTrue(AnnotationUtils.findAnnotation(repositoryInterface, RepositoryDefinition.class) != null,
String.format(NO_ANNOTATION_FOUND, repositoryInterface.getName()));

this.idType = resolveIdType(repositoryInterface);
Expand Down