diff --git a/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/ELContextBuilder.java b/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/ELContextBuilder.java index e97cef0ba2d05cd43d6d5bc1c0b6c0738e049f8d..663807284757299b8174843551303cc971d24d8a 100644 --- a/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/ELContextBuilder.java +++ b/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/ELContextBuilder.java @@ -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; } diff --git a/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/JuelExpressionResolver.java b/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/JuelExpressionResolver.java index fc1d58a7a83d9f5d7c31b77b2992ddb88f78acce..76a8601101ff5af843cc7054c032fa7fa033262f 100644 --- a/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/JuelExpressionResolver.java +++ b/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/JuelExpressionResolver.java @@ -58,6 +58,6 @@ public class JuelExpressionResolver implements ExpressionResolver { beanResolver() ) .withVariables(variables) - .buildWithDateFunctions(); + .buildWithCustomFunctions(); } } diff --git a/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/ListResolverHelper.java b/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/ListResolverHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..07d51eb15c1fef359204f22e16a151b0bab40941 --- /dev/null +++ b/activiti-core-common/activiti-expression-language/src/main/java/org/activiti/core/el/ListResolverHelper.java @@ -0,0 +1,35 @@ +/* + * 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 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)); + } +} diff --git a/activiti-core-common/activiti-expression-language/src/test/java/org/activiti/core/el/JuelResolverTest.java b/activiti-core-common/activiti-expression-language/src/test/java/org/activiti/core/el/JuelResolverTest.java index e7e915c6229358330bf8b82db61d595fe5f70405..43f56e7fe0b0d027ba079d5b35e956d58d49b056 100644 --- a/activiti-core-common/activiti-expression-language/src/test/java/org/activiti/core/el/JuelResolverTest.java +++ b/activiti-core-common/activiti-expression-language/src/test/java/org/activiti/core/el/JuelResolverTest.java @@ -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); + } } diff --git a/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/el/ExpressionManager.java b/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/el/ExpressionManager.java index 03c9ebe61036c46f19af7e4c6d1c07665638161b..b49d3663f65ea7bd77fe984bb616aa6d7f7e1c9b 100644 --- a/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/el/ExpressionManager.java +++ b/activiti-core/activiti-engine/src/main/java/org/activiti/engine/impl/el/ExpressionManager.java @@ -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 availableVariables) { CompositeELResolver elResolver = new CompositeELResolver(); addBaseResolvers(elResolver); - return new ELContextBuilder().withResolvers(elResolver).withVariables(availableVariables).buildWithDateFunctions(); + return new ELContextBuilder().withResolvers(elResolver).withVariables(availableVariables).buildWithCustomFunctions(); } }