From 72053efd269fc40a8ad0d6f4171b687d46610fcb Mon Sep 17 00:00:00 2001 From: Raffi Khatchadourian Date: Thu, 17 Aug 2017 17:45:49 -0400 Subject: [PATCH] Return Java 8 default methods from getAllMethods() Fixes #219 Fixes #220 --- .../wala/core/tests/callGraph/DefaultMethodsTest.java | 11 +++++++++++ .../src/com/ibm/wala/classLoader/BytecodeClass.java | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/callGraph/DefaultMethodsTest.java b/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/callGraph/DefaultMethodsTest.java index 695295f78..d8714afdb 100644 --- a/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/callGraph/DefaultMethodsTest.java +++ b/com.ibm.wala.core.tests/src/com/ibm/wala/core/tests/callGraph/DefaultMethodsTest.java @@ -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 allMethods = test1Class.getAllMethods(); + IMethod defaultMethod = test1Class.getMethod(t1m.getSelector()); + Assert.assertTrue("Expecting default methods to show up in IClass.allMethods()", allMethods.contains(defaultMethod)); } } diff --git a/com.ibm.wala.core/src/com/ibm/wala/classLoader/BytecodeClass.java b/com.ibm.wala.core/src/com/ibm/wala/classLoader/BytecodeClass.java index 5fea61a1b..64e6dfa45 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/classLoader/BytecodeClass.java +++ b/com.ibm.wala.core/src/com/ibm/wala/classLoader/BytecodeClass.java @@ -396,6 +396,12 @@ public abstract class BytecodeClass 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) {