Creating Data Flow Graphs from java input classes
Creating Data Flow Graphs from java input classes
With JavaDataFlow you can create data flow graphs. This is a directed graph where a node represents data (e.g. fields, inputParameters, etc.) and the edges represent a node influencing the state of another node. Nodes are always defined within a specific method, constructor, codeBlock. We refer to such a code block as owner. Every Node has an owner. Every owner can also have an owner, for instance a method is owned by a class. To make it possible to only parse a part of an application we model calls to a method and the dataflow inside the method itself separately. At any moment the flow through a method can be linked to a call to that method to follow the flow of data though multiple methods or classes.
Let's say we have a class as given as below:
public class JavaDataFlowExampleInputClass {
int a;
public int getA() {
return a;
}
public void setA(int inputA) {
this.a = inputA;
}
}
Then we can execute the following commands to create a data flow graph from it. First define the path to your project and setup the path where JavaDataFlow will look for class files. If you have multiple projects where JavaDataFlow needs to look for class files, you can enter multiple project paths there. Then create the data flow graph using the absolute input path to the java class. A DataFlowGraph represents a single class.
String projectPath = "projectPath";
String input = "/relativePath/Example.java";
StaticJavaDataFlow.getConfig().setProjectPaths(projectPath);
DataFlowGraph dfg = JavaDataFlow.create(projectPath + input);
Now if we want to gather all input nodes to this class that can influence the output of the method "getA", we can do that as given below. First get the given method. Now we need to walk back until we reach a node that is an input parameter of a method, for this we can use the method DataFlowNode::isInputParameter. For this example we don't want to go outside of this class so we add dfg::owns as scope to the method walkBackUntil. The scope determines when to stop walking over the nodes, this can become important if multiple data flow graphs are connected to each other. However, this is currently not supported yet.
DataFlowMethod getA = dfg.getMethods().stream().filter(m -> m.getName().equals("getA")).findFirst().get();
List<DataFlowNode> inputNodes = getA.getReturnNode().get().walkBackUntil(DataFlowNode::isInputParameter, dfg::owns);
System.out.println(inputNodes.get(0).getName());
The above code will output the name "inputA". All code above is also given in an example project.
Add the dependency below to the pom of your project.
<dependency>
<groupId>com.github.daanvdh.javadataflow</groupId>
<artifactId>JavaDataFlow</artifactId>
<version>0.0.5</version>
</dependency>
JavaDataFlow is available under the terms of the Apache License. http://www.apache.org/licenses/LICENSE-2.0