private boolean hasAspectName(Advisor anAdvisor) { return (anAdvisor instanceof AspectJPrecedenceInformation || anAdvisor.getAdvice() instanceof AspectJPrecedenceInformation); }
/** * Is the given advice included in any advisor within this proxy configuration? * @param advice the advice to check inclusion of * @return whether this advice instance is included */ public boolean adviceIncluded(@Nullable Advice advice) { if (advice != null) { for (Advisor advisor : this.advisors) { if (advisor.getAdvice() == advice) { return true; } } } return false; }
/** * Return the AspectJPrecedenceInformation provided by this advisor or its advice. * If neither the advisor nor the advice have precedence information, this method * will return {@code null}. */ @Nullable public static AspectJPrecedenceInformation getAspectJPrecedenceInformationFor(Advisor anAdvisor) { if (anAdvisor instanceof AspectJPrecedenceInformation) { return (AspectJPrecedenceInformation) anAdvisor; } Advice advice = anAdvisor.getAdvice(); if (advice instanceof AspectJPrecedenceInformation) { return (AspectJPrecedenceInformation) advice; } return null; }
@Override public int indexOf(Advice advice) { Assert.notNull(advice, "Advice must not be null"); for (int i = 0; i < this.advisors.size(); i++) { Advisor advisor = this.advisors.get(i); if (advisor.getAdvice() == advice) { return i; } } return -1; }
/** * Count advices of the given class. * @param adviceClass the advice class to check * @return the count of the interceptors of this class or subclasses */ public int countAdvicesOfType(@Nullable Class<?> adviceClass) { int count = 0; if (adviceClass != null) { for (Advisor advisor : this.advisors) { if (adviceClass.isInstance(advisor.getAdvice())) { count++; } } } return count; }
@Override public MethodInterceptor getInterceptor(Advisor advisor) { MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice(); return new MethodBeforeAdviceInterceptor(advice); }
@Override public MethodInterceptor getInterceptor(Advisor advisor) { AfterReturningAdvice advice = (AfterReturningAdvice) advisor.getAdvice(); return new AfterReturningAdviceInterceptor(advice); }
/** * Determine whether the given Advisor contains an AspectJ advice. * @param advisor the Advisor to check */ private static boolean isAspectJAdvice(Advisor advisor) { return (advisor instanceof InstantiationModelAwarePointcutAdvisor || advisor.getAdvice() instanceof AbstractAspectJAdvice || (advisor instanceof PointcutAdvisor && ((PointcutAdvisor) advisor).getPointcut() instanceof AspectJExpressionPointcut)); }
@Override public MethodInterceptor getInterceptor(Advisor advisor) { return new ThrowsAdviceInterceptor(advisor.getAdvice()); }
@Override public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException { List<MethodInterceptor> interceptors = new ArrayList<>(3); Advice advice = advisor.getAdvice(); if (advice instanceof MethodInterceptor) { interceptors.add((MethodInterceptor) advice); } for (AdvisorAdapter adapter : this.adapters) { if (adapter.supportsAdvice(advice)) { interceptors.add(adapter.getInterceptor(advisor)); } } if (interceptors.isEmpty()) { throw new UnknownAdviceTypeException(advisor.getAdvice()); } return interceptors.toArray(new MethodInterceptor[0]); }
/** * Return {@code true} if the advisor is a form of before advice. */ public static boolean isBeforeAdvice(Advisor anAdvisor) { AspectJPrecedenceInformation precedenceInfo = getAspectJPrecedenceInformationFor(anAdvisor); if (precedenceInfo != null) { return precedenceInfo.isBeforeAdvice(); } return (anAdvisor.getAdvice() instanceof BeforeAdvice); }
@Override public String toString() { StringBuilder sb = new StringBuilder(); Advice advice = this.advisor.getAdvice(); sb.append(ClassUtils.getShortName(advice.getClass())); sb.append(": "); if (this.advisor instanceof Ordered) { sb.append("order ").append(((Ordered) this.advisor).getOrder()).append(", "); } if (advice instanceof AbstractAspectJAdvice) { AbstractAspectJAdvice ajAdvice = (AbstractAspectJAdvice) advice; sb.append(ajAdvice.getAspectName()); sb.append(", declaration order "); sb.append(ajAdvice.getDeclarationOrder()); } return sb.toString(); } }
/** * Return {@code true} if the advisor is a form of after advice. */ public static boolean isAfterAdvice(Advisor anAdvisor) { AspectJPrecedenceInformation precedenceInfo = getAspectJPrecedenceInformationFor(anAdvisor); if (precedenceInfo != null) { return precedenceInfo.isAfterAdvice(); } return (anAdvisor.getAdvice() instanceof AfterAdvice); }
private SimpleBeforeAdviceImpl getAdviceImpl(ITestBean tb) { Advised advised = (Advised) tb; Advisor advisor = advised.getAdvisors()[0]; return (SimpleBeforeAdviceImpl) advisor.getAdvice(); }
@Override public MethodInterceptor getInterceptor(Advisor advisor) { SimpleBeforeAdvice advice = (SimpleBeforeAdvice) advisor.getAdvice(); return new SimpleBeforeAdviceInterceptor(advice) ; }
@Override public int hashCode() { int hashCode = 0; Advisor[] advisors = this.advised.getAdvisors(); for (Advisor advisor : advisors) { Advice advice = advisor.getAdvice(); hashCode = 13 * hashCode + advice.getClass().hashCode(); } hashCode = 13 * hashCode + (this.advised.isFrozen() ? 1 : 0); hashCode = 13 * hashCode + (this.advised.isExposeProxy() ? 1 : 0); hashCode = 13 * hashCode + (this.advised.isOptimize() ? 1 : 0); hashCode = 13 * hashCode + (this.advised.isOpaque() ? 1 : 0); return hashCode; } }
for (int i = 0; i < advisors.length; i++) { Advisor advisor = advisors[i]; Advice advice = advisor.getAdvice(); if (org.springframework.aop.aspectj.AspectJAroundAdvice.class.isInstance(advice)) {
@Test public void testChainedDecorators() throws Exception { ITestBean bean = (ITestBean) this.beanFactory.getBean("chainedTestBean"); assertTestBean(bean); assertTrue(AopUtils.isAopProxy(bean)); Advisor[] advisors = ((Advised) bean).getAdvisors(); assertEquals("Incorrect number of advisors", 2, advisors.length); assertEquals("Incorrect advice class", DebugInterceptor.class, advisors[0].getAdvice().getClass()); assertEquals("Incorrect advice class", NopInterceptor.class, advisors[1].getAdvice().getClass()); }
PlatformTransactionManager getTransactionManagerSetOnJobRepository(JobRepository jobRepository) throws Exception { TargetSource targetSource = ((Advised) jobRepository).getTargetSource(); // proxy created in SimpleBatchConfiguration.createLazyProxy Advised target = (Advised) targetSource.getTarget(); // initial proxy created in AbstractJobRepositoryFactoryBean.initializeProxy Advisor[] advisors = target.getAdvisors(); for (Advisor advisor : advisors) { if (advisor.getAdvice() instanceof TransactionInterceptor) { TransactionInterceptor transactionInterceptor = (TransactionInterceptor) advisor.getAdvice(); return transactionInterceptor.getTransactionManager(); } } return null; }