Merge pull request #206 from daveboden/SPR-10122
* SPR-10122: Fix SpEL JavaBean compliance for setters
This commit is contained in:
commit
0ed9cb2302
|
|
@ -311,15 +311,10 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
|
||||||
*/
|
*/
|
||||||
protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
||||||
Method[] ms = clazz.getMethods();
|
Method[] ms = clazz.getMethods();
|
||||||
String propertyWriteMethodSuffix;
|
String propertyMethodSuffix = getPropertyMethodSuffix(propertyName);
|
||||||
if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) {
|
|
||||||
propertyWriteMethodSuffix = propertyName;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
propertyWriteMethodSuffix = StringUtils.capitalize(propertyName);
|
|
||||||
}
|
|
||||||
// Try "get*" method...
|
// Try "get*" method...
|
||||||
String getterName = "get" + propertyWriteMethodSuffix;
|
String getterName = "get" + propertyMethodSuffix;
|
||||||
for (Method method : ms) {
|
for (Method method : ms) {
|
||||||
if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
|
if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
|
||||||
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
|
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
|
||||||
|
|
@ -327,7 +322,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Try "is*" method...
|
// Try "is*" method...
|
||||||
getterName = "is" + propertyWriteMethodSuffix;
|
getterName = "is" + propertyMethodSuffix;
|
||||||
for (Method method : ms) {
|
for (Method method : ms) {
|
||||||
if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
|
if (!method.isBridge() && method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
|
||||||
(boolean.class.equals(method.getReturnType()) || Boolean.class.equals(method.getReturnType())) &&
|
(boolean.class.equals(method.getReturnType()) || Boolean.class.equals(method.getReturnType())) &&
|
||||||
|
|
@ -343,7 +338,7 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
|
||||||
*/
|
*/
|
||||||
protected Method findSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
protected Method findSetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
|
||||||
Method[] methods = clazz.getMethods();
|
Method[] methods = clazz.getMethods();
|
||||||
String setterName = "set" + StringUtils.capitalize(propertyName);
|
String setterName = "set" + getPropertyMethodSuffix(propertyName);
|
||||||
for (Method method : methods) {
|
for (Method method : methods) {
|
||||||
if (!method.isBridge() && method.getName().equals(setterName) && method.getParameterTypes().length == 1 &&
|
if (!method.isBridge() && method.getName().equals(setterName) && method.getParameterTypes().length == 1 &&
|
||||||
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
|
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
|
||||||
|
|
@ -353,6 +348,15 @@ public class ReflectivePropertyAccessor implements PropertyAccessor {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String getPropertyMethodSuffix(String propertyName) {
|
||||||
|
if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) {
|
||||||
|
return propertyName;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return StringUtils.capitalize(propertyName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a field of a certain name on a specified class
|
* Find a field of a certain name on a specified class
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ import java.lang.reflect.Method;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.core.convert.TypeDescriptor;
|
import org.springframework.core.convert.TypeDescriptor;
|
||||||
|
|
@ -329,6 +329,10 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
// note: "Id" is not a valid JavaBean name, nevertheless it is treated as "id"
|
// note: "Id" is not a valid JavaBean name, nevertheless it is treated as "id"
|
||||||
Assert.assertEquals("id",rpr.read(ctx,t,"Id").getValue());
|
Assert.assertEquals("id",rpr.read(ctx,t,"Id").getValue());
|
||||||
Assert.assertTrue(rpr.canRead(ctx,t,"Id"));
|
Assert.assertTrue(rpr.canRead(ctx,t,"Id"));
|
||||||
|
|
||||||
|
// SPR-10122, ReflectivePropertyAccessor JavaBean property names compliance tests - setters
|
||||||
|
rpr.write(ctx, t, "pEBS","Test String");
|
||||||
|
Assert.assertEquals("Test String",rpr.read(ctx,t,"pEBS").getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -419,6 +423,7 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
String iD = "iD";
|
String iD = "iD";
|
||||||
String id = "id";
|
String id = "id";
|
||||||
String ID = "ID";
|
String ID = "ID";
|
||||||
|
String pEBS = "pEBS";
|
||||||
|
|
||||||
public String getProperty() { return property; }
|
public String getProperty() { return property; }
|
||||||
public void setProperty(String value) { property = value; }
|
public void setProperty(String value) { property = value; }
|
||||||
|
|
@ -434,6 +439,14 @@ public class ReflectionHelperTests extends ExpressionTestCase {
|
||||||
public String getId() { return id; }
|
public String getId() { return id; }
|
||||||
|
|
||||||
public String getID() { return ID; }
|
public String getID() { return ID; }
|
||||||
|
|
||||||
|
public String getpEBS() {
|
||||||
|
return pEBS;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setpEBS(String pEBS) {
|
||||||
|
this.pEBS = pEBS;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static class Super {
|
static class Super {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue