/** * Can the given pointcut apply at all on the given class? * <p>This is an important test as it can be used to optimize * out a pointcut for a class. * @param pc the static or dynamic pointcut to check * @param targetClass the class to test * @return whether the pointcut can apply on any method */ public static boolean canApply(Pointcut pc, Class<?> targetClass) { return canApply(pc, targetClass, false); }
/** * Can the given advisor apply at all on the given class? * This is an important test as it can be used to optimize * out a advisor for a class. * @param advisor the advisor to check * @param targetClass class we're testing * @return whether the pointcut can apply on any method */ public static boolean canApply(Advisor advisor, Class<?> targetClass) { return canApply(advisor, targetClass, false); }
/** * Check whether the given class is eligible for advising with this * post-processor's {@link Advisor}. * <p>Implements caching of {@code canApply} results per bean target class. * @param targetClass the class to check against * @see AopUtils#canApply(Advisor, Class) */ protected boolean isEligible(Class<?> targetClass) { Boolean eligible = this.eligibleBeans.get(targetClass); if (eligible != null) { return eligible; } if (this.advisor == null) { return false; } eligible = AopUtils.canApply(this.advisor, targetClass); this.eligibleBeans.put(targetClass, eligible); return eligible; }
/** * Determine the sublist of the {@code candidateAdvisors} list * that is applicable to the given class. * @param candidateAdvisors the Advisors to evaluate * @param clazz the target class * @return sublist of Advisors that can apply to an object of the given class * (may be the incoming List as-is) */ public static List<Advisor> findAdvisorsThatCanApply(List<Advisor> candidateAdvisors, Class<?> clazz) { if (candidateAdvisors.isEmpty()) { return candidateAdvisors; } List<Advisor> eligibleAdvisors = new ArrayList<>(); for (Advisor candidate : candidateAdvisors) { if (candidate instanceof IntroductionAdvisor && canApply(candidate, clazz)) { eligibleAdvisors.add(candidate); } } boolean hasIntroductions = !eligibleAdvisors.isEmpty(); for (Advisor candidate : candidateAdvisors) { if (candidate instanceof IntroductionAdvisor) { // already processed continue; } if (canApply(candidate, clazz, hasIntroductions)) { eligibleAdvisors.add(candidate); } } return eligibleAdvisors; }
/** * Can the given advisor apply at all on the given class? * <p>This is an important test as it can be used to optimize out a advisor for a class. * This version also takes into account introductions (for IntroductionAwareMethodMatchers). * @param advisor the advisor to check * @param targetClass class we're testing * @param hasIntroductions whether or not the advisor chain for this bean includes * any introductions * @return whether the pointcut can apply on any method */ public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) { if (advisor instanceof IntroductionAdvisor) { return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass); } else if (advisor instanceof PointcutAdvisor) { PointcutAdvisor pca = (PointcutAdvisor) advisor; return canApply(pca.getPointcut(), targetClass, hasIntroductions); } else { // It doesn't have a pointcut so we assume it applies. return true; } }
@Test public void testPointcutCanNeverApply() { class TestPointcut extends StaticMethodMatcherPointcut { @Override public boolean matches(Method method, @Nullable Class<?> clazzy) { return false; } } Pointcut no = new TestPointcut(); assertFalse(AopUtils.canApply(no, Object.class)); }
@Test public void testPointcutAppliesToOneMethodOnObject() { class TestPointcut extends StaticMethodMatcherPointcut { @Override public boolean matches(Method method, @Nullable Class<?> clazz) { return method.getName().equals("hashCode"); } } Pointcut pc = new TestPointcut(); // will return true if we're not proxying interfaces assertTrue(AopUtils.canApply(pc, Object.class)); }
private boolean canApply(final Class<?> targetClass) { Boolean cachedApplicability = applicabilityCache.get(targetClass); if (cachedApplicability != null) { return (boolean) cachedApplicability; } boolean canApply = AopUtils.canApply(pointcut, targetClass); applicabilityCache.putIfAbsent(targetClass, canApply); return canApply; }
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AopInfrastructureBean) { // Ignore AOP infrastructure such as scoped proxies. return bean; } Class<?> targetClass = AopUtils.getTargetClass(bean); if (AopUtils.canApply(this.advisor, targetClass)) { if (bean instanceof Advised) { ((Advised) bean).addAdvisor(0, this.advisor); return bean; } else { ProxyFactory proxyFactory = new ProxyFactory(bean); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); proxyFactory.addAdvisor(this.advisor); return proxyFactory.getProxy(this.beanClassLoader); } } else { // No async proxy needed. return bean; } }
/** * Can the given advisor apply at all on the given class? * This is an important test as it can be used to optimize * out a advisor for a class. * @param advisor the advisor to check * @param targetClass class we're testing * @return whether the pointcut can apply on any method */ public static boolean canApply(Advisor advisor, Class<?> targetClass) { return canApply(advisor, targetClass, false); }
/** * Can the given pointcut apply at all on the given class? * <p>This is an important test as it can be used to optimize * out a pointcut for a class. * @param pc the static or dynamic pointcut to check * @param targetClass the class to test * @return whether the pointcut can apply on any method */ public static boolean canApply(Pointcut pc, Class<?> targetClass) { return canApply(pc, targetClass, false); }
/** * Can the given pointcut apply at all on the given class? * <p>This is an important test as it can be used to optimize * out a pointcut for a class. * @param pc the static or dynamic pointcut to check * @param targetClass the class to test * @return whether the pointcut can apply on any method */ public static boolean canApply(Pointcut pc, Class<?> targetClass) { return canApply(pc, targetClass, false); }
/** * Check whether the given class is eligible for advising with this * post-processor's {@link Advisor}. * <p>Implements caching of {@code canApply} results per bean target class. * @param targetClass the class to check against * @see AopUtils#canApply(Advisor, Class) */ protected boolean isEligible(Class<?> targetClass) { Boolean eligible = this.eligibleBeans.get(targetClass); if (eligible != null) { return eligible; } eligible = AopUtils.canApply(this.advisor, targetClass); this.eligibleBeans.put(targetClass, eligible); return eligible; }
/** * Check whether the given class is eligible for advising with this * post-processor's {@link Advisor}. * <p>Implements caching of {@code canApply} results per bean target class. * @param targetClass the class to check against * @see AopUtils#canApply(Advisor, Class) */ protected boolean isEligible(Class<?> targetClass) { Boolean eligible = this.eligibleBeans.get(targetClass); if (eligible != null) { return eligible; } if (this.advisor == null) { return false; } eligible = AopUtils.canApply(this.advisor, targetClass); this.eligibleBeans.put(targetClass, eligible); return eligible; }
/** * Convenience method to return the sublist of the candidateAdvisors list * that are applicable to the given class. * @param candidateAdvisors advisors to evaluate * @param clazz target class * @return sublist of advisors that could apply to an object of the given class */ public static List findAdvisorsThatCanApply(List candidateAdvisors, Class clazz) { List eligibleAdvisors = new LinkedList(); for (Iterator it = candidateAdvisors.iterator(); it.hasNext();) { Advisor candidate = (Advisor) it.next(); if (AopUtils.canApply(candidate, clazz)) { eligibleAdvisors.add(candidate); if (logger.isDebugEnabled()) { logger.debug("Candidate advisor [" + candidate + "] accepted for class [" + clazz.getName() + "]"); } } else { if (logger.isDebugEnabled()) { logger.debug("Candidate advisor [" + candidate + "] rejected for class [" + clazz.getName() + "]"); } } } return eligibleAdvisors; }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { Class<?> targetClass = AopUtils.getTargetClass(bean); // the set will hold records of prior class scans and will contain the bean classes that can not // be assigned to the Advisor interface and therefore can be short circuited if (this.nonApplicableCache.contains(targetClass)) { return bean; } if (AopUtils.canApply(this.advisor, targetClass)) { if (bean instanceof Advised) { ((Advised) bean).addAdvisor(this.advisor); return bean; } else { ProxyFactory proxyFactory = new ProxyFactory(bean); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); proxyFactory.addAdvisor(this.advisor); return proxyFactory.getProxy(this.beanClassLoader); } } else { // cannot apply advisor this.nonApplicableCache.add(targetClass); return bean; } }
/** * Can the given advisor apply at all on the given class? * This is an important test as it can be used to optimize * out a advisor for a class. * @param advisor the advisor to check * @param targetClass class we're testing * @return whether the pointcut can apply on any method */ public static boolean canApply(Advisor advisor, Class targetClass) { if (advisor instanceof IntroductionAdvisor) { return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass); } else if (advisor instanceof PointcutAdvisor) { PointcutAdvisor pca = (PointcutAdvisor) advisor; return canApply(pca.getPointcut(), targetClass); } else { // It doesn't have a pointcut so we assume it applies return true; } }
/** * Can the given advisor apply at all on the given class? * <p>This is an important test as it can be used to optimize out a advisor for a class. * This version also takes into account introductions (for IntroductionAwareMethodMatchers). * @param advisor the advisor to check * @param targetClass class we're testing * @param hasIntroductions whether or not the advisor chain for this bean includes * any introductions * @return whether the pointcut can apply on any method */ public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) { if (advisor instanceof IntroductionAdvisor) { return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass); } else if (advisor instanceof PointcutAdvisor) { PointcutAdvisor pca = (PointcutAdvisor) advisor; return canApply(pca.getPointcut(), targetClass, hasIntroductions); } else { // It doesn't have a pointcut so we assume it applies. return true; } }
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AopInfrastructureBean) { // Ignore AOP infrastructure such as scoped proxies. return bean; } Class<?> targetClass = AopUtils.getTargetClass(bean); if (AopUtils.canApply(this.advisor, targetClass)) { if (bean instanceof Advised) { ((Advised) bean).addAdvisor(0, this.advisor); return bean; } else { ProxyFactory proxyFactory = new ProxyFactory(bean); // Copy our properties (proxyTargetClass etc) inherited from ProxyConfig. proxyFactory.copyFrom(this); proxyFactory.addAdvisor(this.advisor); return proxyFactory.getProxy(this.beanClassLoader); } } else { // No async proxy needed. return bean; } }