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

When scanning super interfaces in OgnlRuntime#getMethods(Class, bool… #40

Merged
merged 2 commits into from
Nov 16, 2017
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
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 {
}
*/