use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class IndexingTests method indexIntoGenericPropertyContainingNullList.
@Test
public void indexIntoGenericPropertyContainingNullList() {
SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property");
assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.lang.Object", expression.getValueTypeDescriptor(this).toString());
assertEquals(property, expression.getValue(this));
expression = parser.parseExpression("property[0]");
try {
assertEquals("bar", expression.getValue(this));
} catch (EvaluationException ex) {
assertTrue(ex.getMessage().startsWith("EL1027E"));
}
}
use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class ReflectivePropertyAccessor method write.
@Override
public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException {
if (target == null) {
throw new AccessException("Cannot write property on null target");
}
Class<?> type = (target instanceof Class ? (Class<?>) target : target.getClass());
Object possiblyConvertedNewValue = newValue;
TypeDescriptor typeDescriptor = getTypeDescriptor(context, target, name);
if (typeDescriptor != null) {
try {
possiblyConvertedNewValue = context.getTypeConverter().convertValue(newValue, TypeDescriptor.forObject(newValue), typeDescriptor);
} catch (EvaluationException evaluationException) {
throw new AccessException("Type conversion failure", evaluationException);
}
}
PropertyCacheKey cacheKey = new PropertyCacheKey(type, name, target instanceof Class);
Member cachedMember = this.writerCache.get(cacheKey);
if (cachedMember == null || cachedMember instanceof Method) {
Method method = (Method) cachedMember;
if (method == null) {
method = findSetterForProperty(name, type, target);
if (method != null) {
cachedMember = method;
this.writerCache.put(cacheKey, cachedMember);
}
}
if (method != null) {
try {
ReflectionUtils.makeAccessible(method);
method.invoke(target, possiblyConvertedNewValue);
return;
} catch (Exception ex) {
throw new AccessException("Unable to access property '" + name + "' through setter method", ex);
}
}
}
if (cachedMember == null || cachedMember instanceof Field) {
Field field = (Field) cachedMember;
if (field == null) {
field = findField(name, type, target);
if (field != null) {
cachedMember = field;
this.writerCache.put(cacheKey, cachedMember);
}
}
if (field != null) {
try {
ReflectionUtils.makeAccessible(field);
field.set(target, possiblyConvertedNewValue);
return;
} catch (Exception ex) {
throw new AccessException("Unable to access field '" + name + "'", ex);
}
}
}
throw new AccessException("Neither setter method nor field found for property '" + name + "'");
}
use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class EvaluationTests method testResolvingList.
@Test
public void testResolvingList() throws Exception {
StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
try {
assertFalse(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class));
fail("should have failed to find List");
} catch (EvaluationException ee) {
// success - List not found
}
((StandardTypeLocator) context.getTypeLocator()).registerImport("java.util");
assertTrue(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class));
}
use of org.springframework.expression.EvaluationException 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.EvaluationException in project uPortal by Jasig.
the class StylesheetAttributeSourceForSpELVariablesTest method spELEvaluationForInputThrowsEvaluationException.
private void spELEvaluationForInputThrowsEvaluationException(final String input) {
final Expression expression = mock(Expression.class);
given(this.portalSpELService.parseExpression(input)).willReturn(expression);
given(this.portalSpELService.getValue(eq(expression), any(WebRequest.class), eq(String.class))).willThrow(new EvaluationException(input));
}
Aggregations