From d0b127b84fc3549093ffbd802c3ccb110ecbbd55 Mon Sep 17 00:00:00 2001 From: huangli Date: Tue, 30 Jan 2018 18:02:28 +0800 Subject: [PATCH] fix compatible problem in Spring 4.0 --- .../anno/method/ExpressionEvaluator.java | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/ExpressionEvaluator.java b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/ExpressionEvaluator.java index 1a463f1d6..d0141f3f1 100644 --- a/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/ExpressionEvaluator.java +++ b/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/ExpressionEvaluator.java @@ -5,18 +5,16 @@ import com.alicp.jetcache.CacheConfigException; import com.alicp.jetcache.CacheException; -import com.alicp.jetcache.anno.CacheConsts; -import com.alicp.jetcache.anno.support.CacheAnnoConfig; import org.mvel2.MVEL; import org.springframework.core.DefaultParameterNameDiscoverer; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; import org.springframework.expression.ExpressionParser; -import org.springframework.expression.spel.SpelCompilerMode; import org.springframework.expression.spel.SpelParserConfiguration; import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.function.Function; import java.util.regex.Matcher; @@ -93,10 +91,24 @@ class SpelEvaluator implements Function { private static ExpressionParser parser; - static{ - SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, - SpelEvaluator.class.getClassLoader()); - parser = new SpelExpressionParser(config); + static { + try { + //since spring 4.1 + Class modeClass = Class.forName("org.springframework.expression.spel.SpelCompilerMode"); + + try { + Constructor c = SpelParserConfiguration.class + .getConstructor(modeClass, ClassLoader.class); + Object mode = modeClass.getField("IMMEDIATE").get(null); + SpelParserConfiguration config = c.newInstance(mode, SpelEvaluator.class.getClassLoader()); + parser = new SpelExpressionParser(config); + } catch (Exception e) { + throw new CacheException(e); + } + } catch (ClassNotFoundException e) { + parser = new SpelExpressionParser(); + } + } private final Expression expression;