Search in sources :

Example 21 with EvaluationException

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"));
    }
}
Also used : SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) Expression(org.springframework.expression.Expression) EvaluationException(org.springframework.expression.EvaluationException) Test(org.junit.Test)

Example 22 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 23 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 24 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)

Example 25 with EvaluationException

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

Aggregations

EvaluationException (org.springframework.expression.EvaluationException)25 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