• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/jetcache-anno/src/main/java/com/alicp/jetcache/anno/method/ExpressionEvaluator.java
1
/**
2
 * Created on 2018/1/19.
3
 */
4
package com.alicp.jetcache.anno.method;
5

6
import com.alicp.jetcache.CacheConfigException;
7
import org.mvel2.MVEL;
8
import org.springframework.core.DefaultParameterNameDiscoverer;
9
import org.springframework.core.ParameterNameDiscoverer;
10
import org.springframework.expression.EvaluationContext;
11
import org.springframework.expression.Expression;
12
import org.springframework.expression.ExpressionParser;
13
import org.springframework.expression.spel.standard.SpelExpressionParser;
14
import org.springframework.expression.spel.support.StandardEvaluationContext;
15

16
import java.lang.reflect.Method;
17
import java.util.function.Function;
18
import java.util.regex.Matcher;
19
import java.util.regex.Pattern;
20

21
/**
22
 * @author huangli
23
 */
24
public class ExpressionEvaluator implements Function<Object, Object> {
25
    private static final Pattern pattern = Pattern.compile("\\s*(\\w+)\\s*\\{(.+)\\}\\s*");
×
26
    private Function<Object, Object> target;
27

28
    public ExpressionEvaluator(String script, Method defineMethod) {
×
29
        Object rt[] = parseEL(script);
×
30
        EL el = (EL) rt[0];
×
31
        String realScript = (String) rt[1];
×
32
        if (el == EL.MVEL) {
×
33
            target = new MvelEvaluator(realScript);
×
34
        } else if (el == EL.SPRING_EL) {
×
35
            target = new SpelEvaluator(realScript, defineMethod);
×
36
        }
37
    }
×
38

39
    private Object[] parseEL(String script) {
40
        if (script == null || script.trim().equals("")) {
×
41
            return null;
×
42
        }
43
        Object[] rt = new Object[2];
×
44
        Matcher matcher = pattern.matcher(script);
×
45
        if (!matcher.matches()) {
×
46
            rt[0] = EL.SPRING_EL; // default spel since 2.4
×
47
            rt[1] = script;
×
48
            return rt;
×
49
        } else {
50
            String s = matcher.group(1);
×
51
            if ("spel".equals(s)) {
×
52
                rt[0] = EL.SPRING_EL;
×
53
            } else if ("mvel".equals(s)) {
×
54
                rt[0] = EL.MVEL;
×
55
            }/* else if ("buildin".equals(s)) {
56
                rt[0] = EL.BUILD_IN;
57
            } */ else {
58
                throw new CacheConfigException("Can't parse \"" + script + "\"");
×
59
            }
60
            rt[1] = matcher.group(2);
×
61
            return rt;
×
62
        }
63
    }
64

65
    @Override
66
    public Object apply(Object o) {
67
        return target.apply(o);
×
68
    }
69

70
    Function<Object, Object> getTarget() {
71
        return target;
×
72
    }
73
}
74

75
class MvelEvaluator implements Function<Object, Object> {
76
    private String script;
77

78
    public MvelEvaluator(String script) {
×
79
        this.script = script;
×
80
    }
×
81

82
    @Override
83
    public Object apply(Object context) {
84
        return MVEL.eval(script, context);
×
85
    }
86
}
87

88
class SpelEvaluator implements Function<Object, Object> {
89

90
    private static ExpressionParser parser;
91
    private static ParameterNameDiscoverer parameterNameDiscoverer;
92

93
    static {
94
        parser = new SpelExpressionParser();
×
95
        parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
×
96
    }
×
97

98
    private final Expression expression;
99
    private String[] parameterNames;
100

101
    public SpelEvaluator(String script, Method defineMethod) {
×
102
        expression = parser.parseExpression(script);
×
103
        if (defineMethod.getParameterCount() > 0) {
×
104
            parameterNames = parameterNameDiscoverer.getParameterNames(defineMethod);
×
105
        }
106
    }
×
107

108
    @Override
109
    public Object apply(Object rootObject) {
110
        EvaluationContext context = new StandardEvaluationContext(rootObject);
×
111
        CacheInvokeContext cic = (CacheInvokeContext) rootObject;
×
112
        if (parameterNames != null) {
×
113
            for (int i = 0; i < parameterNames.length; i++) {
×
114
                context.setVariable(parameterNames[i], cic.getArgs()[i]);
×
115
            }
116
        }
117
        context.setVariable("result", cic.getResult());
×
118
        return expression.getValue(context);
×
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc