未验证 提交 47dc61c0 编写于 作者: R Rubén Barroso 提交者: GitHub

Add new custom juel function: list (#3783)

上级 81151c75
......@@ -16,6 +16,7 @@
package org.activiti.core.el;
import static org.activiti.core.el.DateResolverHelper.addDateFunctions;
import static org.activiti.core.el.ListResolverHelper.addListFunctions;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
......@@ -54,13 +55,14 @@ public class ELContextBuilder {
return new ActivitiElContext(elResolver);
}
public ELContext buildWithDateFunctions() {
public ELContext buildWithCustomFunctions() {
CompositeELResolver elResolver = createCompositeResolver();
ActivitiElContext elContext = new ActivitiElContext(elResolver);
try {
addDateFunctions(elContext);
addListFunctions(elContext);
} catch (NoSuchMethodException e) {
logger.error("Error setting up EL date functions", e);
logger.error("Error setting up EL custom functions", e);
}
return elContext;
}
......
......@@ -58,6 +58,6 @@ public class JuelExpressionResolver implements ExpressionResolver {
beanResolver()
)
.withVariables(variables)
.buildWithDateFunctions();
.buildWithCustomFunctions();
}
}
/*
* Copyright 2010-2020 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.activiti.core.el;
import java.util.List;
public class ListResolverHelper {
private static final String LIST_FUNCTION_NAME = "list";
private static final String LIST_INVOKE_METHOD = "list";
public static List<Object> list(Object... objects) {
return List.of(objects);
}
private ListResolverHelper() {
}
public static void addListFunctions(ActivitiElContext elContext) throws NoSuchMethodException {
elContext.setFunction("", LIST_FUNCTION_NAME, ListResolverHelper.class.getMethod(LIST_INVOKE_METHOD, Object[].class));
}
}
......@@ -21,6 +21,7 @@ import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.el.ELException;
import javax.el.PropertyNotFoundException;
......@@ -121,4 +122,17 @@ public class JuelResolverTest {
.isThrownBy(() -> expressionResolver.resolveExpression(expressionString, Collections.emptyMap(), Date.class))
.withMessage("Could not resolve function 'current'");
}
@Test
public void should_returnList_when_expressionIsListFunction() {
//given
String expressionString = "${list(1,'item',3)}";
ExpressionResolver expressionResolver = new JuelExpressionResolver();
//when
List result = expressionResolver.resolveExpression(expressionString, Collections.emptyMap(), List.class);
//then
assertThat(result).contains(1l, "item", 3l);
}
}
......@@ -108,7 +108,7 @@ public class ExpressionManager {
}
protected ActivitiElContext createElContext(VariableScope variableScope) {
return (ActivitiElContext) new ELContextBuilder().withResolvers(createElResolver(variableScope)).buildWithDateFunctions();
return (ActivitiElContext) new ELContextBuilder().withResolvers(createElResolver(variableScope)).buildWithCustomFunctions();
}
protected ELResolver createElResolver(VariableScope variableScope) {
......@@ -150,6 +150,6 @@ public class ExpressionManager {
public ELContext getElContext(Map<String, Object> availableVariables) {
CompositeELResolver elResolver = new CompositeELResolver();
addBaseResolvers(elResolver);
return new ELContextBuilder().withResolvers(elResolver).withVariables(availableVariables).buildWithDateFunctions();
return new ELContextBuilder().withResolvers(elResolver).withVariables(availableVariables).buildWithCustomFunctions();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册