Browse Source

Cache computed values in SynthesizedAnnotationInvocationHandler

Issue: SPR-11512
pull/808/head
Sam Brannen 10 years ago
parent
commit
b6f2d95c3e
  1. 16
      spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java

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

@ -23,6 +23,7 @@ import java.lang.reflect.Method; @@ -23,6 +23,7 @@ import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@ -57,6 +58,8 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { @@ -57,6 +58,8 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
private final Map<String, String> aliasMap;
private final Map<String, Object> computedValueCache;
/**
* Construct a new {@code SynthesizedAnnotationInvocationHandler}.
@ -73,6 +76,7 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { @@ -73,6 +76,7 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
this.annotation = annotation;
this.annotationType = annotation.annotationType();
this.aliasMap = aliasMap;
this.computedValueCache = new ConcurrentHashMap<String, Object>(aliasMap.size());
}
@Override
@ -94,13 +98,19 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { @@ -94,13 +98,19 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
boolean aliasPresent = (aliasedAttributeName != null);
makeAccessible(method);
Object value = invokeMethod(method, this.annotation, args);
// No custom processing necessary?
if (!aliasPresent && !nestedAnnotation) {
return value;
return invokeMethod(method, this.annotation, args);
}
Object cachedValue = this.computedValueCache.get(methodName);
if (cachedValue != null) {
return cachedValue;
}
Object value = invokeMethod(method, this.annotation, args);
if (aliasPresent) {
Method aliasedMethod = null;
try {
@ -147,6 +157,8 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { @@ -147,6 +157,8 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler {
}
}
this.computedValueCache.put(methodName, value);
return value;
}

Loading…
Cancel
Save