use of org.springframework.expression.EvaluationException in project uPortal by Jasig.
the class StylesheetAttributeSource method doSpelEvaluationForAttributeValue.
protected String doSpelEvaluationForAttributeValue(final WebRequest request, final String attributeValue) {
String result;
try {
final String valueForEvaluation = this.getValueForSpelEvaluation(attributeValue);
final Expression expression = this.portalSpELService.parseExpression(valueForEvaluation);
result = this.portalSpELService.getValue(expression, request, String.class);
} catch (ParseException e) {
this.getLogger().info("SpEL parse exception parsing: {}; Exception: {}", attributeValue, e);
result = attributeValue;
} catch (EvaluationException e) {
this.getLogger().info("SpEL evaluation exception evaluating: {}; Exception: {}", attributeValue, e);
result = attributeValue;
}
return result;
}
use of org.springframework.expression.EvaluationException in project scheduling by ow2-proactive.
the class SpelValidator method validate.
@Override
public String validate(String parameterValue, ModelValidatorContext context) throws ValidationException {
try {
context.getSpELContext().setVariable("value", parameterValue);
Object untypedResult = spelExpression.getValue(context.getSpELContext());
if (!(untypedResult instanceof Boolean)) {
throw new ValidationException("SPEL expression did not return a boolean value.");
}
boolean evaluationResult = (Boolean) untypedResult;
if (!evaluationResult) {
throw new ValidationException("SPEL expression returned false, received " + parameterValue);
}
} catch (EvaluationException e) {
throw new ValidationException("SPEL expression raised an error: " + e.getMessage(), e);
}
return parameterValue;
}
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
}
}
Aggregations