Skip to content

Commit fb6e865

Browse files
committed
Polish "Handle arbitrary JoinPoint argument index"
See gh-34316
1 parent 13ba770 commit fb6e865

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -281,7 +281,7 @@ public void setArgumentNamesFromStringArray(String... argumentNames) {
281281
if (argType == JoinPoint.class ||
282282
argType == ProceedingJoinPoint.class ||
283283
argType == JoinPoint.StaticPart.class) {
284-
String[] oldNames = this.argumentNames;
284+
String[] oldNames = this.argumentNames;
285285
this.argumentNames = new String[oldNames.length + 1];
286286
System.arraycopy(oldNames, 0, this.argumentNames, 0, i);
287287
this.argumentNames[i] = "THIS_JOIN_POINT";

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

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
package org.springframework.aop.aspectj;
1818

19-
import java.io.Serial;
20-
import java.lang.reflect.Field;
2119
import java.lang.reflect.Method;
2220
import java.util.Arrays;
21+
import java.util.function.Consumer;
2322

2423
import org.aspectj.lang.JoinPoint;
2524
import org.aspectj.lang.ProceedingJoinPoint;
26-
import org.jetbrains.annotations.NotNull;
25+
import org.assertj.core.api.InstanceOfAssertFactories;
2726
import org.junit.jupiter.api.Test;
2827

2928
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,74 +32,70 @@
3332
* Tests for {@link AbstractAspectJAdvice}.
3433
*
3534
* @author Joshua Chen
35+
* @author Stephane Nicoll
3636
*/
3737
class AbstractAspectJAdviceTests {
3838

39+
@Test
40+
void setArgumentNamesFromStringArray_withoutJoinPointParameter() {
41+
AbstractAspectJAdvice advice = getAspectJAdvice("methodWithNoJoinPoint");
42+
assertThat(advice).satisfies(hasArgumentNames("arg1", "arg2"));
43+
}
44+
3945
@Test
4046
void setArgumentNamesFromStringArray_withJoinPointAsFirstParameter() {
4147
AbstractAspectJAdvice advice = getAspectJAdvice("methodWithJoinPointAsFirstParameter");
42-
assertArgumentNamesFromStringArray(advice);
48+
assertThat(advice).satisfies(hasArgumentNames("THIS_JOIN_POINT", "arg1", "arg2"));
4349
}
4450

4551
@Test
4652
void setArgumentNamesFromStringArray_withJoinPointAsLastParameter() {
4753
AbstractAspectJAdvice advice = getAspectJAdvice("methodWithJoinPointAsLastParameter");
48-
assertThat(getArgumentNames(advice)[0]).isEqualTo("arg1");
49-
assertThat(getArgumentNames(advice)[1]).isEqualTo("arg2");
50-
assertThat(getArgumentNames(advice)[2]).isEqualTo("THIS_JOIN_POINT");
54+
assertThat(advice).satisfies(hasArgumentNames("arg1", "arg2", "THIS_JOIN_POINT"));
5155
}
5256

5357
@Test
5458
void setArgumentNamesFromStringArray_withJoinPointAsMiddleParameter() {
5559
AbstractAspectJAdvice advice = getAspectJAdvice("methodWithJoinPointAsMiddleParameter");
56-
assertThat(getArgumentNames(advice)[0]).isEqualTo("arg1");
57-
assertThat(getArgumentNames(advice)[1]).isEqualTo("THIS_JOIN_POINT");
58-
assertThat(getArgumentNames(advice)[2]).isEqualTo("arg2");
60+
assertThat(advice).satisfies(hasArgumentNames("arg1", "THIS_JOIN_POINT", "arg2"));
5961
}
6062

6163
@Test
6264
void setArgumentNamesFromStringArray_withProceedingJoinPoint() {
6365
AbstractAspectJAdvice advice = getAspectJAdvice("methodWithProceedingJoinPoint");
64-
assertArgumentNamesFromStringArray(advice);
66+
assertThat(advice).satisfies(hasArgumentNames("THIS_JOIN_POINT", "arg1", "arg2"));
6567
}
6668

6769
@Test
6870
void setArgumentNamesFromStringArray_withStaticPart() {
6971
AbstractAspectJAdvice advice = getAspectJAdvice("methodWithStaticPart");
70-
assertArgumentNamesFromStringArray(advice);
72+
assertThat(advice).satisfies(hasArgumentNames("THIS_JOIN_POINT", "arg1", "arg2"));
7173
}
7274

73-
private void assertArgumentNamesFromStringArray(AbstractAspectJAdvice advice) {
74-
assertThat(getArgumentNames(advice)[0]).isEqualTo("THIS_JOIN_POINT");
75-
assertThat(getArgumentNames(advice)[1]).isEqualTo("arg1");
76-
assertThat(getArgumentNames(advice)[2]).isEqualTo("arg2");
75+
private Consumer<AbstractAspectJAdvice> hasArgumentNames(String... argumentNames) {
76+
return advice -> assertThat(advice).extracting("argumentNames")
77+
.asInstanceOf(InstanceOfAssertFactories.array(String[].class))
78+
.containsExactly(argumentNames);
7779
}
7880

79-
private @NotNull AbstractAspectJAdvice getAspectJAdvice(final String methodName) {
80-
AbstractAspectJAdvice advice = new TestAspectJAdvice(getMethod(methodName), mock(AspectJExpressionPointcut.class), mock(AspectInstanceFactory.class));
81+
private AbstractAspectJAdvice getAspectJAdvice(final String methodName) {
82+
AbstractAspectJAdvice advice = new TestAspectJAdvice(getMethod(methodName),
83+
mock(AspectJExpressionPointcut.class), mock(AspectInstanceFactory.class));
8184
advice.setArgumentNamesFromStringArray("arg1", "arg2");
8285
return advice;
8386
}
8487

85-
private Method getMethod(final String name) {
86-
return Arrays.stream(this.getClass().getDeclaredMethods()).filter(m -> m.getName().equals(name)).findFirst().orElseThrow();
87-
}
88-
89-
private String[] getArgumentNames(final AbstractAspectJAdvice advice) {
90-
try {
91-
Field field = AbstractAspectJAdvice.class.getDeclaredField("argumentNames");
92-
field.setAccessible(true);
93-
return (String[]) field.get(advice);
94-
}
95-
catch (NoSuchFieldException | IllegalAccessException e) {
96-
throw new RuntimeException(e);
97-
}
88+
private Method getMethod(final String methodName) {
89+
return Arrays.stream(Sample.class.getDeclaredMethods())
90+
.filter(method -> method.getName().equals(methodName)).findFirst()
91+
.orElseThrow();
9892
}
9993

94+
@SuppressWarnings("serial")
10095
public static class TestAspectJAdvice extends AbstractAspectJAdvice {
101-
@Serial private static final long serialVersionUID = 1L;
10296

103-
public TestAspectJAdvice(Method aspectJAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aspectInstanceFactory) {
97+
public TestAspectJAdvice(Method aspectJAdviceMethod, AspectJExpressionPointcut pointcut,
98+
AspectInstanceFactory aspectInstanceFactory) {
10499
super(aspectJAdviceMethod, pointcut, aspectInstanceFactory);
105100
}
106101

@@ -115,18 +110,26 @@ public boolean isAfterAdvice() {
115110
}
116111
}
117112

118-
void methodWithJoinPointAsFirstParameter(JoinPoint joinPoint, String arg1, String arg2) {
119-
}
113+
@SuppressWarnings("unused")
114+
static class Sample {
120115

121-
void methodWithJoinPointAsLastParameter(String arg1, String arg2, JoinPoint joinPoint) {
122-
}
116+
void methodWithNoJoinPoint(String arg1, String arg2) {
117+
}
123118

124-
void methodWithJoinPointAsMiddleParameter(String arg1, JoinPoint joinPoint, String arg2) {
125-
}
119+
void methodWithJoinPointAsFirstParameter(JoinPoint joinPoint, String arg1, String arg2) {
120+
}
126121

127-
void methodWithProceedingJoinPoint(ProceedingJoinPoint joinPoint, String arg1, String arg2) {
128-
}
122+
void methodWithJoinPointAsLastParameter(String arg1, String arg2, JoinPoint joinPoint) {
123+
}
124+
125+
void methodWithJoinPointAsMiddleParameter(String arg1, JoinPoint joinPoint, String arg2) {
126+
}
127+
128+
void methodWithProceedingJoinPoint(ProceedingJoinPoint joinPoint, String arg1, String arg2) {
129+
}
129130

130-
void methodWithStaticPart(JoinPoint.StaticPart staticPart, String arg1, String arg2) {
131+
void methodWithStaticPart(JoinPoint.StaticPart staticPart, String arg1, String arg2) {
132+
}
131133
}
134+
132135
}

0 commit comments

Comments
 (0)