Skip to content

Commit b6f2d95

Browse files
committed
Cache computed values in SynthesizedAnnotationInvocationHandler
Issue: SPR-11512
1 parent 8ecae86 commit b6f2d95

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Arrays;
2424
import java.util.Iterator;
2525
import java.util.Map;
26+
import java.util.concurrent.ConcurrentHashMap;
2627

2728
import org.springframework.util.ObjectUtils;
2829
import org.springframework.util.StringUtils;
@@ -57,6 +58,8 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
5758

5859
private final Map<String, String> aliasMap;
5960

61+
private final Map<String, Object> computedValueCache;
62+
6063

6164
/**
6265
* Construct a new {@code SynthesizedAnnotationInvocationHandler}.
@@ -73,6 +76,7 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
7376
this.annotation = annotation;
7477
this.annotationType = annotation.annotationType();
7578
this.aliasMap = aliasMap;
79+
this.computedValueCache = new ConcurrentHashMap<String, Object>(aliasMap.size());
7680
}
7781

7882
@Override
@@ -94,13 +98,19 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
9498
boolean aliasPresent = (aliasedAttributeName != null);
9599

96100
makeAccessible(method);
97-
Object value = invokeMethod(method, this.annotation, args);
98101

99102
// No custom processing necessary?
100103
if (!aliasPresent && !nestedAnnotation) {
101-
return value;
104+
return invokeMethod(method, this.annotation, args);
105+
}
106+
107+
Object cachedValue = this.computedValueCache.get(methodName);
108+
if (cachedValue != null) {
109+
return cachedValue;
102110
}
103111

112+
Object value = invokeMethod(method, this.annotation, args);
113+
104114
if (aliasPresent) {
105115
Method aliasedMethod = null;
106116
try {
@@ -147,6 +157,8 @@ else if (value instanceof Annotation[]) {
147157
}
148158
}
149159

160+
this.computedValueCache.put(methodName, value);
161+
150162
return value;
151163
}
152164

0 commit comments

Comments
 (0)