16
16
17
17
package org .springframework .aop .aspectj ;
18
18
19
+ import java .io .IOException ;
20
+ import java .io .ObjectInputStream ;
21
+ import java .io .Serializable ;
19
22
import java .lang .reflect .InvocationTargetException ;
20
23
import java .lang .reflect .Method ;
21
24
import java .lang .reflect .Type ;
55
58
* @author Ramnivas Laddad
56
59
* @since 2.0
57
60
*/
58
- public abstract class AbstractAspectJAdvice implements Advice , AspectJPrecedenceInformation {
61
+ @ SuppressWarnings ("serial" )
62
+ public abstract class AbstractAspectJAdvice implements Advice , AspectJPrecedenceInformation , Serializable {
59
63
60
64
/**
61
65
* Key used in ReflectiveMethodInvocation userAtributes map for the current joinpoint.
@@ -86,10 +90,13 @@ public static JoinPoint currentJoinPoint() {
86
90
}
87
91
88
92
89
- protected final Method aspectJAdviceMethod ;
93
+ private final Class <?> declaringClass ;
90
94
91
- /** The total number of arguments we have to populate on advice dispatch */
92
- private final int adviceInvocationArgumentCount ;
95
+ private final String methodName ;
96
+
97
+ private final Class <?>[] parameterTypes ;
98
+
99
+ protected transient Method aspectJAdviceMethod ;
93
100
94
101
private final AspectJExpressionPointcut pointcut ;
95
102
@@ -135,7 +142,7 @@ public static JoinPoint currentJoinPoint() {
135
142
*/
136
143
private int joinPointStaticPartArgumentIndex = -1 ;
137
144
138
- private Map <String , Integer > argumentBindings = null ;
145
+ private Map <String , Integer > argumentBindings ;
139
146
140
147
private boolean argumentsIntrospected = false ;
141
148
@@ -154,8 +161,10 @@ public AbstractAspectJAdvice(
154
161
Method aspectJAdviceMethod , AspectJExpressionPointcut pointcut , AspectInstanceFactory aspectInstanceFactory ) {
155
162
156
163
Assert .notNull (aspectJAdviceMethod , "Advice method must not be null" );
164
+ this .declaringClass = aspectJAdviceMethod .getDeclaringClass ();
165
+ this .methodName = aspectJAdviceMethod .getName ();
166
+ this .parameterTypes = aspectJAdviceMethod .getParameterTypes ();
157
167
this .aspectJAdviceMethod = aspectJAdviceMethod ;
158
- this .adviceInvocationArgumentCount = this .aspectJAdviceMethod .getParameterTypes ().length ;
159
168
this .pointcut = pointcut ;
160
169
this .aspectInstanceFactory = aspectInstanceFactory ;
161
170
}
@@ -250,17 +259,17 @@ public void setArgumentNamesFromStringArray(String... args) {
250
259
this .argumentNames [i ] + "' that is not a valid Java identifier" );
251
260
}
252
261
}
253
- if (argumentNames != null ) {
254
- if (aspectJAdviceMethod .getParameterTypes ().length == argumentNames .length + 1 ) {
262
+ if (this . argumentNames != null ) {
263
+ if (this . aspectJAdviceMethod .getParameterTypes ().length == this . argumentNames .length + 1 ) {
255
264
// May need to add implicit join point arg name...
256
- Class <?> firstArgType = aspectJAdviceMethod .getParameterTypes ()[0 ];
265
+ Class <?> firstArgType = this . aspectJAdviceMethod .getParameterTypes ()[0 ];
257
266
if (firstArgType == JoinPoint .class ||
258
267
firstArgType == ProceedingJoinPoint .class ||
259
268
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 );
269
+ String [] oldNames = this . argumentNames ;
270
+ this . argumentNames = new String [oldNames .length + 1 ];
271
+ this . argumentNames [0 ] = "THIS_JOIN_POINT" ;
272
+ System .arraycopy (oldNames , 0 , this . argumentNames , 1 , oldNames .length );
264
273
}
265
274
}
266
275
}
@@ -359,11 +368,11 @@ private boolean isVariableName(String name) {
359
368
*/
360
369
public synchronized final void calculateArgumentBindings () {
361
370
// The simple case... nothing to bind.
362
- if (this .argumentsIntrospected || this .adviceInvocationArgumentCount == 0 ) {
371
+ if (this .argumentsIntrospected || this .parameterTypes . length == 0 ) {
363
372
return ;
364
373
}
365
374
366
- int numUnboundArgs = this .adviceInvocationArgumentCount ;
375
+ int numUnboundArgs = this .parameterTypes . length ;
367
376
Class <?>[] parameterTypes = this .aspectJAdviceMethod .getParameterTypes ();
368
377
if (maybeBindJoinPoint (parameterTypes [0 ]) || maybeBindProceedingJoinPoint (parameterTypes [0 ])) {
369
378
numUnboundArgs --;
@@ -456,13 +465,13 @@ private void bindExplicitArguments(int numArgumentsLeftToBind) {
456
465
457
466
int numExpectedArgumentNames = this .aspectJAdviceMethod .getParameterTypes ().length ;
458
467
if (this .argumentNames .length != numExpectedArgumentNames ) {
459
- throw new IllegalStateException ("Expecting to find " + numExpectedArgumentNames
460
- + " arguments to bind by name in advice, but actually found " +
468
+ throw new IllegalStateException ("Expecting to find " + numExpectedArgumentNames +
469
+ " arguments to bind by name in advice, but actually found " +
461
470
this .argumentNames .length + " arguments." );
462
471
}
463
472
464
473
// So we match in number...
465
- int argumentIndexOffset = this .adviceInvocationArgumentCount - numArgumentsLeftToBind ;
474
+ int argumentIndexOffset = this .parameterTypes . length - numArgumentsLeftToBind ;
466
475
for (int i = argumentIndexOffset ; i < this .argumentNames .length ; i ++) {
467
476
this .argumentBindings .put (this .argumentNames [i ], i );
468
477
}
@@ -471,8 +480,8 @@ private void bindExplicitArguments(int numArgumentsLeftToBind) {
471
480
// specified, and find the discovered argument types.
472
481
if (this .returningName != null ) {
473
482
if (!this .argumentBindings .containsKey (this .returningName )) {
474
- throw new IllegalStateException ("Returning argument name '"
475
- + this . returningName + "' was not bound in advice arguments" );
483
+ throw new IllegalStateException ("Returning argument name '" + this . returningName +
484
+ "' was not bound in advice arguments" );
476
485
}
477
486
else {
478
487
Integer index = this .argumentBindings .get (this .returningName );
@@ -482,8 +491,8 @@ private void bindExplicitArguments(int numArgumentsLeftToBind) {
482
491
}
483
492
if (this .throwingName != null ) {
484
493
if (!this .argumentBindings .containsKey (this .throwingName )) {
485
- throw new IllegalStateException ("Throwing argument name '"
486
- + this . throwingName + "' was not bound in advice arguments" );
494
+ throw new IllegalStateException ("Throwing argument name '" + this . throwingName +
495
+ "' was not bound in advice arguments" );
487
496
}
488
497
else {
489
498
Integer index = this .argumentBindings .get (this .throwingName );
@@ -543,7 +552,7 @@ protected Object[] argBinding(JoinPoint jp, JoinPointMatch jpMatch, Object retur
543
552
calculateArgumentBindings ();
544
553
545
554
// AMC start
546
- Object [] adviceInvocationArgs = new Object [this .adviceInvocationArgumentCount ];
555
+ Object [] adviceInvocationArgs = new Object [this .parameterTypes . length ];
547
556
int numBound = 0 ;
548
557
549
558
if (this .joinPointArgumentIndex != -1 ) {
@@ -580,11 +589,10 @@ else if (this.joinPointStaticPartArgumentIndex != -1) {
580
589
}
581
590
}
582
591
583
- 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)" );
592
+ if (numBound != this .parameterTypes .length ) {
593
+ throw new IllegalStateException ("Required to bind " + this .parameterTypes .length +
594
+ " arguments, but only bound " + numBound + " (JoinPointMatch " +
595
+ (jpMatch == null ? "was NOT" : "WAS" ) + " bound in invocation)" );
588
596
}
589
597
590
598
return adviceInvocationArgs ;
@@ -665,6 +673,16 @@ public String toString() {
665
673
"aspect name '" + this .aspectName + "'" ;
666
674
}
667
675
676
+ private void readObject (ObjectInputStream inputStream ) throws IOException , ClassNotFoundException {
677
+ inputStream .defaultReadObject ();
678
+ try {
679
+ this .aspectJAdviceMethod = this .declaringClass .getMethod (this .methodName , this .parameterTypes );
680
+ }
681
+ catch (NoSuchMethodException ex ) {
682
+ throw new IllegalStateException ("Failed to find advice method on deserialization" , ex );
683
+ }
684
+ }
685
+
668
686
669
687
/**
670
688
* MethodMatcher that excludes the specified advice method.
0 commit comments