1
1
/*
2
- * Copyright 2002-2009 the original author or authors.
2
+ * Copyright 2002-2010 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.
54
54
import org .springframework .beans .factory .BeanFactoryAware ;
55
55
import org .springframework .beans .factory .BeanFactoryUtils ;
56
56
import org .springframework .beans .factory .FactoryBean ;
57
+ import org .springframework .beans .factory .config .ConfigurableBeanFactory ;
57
58
import org .springframework .util .ObjectUtils ;
58
59
import org .springframework .util .StringUtils ;
59
60
@@ -95,12 +96,6 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
95
96
96
97
private static final Log logger = LogFactory .getLog (AspectJExpressionPointcut .class );
97
98
98
- private transient PointcutParser pointcutParser ;
99
-
100
- private transient PointcutExpression pointcutExpression ;
101
-
102
- private transient Map <Method , ShadowMatch > shadowMatchCache = new ConcurrentHashMap <Method , ShadowMatch >(32 );
103
-
104
99
private Class pointcutDeclarationScope ;
105
100
106
101
private String [] pointcutParameterNames = new String [0 ];
@@ -109,12 +104,15 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
109
104
110
105
private BeanFactory beanFactory ;
111
106
107
+ private transient PointcutExpression pointcutExpression ;
108
+
109
+ private transient Map <Method , ShadowMatch > shadowMatchCache = new ConcurrentHashMap <Method , ShadowMatch >(32 );
110
+
112
111
113
112
/**
114
113
* Create a new default AspectJExpressionPointcut.
115
114
*/
116
115
public AspectJExpressionPointcut () {
117
- initializePointcutParser ();
118
116
}
119
117
120
118
/**
@@ -124,7 +122,6 @@ public AspectJExpressionPointcut() {
124
122
* @param paramTypes the parameter types for the pointcut
125
123
*/
126
124
public AspectJExpressionPointcut (Class declarationScope , String [] paramNames , Class [] paramTypes ) {
127
- initializePointcutParser ();
128
125
this .pointcutDeclarationScope = declarationScope ;
129
126
if (paramNames .length != paramTypes .length ) {
130
127
throw new IllegalStateException (
@@ -134,13 +131,6 @@ public AspectJExpressionPointcut(Class declarationScope, String[] paramNames, Cl
134
131
this .pointcutParameterTypes = paramTypes ;
135
132
}
136
133
137
- private void initializePointcutParser () {
138
- this .pointcutParser =
139
- PointcutParser .getPointcutParserSupportingSpecifiedPrimitivesAndUsingContextClassloaderForResolution (
140
- SUPPORTED_PRIMITIVES );
141
- this .pointcutParser .registerPointcutDesignatorHandler (new BeanNamePointcutDesignatorHandler ());
142
- }
143
-
144
134
145
135
/**
146
136
* Set the declaration scope for the pointcut.
@@ -196,15 +186,30 @@ private void checkReadyToMatch() {
196
186
* Build the underlying AspectJ pointcut expression.
197
187
*/
198
188
private PointcutExpression buildPointcutExpression () {
189
+ PointcutParser parser = initializePointcutParser ();
199
190
PointcutParameter [] pointcutParameters = new PointcutParameter [this .pointcutParameterNames .length ];
200
191
for (int i = 0 ; i < pointcutParameters .length ; i ++) {
201
- pointcutParameters [i ] = this . pointcutParser .createPointcutParameter (
192
+ pointcutParameters [i ] = parser .createPointcutParameter (
202
193
this .pointcutParameterNames [i ], this .pointcutParameterTypes [i ]);
203
194
}
204
- return this . pointcutParser .parsePointcutExpression (
195
+ return parser .parsePointcutExpression (
205
196
replaceBooleanOperators (getExpression ()), this .pointcutDeclarationScope , pointcutParameters );
206
197
}
207
198
199
+ /**
200
+ * Initialize the underlying AspectJ pointcut parser.
201
+ */
202
+ private PointcutParser initializePointcutParser () {
203
+ ClassLoader cl = (this .beanFactory instanceof ConfigurableBeanFactory ?
204
+ ((ConfigurableBeanFactory ) this .beanFactory ).getBeanClassLoader () :
205
+ Thread .currentThread ().getContextClassLoader ());
206
+ PointcutParser parser =
207
+ PointcutParser .getPointcutParserSupportingSpecifiedPrimitivesAndUsingSpecifiedClassLoaderForResolution (
208
+ SUPPORTED_PRIMITIVES , cl );
209
+ parser .registerPointcutDesignatorHandler (new BeanNamePointcutDesignatorHandler ());
210
+ return parser ;
211
+ }
212
+
208
213
/**
209
214
* If a pointcut expression has been specified in XML, the user cannot
210
215
* write <code>and</code> as "&&" (though && will work).
@@ -218,6 +223,7 @@ private String replaceBooleanOperators(String pcExpr) {
218
223
return result ;
219
224
}
220
225
226
+
221
227
/**
222
228
* Return the underlying AspectJ pointcut expression.
223
229
*/
@@ -226,7 +232,6 @@ public PointcutExpression getPointcutExpression() {
226
232
return this .pointcutExpression ;
227
233
}
228
234
229
-
230
235
public boolean matches (Class targetClass ) {
231
236
checkReadyToMatch ();
232
237
try {
@@ -455,7 +460,6 @@ public ContextBasedMatcher parse(String expression) {
455
460
456
461
/**
457
462
* Matcher class for the BeanNamePointcutDesignatorHandler.
458
- *
459
463
* <p>Dynamic match tests for this matcher always return true,
460
464
* since the matching decision is made at the proxy creation time.
461
465
* For static match tests, this matcher abstains to allow the overall
@@ -535,7 +539,7 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
535
539
ois .defaultReadObject ();
536
540
537
541
// Initialize transient fields.
538
- initializePointcutParser ();
542
+ // pointcutExpression will be initialized lazily by checkReadyToMatch()
539
543
this .shadowMatchCache = new ConcurrentHashMap <Method , ShadowMatch >(32 );
540
544
}
541
545
0 commit comments