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())));
}
}
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
}
}
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);
}
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;
}
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;
}
Aggregations