Skip to content

Commit

Permalink
Merge pull request #40 from harawata/issue35
Browse files Browse the repository at this point in the history
When scanning super interfaces in OgnlRuntime#getMethods(Class, bool…
  • Loading branch information
lukaszlenart authored Nov 16, 2017
2 parents fbe73c6 + 9e64407 commit eb83c4c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
74 changes: 44 additions & 30 deletions src/main/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1768,43 +1768,57 @@ public static Map getMethods(Class targetClass, boolean staticMethods)
if ((result = (Map) cache.get(targetClass)) == null)
{
result = new HashMap(23);
collectMethods(targetClass, result, staticMethods);
cache.put(targetClass, result);
}
}
}
return result;
}

List<Class> toExamined = new LinkedList<Class>();
for (Class c = targetClass; c != null; c = c.getSuperclass())
{
toExamined.add(c);

// Including interfaces is needed as from Java 8 intefaces can implement default methods
toExamined.addAll(Arrays.asList(c.getInterfaces()));
}
private static void collectMethods(Class c, Map result, boolean staticMethods) {
final Method[] ma = c.getDeclaredMethods();
for (int i = 0, icount = ma.length; i < icount; i++)
{
if (c.isInterface())
{
if (isDefaultMethod(ma[i]))
addMethodToResult(result, ma[i]);
continue;
}

for (Class c : toExamined)
{
Method[] ma = c.getDeclaredMethods();
// skip over synthetic methods
if (!isMethodCallable(ma[i]))
continue;

for (int i = 0, icount = ma.length; i < icount; i++)
{
// skip over synthetic methods
if (Modifier.isStatic(ma[i].getModifiers()) == staticMethods)
addMethodToResult(result, ma[i]);
}

if (!isMethodCallable(ma[i]))
continue;
final Class superclass = c.getSuperclass();
if (superclass != null)
collectMethods(superclass, result, staticMethods);

if (Modifier.isStatic(ma[i].getModifiers()) == staticMethods)
{
List ml = (List) result.get(ma[i].getName());
for (final Class iface : c.getInterfaces())
collectMethods(iface, result, staticMethods);
}

if (ml == null)
result.put(ma[i].getName(), ml = new ArrayList());
private static void addMethodToResult(Map result, Method method)
{
List ml = (List) result.get(method.getName());
if (ml == null)
result.put(method.getName(), ml = new ArrayList());
ml.add(method);
}

ml.add(ma[i]);
}
}
}
cache.put(targetClass, result);
}
}
}
return result;
/**
* Backport of java.lang.reflect.Method#isDefault()
*/
private static boolean isDefaultMethod(Method method)
{
return ((method.getModifiers()
& (Modifier.ABSTRACT | Modifier.PUBLIC | Modifier.STATIC)) == Modifier.PUBLIC)
&& method.getDeclaringClass().isInterface();
}

public static Map getAllMethods(Class targetClass, boolean staticMethods)
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/ognl/Java8Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SubClassWithDefaults extends ClassWithDefaults {

}

class ClassWithDefaults /* implements InterfaceWithDefaults */ {
class ClassWithDefaults /* implements SubInterfaceWithDefaults */ {

}

Expand All @@ -50,4 +50,6 @@ interface InterfaceWithDefaults {
default public void defaultMethod() { }
default public String getName() { return "name"; }
}
*/
interface SubInterfaceWithDefaults extends InterfaceWithDefaults {
}
*/

0 comments on commit eb83c4c

Please sign in to comment.