From b6f2d95c3e2c9ef42285dfe86c57dfa5bc193a71 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 24 May 2015 15:56:56 +0200 Subject: [PATCH] Cache computed values in SynthesizedAnnotationInvocationHandler Issue: SPR-11512 --- .../SynthesizedAnnotationInvocationHandler.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java index edb26ef174..66dee3f41c 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/SynthesizedAnnotationInvocationHandler.java @@ -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 { private final Map aliasMap; + private final Map computedValueCache; + /** * Construct a new {@code SynthesizedAnnotationInvocationHandler}. @@ -73,6 +76,7 @@ class SynthesizedAnnotationInvocationHandler implements InvocationHandler { this.annotation = annotation; this.annotationType = annotation.annotationType(); this.aliasMap = aliasMap; + this.computedValueCache = new ConcurrentHashMap(aliasMap.size()); } @Override @@ -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 { } } + this.computedValueCache.put(methodName, value); + return value; }