@Override public Object invoke(MethodInvocation mi) throws Throwable { this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis()); return mi.proceed(); }
/** * Create a new RemoteInvocation for the given AOP method invocation. * @param methodInvocation the AOP invocation to convert */ public RemoteInvocation(MethodInvocation methodInvocation) { this.methodName = methodInvocation.getMethod().getName(); this.parameterTypes = methodInvocation.getMethod().getParameterTypes(); this.arguments = methodInvocation.getArguments(); }
/** * 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() + "]"; }
/** * Perform a JAX-WS service invocation on the given port stub. * @param invocation the AOP method invocation * @param portStub the RMI port stub to invoke * @return the invocation result, if any * @throws Throwable in case of invocation failure * @see #getPortStub() */ @Nullable protected Object doInvoke(MethodInvocation invocation, @Nullable Object portStub) throws Throwable { Method method = invocation.getMethod(); try { return method.invoke(portStub, invocation.getArguments()); } catch (InvocationTargetException ex) { throw ex.getTargetException(); } catch (Throwable ex) { throw new RemoteProxyFailureException("Invocation of stub method failed: " + method, ex); } }
private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable { Object[] handlerArgs; if (method.getParameterCount() == 1) { handlerArgs = new Object[] {ex}; } else { handlerArgs = new Object[] {mi.getMethod(), mi.getArguments(), mi.getThis(), ex}; } try { method.invoke(this.throwsAdvice, handlerArgs); } catch (InvocationTargetException targetEx) { throw targetEx.getTargetException(); } }
@Test public void testExceptionPathStillLogsCorrectly() throws Throwable { MethodInvocation mi = mock(MethodInvocation.class); given(mi.getMethod()).willReturn(String.class.getMethod("toString")); given(mi.getThis()).willReturn(this); IllegalArgumentException exception = new IllegalArgumentException(); given(mi.proceed()).willThrow(exception); Log log = mock(Log.class); final SimpleTraceInterceptor interceptor = new SimpleTraceInterceptor(true); try { interceptor.invokeUnderTrace(mi, log); fail("Must have propagated the IllegalArgumentException."); } catch (IllegalArgumentException expected) { } verify(log).trace(anyString()); verify(log).trace(anyString(), eq(exception)); }
/** * Proceed with the supplied {@link org.aopalliance.intercept.MethodInterceptor}. * Subclasses can override this method to intercept method invocations on the * target object which is useful when an introduction needs to monitor the object * that it is introduced into. This method is <strong>never</strong> called for * {@link MethodInvocation MethodInvocations} on the introduced interfaces. */ protected Object doProceed(MethodInvocation mi) throws Throwable { // If we get here, just pass the invocation on. return mi.proceed(); }
private void addArgumentsAsVariables() { Object[] args = mi.getArguments(); if (args.length == 0) { return; } Object targetObject = mi.getThis(); // SEC-1454 Class<?> targetClass = AopProxyUtils.ultimateTargetClass(targetObject); if (targetClass == null) { // TODO: Spring should do this, but there's a bug in ultimateTargetClass() // which returns null targetClass = targetObject.getClass(); } Method method = AopUtils.getMostSpecificMethod(mi.getMethod(), targetClass); String[] paramNames = parameterNameDiscoverer.getParameterNames(method); if (paramNames == null) { logger.warn("Unable to resolve method parameter names for method: " + method + ". Debug symbol information is required if you are using parameter names in expressions."); return; } for (int i = 0; i < args.length; i++) { if (paramNames[i] != null) { setVariable(paramNames[i], args[i]); } } }
/** * Apply the given AOP method invocation to the given {@link RmiInvocationHandler}. * <p>The default implementation delegates to {@link #createRemoteInvocation}. * @param methodInvocation the current AOP method invocation * @param invocationHandler the RmiInvocationHandler to apply the invocation to * @return the invocation result * @throws RemoteException in case of communication errors * @throws NoSuchMethodException if the method name could not be resolved * @throws IllegalAccessException if the method could not be accessed * @throws InvocationTargetException if the method invocation resulted in an exception * @see org.springframework.remoting.support.RemoteInvocation */ @Nullable protected Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler) throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (AopUtils.isToStringMethod(methodInvocation.getMethod())) { return "RMI invoker proxy for service URL [" + getServiceUrl() + "]"; } return invocationHandler.invoke(createRemoteInvocation(methodInvocation)); }
@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); }
/** * Adds the {@code String} representation of the method return value * to the supplied {@code StringBuffer}. Correctly handles * {@code null} and {@code void} results. * @param methodInvocation the {@code MethodInvocation} that returned the value * @param matcher the {@code Matcher} containing the matched placeholder * @param output the {@code StringBuffer} to write output to * @param returnValue the value returned by the method invocation. */ private void appendReturnValue( MethodInvocation methodInvocation, Matcher matcher, StringBuffer output, @Nullable Object returnValue) { if (methodInvocation.getMethod().getReturnType() == void.class) { matcher.appendReplacement(output, "void"); } else if (returnValue == null) { matcher.appendReplacement(output, "null"); } else { matcher.appendReplacement(output, Matcher.quoteReplacement(returnValue.toString())); } }
@Override public Object invoke(MethodInvocation invocation) throws Throwable { Method method = invocation.getMethod(); if (method == null) { return invocation.proceed(); } Method mostSpecificMethod = AopUtils.getMostSpecificMethod(method, invocation.getThis().getClass()); NewSpan newSpan = SleuthAnnotationUtils.findAnnotation(mostSpecificMethod, NewSpan.class); ContinueSpan continueSpan = SleuthAnnotationUtils .findAnnotation(mostSpecificMethod, ContinueSpan.class); if (newSpan == null && continueSpan == null) { return invocation.proceed(); } return methodInvocationProcessor().process(invocation, newSpan, continueSpan); }
@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 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 { // We know it can only be invoked if there's a single parameter of type string mi.getArguments()[0] = ""; return mi.proceed(); } };
@Override @Nullable public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { MethodInvocation invocation = new CglibMethodInvocation(proxy, this.target, method, args, this.targetClass, this.adviceChain, methodProxy); // If we get here, we need to create a MethodInvocation. Object retVal = invocation.proceed(); retVal = processReturnType(proxy, this.target, method, retVal); return retVal; } }
private Exception adaptExceptionIfNecessary(MethodInvocation methodInvocation, ResourceException ex) { if (ReflectionUtils.declaresException(methodInvocation.getMethod(), ex.getClass())) { return ex; } else { return new InternalResourceException(ex); } }
private static String getClassPart(MethodInvocation invocation) { // si guice et pas Spring, alors remplacer AopUtils.getTargetClass() par getMethod().getDeclaringClass() // http://ninomartinez.wordpress.com/2010/05/14/guice-caching-interceptors/ // (faire exemple avec un interceptor static) final Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis()); final MonitoredWithSpring classAnnotation = targetClass .getAnnotation(MonitoredWithSpring.class); if (classAnnotation == null || classAnnotation.name() == null || classAnnotation.name().isEmpty()) { final Class<?> declaringClass = invocation.getMethod().getDeclaringClass(); final MonitoredWithSpring declaringClassAnnotation = declaringClass .getAnnotation(MonitoredWithSpring.class); if (declaringClassAnnotation == null || declaringClassAnnotation.name() == null || declaringClassAnnotation.name().isEmpty()) { return targetClass.getSimpleName(); } return declaringClassAnnotation.name(); } return classAnnotation.name(); }
/** * Apply the given AOP method invocation to the given {@link RmiInvocationHandler}. * <p>The default implementation delegates to {@link #createRemoteInvocation}. * @param methodInvocation the current AOP method invocation * @param invocationHandler the RmiInvocationHandler to apply the invocation to * @return the invocation result * @throws RemoteException in case of communication errors * @throws NoSuchMethodException if the method name could not be resolved * @throws IllegalAccessException if the method could not be accessed * @throws InvocationTargetException if the method invocation resulted in an exception * @see org.springframework.remoting.support.RemoteInvocation */ protected Object doInvoke(MethodInvocation methodInvocation, RmiInvocationHandler invocationHandler) throws RemoteException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (AopUtils.isToStringMethod(methodInvocation.getMethod())) { return "RMI invoker proxy for service URL [" + getJndiName() + "]"; } return invocationHandler.invoke(createRemoteInvocation(methodInvocation)); }