Skip to content

Commit

Permalink
Merge pull request #56 from harawata/backport-55
Browse files Browse the repository at this point in the history
Backporting #55 to ognl-3-1-x branch.
  • Loading branch information
lukaszlenart authored Oct 4, 2018
2 parents 9f9d3db + 0e0742d commit 8510935
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 32 deletions.
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

0 comments on commit 8510935

Please sign in to comment.