Search in sources :

Example 1 with ReflectivePropertyAccessor

use of org.springframework.expression.spel.support.ReflectivePropertyAccessor in project spring-framework by spring-projects.

the class PropertyOrFieldReference method readProperty.

/**
	 * Attempt to read the named property from the current context object.
	 * @return the value of the property
	 * @throws EvaluationException if any problem accessing the property or it cannot be found
	 */
private TypedValue readProperty(TypedValue contextObject, EvaluationContext evalContext, String name) throws EvaluationException {
    Object targetObject = contextObject.getValue();
    if (targetObject == null && this.nullSafe) {
        return TypedValue.NULL;
    }
    PropertyAccessor accessorToUse = this.cachedReadAccessor;
    if (accessorToUse != null) {
        try {
            return accessorToUse.read(evalContext, contextObject.getValue(), name);
        } catch (Exception ex) {
            // This is OK - it may have gone stale due to a class change,
            // let's try to get a new one and call it before giving up...
            this.cachedReadAccessor = null;
        }
    }
    List<PropertyAccessor> accessorsToTry = getPropertyAccessorsToTry(contextObject.getValue(), evalContext.getPropertyAccessors());
    // then ask them to read it
    if (accessorsToTry != null) {
        try {
            for (PropertyAccessor accessor : accessorsToTry) {
                if (accessor.canRead(evalContext, contextObject.getValue(), name)) {
                    if (accessor instanceof ReflectivePropertyAccessor) {
                        accessor = ((ReflectivePropertyAccessor) accessor).createOptimalAccessor(evalContext, contextObject.getValue(), name);
                    }
                    this.cachedReadAccessor = accessor;
                    return accessor.read(evalContext, contextObject.getValue(), name);
                }
            }
        } catch (Exception ex) {
            throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_DURING_PROPERTY_READ, name, ex.getMessage());
        }
    }
    if (contextObject.getValue() == null) {
        throw new SpelEvaluationException(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE_ON_NULL, name);
    } else {
        throw new SpelEvaluationException(getStartPosition(), SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE, name, FormatHelper.formatClassNameForMessage(getObjectClass(contextObject.getValue())));
    }
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) CompilablePropertyAccessor(org.springframework.expression.spel.CompilablePropertyAccessor) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) EvaluationException(org.springframework.expression.EvaluationException) AccessException(org.springframework.expression.AccessException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ReflectivePropertyAccessor

use of org.springframework.expression.spel.support.ReflectivePropertyAccessor in project spring-framework by spring-projects.

the class SpelReproTests method accessingNullPropertyViaReflection_SPR5663.

@Test
public void accessingNullPropertyViaReflection_SPR5663() throws AccessException {
    PropertyAccessor propertyAccessor = new ReflectivePropertyAccessor();
    EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
    assertFalse(propertyAccessor.canRead(context, null, "abc"));
    assertFalse(propertyAccessor.canWrite(context, null, "abc"));
    try {
        propertyAccessor.read(context, null, "abc");
        fail("Should have failed with an AccessException");
    } catch (AccessException ae) {
    // success
    }
    try {
        propertyAccessor.write(context, null, "abc", "foo");
        fail("Should have failed with an AccessException");
    } catch (AccessException ae) {
    // success
    }
}
Also used : ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) PropertyAccessor(org.springframework.expression.PropertyAccessor) AccessException(org.springframework.expression.AccessException) EvaluationContext(org.springframework.expression.EvaluationContext) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) Test(org.junit.Test)

Example 3 with ReflectivePropertyAccessor

use of org.springframework.expression.spel.support.ReflectivePropertyAccessor in project spring-framework by spring-projects.

the class SpelReproTests method SPR10162_onlyBridgeMethod.

@Test
void SPR10162_onlyBridgeMethod() throws Exception {
    ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
    StandardEvaluationContext context = new StandardEvaluationContext();
    Object target = new OnlyBridgeMethod();
    TypedValue value = accessor.read(context, target, "property");
    assertThat(value.getValue()).isNull();
    assertThat(value.getTypeDescriptor().getType()).isEqualTo(Integer.class);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) TypedValue(org.springframework.expression.TypedValue) Test(org.junit.jupiter.api.Test)

Example 4 with ReflectivePropertyAccessor

use of org.springframework.expression.spel.support.ReflectivePropertyAccessor in project cas by apereo.

the class AbstractCasWebflowConfigurer method getSpringExpressionParser.

/**
     * Gets spring expression parser.
     *
     * @return the spring expression parser
     */
protected SpringELExpressionParser getSpringExpressionParser() {
    final SpelParserConfiguration configuration = new SpelParserConfiguration();
    final SpelExpressionParser spelExpressionParser = new SpelExpressionParser(configuration);
    final SpringELExpressionParser parser = new SpringELExpressionParser(spelExpressionParser, this.flowBuilderServices.getConversionService());
    parser.addPropertyAccessor(new ActionPropertyAccessor());
    parser.addPropertyAccessor(new BeanFactoryPropertyAccessor());
    parser.addPropertyAccessor(new FlowVariablePropertyAccessor());
    parser.addPropertyAccessor(new MapAdaptablePropertyAccessor());
    parser.addPropertyAccessor(new MessageSourcePropertyAccessor());
    parser.addPropertyAccessor(new ScopeSearchingPropertyAccessor());
    parser.addPropertyAccessor(new BeanExpressionContextAccessor());
    parser.addPropertyAccessor(new MapAccessor());
    parser.addPropertyAccessor(new MapAdaptablePropertyAccessor());
    parser.addPropertyAccessor(new EnvironmentAccessor());
    parser.addPropertyAccessor(new ReflectivePropertyAccessor());
    return parser;
}
Also used : ActionPropertyAccessor(org.springframework.webflow.expression.spel.ActionPropertyAccessor) MessageSourcePropertyAccessor(org.springframework.webflow.expression.spel.MessageSourcePropertyAccessor) BeanFactoryPropertyAccessor(org.springframework.webflow.expression.spel.BeanFactoryPropertyAccessor) ScopeSearchingPropertyAccessor(org.springframework.webflow.expression.spel.ScopeSearchingPropertyAccessor) FlowVariablePropertyAccessor(org.springframework.webflow.expression.spel.FlowVariablePropertyAccessor) SpringELExpressionParser(org.springframework.binding.expression.spel.SpringELExpressionParser) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor) BeanExpressionContextAccessor(org.springframework.context.expression.BeanExpressionContextAccessor) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) MapAccessor(org.springframework.context.expression.MapAccessor) EnvironmentAccessor(org.springframework.context.expression.EnvironmentAccessor) MapAdaptablePropertyAccessor(org.springframework.webflow.expression.spel.MapAdaptablePropertyAccessor) SpelParserConfiguration(org.springframework.expression.spel.SpelParserConfiguration)

Example 5 with ReflectivePropertyAccessor

use of org.springframework.expression.spel.support.ReflectivePropertyAccessor in project spring-data-commons by spring-projects.

the class ExtensionAwareEvaluationContextProvider method getEvaluationContext.

/* (non-Javadoc)
	 * @see org.springframework.data.jpa.repository.support.EvaluationContextProvider#getEvaluationContext()
	 */
@Override
public <T extends Parameters<?, ?>> StandardEvaluationContext getEvaluationContext(T parameters, Object[] parameterValues) {
    StandardEvaluationContext ec = new StandardEvaluationContext();
    beanFactory.ifPresent(it -> ec.setBeanResolver(new BeanFactoryResolver(it)));
    ExtensionAwarePropertyAccessor accessor = new ExtensionAwarePropertyAccessor(extensions.get());
    ec.addPropertyAccessor(accessor);
    ec.addPropertyAccessor(new ReflectivePropertyAccessor());
    ec.addMethodResolver(accessor);
    // Add parameters for indexed access
    ec.setRootObject(parameterValues);
    ec.setVariables(collectVariables(parameters, parameterValues));
    return ec;
}
Also used : BeanFactoryResolver(org.springframework.context.expression.BeanFactoryResolver) StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) ReflectivePropertyAccessor(org.springframework.expression.spel.support.ReflectivePropertyAccessor)

Aggregations

ReflectivePropertyAccessor (org.springframework.expression.spel.support.ReflectivePropertyAccessor)8 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)5 Test (org.junit.jupiter.api.Test)3 PropertyAccessor (org.springframework.expression.PropertyAccessor)3 SpringELExpressionParser (org.springframework.binding.expression.spel.SpringELExpressionParser)2 BeanExpressionContextAccessor (org.springframework.context.expression.BeanExpressionContextAccessor)2 EnvironmentAccessor (org.springframework.context.expression.EnvironmentAccessor)2 MapAccessor (org.springframework.context.expression.MapAccessor)2 AccessException (org.springframework.expression.AccessException)2 EvaluationContext (org.springframework.expression.EvaluationContext)2 TypedValue (org.springframework.expression.TypedValue)2 SpelParserConfiguration (org.springframework.expression.spel.SpelParserConfiguration)2 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)2 ActionPropertyAccessor (org.springframework.webflow.expression.spel.ActionPropertyAccessor)2 BeanFactoryPropertyAccessor (org.springframework.webflow.expression.spel.BeanFactoryPropertyAccessor)2 FlowVariablePropertyAccessor (org.springframework.webflow.expression.spel.FlowVariablePropertyAccessor)2 MapAdaptablePropertyAccessor (org.springframework.webflow.expression.spel.MapAdaptablePropertyAccessor)2 MessageSourcePropertyAccessor (org.springframework.webflow.expression.spel.MessageSourcePropertyAccessor)2 ScopeSearchingPropertyAccessor (org.springframework.webflow.expression.spel.ScopeSearchingPropertyAccessor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1