use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR13918.
@Test
public void SPR13918() {
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("encoding", "UTF-8");
Expression ex = parser.parseExpression("T(java.nio.charset.Charset).forName(#encoding)");
Object result = ex.getValue(context);
assertEquals(StandardCharsets.UTF_8, result);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method varargsAndPrimitives_SPR8174.
@Test
public void varargsAndPrimitives_SPR8174() throws Exception {
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.forObject(34L));
ReflectionUtil<Integer> ru = new ReflectionUtil<>();
MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args);
args.set(0, TypeDescriptor.forObject(23));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 45);
args.set(0, TypeDescriptor.forObject(23f));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 45f);
args.set(0, TypeDescriptor.forObject(23d));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 23d);
args.set(0, TypeDescriptor.forObject((short) 23));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, (short) 23);
args.set(0, TypeDescriptor.forObject(23L));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, 23L);
args.set(0, TypeDescriptor.forObject((char) 65));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, (char) 65);
args.set(0, TypeDescriptor.forObject((byte) 23));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, (byte) 23);
args.set(0, TypeDescriptor.forObject(true));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
me.execute(emptyEvalContext, ru, true);
// trickier:
args.set(0, TypeDescriptor.forObject(12));
args.add(TypeDescriptor.forObject(23f));
me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args);
me.execute(emptyEvalContext, ru, 12, 23f);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method wideningPrimitiveConversion_8224.
/**
* Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
* method accepting 'long' is ok.
*/
@Test
public void wideningPrimitiveConversion_8224() throws Exception {
class WideningPrimitiveConversion {
public int getX(long i) {
return 10;
}
}
final Integer INTEGER_VALUE = Integer.valueOf(7);
WideningPrimitiveConversion target = new WideningPrimitiveConversion();
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.forObject(INTEGER_VALUE));
MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue();
final int compiler = target.getX(INTEGER_VALUE);
assertEquals(compiler, actual);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelCompilationCoverageTests method variableReference_userDefined.
@Test
public void variableReference_userDefined() throws Exception {
EvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable("target", "abc");
expression = parser.parseExpression("#target");
assertEquals("abc", expression.getValue(ctx));
assertCanCompile(expression);
assertEquals("abc", expression.getValue(ctx));
ctx.setVariable("target", "123");
assertEquals("123", expression.getValue(ctx));
ctx.setVariable("target", 42);
try {
assertEquals(42, expression.getValue(ctx));
fail();
} catch (SpelEvaluationException see) {
assertTrue(see.getCause() instanceof ClassCastException);
}
ctx.setVariable("target", "abc");
expression = parser.parseExpression("#target.charAt(0)");
assertEquals('a', expression.getValue(ctx));
assertCanCompile(expression);
assertEquals('a', expression.getValue(ctx));
ctx.setVariable("target", "1");
assertEquals('1', expression.getValue(ctx));
ctx.setVariable("target", 42);
try {
assertEquals('4', expression.getValue(ctx));
fail();
} catch (SpelEvaluationException see) {
assertTrue(see.getCause() instanceof ClassCastException);
}
}
use of org.springframework.expression.EvaluationContext in project uPortal by Jasig.
the class PortletSpELServiceImpl method getValue.
@Override
public <T> T getValue(String expressionString, PortletRequest request, Class<T> desiredResultType) {
final Expression expression = parseExpression(expressionString);
final EvaluationContext evaluationContext = getEvaluationContext(request);
return expression.getValue(evaluationContext, desiredResultType);
}
Aggregations