MethodInvocations and ProceedingJoinPoints always expose original method (not bridge); ProceedingJoinPoint resolves parameter names using ASM-based parameter name discovery

This commit is contained in:
Juergen Hoeller 2009-02-18 23:45:14 +00:00
parent 02164ab6a7
commit 85bc98ea4b
4 changed files with 47 additions and 58 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,6 +27,7 @@ import org.aspectj.lang.reflect.SourceLocation;
import org.aspectj.runtime.internal.AroundClosure;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.util.Assert;
/**
@ -151,7 +152,9 @@ public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint,
/**
* Lazily initialized MethodSignature.
*/
private class MethodSignatureImpl implements Signature, MethodSignature {
private class MethodSignatureImpl implements MethodSignature {
private volatile String[] parameterNames;
public String getName() {
return methodInvocation.getMethod().getName();
@ -182,10 +185,10 @@ public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint,
}
public String[] getParameterNames() {
// TODO consider allowing use of ParameterNameDiscoverer, or tying into
// parameter names exposed for argument binding...
throw new UnsupportedOperationException(
"Parameter names cannot be determined unless compiled by AspectJ compiler");
if (this.parameterNames == null) {
this.parameterNames = (new LocalVariableTableParameterNameDiscoverer()).getParameterNames(getMethod());
}
return this.parameterNames;
}
public Class[] getExceptionTypes() {
@ -204,62 +207,55 @@ public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint,
return toString(false, true, false, true);
}
private String toString(boolean includeModifier,
boolean includeReturnTypeAndArgs,
boolean useLongReturnAndArgumentTypeName,
boolean useLongTypeName) {
private String toString(boolean includeModifier, boolean includeReturnTypeAndArgs,
boolean useLongReturnAndArgumentTypeName, boolean useLongTypeName) {
StringBuilder sb = new StringBuilder();
if (includeModifier) {
sb.append(Modifier.toString(getModifiers()));
sb.append(" ");
}
if (includeReturnTypeAndArgs) {
appendType(sb, getReturnType(),
useLongReturnAndArgumentTypeName);
appendType(sb, getReturnType(), useLongReturnAndArgumentTypeName);
sb.append(" ");
}
appendType(sb, getDeclaringType(), useLongTypeName);
sb.append(".");
sb.append(getMethod().getName());
sb.append("(");
Class[] parametersTypes = getParameterTypes();
appendTypes(sb, parametersTypes, includeReturnTypeAndArgs,
useLongReturnAndArgumentTypeName);
appendTypes(sb, parametersTypes, includeReturnTypeAndArgs, useLongReturnAndArgumentTypeName);
sb.append(")");
return sb.toString();
}
}
private void appendTypes(StringBuilder sb, Class<?>[] types,
boolean includeArgs, boolean useLongReturnAndArgumentTypeName) {
if (includeArgs) {
for (int size = types.length, i = 0; i < size; i++) {
appendType(sb, types[i], useLongReturnAndArgumentTypeName);
if (i < size - 1) {
sb.append(",");
private void appendTypes(StringBuilder sb, Class<?>[] types,
boolean includeArgs, boolean useLongReturnAndArgumentTypeName) {
if (includeArgs) {
for (int size = types.length, i = 0; i < size; i++) {
appendType(sb, types[i], useLongReturnAndArgumentTypeName);
if (i < size - 1) {
sb.append(",");
}
}
}
} else {
if (types.length != 0) {
sb.append("..");
else {
if (types.length != 0) {
sb.append("..");
}
}
}
private void appendType(StringBuilder sb, Class<?> type, boolean useLongTypeName) {
if (type.isArray()) {
appendType(sb, type.getComponentType(), useLongTypeName);
sb.append("[]");
}
else {
sb.append(useLongTypeName ? type.getName() : type.getSimpleName());
}
}
}
private void appendType(StringBuilder sb, Class<?> type,
boolean useLongTypeName) {
if (type.isArray()) {
appendType(sb, type.getComponentType(), useLongTypeName);
sb.append("[]");
} else {
if (type.getPackage() != null
&& type.getPackage().equals("java.lang")) {
useLongTypeName = false;
}
sb.append(useLongTypeName ? type.getName() : type.getSimpleName());
}
}
/**
* Lazily initialized SourceLocation.

View File

@ -177,7 +177,7 @@ final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializa
setProxyContext = true;
}
// May be <code>null</code>. Get as late as possible to minimize the time we "own" the target,
// May be null. Get as late as possible to minimize the time we "own" the target,
// in case it comes from a pool.
target = targetSource.getTarget();
if (target != null) {
@ -255,7 +255,7 @@ final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializa
return false;
}
// If we get here, aopr2 is the other AopProxy.
// If we get here, otherProxy is the other AopProxy.
return AopProxyUtils.equalsInProxy(this.advised, otherProxy.advised);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -27,6 +27,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.ProxyMethodInvocation;
import org.springframework.aop.support.AopUtils;
import org.springframework.core.BridgeMethodResolver;
/**
* Spring's implementation of the AOP Alliance
@ -107,7 +108,7 @@ public class ReflectiveMethodInvocation implements ProxyMethodInvocation, Clonea
this.proxy = proxy;
this.target = target;
this.targetClass = targetClass;
this.method = method;
this.method = BridgeMethodResolver.findBridgedMethod(method);
this.arguments = arguments;
this.interceptorsAndDynamicMethodMatchers = interceptorsAndDynamicMethodMatchers;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,8 +16,6 @@
package org.springframework.aop.aspectj;
import static org.junit.Assert.*;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
@ -28,7 +26,10 @@ import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.lang.reflect.SourceLocation;
import org.aspectj.runtime.reflect.Factory;
import static org.junit.Assert.*;
import org.junit.Test;
import test.beans.ITestBean;
import test.beans.TestBean;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.AopContext;
@ -36,9 +37,6 @@ import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
import org.springframework.aop.support.AopUtils;
import test.beans.ITestBean;
import test.beans.TestBean;
/**
* @author Rod Johnson
* @author Chris Beams
@ -83,7 +81,7 @@ public final class MethodInvocationProceedingJoinPointTests {
public void before(Method method, Object[] args, Object target) throws Throwable {
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
assertTrue("Method named in toString", jp.toString().indexOf(method.getName()) != -1);
assertTrue("Method named in toString", jp.toString().contains(method.getName()));
// Ensure that these don't cause problems
jp.toShortString();
jp.toLongString();
@ -108,7 +106,7 @@ public final class MethodInvocationProceedingJoinPointTests {
thisProxy.setAge(newAge);
assertEquals(newAge, thisProxy.getAge());
}
assertSame(AopContext.currentProxy(), thisProxy);
assertSame(target, raw);
@ -122,13 +120,6 @@ public final class MethodInvocationProceedingJoinPointTests {
assertTrue(Arrays.equals(method.getParameterTypes(), msig.getParameterTypes()));
assertEquals(method.getReturnType(), msig.getReturnType());
assertTrue(Arrays.equals(method.getExceptionTypes(), msig.getExceptionTypes()));
try {
msig.getParameterNames();
fail("Can't determine parameter names");
}
catch (UnsupportedOperationException ex) {
// Expected
}
msig.toLongString();
msig.toShortString();
}
@ -223,4 +214,5 @@ public final class MethodInvocationProceedingJoinPointTests {
// we don't realy care...
}
}
}