Bypass method traversal for annotation introspection if possible

The isCandidateClass mechanism is consistently used for a bypass check before method traversal attempts. While by default this is only bypassing standard java types, the same mechanism can be used with index metadata which indicates non-presence of certain annotations.

See gh-22420
This commit is contained in:
Juergen Hoeller
2019-03-12 00:12:22 +01:00
parent 6266370a7a
commit e3a9826e56
25 changed files with 400 additions and 88 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.lang.annotation.Annotation;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -96,7 +97,7 @@ public class AnnotationMatchingPointcut implements Pointcut {
this.classFilter = new AnnotationClassFilter(classAnnotationType, checkInherited);
}
else {
this.classFilter = ClassFilter.TRUE;
this.classFilter = new AnnotationCandidateClassFilter(methodAnnotationType);
}
if (methodAnnotationType != null) {
@@ -164,4 +165,23 @@ public class AnnotationMatchingPointcut implements Pointcut {
return new AnnotationMatchingPointcut(null, annotationType);
}
/**
* {@link ClassFilter} that delegates to {@link AnnotationUtils#isCandidateClass}
* for filtering classes whose methods are not worth searching to begin with.
*/
private static class AnnotationCandidateClassFilter implements ClassFilter {
private final Class<? extends Annotation> annotationType;
public AnnotationCandidateClassFilter(Class<? extends Annotation> annotationType) {
this.annotationType = annotationType;
}
@Override
public boolean matches(Class<?> clazz) {
return AnnotationUtils.isCandidateClass(clazz, this.annotationType);
}
}
}