Skip to content

Commit a4135ba

Browse files
committed
Leniently ignore unsupported pointcut expression
See gh-32793
1 parent bf08e0c commit a4135ba

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

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

+14-9
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,30 @@ public void setBeanFactory(BeanFactory beanFactory) {
170170

171171
@Override
172172
public ClassFilter getClassFilter() {
173-
obtainPointcutExpression();
173+
checkExpression();
174174
return this;
175175
}
176176

177177
@Override
178178
public MethodMatcher getMethodMatcher() {
179-
obtainPointcutExpression();
179+
checkExpression();
180180
return this;
181181
}
182182

183183

184184
/**
185-
* Check whether this pointcut is ready to match,
186-
* lazily building the underlying AspectJ pointcut expression.
185+
* Check whether this pointcut is ready to match.
187186
*/
188-
private PointcutExpression obtainPointcutExpression() {
187+
private void checkExpression() {
189188
if (getExpression() == null) {
190189
throw new IllegalStateException("Must set property 'expression' before attempting to match");
191190
}
191+
}
192+
193+
/**
194+
* Lazily build the underlying AspectJ pointcut expression.
195+
*/
196+
private PointcutExpression obtainPointcutExpression() {
192197
if (this.pointcutExpression == null) {
193198
this.pointcutClassLoader = determinePointcutClassLoader();
194199
this.pointcutExpression = buildPointcutExpression(this.pointcutClassLoader);
@@ -265,10 +270,9 @@ public PointcutExpression getPointcutExpression() {
265270

266271
@Override
267272
public boolean matches(Class<?> targetClass) {
268-
PointcutExpression pointcutExpression = obtainPointcutExpression();
269273
try {
270274
try {
271-
return pointcutExpression.couldMatchJoinPointsInType(targetClass);
275+
return obtainPointcutExpression().couldMatchJoinPointsInType(targetClass);
272276
}
273277
catch (ReflectionWorldException ex) {
274278
logger.debug("PointcutExpression matching rejected target class - trying fallback expression", ex);
@@ -279,6 +283,9 @@ public boolean matches(Class<?> targetClass) {
279283
}
280284
}
281285
}
286+
catch (IllegalArgumentException | IllegalStateException ex) {
287+
throw ex;
288+
}
282289
catch (Throwable ex) {
283290
logger.debug("PointcutExpression matching rejected target class", ex);
284291
}
@@ -287,7 +294,6 @@ public boolean matches(Class<?> targetClass) {
287294

288295
@Override
289296
public boolean matches(Method method, Class<?> targetClass, boolean hasIntroductions) {
290-
obtainPointcutExpression();
291297
ShadowMatch shadowMatch = getTargetShadowMatch(method, targetClass);
292298

293299
// Special handling for this, target, @this, @target, @annotation
@@ -325,7 +331,6 @@ public boolean isRuntime() {
325331

326332
@Override
327333
public boolean matches(Method method, Class<?> targetClass, Object... args) {
328-
obtainPointcutExpression();
329334
ShadowMatch shadowMatch = getTargetShadowMatch(method, targetClass);
330335

331336
// Bind Spring AOP proxy to AspectJ "this" and Spring AOP target to AspectJ target,

spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJExpressionPointcutTests.java

+5-10
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
import org.aopalliance.intercept.MethodInterceptor;
2525
import org.aopalliance.intercept.MethodInvocation;
26-
import org.aspectj.weaver.tools.PointcutPrimitive;
27-
import org.aspectj.weaver.tools.UnsupportedPointcutPrimitiveException;
2826
import org.junit.jupiter.api.BeforeEach;
2927
import org.junit.jupiter.api.Test;
3028
import test.annotation.EmptySpringAnnotation;
@@ -41,7 +39,6 @@
4139
import org.springframework.beans.testfixture.beans.subpkg.DeepBean;
4240

4341
import static org.assertj.core.api.Assertions.assertThat;
44-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4542
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
4643
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
4744

@@ -174,23 +171,23 @@ private void testWithinPackage(boolean matchSubpackages) throws SecurityExceptio
174171
void testFriendlyErrorOnNoLocationClassMatching() {
175172
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
176173
assertThatIllegalStateException()
177-
.isThrownBy(() -> pc.matches(ITestBean.class))
174+
.isThrownBy(() -> pc.getClassFilter().matches(ITestBean.class))
178175
.withMessageContaining("expression");
179176
}
180177

181178
@Test
182179
void testFriendlyErrorOnNoLocation2ArgMatching() {
183180
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
184181
assertThatIllegalStateException()
185-
.isThrownBy(() -> pc.matches(getAge, ITestBean.class))
182+
.isThrownBy(() -> pc.getMethodMatcher().matches(getAge, ITestBean.class))
186183
.withMessageContaining("expression");
187184
}
188185

189186
@Test
190187
void testFriendlyErrorOnNoLocation3ArgMatching() {
191188
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
192189
assertThatIllegalStateException()
193-
.isThrownBy(() -> pc.matches(getAge, ITestBean.class, (Object[]) null))
190+
.isThrownBy(() -> pc.getMethodMatcher().matches(getAge, ITestBean.class, (Object[]) null))
194191
.withMessageContaining("expression");
195192
}
196193

@@ -246,7 +243,7 @@ void testDynamicMatchingProxy() {
246243
@Test
247244
void testInvalidExpression() {
248245
String expression = "execution(void org.springframework.beans.testfixture.beans.TestBean.setSomeNumber(Number) && args(Double)";
249-
assertThatIllegalArgumentException().isThrownBy(getPointcut(expression)::getClassFilter); // call to getClassFilter forces resolution
246+
assertThatIllegalArgumentException().isThrownBy(() -> getPointcut(expression).getClassFilter().matches(Object.class));
250247
}
251248

252249
private TestBean getAdvisedProxy(String pointcutExpression, CallCountingInterceptor interceptor) {
@@ -276,9 +273,7 @@ private void assertMatchesTestBeanClass(ClassFilter classFilter) {
276273
@Test
277274
void testWithUnsupportedPointcutPrimitive() {
278275
String expression = "call(int org.springframework.beans.testfixture.beans.TestBean.getAge())";
279-
assertThatExceptionOfType(UnsupportedPointcutPrimitiveException.class)
280-
.isThrownBy(() -> getPointcut(expression).getClassFilter()) // call to getClassFilter forces resolution...
281-
.satisfies(ex -> assertThat(ex.getUnsupportedPrimitive()).isEqualTo(PointcutPrimitive.CALL));
276+
assertThat(getPointcut(expression).getClassFilter().matches(Object.class)).isFalse();
282277
}
283278

284279
@Test

0 commit comments

Comments
 (0)