Skip to content

Commit deae872

Browse files
committed
Consistent use of varargs plus related polishing
1 parent 36940e0 commit deae872

35 files changed

+460
-431
lines changed

spring-aop/src/main/java/org/springframework/aop/MethodMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -86,7 +86,7 @@ public interface MethodMatcher {
8686
* @return whether there's a runtime match
8787
* @see MethodMatcher#matches(Method, Class)
8888
*/
89-
boolean matches(Method method, Class<?> targetClass, Object[] args);
89+
boolean matches(Method method, Class<?> targetClass, Object... args);
9090

9191

9292
/**

spring-aop/src/main/java/org/springframework/aop/TrueMethodMatcher.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -29,12 +29,14 @@ class TrueMethodMatcher implements MethodMatcher, Serializable {
2929

3030
public static final TrueMethodMatcher INSTANCE = new TrueMethodMatcher();
3131

32+
3233
/**
3334
* Enforce Singleton pattern.
3435
*/
3536
private TrueMethodMatcher() {
3637
}
3738

39+
3840
@Override
3941
public boolean isRuntime() {
4042
return false;
@@ -46,11 +48,17 @@ public boolean matches(Method method, Class<?> targetClass) {
4648
}
4749

4850
@Override
49-
public boolean matches(Method method, Class<?> targetClass, Object[] args) {
51+
public boolean matches(Method method, Class<?> targetClass, Object... args) {
5052
// Should never be invoked as isRuntime returns false.
5153
throw new UnsupportedOperationException();
5254
}
5355

56+
57+
@Override
58+
public String toString() {
59+
return "MethodMatcher.TRUE";
60+
}
61+
5462
/**
5563
* Required to support serialization. Replaces with canonical
5664
* instance on deserialization, protecting Singleton pattern.
@@ -60,9 +68,4 @@ private Object readResolve() {
6068
return INSTANCE;
6169
}
6270

63-
@Override
64-
public String toString() {
65-
return "MethodMatcher.TRUE";
66-
}
67-
6871
}

spring-aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static JoinPoint currentJoinPoint() {
135135
*/
136136
private int joinPointStaticPartArgumentIndex = -1;
137137

138-
private Map<String, Integer> argumentBindings = null;
138+
private Map<String, Integer> argumentBindings;
139139

140140
private boolean argumentsIntrospected = false;
141141

@@ -250,17 +250,17 @@ public void setArgumentNamesFromStringArray(String... args) {
250250
this.argumentNames[i] + "' that is not a valid Java identifier");
251251
}
252252
}
253-
if (argumentNames != null) {
254-
if (aspectJAdviceMethod.getParameterTypes().length == argumentNames.length + 1) {
253+
if (this.argumentNames != null) {
254+
if (this.aspectJAdviceMethod.getParameterTypes().length == this.argumentNames.length + 1) {
255255
// May need to add implicit join point arg name...
256-
Class<?> firstArgType = aspectJAdviceMethod.getParameterTypes()[0];
256+
Class<?> firstArgType = this.aspectJAdviceMethod.getParameterTypes()[0];
257257
if (firstArgType == JoinPoint.class ||
258258
firstArgType == ProceedingJoinPoint.class ||
259259
firstArgType == JoinPoint.StaticPart.class) {
260-
String[] oldNames = argumentNames;
261-
argumentNames = new String[oldNames.length + 1];
262-
argumentNames[0] = "THIS_JOIN_POINT";
263-
System.arraycopy(oldNames, 0, argumentNames, 1, oldNames.length);
260+
String[] oldNames = this.argumentNames;
261+
this.argumentNames = new String[oldNames.length + 1];
262+
this.argumentNames[0] = "THIS_JOIN_POINT";
263+
System.arraycopy(oldNames, 0, this.argumentNames, 1, oldNames.length);
264264
}
265265
}
266266
}
@@ -456,8 +456,8 @@ private void bindExplicitArguments(int numArgumentsLeftToBind) {
456456

457457
int numExpectedArgumentNames = this.aspectJAdviceMethod.getParameterTypes().length;
458458
if (this.argumentNames.length != numExpectedArgumentNames) {
459-
throw new IllegalStateException("Expecting to find " + numExpectedArgumentNames
460-
+ " arguments to bind by name in advice, but actually found " +
459+
throw new IllegalStateException("Expecting to find " + numExpectedArgumentNames +
460+
" arguments to bind by name in advice, but actually found " +
461461
this.argumentNames.length + " arguments.");
462462
}
463463

@@ -471,8 +471,8 @@ private void bindExplicitArguments(int numArgumentsLeftToBind) {
471471
// specified, and find the discovered argument types.
472472
if (this.returningName != null) {
473473
if (!this.argumentBindings.containsKey(this.returningName)) {
474-
throw new IllegalStateException("Returning argument name '"
475-
+ this.returningName + "' was not bound in advice arguments");
474+
throw new IllegalStateException("Returning argument name '" + this.returningName +
475+
"' was not bound in advice arguments");
476476
}
477477
else {
478478
Integer index = this.argumentBindings.get(this.returningName);
@@ -482,8 +482,8 @@ private void bindExplicitArguments(int numArgumentsLeftToBind) {
482482
}
483483
if (this.throwingName != null) {
484484
if (!this.argumentBindings.containsKey(this.throwingName)) {
485-
throw new IllegalStateException("Throwing argument name '"
486-
+ this.throwingName + "' was not bound in advice arguments");
485+
throw new IllegalStateException("Throwing argument name '" + this.throwingName +
486+
"' was not bound in advice arguments");
487487
}
488488
else {
489489
Integer index = this.argumentBindings.get(this.throwingName);
@@ -581,10 +581,9 @@ else if (this.joinPointStaticPartArgumentIndex != -1) {
581581
}
582582

583583
if (numBound != this.adviceInvocationArgumentCount) {
584-
throw new IllegalStateException("Required to bind " + this.adviceInvocationArgumentCount
585-
+ " arguments, but only bound " + numBound + " (JoinPointMatch " +
586-
(jpMatch == null ? "was NOT" : "WAS") +
587-
" bound in invocation)");
584+
throw new IllegalStateException("Required to bind " + this.adviceInvocationArgumentCount +
585+
" arguments, but only bound " + numBound + " (JoinPointMatch " +
586+
(jpMatch == null ? "was NOT" : "WAS") + " bound in invocation)");
588587
}
589588

590589
return adviceInvocationArgs;

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterAdvice.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ public AspectJAfterAdvice(
3737
super(aspectJBeforeAdviceMethod, pointcut, aif);
3838
}
3939

40+
4041
@Override
4142
public Object invoke(MethodInvocation mi) throws Throwable {
4243
try {

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterReturningAdvice.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public AspectJAfterReturningAdvice(
4040
super(aspectJBeforeAdviceMethod, pointcut, aif);
4141
}
4242

43+
4344
@Override
4445
public boolean isBeforeAdvice() {
4546
return false;
@@ -62,6 +63,7 @@ public void afterReturning(Object returnValue, Method method, Object[] args, Obj
6263
}
6364
}
6465

66+
6567
/**
6668
* Following AspectJ semantics, if a returning clause was specified, then the
6769
* advice is only invoked if the returned value is an instance of the given

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAfterThrowingAdvice.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,6 +37,7 @@ public AspectJAfterThrowingAdvice(
3737
super(aspectJBeforeAdviceMethod, pointcut, aif);
3838
}
3939

40+
4041
@Override
4142
public boolean isBeforeAdvice() {
4243
return false;
@@ -57,20 +58,20 @@ public Object invoke(MethodInvocation mi) throws Throwable {
5758
try {
5859
return mi.proceed();
5960
}
60-
catch (Throwable t) {
61-
if (shouldInvokeOnThrowing(t)) {
62-
invokeAdviceMethod(getJoinPointMatch(), null, t);
61+
catch (Throwable ex) {
62+
if (shouldInvokeOnThrowing(ex)) {
63+
invokeAdviceMethod(getJoinPointMatch(), null, ex);
6364
}
64-
throw t;
65+
throw ex;
6566
}
6667
}
6768

6869
/**
6970
* In AspectJ semantics, after throwing advice that specifies a throwing clause
7071
* is only invoked if the thrown exception is a subtype of the given throwing type.
7172
*/
72-
private boolean shouldInvokeOnThrowing(Throwable t) {
73-
return getDiscoveredThrowingType().isAssignableFrom(t.getClass());
73+
private boolean shouldInvokeOnThrowing(Throwable ex) {
74+
return getDiscoveredThrowingType().isAssignableFrom(ex.getClass());
7475
}
7576

7677
}

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAroundAdvice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,6 +41,7 @@ public AspectJAroundAdvice(
4141
super(aspectJAroundAdviceMethod, pointcut, aif);
4242
}
4343

44+
4445
@Override
4546
public boolean isBeforeAdvice() {
4647
return false;
@@ -56,7 +57,6 @@ protected boolean supportsProceedingJoinPoint() {
5657
return true;
5758
}
5859

59-
6060
@Override
6161
public Object invoke(MethodInvocation mi) throws Throwable {
6262
if (!(mi instanceof ProxyMethodInvocation)) {

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public boolean isRuntime() {
305305
}
306306

307307
@Override
308-
public boolean matches(Method method, Class<?> targetClass, Object[] args) {
308+
public boolean matches(Method method, Class<?> targetClass, Object... args) {
309309
checkReadyToMatch();
310310
ShadowMatch shadowMatch = getShadowMatch(AopUtils.getMostSpecificMethod(method, targetClass), method);
311311
ShadowMatch originalShadowMatch = getShadowMatch(method, method);

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJMethodBeforeAdvice.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ public AspectJMethodBeforeAdvice(
3535
super(aspectJBeforeAdviceMethod, pointcut, aif);
3636
}
3737

38+
3839
@Override
3940
public void before(Method method, Object[] args, Object target) throws Throwable {
4041
invokeAdviceMethod(getJoinPointMatch(), null, null);

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AbstractAspectJAdvisorFactory.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,34 +61,6 @@ public abstract class AbstractAspectJAdvisorFactory implements AspectJAdvisorFac
6161
private static final String AJC_MAGIC = "ajc$";
6262

6363

64-
/**
65-
* Find and return the first AspectJ annotation on the given method
66-
* (there <i>should</i> only be one anyway...)
67-
*/
68-
@SuppressWarnings("unchecked")
69-
protected static AspectJAnnotation<?> findAspectJAnnotationOnMethod(Method method) {
70-
Class<?>[] classesToLookFor = new Class<?>[] {
71-
Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class, Pointcut.class};
72-
for (Class<?> c : classesToLookFor) {
73-
AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) c);
74-
if (foundAnnotation != null) {
75-
return foundAnnotation;
76-
}
77-
}
78-
return null;
79-
}
80-
81-
private static <A extends Annotation> AspectJAnnotation<A> findAnnotation(Method method, Class<A> toLookFor) {
82-
A result = AnnotationUtils.findAnnotation(method, toLookFor);
83-
if (result != null) {
84-
return new AspectJAnnotation<A>(result);
85-
}
86-
else {
87-
return null;
88-
}
89-
}
90-
91-
9264
/** Logger available to subclasses */
9365
protected final Log logger = LogFactory.getLog(getClass());
9466

@@ -181,6 +153,7 @@ private Class<?>[] extractPointcutParameterTypes(String[] argNames, Method advic
181153
throw new IllegalStateException("Expecting at least " + argNames.length +
182154
" arguments in the advice declaration, but only found " + paramTypes.length);
183155
}
156+
184157
// Make the simplifying assumption for now that all of the JoinPoint based arguments
185158
// come first in the advice declaration.
186159
int typeOffset = paramTypes.length - argNames.length;
@@ -191,7 +164,36 @@ private Class<?>[] extractPointcutParameterTypes(String[] argNames, Method advic
191164
}
192165

193166

167+
/**
168+
* Find and return the first AspectJ annotation on the given method
169+
* (there <i>should</i> only be one anyway...)
170+
*/
171+
@SuppressWarnings("unchecked")
172+
protected static AspectJAnnotation<?> findAspectJAnnotationOnMethod(Method method) {
173+
Class<?>[] classesToLookFor = new Class<?>[] {
174+
Before.class, Around.class, After.class, AfterReturning.class, AfterThrowing.class, Pointcut.class};
175+
for (Class<?> c : classesToLookFor) {
176+
AspectJAnnotation<?> foundAnnotation = findAnnotation(method, (Class<Annotation>) c);
177+
if (foundAnnotation != null) {
178+
return foundAnnotation;
179+
}
180+
}
181+
return null;
182+
}
183+
184+
private static <A extends Annotation> AspectJAnnotation<A> findAnnotation(Method method, Class<A> toLookFor) {
185+
A result = AnnotationUtils.findAnnotation(method, toLookFor);
186+
if (result != null) {
187+
return new AspectJAnnotation<A>(result);
188+
}
189+
else {
190+
return null;
191+
}
192+
}
193+
194+
194195
protected enum AspectJAnnotationType {
196+
195197
AtPointcut,
196198
AtBefore,
197199
AtAfter,

0 commit comments

Comments
 (0)