diff --git a/src/java/ognl/OgnlRuntime.java b/src/java/ognl/OgnlRuntime.java index 6e0f192a..68fb54c2 100644 --- a/src/java/ognl/OgnlRuntime.java +++ b/src/java/ognl/OgnlRuntime.java @@ -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; } /** diff --git a/src/test/java/ognl/Java8Test.java b/src/test/java/ognl/Java8Test.java index 787121ed..32793dcc 100644 --- a/src/test/java/ognl/Java8Test.java +++ b/src/test/java/ognl/Java8Test.java @@ -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 */ { - } /**