Skip to content
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

Backporting #55 to ognl-3-1-x branch. #56

Merged
merged 2 commits into from
Oct 4, 2018
Merged
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
72 changes: 44 additions & 28 deletions src/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -2138,45 +2138,61 @@ public static List getDeclaredMethods(Class targetClass, String propertyName, bo
if ((propertyCache == null) || ((result = (List) propertyCache.get(propertyName)) == null)) {

String baseName = capitalizeBeanPropertyName(propertyName);
result = new ArrayList();
collectAccessors(targetClass, baseName, result, findSets);

for (Class c = targetClass; c != null; c = c.getSuperclass()) {
Method[] methods = c.getDeclaredMethods();
if (propertyCache == null) {
cache.put(targetClass, propertyCache = new HashMap(101));
}
propertyCache.put(propertyName, result.isEmpty() ? NotFoundList : result);

return result.isEmpty() ? null : result;
}
}
}
return (result == NotFoundList) ? null : result;
}

for (int i = 0; i < methods.length; i++) {
private static void collectAccessors(Class c, String baseName, List result, boolean findSets)
{
final Method[] methods = c.getDeclaredMethods();
for (int i = 0; i < methods.length; i++) {
if (c.isInterface()) {
if (isDefaultMethod(methods[i])) {
addIfAccessor(result, methods[i], baseName, findSets);
}
continue;
}

if (!isMethodCallable(methods[i]))
continue;
if (!isMethodCallable(methods[i]))
continue;

String ms = methods[i].getName();
addIfAccessor(result, methods[i], baseName, findSets);
}

if (ms.endsWith(baseName)) {
boolean isSet = false, isIs = false;
final Class superclass = c.getSuperclass();
if (superclass != null)
collectAccessors(superclass, baseName, result, findSets);

if ((isSet = ms.startsWith(SET_PREFIX)) || ms.startsWith(GET_PREFIX)
|| (isIs = ms.startsWith(IS_PREFIX))) {
int prefixLength = (isIs ? 2 : 3);
for (final Class iface : c.getInterfaces())
collectAccessors(iface, baseName, result, findSets);
}

if (isSet == findSets) {
if (baseName.length() == (ms.length() - prefixLength)) {
if (result == null) {
result = new ArrayList();
}
result.add(methods[i]);
}
}
}
}
}
}
if (propertyCache == null) {
cache.put(targetClass, propertyCache = new HashMap(101));
private static void addIfAccessor(List result, Method method, String baseName, boolean findSets)
{
final String ms = method.getName();
if (ms.endsWith(baseName)) {
boolean isSet = false, isIs = false;
if ((isSet = ms.startsWith(SET_PREFIX)) || ms.startsWith(GET_PREFIX)
|| (isIs = ms.startsWith(IS_PREFIX))) {
int prefixLength = (isIs ? 2 : 3);
if (isSet == findSets) {
if (baseName.length() == (ms.length() - prefixLength)) {
result.add(method);
}

propertyCache.put(propertyName, (result == null) ? NotFoundList : result);
}
}
}
return (result == NotFoundList) ? null : result;
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/ognl/Java8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@ public void testDefaultMethodOnSubClass() {
assertNotNull(defaultMethod);
}

public void testGetDeclaredMethods() {
List defaultMethod = OgnlRuntime.getDeclaredMethods(SubClassWithDefaults.class, "name", false);
assertNotNull(defaultMethod);
defaultMethod = OgnlRuntime.getDeclaredMethods(ClassWithDefaults.class, "name", false);
assertNotNull(defaultMethod);
}

}

class SubClassWithDefaults extends ClassWithDefaults {

public String getName() { return "name"; }

}

class ClassWithDefaults /* implements SubInterfaceWithDefaults */ {

}

/**
Expand Down