Browse Source

PropertyOrFieldReference checks cached PropertyAccessor against current EvaluationContext

Issue: SPR-15769
pull/1477/head
Juergen Hoeller 7 years ago
parent
commit
bcf9f21ecc
  1. 34
      spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java
  2. 55
      spring-expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java

34
spring-expression/src/main/java/org/springframework/expression/spel/ast/PropertyOrFieldReference.java

@ -172,14 +172,16 @@ public class PropertyOrFieldReference extends SpelNodeImpl { @@ -172,14 +172,16 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
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;
if (evalContext.getPropertyAccessors().contains(accessorToUse)) {
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 =
@ -225,15 +227,17 @@ public class PropertyOrFieldReference extends SpelNodeImpl { @@ -225,15 +227,17 @@ public class PropertyOrFieldReference extends SpelNodeImpl {
PropertyAccessor accessorToUse = this.cachedWriteAccessor;
if (accessorToUse != null) {
try {
accessorToUse.write(evalContext, contextObject.getValue(), name, newValue);
return;
}
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.cachedWriteAccessor = null;
if (evalContext.getPropertyAccessors().contains(accessorToUse)) {
try {
accessorToUse.write(evalContext, contextObject.getValue(), name, newValue);
return;
}
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.cachedWriteAccessor = null;
}
List<PropertyAccessor> accessorsToTry =

55
spring-expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,7 +17,9 @@ @@ -17,7 +17,9 @@
package org.springframework.expression.spel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Test;
@ -34,12 +36,11 @@ import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -34,12 +36,11 @@ import org.springframework.expression.spel.support.StandardEvaluationContext;
import static org.junit.Assert.*;
///CLOVER:OFF
/**
* Tests accessing of properties.
*
* @author Andy Clement
* @author Juergen Hoeller
*/
public class PropertyAccessTests extends AbstractExpressionTests {
@ -167,6 +168,20 @@ public class PropertyAccessTests extends AbstractExpressionTests { @@ -167,6 +168,20 @@ public class PropertyAccessTests extends AbstractExpressionTests {
assertEquals(value, "java.lang.String");
}
@Test
public void shouldAlwaysUsePropertyAccessorFromEvaluationContext() {
SpelExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression("name");
StandardEvaluationContext context = new StandardEvaluationContext();
context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Ollie")));
assertEquals("Ollie", expression.getValue(context));
context = new StandardEvaluationContext();
context.addPropertyAccessor(new ConfigurablePropertyAccessor(Collections.singletonMap("name", "Jens")));
assertEquals("Jens", expression.getValue(context));
}
// This can resolve the property 'flibbles' on any String (very useful...)
private static class StringyPropertyAccessor implements PropertyAccessor {
@ -216,4 +231,38 @@ public class PropertyAccessTests extends AbstractExpressionTests { @@ -216,4 +231,38 @@ public class PropertyAccessTests extends AbstractExpressionTests {
}
}
private static class ConfigurablePropertyAccessor implements PropertyAccessor {
private final Map<String, Object> values;
public ConfigurablePropertyAccessor(Map<String, Object> values) {
this.values = values;
}
@Override
public Class<?>[] getSpecificTargetClasses() {
return null;
}
@Override
public boolean canRead(EvaluationContext context, Object target, String name) {
return true;
}
@Override
public TypedValue read(EvaluationContext context, Object target, String name) {
return new TypedValue(this.values.get(name));
}
@Override
public boolean canWrite(EvaluationContext context, Object target, String name) {
return false;
}
@Override
public void write(EvaluationContext context, Object target, String name, Object newValue) {
}
}
}

Loading…
Cancel
Save