Return Java 8 default methods from getAllMethods()

Fixes #219 
Fixes #220
This commit is contained in:
Raffi Khatchadourian 2017-08-17 17:45:49 -04:00 committed by Manu Sridharan
parent b70d69d62f
commit 72053efd26
2 changed files with 17 additions and 0 deletions

View File

@ -11,10 +11,13 @@
package com.ibm.wala.core.tests.callGraph;
import java.io.IOException;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.core.tests.util.TestConstants;
import com.ibm.wala.core.tests.util.WalaTestCase;
import com.ibm.wala.ipa.callgraph.AnalysisCacheImpl;
@ -78,5 +81,13 @@ public class DefaultMethodsTest extends WalaTestCase {
// Check call from main to Test3.silly
Assert.assertTrue("should have call site from main to Test3.silly", cg.getPossibleSites(mnode, ttnode).hasNext());
// Check that IClass.getAllMethods() returns default methods #219.
TypeReference test1Type = TypeReference.findOrCreate(ClassLoaderReference.Application, "LdefaultMethods/DefaultMethods$Test1");
IClass test1Class = cha.lookupClass(test1Type);
Collection<IMethod> allMethods = test1Class.getAllMethods();
IMethod defaultMethod = test1Class.getMethod(t1m.getSelector());
Assert.assertTrue("Expecting default methods to show up in IClass.allMethods()", allMethods.contains(defaultMethod));
}
}

View File

@ -396,6 +396,12 @@ public abstract class BytecodeClass<T extends IClassLoader> implements IClass {
for (IClass i : getDirectInterfaces()) {
result.addAll(i.getAllMethods());
}
} else {
// for non-interfaces, add default methods inherited from interfaces #219.
for (IClass i : this.getAllImplementedInterfaces())
for (IMethod m : i.getDeclaredMethods())
if (!m.isAbstract())
result.add(m);
}
IClass s = getSuperclass();
while (s != null) {