Search in sources :

Example 16 with EvaluationException

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;
}
Also used : Expression(org.springframework.expression.Expression) ParseException(org.springframework.expression.ParseException) EvaluationException(org.springframework.expression.EvaluationException)

Example 17 with EvaluationException

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;
}
Also used : ValidationException(org.ow2.proactive.scheduler.common.job.factories.spi.model.exceptions.ValidationException) EvaluationException(org.springframework.expression.EvaluationException)

Example 18 with EvaluationException

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 + "'");
}
Also used : Field(java.lang.reflect.Field) AccessException(org.springframework.expression.AccessException) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) EvaluationException(org.springframework.expression.EvaluationException) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member) EvaluationException(org.springframework.expression.EvaluationException) AccessException(org.springframework.expression.AccessException)

Example 19 with EvaluationException

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));
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) StandardTypeLocator(org.springframework.expression.spel.support.StandardTypeLocator) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 20 with EvaluationException

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
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) CompositeStringExpression(org.springframework.expression.common.CompositeStringExpression) Expression(org.springframework.expression.Expression) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) EvaluationContext(org.springframework.expression.EvaluationContext) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Aggregations

EvaluationException (org.springframework.expression.EvaluationException)26 Expression (org.springframework.expression.Expression)16 Test (org.junit.Test)14 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)11 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)10 ArrayList (java.util.ArrayList)5 AccessException (org.springframework.expression.AccessException)5 ParseException (org.springframework.expression.ParseException)5 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)4 SpelEvaluationException (org.springframework.expression.spel.SpelEvaluationException)4 MethodParameter (org.springframework.core.MethodParameter)3 TypeConverter (org.springframework.expression.TypeConverter)3 SpelExpression (org.springframework.expression.spel.standard.SpelExpression)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 PropertyAccessor (org.springframework.expression.PropertyAccessor)2 CompilablePropertyAccessor (org.springframework.expression.spel.CompilablePropertyAccessor)2 ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)2 StandardTypeLocator (org.springframework.expression.spel.support.StandardTypeLocator)2 Constructor (java.lang.reflect.Constructor)1