Skip to content

Fix SpEL JavaBean compliance #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,21 +282,26 @@ else if (canWrite(context, target, name)) {
}

/**
* Find a getter method for the specified property. A getter is defined as a method whose name start with the prefix
* 'get' and the rest of the name is the same as the property name (with the first character uppercased).
* Find a getter method for the specified property.
*/
protected Method findGetterForProperty(String propertyName, Class<?> clazz, boolean mustBeStatic) {
Method[] ms = clazz.getMethods();
String propertyWriteMethodSuffix;
if (propertyName.length() > 1 && Character.isUpperCase(propertyName.charAt(1))) {
propertyWriteMethodSuffix = propertyName;
} else {
propertyWriteMethodSuffix = StringUtils.capitalize(propertyName);
}
// Try "get*" method...
String getterName = "get" + StringUtils.capitalize(propertyName);
String getterName = "get" + propertyWriteMethodSuffix;
for (Method method : ms) {
if (method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
(!mustBeStatic || Modifier.isStatic(method.getModifiers()))) {
return method;
}
}
// Try "is*" method...
getterName = "is" + StringUtils.capitalize(propertyName);
getterName = "is" + propertyWriteMethodSuffix;
for (Method method : ms) {
if (method.getName().equals(getterName) && method.getParameterTypes().length == 0 &&
boolean.class.equals(method.getReturnType()) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ public void testReflectivePropertyResolver() throws Exception {
Assert.assertEquals(false,rpr.read(ctx,t,"property4").getValue());
Assert.assertTrue(rpr.canRead(ctx,t,"property4"));

// repro SPR-9123, ReflectivePropertyAccessor JavaBean property names compliance tests
Assert.assertEquals("iD",rpr.read(ctx,t,"iD").getValue());
Assert.assertTrue(rpr.canRead(ctx,t,"iD"));
Assert.assertEquals("id",rpr.read(ctx,t,"id").getValue());
Assert.assertTrue(rpr.canRead(ctx,t,"id"));
Assert.assertEquals("ID",rpr.read(ctx,t,"ID").getValue());
Assert.assertTrue(rpr.canRead(ctx,t,"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.assertTrue(rpr.canRead(ctx,t,"Id"));

}

@Test
Expand Down Expand Up @@ -406,6 +417,9 @@ static class Tester {
String property2;
String property3 = "doodoo";
boolean property4 = false;
String iD = "iD";
String id = "id";
String ID = "ID";

public String getProperty() { return property; }
public void setProperty(String value) { property = value; }
Expand All @@ -415,6 +429,12 @@ static class Tester {
public String getProperty3() { return property3; }

public boolean isProperty4() { return property4; }

public String getiD() { return iD; }

public String getId() { return id; }

public String getID() { return ID; }
}

static class Super {
Expand Down