1
1
/*
2
- * Copyright 2002-2007 the original author or authors.
2
+ * Copyright 2002-2008 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .aop .aspectj ;
18
18
19
+ import java .lang .annotation .Annotation ;
19
20
import java .lang .reflect .Constructor ;
20
21
import java .lang .reflect .Method ;
21
22
import java .util .ArrayList ;
30
31
import org .aspectj .weaver .tools .PointcutPrimitive ;
31
32
32
33
import org .springframework .core .ParameterNameDiscoverer ;
33
- import org .springframework .util .ClassUtils ;
34
34
import org .springframework .util .StringUtils ;
35
35
36
36
/**
118
118
*/
119
119
public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscoverer {
120
120
121
- private static final String ANNOTATION_CLASS_NAME = "java.lang.annotation.Annotation" ;
122
-
123
121
private static final String THIS_JOIN_POINT = "thisJoinPoint" ;
124
122
private static final String THIS_JOIN_POINT_STATIC_PART = "thisJoinPointStaticPart" ;
125
123
@@ -133,10 +131,8 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov
133
131
private static final int STEP_REFERENCE_PCUT_BINDING = 7 ;
134
132
private static final int STEP_FINISHED = 8 ;
135
133
136
- private static final Set singleValuedAnnotationPcds = new HashSet ();
137
- private static final Set nonReferencePointcutTokens = new HashSet ();
138
-
139
- private static Class annotationClass ;
134
+ private static final Set <String > singleValuedAnnotationPcds = new HashSet <String >();
135
+ private static final Set <String > nonReferencePointcutTokens = new HashSet <String >();
140
136
141
137
142
138
static {
@@ -157,15 +153,6 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov
157
153
nonReferencePointcutTokens .add ("and" );
158
154
nonReferencePointcutTokens .add ("or" );
159
155
nonReferencePointcutTokens .add ("not" );
160
-
161
- try {
162
- annotationClass = ClassUtils .forName (ANNOTATION_CLASS_NAME ,
163
- AspectJAdviceParameterNameDiscoverer .class .getClassLoader ());
164
- }
165
- catch (ClassNotFoundException ex ) {
166
- // Running on < JDK 1.5, this is OK...
167
- annotationClass = null ;
168
- }
169
156
}
170
157
171
158
@@ -192,8 +179,6 @@ public class AspectJAdviceParameterNameDiscoverer implements ParameterNameDiscov
192
179
193
180
private int numberOfRemainingUnboundArguments ;
194
181
195
- private int algorithmicStep = STEP_JOIN_POINT_BINDING ;
196
-
197
182
198
183
/**
199
184
* Create a new discoverer that attempts to discover parameter names
@@ -241,7 +226,6 @@ public String[] getParameterNames(Method method) {
241
226
this .argumentTypes = method .getParameterTypes ();
242
227
this .numberOfRemainingUnboundArguments = this .argumentTypes .length ;
243
228
this .parameterNameBindings = new String [this .numberOfRemainingUnboundArguments ];
244
- this .algorithmicStep = STEP_JOIN_POINT_BINDING ;
245
229
246
230
int minimumNumberUnboundArgs = 0 ;
247
231
if (this .returningName != null ) {
@@ -256,8 +240,9 @@ public String[] getParameterNames(Method method) {
256
240
}
257
241
258
242
try {
259
- while ((this .numberOfRemainingUnboundArguments > 0 ) && (this .algorithmicStep < STEP_FINISHED )) {
260
- switch (this .algorithmicStep ++) {
243
+ int algorithmicStep = STEP_JOIN_POINT_BINDING ;
244
+ while ((this .numberOfRemainingUnboundArguments > 0 ) && algorithmicStep < STEP_FINISHED ) {
245
+ switch (algorithmicStep ++) {
261
246
case STEP_JOIN_POINT_BINDING :
262
247
if (!maybeBindThisJoinPoint ()) {
263
248
maybeBindThisJoinPointStaticPart ();
@@ -282,7 +267,7 @@ public String[] getParameterNames(Method method) {
282
267
maybeBindReferencePointcutParameter ();
283
268
break ;
284
269
default :
285
- throw new IllegalStateException ("Unknown algorithmic step: " + (this . algorithmicStep - 1 ));
270
+ throw new IllegalStateException ("Unknown algorithmic step: " + (algorithmicStep - 1 ));
286
271
}
287
272
}
288
273
}
@@ -429,7 +414,7 @@ private void maybeBindReturningVariable() {
429
414
* <p>Some more support from AspectJ in doing this exercise would be nice... :)
430
415
*/
431
416
private void maybeBindAnnotationsFromPointcutExpression () {
432
- List varNames = new ArrayList ();
417
+ List < String > varNames = new ArrayList < String > ();
433
418
String [] tokens = StringUtils .tokenizeToStringArray (this .pointcutExpression , " " );
434
419
for (int i = 0 ; i < tokens .length ; i ++) {
435
420
String toMatch = tokens [i ];
@@ -458,7 +443,7 @@ else if (tokens[i].startsWith("@args(") || tokens[i].equals("@args")) {
458
443
/**
459
444
* Match the given list of extracted variable names to argument slots.
460
445
*/
461
- private void bindAnnotationsFromVarNames (List varNames ) {
446
+ private void bindAnnotationsFromVarNames (List < String > varNames ) {
462
447
if (!varNames .isEmpty ()) {
463
448
// we have work to do...
464
449
int numAnnotationSlots = countNumberOfUnboundAnnotationArguments ();
@@ -470,7 +455,7 @@ private void bindAnnotationsFromVarNames(List varNames) {
470
455
else if (numAnnotationSlots == 1 ) {
471
456
if (varNames .size () == 1 ) {
472
457
// it's a match
473
- findAndBind (annotationClass , ( String ) varNames .get (0 ));
458
+ findAndBind (Annotation . class , varNames .get (0 ));
474
459
}
475
460
else {
476
461
// multiple candidate vars, but only one slot
@@ -495,8 +480,8 @@ private String maybeExtractVariableName(String candidateToken) {
495
480
if (Character .isJavaIdentifierStart (candidateToken .charAt (0 )) &&
496
481
Character .isLowerCase (candidateToken .charAt (0 ))) {
497
482
char [] tokenChars = candidateToken .toCharArray ();
498
- for (int i = 0 ; i < tokenChars . length ; i ++ ) {
499
- if (!Character .isJavaIdentifierPart (tokenChars [ i ] )) {
483
+ for (char tokenChar : tokenChars ) {
484
+ if (!Character .isJavaIdentifierPart (tokenChar )) {
500
485
return null ;
501
486
}
502
487
}
@@ -511,11 +496,10 @@ private String maybeExtractVariableName(String candidateToken) {
511
496
* Given an args pointcut body (could be <code>args</code> or <code>at_args</code>),
512
497
* add any candidate variable names to the given list.
513
498
*/
514
- private void maybeExtractVariableNamesFromArgs (String argsSpec , List varNames ) {
499
+ private void maybeExtractVariableNamesFromArgs (String argsSpec , List < String > varNames ) {
515
500
if (argsSpec == null ) {
516
501
return ;
517
502
}
518
-
519
503
String [] tokens = StringUtils .tokenizeToStringArray (argsSpec , "," );
520
504
for (int i = 0 ; i < tokens .length ; i ++) {
521
505
tokens [i ] = StringUtils .trimWhitespace (tokens [i ]);
@@ -536,7 +520,7 @@ private void maybeBindThisOrTargetOrArgsFromPointcutExpression() {
536
520
+ " unbound args at this(),target(),args() binding stage, with no way to determine between them" );
537
521
}
538
522
539
- List varNames = new ArrayList ();
523
+ List < String > varNames = new ArrayList < String > ();
540
524
String [] tokens = StringUtils .tokenizeToStringArray (this .pointcutExpression , " " );
541
525
for (int i = 0 ; i < tokens .length ; i ++) {
542
526
if (tokens [i ].equals ("this" ) ||
@@ -553,12 +537,11 @@ private void maybeBindThisOrTargetOrArgsFromPointcutExpression() {
553
537
else if (tokens [i ].equals ("args" ) || tokens [i ].startsWith ("args(" )) {
554
538
PointcutBody body = getPointcutBody (tokens , i );
555
539
i += body .numTokensConsumed ;
556
- List candidateVarNames = new ArrayList ();
540
+ List < String > candidateVarNames = new ArrayList < String > ();
557
541
maybeExtractVariableNamesFromArgs (body .text , candidateVarNames );
558
542
// we may have found some var names that were bound in previous primitive args binding step,
559
543
// filter them out...
560
- for (Iterator iter = candidateVarNames .iterator (); iter .hasNext ();) {
561
- String varName = (String ) iter .next ();
544
+ for (String varName : candidateVarNames ) {
562
545
if (!alreadyBound (varName )) {
563
546
varNames .add (varName );
564
547
}
@@ -574,7 +557,7 @@ else if (tokens[i].equals("args") || tokens[i].startsWith("args(")) {
574
557
else if (varNames .size () == 1 ) {
575
558
for (int j = 0 ; j < this .parameterNameBindings .length ; j ++) {
576
559
if (isUnbound (j )) {
577
- bindParameterName (j , ( String ) varNames .get (0 ));
560
+ bindParameterName (j , varNames .get (0 ));
578
561
break ;
579
562
}
580
563
}
@@ -588,7 +571,7 @@ private void maybeBindReferencePointcutParameter() {
588
571
+ " unbound args at reference pointcut binding stage, with no way to determine between them" );
589
572
}
590
573
591
- List varNames = new ArrayList ();
574
+ List < String > varNames = new ArrayList < String > ();
592
575
String [] tokens = StringUtils .tokenizeToStringArray (this .pointcutExpression , " " );
593
576
for (int i = 0 ; i < tokens .length ; i ++) {
594
577
String toMatch = tokens [i ];
@@ -634,7 +617,7 @@ private void maybeBindReferencePointcutParameter() {
634
617
else if (varNames .size () == 1 ) {
635
618
for (int j = 0 ; j < this .parameterNameBindings .length ; j ++) {
636
619
if (isUnbound (j )) {
637
- bindParameterName (j , ( String ) varNames .get (0 ));
620
+ bindParameterName (j , varNames .get (0 ));
638
621
break ;
639
622
}
640
623
}
@@ -700,7 +683,7 @@ private void maybeBindPrimitiveArgsFromPointcutExpression() {
700
683
}
701
684
if (numUnboundPrimitives == 1 ) {
702
685
// Look for arg variable and bind it if we find exactly one...
703
- List varNames = new ArrayList ();
686
+ List < String > varNames = new ArrayList < String > ();
704
687
String [] tokens = StringUtils .tokenizeToStringArray (this .pointcutExpression , " " );
705
688
for (int i = 0 ; i < tokens .length ; i ++) {
706
689
if (tokens [i ].equals ("args" ) || tokens [i ].startsWith ("args(" )) {
@@ -751,14 +734,9 @@ private boolean isSubtypeOf(Class supertype, int argumentNumber) {
751
734
}
752
735
753
736
private int countNumberOfUnboundAnnotationArguments () {
754
- if (annotationClass == null ) {
755
- // We're running on a JDK < 1.5
756
- return 0 ;
757
- }
758
-
759
737
int count = 0 ;
760
738
for (int i = 0 ; i < this .argumentTypes .length ; i ++) {
761
- if (isUnbound (i ) && isSubtypeOf (annotationClass , i )) {
739
+ if (isUnbound (i ) && isSubtypeOf (Annotation . class , i )) {
762
740
count ++;
763
741
}
764
742
}
0 commit comments