/** * Create a new DefaultAdvisorAdapterRegistry, registering well-known adapters. */ public DefaultAdvisorAdapterRegistry() { registerAdvisorAdapter(new MethodBeforeAdviceAdapter()); registerAdvisorAdapter(new AfterReturningAdviceAdapter()); registerAdvisorAdapter(new ThrowsAdviceAdapter()); }
/** * Reset the singleton {@link DefaultAdvisorAdapterRegistry}, removing any * {@link AdvisorAdapterRegistry#registerAdvisorAdapter(AdvisorAdapter) registered} * adapters. */ static void reset() { instance = new DefaultAdvisorAdapterRegistry(); }
@Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof AdvisorAdapter){ this.advisorAdapterRegistry.registerAdvisorAdapter((AdvisorAdapter) bean); } return bean; }
@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]); }
/** * Convert the following object sourced from calling getBean() on a name in the * interceptorNames array to an Advisor or TargetSource. */ private Advisor namedBeanToAdvisor(Object next) { try { return this.advisorAdapterRegistry.wrap(next); } catch (UnknownAdviceTypeException ex) { // We expected this to be an Advisor or Advice, // but it wasn't. This is a configuration error. throw new AopConfigException("Unknown advisor type " + next.getClass() + "; Can only include Advisor or Advice type beans in interceptorNames chain except for last entry," + "which may also be target or TargetSource", ex); } }
@Override public Object invoke(MethodInvocation mi) throws Throwable { try { return mi.proceed(); } catch (Throwable ex) { Method handlerMethod = getExceptionHandler(ex); if (handlerMethod != null) { invokeHandlerMethod(mi, ex, handlerMethod); } throw ex; } }
@Test public void testNotInvoked() throws Throwable { MyThrowsHandler th = new MyThrowsHandler(); ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th); Object ret = new Object(); MethodInvocation mi = mock(MethodInvocation.class); given(mi.proceed()).willReturn(ret); assertEquals(ret, ti.invoke(mi)); assertEquals(0, th.getCalls()); }
@Override public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException { if (adviceObject instanceof Advisor) { return (Advisor) adviceObject; } if (!(adviceObject instanceof Advice)) { throw new UnknownAdviceTypeException(adviceObject); } Advice advice = (Advice) adviceObject; if (advice instanceof MethodInterceptor) { // So well-known it doesn't even need an adapter. return new DefaultPointcutAdvisor(advice); } for (AdvisorAdapter adapter : this.adapters) { // Check that it is supported. if (adapter.supportsAdvice(advice)) { return new DefaultPointcutAdvisor(advice); } } throw new UnknownAdviceTypeException(advice); }
@Override public MethodInterceptor getInterceptor(Advisor advisor) { return new ThrowsAdviceInterceptor(advisor.getAdvice()); }
@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); }
@Test public void testAdvisorAdapterRegistrationManagerPresentInContext() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-with-bpp.xml", getClass()); ITestBean tb = (ITestBean) ctx.getBean("testBean"); // just invoke any method to see if advice fired try { tb.getName(); assertEquals(1, getAdviceImpl(tb).getInvocationCounter()); } catch (UnknownAdviceTypeException ex) { fail("Should not throw UnknownAdviceTypeException"); } }
public void afterThrowing(RemoteException ex) throws Throwable { count("remoteException"); }
@Override public void afterThrowing(RemoteException ex) throws Throwable { super.afterThrowing(ex); throw t; } };
@Override public Object invoke(MethodInvocation mi) throws Throwable { advice.before(); return mi.proceed(); } }
@Override public MethodInterceptor getInterceptor(Advisor advisor) { SimpleBeforeAdvice advice = (SimpleBeforeAdvice) advisor.getAdvice(); return new SimpleBeforeAdviceInterceptor(advice) ; }
@Before @After public void resetGlobalAdvisorAdapterRegistry() { GlobalAdvisorAdapterRegistry.reset(); }
@Test(expected = IllegalArgumentException.class) public void testNoHandlerMethods() { // should require one handler method at least new ThrowsAdviceInterceptor(new Object()); }
@Test public void testAdvisorAdapterRegistrationManagerNotPresentInContext() { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-without-bpp.xml", getClass()); ITestBean tb = (ITestBean) ctx.getBean("testBean"); // just invoke any method to see if advice fired try { tb.getName(); fail("Should throw UnknownAdviceTypeException"); } catch (UnknownAdviceTypeException ex) { // expected assertEquals(0, getAdviceImpl(tb).getInvocationCounter()); } }
public void afterThrowing(Method m, Object[] args, Object target, IOException ex) { count("ioException"); }