Skip to content

Commit

Permalink
Restore retrieval of plain annotations through direct presence checks
Browse files Browse the repository at this point in the history
Includes deprecation of several AnnotationUtils methods and nullability refinements for passed-in function arguments at the MergedAnnotation API level... also, MergedAnnotation.getType() returns a Class now.

Closes gh-22663
Closes gh-22685
  • Loading branch information
jhoeller committed Mar 26, 2019
1 parent f7a4850 commit 210b178
Show file tree
Hide file tree
Showing 16 changed files with 431 additions and 553 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,10 @@ private MergedAnnotation<?> findAutowiredAnnotation(AccessibleObject ao) {
* @param ann the Autowired annotation
* @return whether the annotation indicates that a dependency is required
*/
@SuppressWarnings("deprecation")
protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
return determineRequiredStatus(
ann.asMap(mergedAnnotation -> new AnnotationAttributes()));
return determineRequiredStatus((AnnotationAttributes)
ann.asMap(mergedAnnotation -> new AnnotationAttributes(mergedAnnotation.getType())));
}

/**
Expand All @@ -533,7 +534,7 @@ protected boolean determineRequiredStatus(MergedAnnotation<?> ann) {
* or method when no beans are found.
* @param ann the Autowired annotation
* @return whether the annotation indicates that a dependency is required
* @deprecated since 5.2 in favor of {@link #determineRequiredStatus(MergedAnnotation)}
* @deprecated since 5.2, in favor of {@link #determineRequiredStatus(MergedAnnotation)}
*/
@Deprecated
protected boolean determineRequiredStatus(AnnotationAttributes ann) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Predicate;
Expand All @@ -30,6 +29,7 @@
* Abstract base class for {@link MergedAnnotation} implementations.
*
* @author Phillip Webb
* @author Juergen Hoeller
* @since 5.2
* @param <A> the annotation type
*/
Expand Down Expand Up @@ -167,19 +167,10 @@ public MergedAnnotation<A> filterDefaultValues() {
}

@Override
public Map<String, Object> asMap(MapValues... options) {
return asMap(null, options);
}

@Override
public Optional<A> synthesize(
@Nullable Predicate<? super MergedAnnotation<A>> condition)
public Optional<A> synthesize(Predicate<? super MergedAnnotation<A>> condition)
throws NoSuchElementException {

if (condition == null || condition.test(this)) {
return Optional.of(synthesize());
}
return Optional.empty();
return (condition.test(this) ? Optional.of(synthesize()) : Optional.empty());
}

@Override
Expand All @@ -199,7 +190,7 @@ private <T> T getRequiredAttributeValue(String attributeName, Class<T> type) {
T value = getAttributeValue(attributeName, type);
if (value == null) {
throw new NoSuchElementException("No attribute named '" + attributeName +
"' present in merged annotation " + getType());
"' present in merged annotation " + getType().getName());
}
return value;
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
package org.springframework.core.annotation;

import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.Collection;

import org.springframework.util.Assert;

/**
* Callback interface that can be used to filter specific annotation types.
Expand Down Expand Up @@ -96,52 +92,4 @@ static AnnotationFilter packages(String... packages) {
return new PackagesAnnotationFilter(packages);
}

/**
* Return an {@link AnnotationFilter} that is the most appropriate for, and
* will always match the given annotation type. Whenever possible,
* {@link AnnotationFilter#PLAIN} will be returned.
* @param annotationType the annotation type to check
* @return the most appropriate annotation filter
*/
static AnnotationFilter mostAppropriateFor(Class<?> annotationType) {
return (PLAIN.matches(annotationType) ? NONE : PLAIN);
}

/**
* Return an {@link AnnotationFilter} that is the most appropriate for, and
* will always match all the given annotation types. Whenever possible,
* {@link AnnotationFilter#PLAIN} will be returned.
* @param annotationTypes the annotation types to check
* @return the most appropriate annotation filter
*/
static AnnotationFilter mostAppropriateFor(Class<?>... annotationTypes) {
return mostAppropriateFor(Arrays.asList(annotationTypes));
}

/**
* Return an {@link AnnotationFilter} that is the most appropriate for, and
* will always match all the given annotation types. Whenever possible,
* {@link AnnotationFilter#PLAIN} will be returned.
* @param annotationTypes the annotation types to check (may be class names
* or class types)
* @return the most appropriate annotation filter
*/
@SuppressWarnings("unchecked")
static AnnotationFilter mostAppropriateFor(Collection<?> annotationTypes) {
for (Object annotationType : annotationTypes) {
if (annotationType == null) {
continue;
}
Assert.isTrue(annotationType instanceof Class || annotationType instanceof String,
"AnnotationType must be a Class or String");
if (annotationType instanceof Class && PLAIN.matches((Class<Annotation>) annotationType)) {
return NONE;
}
if (annotationType instanceof String && PLAIN.matches((String) annotationType)) {
return NONE;
}
}
return PLAIN;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ final class AnnotationTypeMappings {
private final List<AnnotationTypeMapping> mappings;


private AnnotationTypeMappings(AnnotationFilter filter,
Class<? extends Annotation> annotationType) {
private AnnotationTypeMappings(AnnotationFilter filter, Class<? extends Annotation> annotationType) {
this.filter = filter;
this.mappings = new ArrayList<>();
addAllMappings(annotationType);
Expand Down Expand Up @@ -97,27 +96,23 @@ private void addMetaAnnotationsToQueue(Deque<AnnotationTypeMapping> queue, Annot
}
}

private void addIfPossible(Deque<AnnotationTypeMapping> queue,
AnnotationTypeMapping parent, Annotation annotation) {
addIfPossible(queue, parent, annotation.annotationType(), annotation);
private void addIfPossible(Deque<AnnotationTypeMapping> queue, AnnotationTypeMapping parent, Annotation ann) {
addIfPossible(queue, parent, ann.annotationType(), ann);
}

private void addIfPossible(Deque<AnnotationTypeMapping> queue,
@Nullable AnnotationTypeMapping parent,
Class<? extends Annotation> annotationType, @Nullable Annotation annotation) {
private void addIfPossible(Deque<AnnotationTypeMapping> queue, @Nullable AnnotationTypeMapping parent,
Class<? extends Annotation> annotationType, @Nullable Annotation ann) {

try {
queue.addLast(new AnnotationTypeMapping(parent, annotationType, annotation));
queue.addLast(new AnnotationTypeMapping(parent, annotationType, ann));
}
catch (Exception ex) {
if (ex instanceof AnnotationConfigurationException) {
throw (AnnotationConfigurationException) ex;
}
if (failureLogger.isEnabled()) {
failureLogger.log(
"Failed to introspect meta-annotation "
+ annotationType.getName(),
(parent != null) ? parent.getAnnotationType() : null, ex);
failureLogger.log("Failed to introspect meta-annotation " + annotationType.getName(),
(parent != null ? parent.getAnnotationType() : null), ex);
}
}
}
Expand Down Expand Up @@ -167,7 +162,7 @@ AnnotationTypeMapping get(int index) {
* @return type mappings for the annotation type
*/
static AnnotationTypeMappings forAnnotationType(Class<? extends Annotation> annotationType) {
return forAnnotationType(annotationType, AnnotationFilter.mostAppropriateFor(annotationType));
return forAnnotationType(annotationType, AnnotationFilter.PLAIN);
}

/**
Expand Down Expand Up @@ -197,7 +192,6 @@ private static class Cache {

private final Map<Class<? extends Annotation>, AnnotationTypeMappings> mappings;


/**
* Create a cache instance with the specified filter.
* @param filter the annotation filter
Expand All @@ -207,7 +201,6 @@ private static class Cache {
this.mappings = new ConcurrentReferenceHashMap<>();
}


/**
* Return or create {@link AnnotationTypeMappings} for the specified
* annotation type.
Expand All @@ -218,8 +211,7 @@ AnnotationTypeMappings get(Class<? extends Annotation> annotationType) {
return this.mappings.computeIfAbsent(annotationType, this::createMappings);
}

AnnotationTypeMappings createMappings(
Class<? extends Annotation> annotationType) {
AnnotationTypeMappings createMappings(Class<? extends Annotation> annotationType) {
return new AnnotationTypeMappings(this.filter, annotationType);
}

Expand Down
Loading

0 comments on commit 210b178

Please sign in to comment.