/** * Return a description for the given method invocation. * @param invocation the invocation to describe * @return the description */ protected String getInvocationDescription(MethodInvocation invocation) { return "method '" + invocation.getMethod().getName() + "' of class [" + invocation.getThis().getClass().getName() + "]"; }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Object retVal = invocation.proceed(); Assert.state(this.applicationEventClassConstructor != null, "No ApplicationEvent class set"); ApplicationEvent event = (ApplicationEvent) this.applicationEventClassConstructor.newInstance(invocation.getThis()); Assert.state(this.applicationEventPublisher != null, "No ApplicationEventPublisher available"); this.applicationEventPublisher.publishEvent(event); return retVal; }
@Override public Object invoke(MethodInvocation mi) throws Throwable { this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis()); return mi.proceed(); }
@Override public Object invoke(MethodInvocation mi) throws Throwable { Object retVal = mi.proceed(); this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis()); return retVal; }
@Override @Nullable public Object invoke(MethodInvocation invocation) throws Throwable { // Work out the target class: may be {@code null}. // The TransactionAttributeSource should be passed the target class // as well as the method, which may be from an interface. Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null); // Adapt to TransactionAspectSupport's invokeWithinTransaction... return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed); }
@Override @Nullable public Object invoke(org.aopalliance.intercept.MethodInvocation inv) throws Throwable { return intercept(inv.getThis(), inv.getMethod(), inv.getArguments(), null); }
/** * Determine the validation groups to validate against for the given method invocation. * <p>Default are the validation groups as specified in the {@link Validated} annotation * on the containing target class of the method. * @param invocation the current MethodInvocation * @return the applicable validation groups as a Class array */ protected Class<?>[] determineValidationGroups(MethodInvocation invocation) { Validated validatedAnn = AnnotationUtils.findAnnotation(invocation.getMethod(), Validated.class); if (validatedAnn == null) { validatedAnn = AnnotationUtils.findAnnotation(invocation.getThis().getClass(), Validated.class); } return (validatedAnn != null ? validatedAnn.value() : new Class<?>[0]); }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { this.target = invocation.getThis(); return invocation.proceed(); } }
@Override @Nullable public Object invoke(org.aopalliance.intercept.MethodInvocation inv) throws Throwable { return intercept(inv.getThis(), inv.getMethod(), inv.getArguments(), null); } }
@Override @Nullable public Object invoke(org.aopalliance.intercept.MethodInvocation inv) throws Throwable { return intercept(inv.getThis(), inv.getMethod(), inv.getArguments(), null); } }
@Override protected void assertions(MethodInvocation invocation) { assertTrue(invocation.getThis() == this); assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(), ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())); } }
@Override protected void assertions(MethodInvocation invocation) { assertSame(this, invocation.getThis()); assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(), ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass())); } }
@Override public Object invoke(MethodInvocation mi) throws Throwable { MethodInvocation clone = ((ReflectiveMethodInvocation) mi).invocableClone(); String oldName = ((ITestBean) mi.getThis()).getName(); clone.getArguments()[0] = oldName; // Original method invocation should be unaffected by changes to argument list of clone mi.proceed(); return clone.proceed(); } };
@Override protected void assertions(MethodInvocation invocation) { assertEquals(this, invocation.getThis()); assertEquals("Invocation should be on ITestBean: " + invocation.getMethod(), ITestBean.class, invocation.getMethod().getDeclaringClass()); } };
@Override public Object invoke(MethodInvocation invocation) throws Throwable { // Assert that target matches BEFORE invocation returns assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } };
@Override public Object invoke(MethodInvocation invocation) throws Throwable { // Assert that target matches BEFORE invocation returns assertEquals("Target is correct", expectedTarget, invocation.getThis()); return super.invoke(invocation); } };
@Test public void testInterceptorWithoutJoinpoint() { final TestBean target = new TestBean("tb"); ITestBean proxy = ProxyFactory.getProxy(ITestBean.class, (MethodInterceptor) invocation -> { assertNull(invocation.getThis()); return invocation.getMethod().invoke(target, invocation.getArguments()); }); assertEquals("tb", proxy.getName()); }
@Test public void testSunnyDayPathLogsCorrectly() throws Throwable { MethodInvocation methodInvocation = mock(MethodInvocation.class); given(methodInvocation.getMethod()).willReturn(String.class.getMethod("toString")); given(methodInvocation.getThis()).willReturn(this); Log log = mock(Log.class); given(log.isTraceEnabled()).willReturn(true); CustomizableTraceInterceptor interceptor = new StubCustomizableTraceInterceptor(log); interceptor.invoke(methodInvocation); verify(log, times(2)).trace(anyString()); }
@Test public void testSunnyDayPathLogsCorrectly() throws Throwable { MethodInvocation mi = mock(MethodInvocation.class); given(mi.getMethod()).willReturn(String.class.getMethod("toString")); given(mi.getThis()).willReturn(this); Log log = mock(Log.class); SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true); interceptor.invokeUnderTrace(mi, log); verify(log, times(2)).trace(anyString()); }
@Test public void testEventPublicationInterceptor() throws Throwable { MethodInvocation invocation = mock(MethodInvocation.class); ApplicationContext ctx = mock(ApplicationContext.class); EventPublicationInterceptor interceptor = new EventPublicationInterceptor(); interceptor.setApplicationEventClass(MyEvent.class); interceptor.setApplicationEventPublisher(ctx); interceptor.afterPropertiesSet(); given(invocation.proceed()).willReturn(new Object()); given(invocation.getThis()).willReturn(new Object()); interceptor.invoke(invocation); verify(ctx).publishEvent(isA(MyEvent.class)); }