Here are the examples of the java api org.springframework.expression.ExpressionParser.parseExpression() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
195 Examples
19
View Source File : ConvertUtils.java
License : Apache License 2.0
Project Creator : Yiuman
License : Apache License 2.0
Project Creator : Yiuman
public static <T> T parseEl(String el, Clreplaced<T> clazz) {
return EXPRESSION_PARSER.parseExpression(el).getValue(clazz);
}
19
View Source File : ConvertUtils.java
License : Apache License 2.0
Project Creator : Yiuman
License : Apache License 2.0
Project Creator : Yiuman
public static Object parseEl(String el) {
return EXPRESSION_PARSER.parseExpression(el).getValue();
}
19
View Source File : SpringUtil.java
License : MIT License
Project Creator : toesbieya
License : MIT License
Project Creator : toesbieya
/**
* el表达式解析
*
* @param el 表达式
* @param names 参数名称数组
* @param args 参数数组
*/
public static Object spell(String el, String[] names, Object[] args) {
if (StringUtils.isEmpty(el)) {
return null;
}
EvaluationContext context = new StandardEvaluationContext();
for (int i = 0; i < args.length; i++) {
context.setVariable(names[i], args[i]);
}
return parser.parseExpression(el).getValue(context);
}
19
View Source File : LockInfoProvider.java
License : Apache License 2.0
Project Creator : open-hand
License : Apache License 2.0
Project Creator : open-hand
/**
* 获取方法定义KEY
*/
private List<String> getSpelDefinitionKey(String[] definitionKeys, EvaluationContext context) {
List<String> definitionKeyList = new ArrayList<>();
for (String definitionKey : definitionKeys) {
if (definitionKey == null || definitionKey.isEmpty()) {
continue;
}
Object value = parser.parseExpression(definitionKey).getValue(context);
if (value != null) {
definitionKeyList.add(value.toString());
}
}
return definitionKeyList;
}
19
View Source File : DiscoveryExpressionResolver.java
License : Apache License 2.0
Project Creator : Nepxion
License : Apache License 2.0
Project Creator : Nepxion
public static boolean eval(String expression, StandardEvaluationContext context) {
try {
Boolean result = EXPRESSION_PARSER.parseExpression(expression).getValue(context, Boolean.clreplaced);
return result != null ? result.booleanValue() : false;
} catch (Exception e) {
return false;
}
}
19
View Source File : ExpressionEvaluator.java
License : Apache License 2.0
Project Creator : microconfig
License : Apache License 2.0
Project Creator : microconfig
public String evaluate(String value) {
return parser.parseExpression(value).getValue(context, String.clreplaced);
}
19
View Source File : ElParser.java
License : Apache License 2.0
Project Creator : learningtcc
License : Apache License 2.0
Project Creator : learningtcc
public <T> T eval(String expr, Clreplaced<T> returnClazz) {
if (StringUtils.isBlank(expr)) {
return null;
}
return expressionParser.parseExpression(expr).getValue(evalCtx, returnClazz);
}
19
View Source File : CalledMethodsPredicateEvaluator.java
License : MIT License
Project Creator : kelloggm
License : MIT License
Project Creator : kelloggm
/**
* Evaluate the given expression if every String in {@code trueVariables} is replaced by "true" in
* the boolean formula.
*
* @param expression a boolean formula in Java format, as a String
* @param trueVariables the variables in the boolean expression to treat as "true"
* @return whether the expression evaluates to true in the context where only the variables in
* trueVariables are true, and all other variables are false
*/
public static boolean evaluate(String expression, Collection<String> trueVariables) {
for (String cmMethod : trueVariables) {
expression = expression.replaceAll("\\b" + cmMethod + "\\b", "true");
}
expression = expression.replaceAll("(?!true)\\b[_a-zA-Z][_a-zA-Z0-9]*\\b", "false");
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(expression);
return exp.getValue(Boolean.clreplaced);
}
19
View Source File : SpELUtil.java
License : MIT License
Project Creator : Hccake
License : MIT License
Project Creator : Hccake
public static String parseValueToString(StandardEvaluationContext context, String spEL) {
return PARSER.parseExpression(spEL).getValue(context, String.clreplaced);
}
19
View Source File : SpringElCheckUtil.java
License : Apache License 2.0
Project Creator : eacdy
License : Apache License 2.0
Project Creator : eacdy
/**
* 校验expression是否能通过rootObject的检测
*
* @param context 上下文
* @param expression 表达式
* @return 是否通过
*/
public static boolean check(EvaluationContext context, String expression) {
Boolean result = PARSER.parseExpression(expression).getValue(context, Boolean.clreplaced);
log.info("expression = {}, eval result = {}", expression, result);
return result != null ? result : false;
}
19
View Source File : ExcelWriteExecutor.java
License : MIT License
Project Creator : archine
License : MIT License
Project Creator : archine
/**
* Data convert
*
* @param field Current field
* @param value Attribute values
* @param obj Current object
* @param parser El parser
* @param excelDataConvert excelDataConvert
* @param context EL context
* @return new value
*/
private Object convert(Field field, Object value, Object obj, ExpressionParser parser, ExcelDataConvert excelDataConvert, EvaluationContext context, DataConvert<?> dataConvert) {
if (excelDataConvert != null && !"".equals(excelDataConvert.expr1())) {
return parser.parseExpression(excelDataConvert.expr1()).getValue(context);
}
if (dataConvert != null) {
return dataConvert.toExcelAttribute(this.gson.fromJson(this.gson.toJson(obj), (Type) obj.getClreplaced()), value, field);
}
return value;
}
19
View Source File : ExcelReadExecutor.java
License : MIT License
Project Creator : archine
License : MIT License
Project Creator : archine
/**
* Data convert
*
* @param field Current field
* @param value Attribute values
* @param parser El parser
* @param context EL context
* @return new value
*/
private Object convert(Field field, Object value, ExpressionParser parser, EvaluationContext context, DataConvert<?> dataConvert) {
ExcelDataConvert excelDataConvert = field.getAnnotation(ExcelDataConvert.clreplaced);
if (excelDataConvert != null && !"".equals(excelDataConvert.expr2())) {
return parser.parseExpression(excelDataConvert.expr2()).getValue(context);
}
return dataConvert.toEnreplacedyAttribute(value, field);
}
19
View Source File : JsonTemplateInterpolation.java
License : Apache License 2.0
Project Creator : adorsys
License : Apache License 2.0
Project Creator : adorsys
private String doParse(String expression, HbciSandboxContext context) {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext parseContext = new StandardEvaluationContext(new SpelCtx(context));
return parser.parseExpression(expression, new TemplateParserContext()).getValue(parseContext, String.clreplaced);
}
18
View Source File : LockAop.java
License : GNU Lesser General Public License v3.0
Project Creator : z744489075
License : GNU Lesser General Public License v3.0
Project Creator : z744489075
/**
* 通过spring Spel 获取参数
* @param key 定义的key值 以#开头 例如:#user
* @param parameterNames 形参
* @param values 形参值
* @param keyConstant key的常亮
* @return
*/
public List<String> getVauleBySpel(String key, String[] parameterNames, Object[] values, String keyConstant) {
List<String> keys = new ArrayList<>();
if (!key.contains("#")) {
String s = "redisson:lock:" + key + keyConstant;
log.info("没有使用spel表达式value->{}", s);
keys.add(s);
return keys;
}
// spel解析器
ExpressionParser parser = new SpelExpressionParser();
// spel上下文
EvaluationContext context = new StandardEvaluationContext();
for (int i = 0; i < parameterNames.length; i++) {
context.setVariable(parameterNames[i], values[i]);
}
Expression expression = parser.parseExpression(key);
Object value = expression.getValue(context);
if (value != null) {
if (value instanceof List) {
List value1 = (List) value;
for (Object o : value1) {
keys.add("redisson:lock:" + o.toString() + keyConstant);
}
} else if (value.getClreplaced().isArray()) {
Object[] obj = (Object[]) value;
for (Object o : obj) {
keys.add("redisson:lock:" + o.toString() + keyConstant);
}
} else {
keys.add("redisson:lock:" + value.toString() + keyConstant);
}
}
log.info("spel表达式key={},value={}", key, keys);
return keys;
}
18
View Source File : LimiterInterceptor.java
License : MIT License
Project Creator : yu120
License : MIT License
Project Creator : yu120
/**
* 解析 spel 表达式
*
* @param method 方法
* @param arguments 参数
* @param spel 表达式
* @return 执行spel表达式后的结果
*/
private Object parseSpel(Method method, Object[] arguments, String spel) {
String[] params = discoverer.getParameterNames(method);
if (params == null) {
return spel;
}
EvaluationContext context = new StandardEvaluationContext();
for (int len = 0; len < params.length; len++) {
context.setVariable(params[len], arguments[len]);
}
try {
Expression expression = parser.parseExpression(spel);
return expression.getValue(context);
} catch (Exception e) {
return spel;
}
}
18
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* Verifies behavior requested in SPR-9613.
*/
@Test
public void caseInsensitiveNullLiterals() {
ExpressionParser parser = new SpelExpressionParser();
Expression e = parser.parseExpression("null");
replacedertNull(e.getValue());
e = parser.parseExpression("NULL");
replacedertNull(e.getValue());
e = parser.parseExpression("NuLl");
replacedertNull(e.getValue());
}
18
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test(expected = SpelEvaluationException.clreplaced)
public void testCreateMapsOnAttemptToIndexNull01() {
TestClreplaced testClreplaced = new TestClreplaced();
StandardEvaluationContext ctx = new StandardEvaluationContext(testClreplaced);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Object o = parser.parseExpression("map['a']").getValue(ctx);
replacedertNull(o);
o = parser.parseExpression("map").getValue(ctx);
replacedertNotNull(o);
o = parser.parseExpression("map2['a']").getValue(ctx);
// map2 should be null, there is no setter
}
18
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// wibble2 should be null (cannot be initialized dynamically), there is no setter
@Test(expected = SpelEvaluationException.clreplaced)
public void testCreateObjectsOnAttemptToReferenceNull() {
TestClreplaced testClreplaced = new TestClreplaced();
StandardEvaluationContext ctx = new StandardEvaluationContext(testClreplaced);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Object o = parser.parseExpression("wibble.bar").getValue(ctx);
replacedertEquals("hello", o);
o = parser.parseExpression("wibble").getValue(ctx);
replacedertNotNull(o);
o = parser.parseExpression("wibble2.bar").getValue(ctx);
}
18
View Source File : AbstractExpressionTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* Parse the specified expression and ensure the expected message comes out.
* The message may have inserts and they will be checked if otherProperties is specified.
* The first entry in otherProperties should always be the position.
* @param expression the expression to evaluate
* @param expectedMessage the expected message
* @param otherProperties the expected inserts within the message
*/
protected void parseAndCheckError(String expression, SpelMessage expectedMessage, Object... otherProperties) {
try {
Expression expr = parser.parseExpression(expression);
SpelUtilities.printAbstractSyntaxTree(System.out, expr);
fail("Parsing should have failed!");
} catch (ParseException pe) {
SpelParseException ex = (SpelParseException) pe;
if (ex.getMessageCode() != expectedMessage) {
replacedertEquals("Failed to get expected message", expectedMessage, ex.getMessageCode());
}
if (otherProperties != null && otherProperties.length != 0) {
// first one is expected position of the error within the string
int pos = ((Integer) otherProperties[0]).intValue();
replacedertEquals("Did not get correct position reported in error ", pos, ex.getPosition());
if (otherProperties.length > 1) {
// Check inserts match
Object[] inserts = ex.getInserts();
if (inserts == null) {
inserts = new Object[0];
}
if (inserts.length < otherProperties.length - 1) {
fail("Cannot check " + (otherProperties.length - 1) + " properties of the exception, it only has " + inserts.length + " inserts");
}
for (int i = 1; i < otherProperties.length; i++) {
if (!inserts[i - 1].equals(otherProperties[i])) {
fail("Insert does not match, expected '" + otherProperties[i] + "' but insert value was '" + inserts[i - 1] + "'");
}
}
}
}
}
}
18
View Source File : SpringELExpressionFactory.java
License : Apache License 2.0
Project Creator : seata
License : Apache License 2.0
Project Creator : seata
@Override
public Expression createExpression(String expression) {
org.springframework.expression.Expression defaultExpression = parser.parseExpression(expression);
EvaluationContext evaluationContext = ((SpelExpression) defaultExpression).getEvaluationContext();
((StandardEvaluationContext) evaluationContext).setBeanResolver(new AppContextBeanResolver());
return new SpringELExpression(defaultExpression);
}
18
View Source File : SpelExpressionUtils.java
License : Apache License 2.0
Project Creator : penggle
License : Apache License 2.0
Project Creator : penggle
/**
* 对表达式进行求值
* @param parser
* @param expression
* @param variables
* @param valueType
*/
public static <T> T evalExpression(ExpressionParser parser, String expression, Map<String, Object> variables, Clreplaced<T> valueType) {
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
evaluationContext.setVariables(variables);
return parser.parseExpression(expression).getValue(evaluationContext, valueType);
}
18
View Source File : LockInfoProvider.java
License : Apache License 2.0
Project Creator : open-hand
License : Apache License 2.0
Project Creator : open-hand
/**
* 获取参数KEY
*/
private List<String> getParameterKey(Parameter[] parameters, Object[] parameterValues) {
List<String> parameterKey = new ArrayList<>();
for (int i = 0; i < parameters.length; i++) {
if (parameters[i].getAnnotation(LockKey.clreplaced) != null) {
LockKey keyAnnotation = parameters[i].getAnnotation(LockKey.clreplaced);
if (keyAnnotation.value().isEmpty()) {
parameterKey.add(parameterValues[i].toString());
} else {
StandardEvaluationContext context = new StandardEvaluationContext(parameterValues[i]);
Object value = parser.parseExpression(keyAnnotation.value()).getValue(context);
if (value != null) {
parameterKey.add(value.toString());
}
}
}
}
return parameterKey;
}
18
View Source File : SimplePolicyDefinition.java
License : MIT License
Project Creator : mostafa-eltaher
License : MIT License
Project Creator : mostafa-eltaher
@PostConstruct
private void init() {
ExpressionParser exp = new SpelExpressionParser();
rules = new ArrayList<>();
PolicyRule newRule = new PolicyRule();
newRule.setName("ResourceOwner");
newRule.setDescription("Resource owner should have access to it.");
newRule.setCondition(exp.parseExpression("true"));
newRule.setTarget(exp.parseExpression("subject.name == resource.owner"));
rules.add(newRule);
}
18
View Source File : Bucket4JBaseConfiguration.java
License : Apache License 2.0
Project Creator : MarcGiffing
License : Apache License 2.0
Project Creator : MarcGiffing
private List<MetricTagResult> getMetricTags(ExpressionParser expressionParser, ConfigurableBeanFactory beanFactory, FilterConfiguration<R> filterConfig, R servletRequest) {
List<MetricTagResult> metricTagResults = filterConfig.getMetrics().getTags().stream().map((metricMetaTag) -> {
String expression = metricMetaTag.getExpression();
if (StringUtils.isEmpty(expression)) {
throw new MissingMetricTagExpressionException(metricMetaTag.getKey());
}
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new BeanFactoryResolver(beanFactory));
// TODO performance problem - how can the request object reused in the expression without setting it as a rootObject
Expression expr = expressionParser.parseExpression(expression);
final String value = expr.getValue(context, servletRequest, String.clreplaced);
return new MetricTagResult(metricMetaTag.getKey(), value, metricMetaTag.getTypes());
}).collect(toList());
if (metricTagResults == null) {
metricTagResults = new ArrayList<>();
}
return metricTagResults;
}
18
View Source File : AopUtil.java
License : Apache License 2.0
Project Creator : leiyong0326
License : Apache License 2.0
Project Creator : leiyong0326
/**
* 获取redis缓存key通过集合参数
*
* @param keys
* 缓存keys
* @param params
* 参数名
* @param returnObj
* 参数列表
* @return
*/
private String parseKeyByReturn(String keys, Object returnObj) {
ExpressionParser parser = getParser();
StandardEvaluationContext context = new StandardEvaluationContext();
// 把方法参数放入SPEL上下文中
context.setVariable("obj", returnObj);
// 获取参数key
StringBuffer sb = new StringBuffer();
sb.append(parser.parseExpression(keys).getValue(context, String.clreplaced));
return sb.toString();
}
18
View Source File : EvaluationTests.java
License : Apache License 2.0
Project Creator : langtianya
License : Apache License 2.0
Project Creator : langtianya
@Test
public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException {
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression expression = parser.parseExpression("list[0]");
TestClreplaced testClreplaced = new TestClreplaced();
Object o = null;
o = expression.getValue(new StandardEvaluationContext(testClreplaced));
replacedertEquals("", o);
o = parser.parseExpression("list[3]").getValue(new StandardEvaluationContext(testClreplaced));
replacedertEquals("", o);
replacedertEquals(4, testClreplaced.list.size());
try {
o = parser.parseExpression("list2[3]").getValue(new StandardEvaluationContext(testClreplaced));
fail();
} catch (EvaluationException ee) {
ee.printStackTrace();
// success!
}
o = parser.parseExpression("foo[3]").getValue(new StandardEvaluationContext(testClreplaced));
replacedertEquals("", o);
replacedertEquals(4, testClreplaced.getFoo().size());
}
18
View Source File : AbstractRequestLockInterceptor.java
License : Apache License 2.0
Project Creator : jiangmin168168
License : Apache License 2.0
Project Creator : jiangmin168168
private String getLockKey(Method method, String targetName, String methodName, String[] keys, Object[] arguments) {
StringBuilder sb = new StringBuilder();
sb.append("lock.").append(targetName).append(".").append(methodName);
if (keys != null) {
String keyStr = Joiner.on(".").skipNulls().join(keys);
if (!StringUtils.isBlank(keyStr)) {
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
String[] parameters = discoverer.getParameterNames(method);
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(keyStr);
EvaluationContext context = new StandardEvaluationContext();
int length = parameters.length;
if (length > 0) {
for (int i = 0; i < length; i++) {
context.setVariable(parameters[i], arguments[i]);
}
}
String keysValue = expression.getValue(context, String.clreplaced);
sb.append("#").append(keysValue);
}
}
return sb.toString();
}
18
View Source File : MappingProcessor.java
License : Apache License 2.0
Project Creator : i-novus-llc
License : Apache License 2.0
Project Creator : i-novus-llc
/**
* Исходящее преобразование target согласно mapping выражению
*
* @param target исходное значение
* @param mapping выражения преобразования
* @return результат преобразования
*/
public static <T> T outMap(Object target, String mapping, Clreplaced<T> clazz) {
T result;
if (mapping != null) {
Expression expression = readParser.parseExpression(mapping);
result = expression.getValue(target, clazz);
} else {
result = (T) target;
}
if (clazz != null && result == null)
throw new N2oException("Expected is " + clazz + ", but actual is null");
if (clazz != null && !clazz.isreplacedignableFrom(result.getClreplaced()))
throw new N2oException("Expected is " + clazz + ", but actual is " + result.getClreplaced());
return result;
}
18
View Source File : SpringExpressionParser.java
License : Apache License 2.0
Project Creator : HotelsDotCom
License : Apache License 2.0
Project Creator : HotelsDotCom
/**
* Applies any defined functions to the preplaceded expression string and returns the result of this.
*
* @param expressionString
* @return
*/
public String parse(String expressionString) {
if (expressionString != null) {
Expression expression = parser.parseExpression(expressionString, templateContext);
expressionString = expression.getValue(evalContext).toString();
}
return expressionString;
}
18
View Source File : SecurityExpressionEvaluator.java
License : Apache License 2.0
Project Creator : helicalinsight
License : Apache License 2.0
Project Creator : helicalinsight
public static boolean evaluateExpression(String expression) {
try {
logger.info("expression " + expression);
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(expression);
return exp.getValue(context, Boolean.clreplaced);
} catch (Exception exception) {
logger.error("Exception occurred", exception);
return false;
}
}
18
View Source File : SecurityExpressionEvaluator.java
License : Apache License 2.0
Project Creator : helicalinsight
License : Apache License 2.0
Project Creator : helicalinsight
public static JSONObject validateCondition(String expression) {
JSONObject response = new JSONObject();
try {
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression(expression);
exp.getValue(context, Boolean.clreplaced);
response.put("condition", "Test Success");
response.put("result", true);
} catch (Exception exception) {
response.put("result", false);
String message = exception.getMessage();
response.put("errorMessage", message == null ? " Unknown. Please check the logs" : message);
}
return response;
}
18
View Source File : NameSpelExpressionProcessor.java
License : MIT License
Project Creator : Hccake
License : MIT License
Project Creator : Hccake
@Override
public String doDetermineName(Object[] args, Method method, String key) {
if (!key.contains("#")) {
return key;
}
EvaluationContext context = new MethodBasedEvaluationContext(null, method, args, NAME_DISCOVERER);
final Object value = PARSER.parseExpression(key).getValue(context);
return value == null ? null : value.toString();
}
18
View Source File : RCacheAspect.java
License : MIT License
Project Creator : gnanquanmama
License : MIT License
Project Creator : gnanquanmama
/**
* 解析 spel 表达式
*
* @param method 方法
* @param arguments 参数
* @param spel 表达式
* @param clazz 返回结果的类型
* @param defaultResult 默认结果
* @return 执行spel表达式后的结果
*/
private <T> T parseSpel(Method method, Object[] arguments, String spel, Clreplaced<T> clazz, T defaultResult) {
String[] params = discoverer.getParameterNames(method);
EvaluationContext context = new StandardEvaluationContext();
for (int len = 0; len < params.length; len++) {
context.setVariable(params[len], arguments[len]);
}
try {
Expression expression = parser.parseExpression(spel);
return expression.getValue(context, clazz);
} catch (Exception e) {
return defaultResult;
}
}
18
View Source File : LimitAop.java
License : Apache License 2.0
Project Creator : forezp
License : Apache License 2.0
Project Creator : forezp
/**
* 该方法只适用cglib
*
* @param key
* @param method
* @param args
* @return
*/
private String parseKey(String key, Method method, Object[] args) {
// 如果不含# 则判断不为spel表达式,这有一定的误差,但是够用
// fixme
// TODO
if (!key.contains("#") || StringUtils.isEmpty(key)) {
return key;
}
// 获取被拦截方法参数名列表(使用Spring支持类库)
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
String[] paraNameArr = u.getParameterNames(method);
// 使用SPEL进行key的解析
ExpressionParser parser = new SpelExpressionParser();
// SPEL上下文
StandardEvaluationContext context = new StandardEvaluationContext();
// 把方法参数放入SPEL上下文中
for (int i = 0; i < paraNameArr.length; i++) {
context.setVariable(paraNameArr[i], args[i]);
}
return parser.parseExpression(key).getValue(context, String.clreplaced);
}
18
View Source File : ReactiveSpringElCheckUtil.java
License : Apache License 2.0
Project Creator : eacdy
License : Apache License 2.0
Project Creator : eacdy
/**
* 校验expression是否能通过rootObject的检测
*
* @param context 上下文
* @param expression 表达式
* @return 是否通过
*/
@SuppressWarnings("ALL")
public static Mono<Boolean> check(EvaluationContext context, String expression) {
return PARSER.parseExpression(expression).getValue(context, Mono.clreplaced);
}
18
View Source File : SpelCalculator.java
License : MIT License
Project Creator : aoju
License : MIT License
Project Creator : aoju
public static Object calcSpelWithNoContext(String spel, Object defaultValue) {
if (Strings.isNullOrEmpty(spel)) {
return defaultValue;
}
return parser.parseExpression(spel).getValue(defaultValue);
}
18
View Source File : DistributedLockAspect.java
License : Apache License 2.0
Project Creator : 2227324689
License : Apache License 2.0
Project Creator : 2227324689
/**
* 获取缓存的key
* key 定义在注解上,支持SPEL表达式
*/
private String parseKey(String key, Method method, Object[] args) {
// 获取被拦截方法参数名列表(使用Spring支持类库)
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
String[] paraNameArr = u.getParameterNames(method);
// 使用SPEL进行key的解析
ExpressionParser parser = new SpelExpressionParser();
// SPEL上下文
StandardEvaluationContext context = new StandardEvaluationContext();
// 把方法参数放入SPEL上下文中
for (int i = 0; i < paraNameArr.length; i++) {
context.setVariable(paraNameArr[i], args[i]);
}
return parser.parseExpression(key).getValue(context, String.clreplaced);
}
17
View Source File : GenericScope.java
License : Apache License 2.0
Project Creator : zouzhirong
License : Apache License 2.0
Project Creator : zouzhirong
private Expression parseExpression(String input) {
if (StringUtils.hasText(input)) {
ExpressionParser parser = new SpelExpressionParser();
try {
return parser.parseExpression(input);
} catch (ParseException e) {
throw new IllegalArgumentException("Cannot parse expression: " + input, e);
}
} else {
return null;
}
}
17
View Source File : SimpleElParser.java
License : MIT License
Project Creator : yizzuide
License : MIT License
Project Creator : yizzuide
public static <T> T parse(String expression, Object root, Clreplaced<T> resultType) {
Expression expressionWrapper = EL_PARSER.parseExpression(expression);
if (root == null) {
return expressionWrapper.getValue(resultType);
}
return expressionWrapper.getValue(root, resultType);
}
17
View Source File : DefaultBusinessKeyProvider.java
License : Apache License 2.0
Project Creator : yin5980280
License : Apache License 2.0
Project Creator : yin5980280
private List<String> getSpelDefinitionKey(String[] definitionKeys, Method method, Object[] parameterValues) {
List<String> definitionKeyList = new ArrayList<>();
for (String definitionKey : definitionKeys) {
if (definitionKey != null && !definitionKey.isEmpty()) {
EvaluationContext context = new MethodBasedEvaluationContext(null, method, parameterValues, nameDiscoverer);
Object key = parser.parseExpression(definitionKey).getValue(context);
definitionKeyList.add(key == null ? "" : key.toString());
}
}
return definitionKeyList;
}
17
View Source File : SecureCheckUtil.java
License : GNU Lesser General Public License v3.0
Project Creator : xkcoding
License : GNU Lesser General Public License v3.0
Project Creator : xkcoding
/**
* 检查 SPEL 表达式
*
* @param spel 表达式
* @param context 上下文
* @return {@code true} / {@code false}
*/
public static boolean checkExpression(String spel, StandardEvaluationContext context) {
Expression expression = PARSER.parseExpression(spel);
Boolean result = expression.getValue(context, Boolean.clreplaced);
return result != null ? result : false;
}
17
View Source File : SpringHeplerUtil.java
License : Apache License 2.0
Project Creator : x-ream
License : Apache License 2.0
Project Creator : x-ream
public static String parseSPEL(String condition, Method method, Object[] args) {
if (StringUtil.isNullOrEmpty(condition))
return "";
if (!condition.contains("#") || (args == null || args.length == 0))
return condition;
ExpressionParser parser = new SpelExpressionParser();
LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();
String[] names = discoverer.getParameterNames(method);
EvaluationContext ctx = new StandardEvaluationContext();
int length = names.length;
for (int i = 0; i < length; i++) {
ctx.setVariable(names[i], args[i]);
}
return parser.parseExpression(condition).getValue(ctx, String.clreplaced);
}
17
View Source File : SpringELUtil.java
License : MIT License
Project Creator : WeiYe-Jing
License : MIT License
Project Creator : WeiYe-Jing
public static void main(String[] args) {
// 定义变量
// 表达式的上下文,
EvaluationContext context = new StandardEvaluationContext();
String date = "2019-11-22";
// 为了让表达式可以访问该对象, 先把对象放到上下文中
context.setVariable("today", new Date());
ExpressionParser parser = new SpelExpressionParser();
// Tom , 使用变量
Date a = parser.parseExpression("#today").getValue(context, Date.clreplaced);
System.out.println(a);
}
17
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
// For now I am making #this not replacedignable
@Test
public void increment01root() {
Integer i = 42;
StandardEvaluationContext ctx = new StandardEvaluationContext(i);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("#this++");
replacedertEquals(42, i.intValue());
try {
e.getValue(ctx, Integer.clreplaced);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(SpelMessage.NOT_replacedIGNABLE, see.getMessageCode());
}
}
17
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void decrement01root() {
Integer i = 42;
StandardEvaluationContext ctx = new StandardEvaluationContext(i);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("#this--");
replacedertEquals(42, i.intValue());
try {
e.getValue(ctx, Integer.clreplaced);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(SpelMessage.NOT_replacedIGNABLE, see.getMessageCode());
}
}
17
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
private void expectFail(ExpressionParser parser, EvaluationContext eContext, String expressionString, SpelMessage messageCode) {
try {
Expression e = parser.parseExpression(expressionString);
SpelUtilities.printAbstractSyntaxTree(System.out, e);
e.getValue(eContext);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(messageCode, see.getMessageCode());
}
}
17
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void increment04() {
Integer i = 42;
StandardEvaluationContext ctx = new StandardEvaluationContext(i);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
try {
Expression e = parser.parseExpression("++1");
e.getValue(ctx, Integer.clreplaced);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(SpelMessage.NOT_replacedIGNABLE, see.getMessageCode());
}
try {
Expression e = parser.parseExpression("1++");
e.getValue(ctx, Integer.clreplaced);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(SpelMessage.NOT_replacedIGNABLE, see.getMessageCode());
}
}
17
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void decrement04() {
Integer i = 42;
StandardEvaluationContext ctx = new StandardEvaluationContext(i);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
try {
Expression e = parser.parseExpression("--1");
e.getValue(ctx, Integer.clreplaced);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(SpelMessage.NOT_replacedIGNABLE, see.getMessageCode());
}
try {
Expression e = parser.parseExpression("1--");
e.getValue(ctx, Integer.clreplaced);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(SpelMessage.NOT_replacedIGNABLE, see.getMessageCode());
}
}
17
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
/**
* This test is checking that with the changes for 9751 that the refactoring in Indexer is
* coping correctly for references beyond collection boundaries.
*/
@Test
public void collectionGrowingViaIndexer() {
Spr9751 instance = new Spr9751();
// Add a new element to the list
StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("listOfStrings[++index3]='def'");
e.getValue(ctx);
replacedertEquals(2, instance.listOfStrings.size());
replacedertEquals("def", instance.listOfStrings.get(1));
// Check reference beyond end of collection
ctx = new StandardEvaluationContext(instance);
parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
e = parser.parseExpression("listOfStrings[0]");
String value = e.getValue(ctx, String.clreplaced);
replacedertEquals("abc", value);
e = parser.parseExpression("listOfStrings[1]");
value = e.getValue(ctx, String.clreplaced);
replacedertEquals("def", value);
e = parser.parseExpression("listOfStrings[2]");
value = e.getValue(ctx, String.clreplaced);
replacedertEquals("", value);
// Now turn off growing and reference off the end
ctx = new StandardEvaluationContext(instance);
parser = new SpelExpressionParser(new SpelParserConfiguration(false, false));
e = parser.parseExpression("listOfStrings[3]");
try {
e.getValue(ctx, String.clreplaced);
fail();
} catch (SpelEvaluationException see) {
replacedertEquals(SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, see.getMessageCode());
}
}
17
View Source File : EvaluationTests.java
License : MIT License
Project Creator : Vip-Augus
License : MIT License
Project Creator : Vip-Augus
@Test
public void testCreateListsOnAttemptToIndexNull01() throws EvaluationException, ParseException {
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("list[0]");
TestClreplaced testClreplaced = new TestClreplaced();
Object o = e.getValue(new StandardEvaluationContext(testClreplaced));
replacedertEquals("", o);
o = parser.parseExpression("list[3]").getValue(new StandardEvaluationContext(testClreplaced));
replacedertEquals("", o);
replacedertEquals(4, testClreplaced.list.size());
try {
o = parser.parseExpression("list2[3]").getValue(new StandardEvaluationContext(testClreplaced));
fail();
} catch (EvaluationException ee) {
ee.printStackTrace();
// success!
}
o = parser.parseExpression("foo[3]").getValue(new StandardEvaluationContext(testClreplaced));
replacedertEquals("", o);
replacedertEquals(4, testClreplaced.getFoo().size());
}
See More Examples