Skip to content

Commit c8d6c15

Browse files
committed
Fixed toString(), toShortString(), and toLongString() methods for signature and join point (SPR-5437)
1 parent 27460e3 commit c8d6c15

File tree

2 files changed

+109
-13
lines changed

2 files changed

+109
-13
lines changed

org.springframework.aop/src/main/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPoint.java

+72-13
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.aop.aspectj;
1818

1919
import java.lang.reflect.Method;
20+
import java.lang.reflect.Modifier;
2021

2122
import org.aspectj.lang.JoinPoint;
2223
import org.aspectj.lang.ProceedingJoinPoint;
@@ -44,6 +45,7 @@
4445
* @author Rod Johnson
4546
* @author Juergen Hoeller
4647
* @author Adrian Colyer
48+
* @author Ramnivas Laddad
4749
* @since 2.0
4850
*/
4951
public class MethodInvocationProceedingJoinPoint implements ProceedingJoinPoint, JoinPoint.StaticPart {
@@ -132,19 +134,17 @@ public String getKind() {
132134
public JoinPoint.StaticPart getStaticPart() {
133135
return this;
134136
}
135-
136137

137138
public String toShortString() {
138-
return "execution(" + this.methodInvocation.getMethod().getName() + ")";
139+
return "execution(" + getSignature().toShortString() + ")";
139140
}
140141

141142
public String toLongString() {
142-
return getClass().getName() + ": execution: [" + this.methodInvocation + "]";
143+
return "execution(" + getSignature().toLongString() + ")";
143144
}
144145

145-
@Override
146146
public String toString() {
147-
return getClass().getName() + ": " + toShortString();
147+
return "execution(" + getSignature().toString() + ")";
148148
}
149149

150150

@@ -153,14 +153,6 @@ public String toString() {
153153
*/
154154
private class MethodSignatureImpl implements Signature, MethodSignature {
155155

156-
public String toShortString() {
157-
return methodInvocation.getMethod().getName();
158-
}
159-
160-
public String toLongString() {
161-
return methodInvocation.getMethod().toString();
162-
}
163-
164156
public String getName() {
165157
return methodInvocation.getMethod().getName();
166158
}
@@ -199,8 +191,75 @@ public String[] getParameterNames() {
199191
public Class[] getExceptionTypes() {
200192
return methodInvocation.getMethod().getExceptionTypes();
201193
}
194+
195+
public String toShortString() {
196+
return toString(false, false, false, false);
197+
}
198+
199+
public String toLongString() {
200+
return toString(true, true, true, true);
201+
}
202+
203+
public String toString() {
204+
return toString(false, true, false, true);
205+
}
206+
207+
private String toString(boolean includeModifier,
208+
boolean includeReturnTypeAndArgs,
209+
boolean useLongReturnAndArgumentTypeName,
210+
boolean useLongTypeName) {
211+
StringBuilder sb = new StringBuilder();
212+
if (includeModifier) {
213+
sb.append(Modifier.toString(getModifiers()));
214+
sb.append(" ");
215+
}
216+
if (includeReturnTypeAndArgs) {
217+
appendType(sb, getReturnType(),
218+
useLongReturnAndArgumentTypeName);
219+
sb.append(" ");
220+
}
221+
appendType(sb, getDeclaringType(), useLongTypeName);
222+
sb.append(".");
223+
sb.append(getMethod().getName());
224+
sb.append("(");
225+
226+
Class[] parametersTypes = getParameterTypes();
227+
appendTypes(sb, parametersTypes, includeReturnTypeAndArgs,
228+
useLongReturnAndArgumentTypeName);
229+
sb.append(")");
230+
return sb.toString();
231+
}
232+
}
233+
234+
private void appendTypes(StringBuilder sb, Class<?>[] types,
235+
boolean includeArgs, boolean useLongReturnAndArgumentTypeName) {
236+
if (includeArgs) {
237+
for (int size = types.length, i = 0; i < size; i++) {
238+
appendType(sb, types[i], useLongReturnAndArgumentTypeName);
239+
if (i < size - 1) {
240+
sb.append(",");
241+
}
242+
}
243+
} else {
244+
if (types.length != 0) {
245+
sb.append("..");
246+
}
247+
}
202248
}
203249

250+
private void appendType(StringBuilder sb, Class<?> type,
251+
boolean useLongTypeName) {
252+
if (type.isArray()) {
253+
appendType(sb, type.getComponentType(), useLongTypeName);
254+
sb.append("[]");
255+
} else {
256+
if (type.getPackage() != null
257+
&& type.getPackage().equals("java.lang")) {
258+
useLongTypeName = false;
259+
}
260+
sb.append(useLongTypeName ? type.getName() : type.getSimpleName());
261+
}
262+
}
204263

205264
/**
206265
* Lazily initialized SourceLocation.

org.springframework.aop/src/test/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPointTests.java

+37
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.Assert.*;
2020

21+
import java.io.IOException;
2122
import java.lang.reflect.Method;
2223
import java.util.Arrays;
2324

@@ -26,6 +27,7 @@
2627
import org.aspectj.lang.ProceedingJoinPoint;
2728
import org.aspectj.lang.reflect.MethodSignature;
2829
import org.aspectj.lang.reflect.SourceLocation;
30+
import org.aspectj.runtime.reflect.Factory;
2931
import org.junit.Test;
3032

3133
import org.springframework.aop.MethodBeforeAdvice;
@@ -40,6 +42,7 @@
4042
/**
4143
* @author Rod Johnson
4244
* @author Chris Beams
45+
* @author Ramnivas Laddad
4346
* @since 2.0
4447
*/
4548
public final class MethodInvocationProceedingJoinPointTests {
@@ -186,4 +189,38 @@ public void before(Method method, Object[] args, Object target) throws Throwable
186189
itb.getAge();
187190
}
188191

192+
@Test
193+
public void toShortAndLongStringFormedCorrectly() throws Exception {
194+
final Object raw = new TestBean();
195+
ProxyFactory pf = new ProxyFactory(raw);
196+
pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
197+
pf.addAdvice(new MethodBeforeAdvice() {
198+
public void before(Method method, Object[] args, Object target) throws Throwable {
199+
// makeEncSJP, although meant for computing the enclosing join point,
200+
// it serves our purpose here
201+
JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method);
202+
JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint();
203+
204+
assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString());
205+
assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString());
206+
assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString());
207+
208+
assertEquals(aspectJVersionJp.toLongString(), jp.toLongString());
209+
assertEquals(aspectJVersionJp.toShortString(), jp.toShortString());
210+
assertEquals(aspectJVersionJp.toString(), jp.toString());
211+
}
212+
});
213+
ITestBean itb = (ITestBean) pf.getProxy();
214+
itb.getAge();
215+
itb.setName("foo");
216+
itb.getDoctor();
217+
itb.getStringArray();
218+
itb.getSpouses();
219+
itb.setSpouse(new TestBean());
220+
try {
221+
itb.unreliableFileOperation();
222+
} catch (IOException ex) {
223+
// we don't realy care...
224+
}
225+
}
189226
}

0 commit comments

Comments
 (0)