use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class MethodReference method getValueInternal.
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
EvaluationContext evaluationContext = state.getEvaluationContext();
Object value = state.getActiveContextObject().getValue();
TypeDescriptor targetType = state.getActiveContextObject().getTypeDescriptor();
Object[] arguments = getArguments(state);
TypedValue result = getValueInternal(evaluationContext, value, targetType, arguments);
updateExitTypeDescriptor();
return result;
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR10417_maps.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void SPR10417_maps() {
Map map1 = new HashMap();
map1.put("A", 65);
map1.put("B", 66);
map1.put("X", 66);
Map map2 = new HashMap();
map2.put("X", 66);
EvaluationContext context = new StandardEvaluationContext();
context.setVariable("map1", map1);
context.setVariable("map2", map2);
// #this should be the element from list1
Expression ex = parser.parseExpression("#map1.?[#map2.containsKey(#this.getKey())]");
Object result = ex.getValue(context);
assertEquals("{X=66}", result.toString());
ex = parser.parseExpression("#map1.?[#map2.containsKey(key)]");
result = ex.getValue(context);
assertEquals("{X=66}", result.toString());
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method conversionPriority_8224.
/**
* Test whether {@link ReflectiveMethodResolver} follows Java Method Invocation
* Conversion order. And more precisely that widening reference conversion is 'higher'
* than a unboxing conversion.
*/
@Test
public void conversionPriority_8224() throws Exception {
@SuppressWarnings("unused")
class ConversionPriority1 {
public int getX(Number i) {
return 20;
}
public int getX(int i) {
return 10;
}
}
@SuppressWarnings("unused")
class ConversionPriority2 {
public int getX(int i) {
return 10;
}
public int getX(Number i) {
return 20;
}
}
final Integer INTEGER = Integer.valueOf(7);
EvaluationContext emptyEvalContext = new StandardEvaluationContext();
List<TypeDescriptor> args = new ArrayList<>();
args.add(TypeDescriptor.forObject(new Integer(42)));
ConversionPriority1 target = new ConversionPriority1();
MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
// MethodInvoker chooses getX(int i) when passing Integer
final int actual = (Integer) me.execute(emptyEvalContext, target, new Integer(42)).getValue();
// Compiler chooses getX(Number i) when passing Integer
final int compiler = target.getX(INTEGER);
// Fails!
assertEquals(compiler, actual);
ConversionPriority2 target2 = new ConversionPriority2();
MethodExecutor me2 = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target2, "getX", args);
// MethodInvoker chooses getX(int i) when passing Integer
int actual2 = (Integer) me2.execute(emptyEvalContext, target2, new Integer(42)).getValue();
// Compiler chooses getX(Number i) when passing Integer
int compiler2 = target2.getX(INTEGER);
// Fails!
assertEquals(compiler2, actual2);
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class TemplateExpressionParsingTests method testCompositeStringExpression.
@Test
public void testCompositeStringExpression() throws Exception {
SpelExpressionParser parser = new SpelExpressionParser();
Expression ex = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
checkString("hello world", ex.getValue());
checkString("hello world", ex.getValue(String.class));
checkString("hello world", ex.getValue((Object) null, String.class));
checkString("hello world", ex.getValue(new Rooty()));
checkString("hello world", ex.getValue(new Rooty(), String.class));
EvaluationContext ctx = new StandardEvaluationContext();
checkString("hello world", ex.getValue(ctx));
checkString("hello world", ex.getValue(ctx, String.class));
checkString("hello world", ex.getValue(ctx, null, String.class));
checkString("hello world", ex.getValue(ctx, new Rooty()));
checkString("hello world", ex.getValue(ctx, new Rooty(), String.class));
checkString("hello world", ex.getValue(ctx, new Rooty(), String.class));
assertEquals("hello ${'world'}", ex.getExpressionString());
assertFalse(ex.isWritable(new StandardEvaluationContext()));
assertFalse(ex.isWritable(new Rooty()));
assertFalse(ex.isWritable(new StandardEvaluationContext(), new Rooty()));
assertEquals(String.class, ex.getValueType());
assertEquals(String.class, ex.getValueType(ctx));
assertEquals(String.class, ex.getValueTypeDescriptor().getType());
assertEquals(String.class, ex.getValueTypeDescriptor(ctx).getType());
assertEquals(String.class, ex.getValueType(new Rooty()));
assertEquals(String.class, ex.getValueType(ctx, new Rooty()));
assertEquals(String.class, ex.getValueTypeDescriptor(new Rooty()).getType());
assertEquals(String.class, ex.getValueTypeDescriptor(ctx, new Rooty()).getType());
try {
ex.setValue(ctx, null);
fail();
} catch (EvaluationException ee) {
// success
}
try {
ex.setValue((Object) null, null);
fail();
} catch (EvaluationException ee) {
// success
}
try {
ex.setValue(ctx, null, null);
fail();
} catch (EvaluationException ee) {
// success
}
}
use of org.springframework.expression.EvaluationContext in project spring-framework by spring-projects.
the class SpelReproTests method SPR9735.
@Test
public void SPR9735() {
Item item = new Item();
item.setName("parent");
Item item1 = new Item();
item1.setName("child1");
Item item2 = new Item();
item2.setName("child2");
item.add(item1);
item.add(item2);
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = new StandardEvaluationContext();
Expression exp = parser.parseExpression("#item[0].name");
context.setVariable("item", item);
assertEquals("child1", exp.getValue(context));
}
Aggregations