use of cn.taketoday.expression.EvaluationException in project today-framework by TAKETODAY.
the class FunctionReference method executeFunctionJLRMethod.
/**
* Execute a function represented as a {@code java.lang.reflect.Method}.
*
* @param state the expression evaluation state
* @param method the method to invoke
* @return the return value of the invoked Java method
* @throws EvaluationException if there is any problem invoking the method
*/
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
Object[] functionArgs = getArguments(state);
if (!method.isVarArgs()) {
int declaredParamCount = method.getParameterCount();
if (declaredParamCount != functionArgs.length) {
throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION, functionArgs.length, declaredParamCount);
}
}
if (!Modifier.isStatic(method.getModifiers())) {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
}
// Convert arguments if necessary and remap them for varargs if required
TypeConverter converter = state.getEvaluationContext().getTypeConverter();
boolean argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
if (method.isVarArgs()) {
functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(method.getParameterTypes(), functionArgs);
}
boolean compilable = false;
try {
ReflectionUtils.makeAccessible(method);
Object result = method.invoke(method.getClass(), functionArgs);
compilable = !argumentConversionOccurred;
return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
} catch (Exception ex) {
throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL, this.name, ex.getMessage());
} finally {
if (compilable) {
this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
this.method = method;
} else {
this.exitTypeDescriptor = null;
this.method = null;
}
}
}
use of cn.taketoday.expression.EvaluationException in project today-framework by TAKETODAY.
the class IndexingTests method indexIntoGenericPropertyContainingNullList.
@Test
void indexIntoGenericPropertyContainingNullList() {
SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("@cn.taketoday.expression.spel.IndexingTests$FieldAnnotation java.lang.Object");
assertThat(expression.getValue(this)).isEqualTo(property);
expression = parser.parseExpression("property[0]");
try {
assertThat(expression.getValue(this)).isEqualTo("bar");
} catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1027E")).isTrue();
}
}
use of cn.taketoday.expression.EvaluationException in project today-framework by TAKETODAY.
the class IndexingTests method indexIntoGenericPropertyContainingGrowingList.
@Test
void indexIntoGenericPropertyContainingGrowingList() {
List<String> property = new ArrayList<>();
this.property = property;
SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("@cn.taketoday.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>");
assertThat(expression.getValue(this)).isEqualTo(property);
expression = parser.parseExpression("property[0]");
try {
assertThat(expression.getValue(this)).isEqualTo("bar");
} catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
}
}
use of cn.taketoday.expression.EvaluationException in project today-framework by TAKETODAY.
the class IndexingTests method indexIntoGenericPropertyContainingGrowingList2.
@Test
void indexIntoGenericPropertyContainingGrowingList2() {
List<String> property2 = new ArrayList<>();
this.property2 = property2;
SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property2");
assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("java.util.ArrayList<?>");
assertThat(expression.getValue(this)).isEqualTo(property2);
expression = parser.parseExpression("property2[0]");
try {
assertThat(expression.getValue(this)).isEqualTo("bar");
} catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
}
}
use of cn.taketoday.expression.EvaluationException in project today-framework by TAKETODAY.
the class IndexingTests method setGenericPropertyContainingListAutogrow.
@Test
void setGenericPropertyContainingListAutogrow() {
List<Integer> property = new ArrayList<>();
this.property = property;
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression expression = parser.parseExpression("property");
assertThat(expression.getValueTypeDescriptor(this).toString()).isEqualTo("@cn.taketoday.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>");
assertThat(expression.getValue(this)).isEqualTo(property);
expression = parser.parseExpression("property[0]");
try {
expression.setValue(this, "4");
} catch (EvaluationException ex) {
assertThat(ex.getMessage().startsWith("EL1053E")).isTrue();
}
}
Aggregations