Complete re-design.

This commit is contained in:
Achim D. Brucker 2016-09-10 19:27:10 +01:00
parent 5861bedf02
commit 0382167bff
2 changed files with 38 additions and 104 deletions

View File

@ -10,48 +10,24 @@
package eu.aniketos.dasca.crosslanguage.cg; package eu.aniketos.dasca.crosslanguage.cg;
import java.util.ArrayList
import com.ibm.wala.classLoader.CallSiteReference
import com.ibm.wala.classLoader.Language
import com.ibm.wala.ipa.callgraph.CGNode; import com.ibm.wala.ipa.callgraph.CGNode;
import com.ibm.wala.cast.js.loader.JavaScriptLoader
import com.ibm.wala.cast.js.types.JavaScriptMethods
class CallTree( data:CGNode, parent:CallTree,level:Integer){ class CallTree(value: CGNode, children: List[CallTree]) {
def this(value: CGNode) = this(value, null)
private var children = List[CallTree]()
private val indent = 5 private val indent = 5
def contains(v: CGNode): Boolean = {
if (value == v) {
def this(data:CGNode) = this(data, null, 0) true
} else {
def addChildren(data:CGNode) = children :+ new CallTree(data, this, level+1) (children.map { c => c.contains(v) }).fold(false) { (a, b) => a || b }
/**
* this method return the path from current Node to root
* @return
* return An ArrayList which contains path of currentNode
*/
def getPathOfNode():List[CGNode] = {
var path = List[CGNode]()
var currentNode = this
do{
path :+ this
currentNode = currentNode.getParent()
if(currentNode.getParent()==null){
path :+ currentNode.getValue()
} }
}while(currentNode.getParent()!=null);
return path;
} }
def getValue() = data override def toString(): String = {
def getParent() = parent value.getMethod().getName().toString() + "\n" + ((children.map { c => "├── " + c.toString() + "\n" })
def getChildren() = children .fold("") { (a, b) => a + b })
def getLevel() = level }
} }

View File

@ -27,70 +27,28 @@ import eu.aniketos.dasca.crosslanguage.builder.CordovaCGBuilder
import eu.aniketos.dasca.crosslanguage.builder.CrossBuilderOption import eu.aniketos.dasca.crosslanguage.builder.CrossBuilderOption
import collection.JavaConverters._ import collection.JavaConverters._
class CallTreeBuilder { object CallTreeBuilder {
var queue = new LinkedList[CGNode](); def buildCallForest(cg: MergedCallGraph, sources: List[CGNode], sinks: List[CGNode]): List[CallTree] = {
var rootqueue = new LinkedList[CallTree](); return sources.map { src => buildCallTree(cg, src, sinks) }.filter { x => null != x }
def buildCallForest(cg:MergedCallGraph, sources:List[CGNode], sinks:List[CGNode]):List[CallTree] = {
return sources.map { src => buildCallTree(cg,src,sinks) }.filter { x => null != x }
} }
def buildCallTree(cg:MergedCallGraph, root: CGNode, sinks:List[CGNode]):CallTree = { def buildCallTree(cg: MergedCallGraph, root: CGNode, sinks: List[CGNode]): CallTree = {
var ct = null:CallTree build(cg, List[CGNode](), sinks, root)
var visited = List[CGNode]()
var NumofCount = 0;
queue.add(root)
while(!queue.isEmpty()){
var currentNode = queue.poll()
var RootOfCurrentNode = rootqueue.poll()
if(!visited.contains(currentNode)){
visited :+ currentNode
// search for the whether this node contains the method
var it = currentNode.iterateCallSites()
var method = null:CallSiteReference
while (it.hasNext())
method = it.next()
// if this node contains the method we want
// print the path
if(sinks.contains(method.asInstanceOf[CallSiteReference].getDeclaredTarget())){
// RootOfCurrentNode.printPath(method)
NumofCount = NumofCount+1
}else{
// if not find the method in current Node,
// add this node to the Node tree which used to create the path
if(currentNode.equals(root)){
var tree = new CallTree(currentNode)
createTree(cg,currentNode,method,tree)
}else{
createTree(cg,currentNode,method,RootOfCurrentNode)
}
}
}
}
return ct
} }
/** private def build(cg: MergedCallGraph, visited: List[CGNode], sinks: List[CGNode], root: CGNode): CallTree = {
* this method is used to create the Node Tree. if (sinks.contains(root)) {
* @param cg cg is the call graph return new CallTree(root)
* @param currentNode this parameter is the node that be search } else {
* @param method method is the function that need to find in a particular node val targetNodes = cg.getAllPossibleTargetNodes(root).filterNot { n => visited.contains(n) }
* @param RootOfCurrentNode the previous node that has be search which is the parent of current node if (targetNodes.isEmpty) {
*/ return null
def createTree(cg:MergedCallGraph, currentNode: CGNode, method:CallSiteReference, RootOfCurrentNode:CallTree) = { } else {
var n = null:CGNode val children = targetNodes.toList.map { x => build(cg, visited ++ targetNodes, sinks, x) }
var child = null:CallTree .filterNot { x => (null == x) }
var targetNode = cg.getAllPossibleTargetNodes(currentNode) return new CallTree(root, children)
var it = targetNode.iterator
for (n <- it){
queue.add(n);
if(RootOfCurrentNode!=null){
child = new CallTree(n,RootOfCurrentNode,RootOfCurrentNode.getLevel()+1);
rootqueue.add(child);
}
}
} }
}
}
} }