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

ClassHierarchy toJson Function #1392

Open
wants to merge 54 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
1187f33
added toJson function to AnalysisScope
aakgna Nov 20, 2023
3a90899
added one successful test for toJson in Analysis Scope
aakgna Nov 28, 2023
6c81529
added json capability
aakgna Dec 5, 2023
163aa8e
implemented toJson functionality
aakgna Dec 23, 2023
6f69ce9
returned right value
aakgna Dec 23, 2023
307607a
added changes to toJson
aakgna Jan 3, 2024
cb5dd7b
returned correct value
aakgna Jan 4, 2024
b489343
changes .settings
aakgna Jan 13, 2024
a38c5b7
removed unnecessary changes
aakgna Jan 23, 2024
ab16227
Merge branch 'wala:master' into master
aakgna Jan 23, 2024
a43b836
Merge branch 'wala:master' into master
aakgna Jan 30, 2024
6d15461
added linkedHashmap to AnalysisScope and created test for toJson
aakgna Jan 30, 2024
e57b590
deleted unnecessary lines
aakgna Jan 30, 2024
0f8f978
Merge branch 'wala:master' into master
aakgna Feb 11, 2024
c237c76
implemented changes for toJson and added endlines
aakgna Feb 12, 2024
fbe9c59
completed changes for AnalysisScopeTest and removed 1 import statemen…
aakgna Feb 13, 2024
3cfff82
Tune up test and fix formatting
msridhar Feb 14, 2024
ebd73ae
suppress a warning
msridhar Feb 14, 2024
89359aa
added custom analysisScope test
aakgna Feb 27, 2024
ed5500c
sorted stdlib
aakgna Feb 28, 2024
7036d90
formatting
msridhar Feb 28, 2024
9b7b7e9
updated toJson custom test and added onto toJson method
aakgna Feb 28, 2024
a41401a
test out path for stdlibs
aakgna Mar 1, 2024
c7707eb
Merge branch 'master' into master
aakgna Mar 3, 2024
8b9c04d
modified file path to match all machines
aakgna Mar 3, 2024
15a4a1e
formatting
msridhar Mar 4, 2024
7729aa7
Merge branch 'master' into aakgna/master
msridhar Mar 4, 2024
f777f66
changed assertion in testToJsonCustom
aakgna Mar 4, 2024
efad143
added for loop in testToJsonCustom (stdlibs)
aakgna Mar 5, 2024
016896e
initial algorithm for JSON Serialization
aakgna Mar 12, 2024
c050542
merged all hashmaps into one
aakgna Mar 12, 2024
dcb0652
Merge branch 'master' into ClassHierarchyToJson
msridhar Apr 15, 2024
ed87a3a
comment out cnt variable, test seems to pass
msridhar Apr 15, 2024
030e8b3
Removed Primordial and Application from Klass of a Node
aakgna Apr 16, 2024
d09462c
added comments on toJson
aakgna Apr 16, 2024
9147bc8
added spotlessApply
aakgna Apr 16, 2024
474cbfd
implemented GSON
aakgna Apr 17, 2024
2ec38a9
addressed comments to simplify code
aakgna Apr 24, 2024
246e59a
cleaned up code
aakgna Apr 24, 2024
c5256ab
Merge branch 'master' into ClassHierarchyToJson
msridhar Apr 24, 2024
050da9f
addressed comments
aakgna Apr 25, 2024
52574e6
combined some code together to simplify code
aakgna Apr 25, 2024
9b52619
Merge branch 'master' into ClassHierarchyToJson
msridhar May 17, 2024
731deed
simplified toJson and added base case to helperToJson
aakgna May 21, 2024
e6f43dd
did ./gradlew spotlessapply
aakgna May 27, 2024
5016ea2
Merge branch 'wala:master' into ClassHierarchyToJson
aakgna May 27, 2024
8eb7723
test toJson
aakgna Jun 6, 2024
f62575e
Merge branch 'wala:master' into ClassHierarchyToJson
aakgna Jun 6, 2024
113b2a8
Merge branch 'master' into ClassHierarchyToJson
msridhar Jun 6, 2024
23412c2
Merge branch 'master' into ClassHierarchyToJson
msridhar Jul 24, 2024
65b08aa
Merge branch 'wala:master' into ClassHierarchyToJson
aakgna Aug 3, 2024
1595369
made test and simplified toJson
aakgna Aug 3, 2024
9b8ac2e
Merge branch 'master' into ClassHierarchyToJson
msridhar Aug 15, 2024
b430981
better string formatting
msridhar Aug 15, 2024
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
81 changes: 81 additions & 0 deletions core/src/main/java/com/ibm/wala/ipa/cha/ClassHierarchy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package com.ibm.wala.ipa.cha;

import com.google.gson.Gson;
import com.ibm.wala.classLoader.ArrayClass;
import com.ibm.wala.classLoader.BytecodeClass;
import com.ibm.wala.classLoader.ClassLoaderFactory;
Expand Down Expand Up @@ -706,6 +707,86 @@ private void recursiveStringify(Node n, StringBuilder buffer) {
}
}

/*
aakgna marked this conversation as resolved.
Show resolved Hide resolved
* Puts ClassHierarchy into a JSON readable variable that maps the class to a list of its
* subclasses
aakgna marked this conversation as resolved.
Show resolved Hide resolved
*/
public String toJson() {
// initialize variables (subclass and dag) to store important values like hashmap of each
// iteration and a list of all maps of class to subclasses
aakgna marked this conversation as resolved.
Show resolved Hide resolved
HashMap<String, Set<String>> subclass = new HashMap<>();
aakgna marked this conversation as resolved.
Show resolved Hide resolved
Iterator<Node> children = root.getChildren();
// Goes through all subclasses of the root class, and adds the subclass hashmap to the
// list of all subclass hashmaps
aakgna marked this conversation as resolved.
Show resolved Hide resolved
while (children.hasNext()) {
helperToJson(children.next(), subclass);
}
// Removes unnecesarry parts from name of the class
aakgna marked this conversation as resolved.
Show resolved Hide resolved
String key = nodeToString(root);
// inserting the root class to its subclasses into the main hashmap while removing
// unnecessary substrings from the class name
Iterator<Node> rootChildren = root.getChildren();
Set<String> finalRootChildren = new HashSet<>();
while (rootChildren.hasNext()) {
finalRootChildren.add(nodeToString(rootChildren.next()));
}
subclass.put(key, finalRootChildren);
Gson gson = new Gson();
return gson.toJson(subclass);
}

/*
aakgna marked this conversation as resolved.
Show resolved Hide resolved
* helper function to toJson that performs recursion to go through all of the DAG
*/
private void helperToJson(Node n, HashMap<String, Set<String>> hash) {
aakgna marked this conversation as resolved.
Show resolved Hide resolved
if (n.children.size() > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By printing out the JSON in the test and looking it over, I realized that it only handles subclassing, but not interface implementations. E.g., for the interface java.util.Map we see "java.util.Map":[] in the output, missing all the classes that implement that interface. We should update this logic so that for interfaces, we treat all implementing classes and sub-interfaces as subtypes in the outputted JSON. In other words, the JSON should map each class or interface to all of its immediate subtypes, i.e., direct subclasses, direct sub-interfaces, or classes directly implementing an interface.

// while loop: defines iterator to go through the class's subclasses
aakgna marked this conversation as resolved.
Show resolved Hide resolved
Iterator<Node> children = n.getChildren();
while (children.hasNext()) {
// puts subclass variable into temp, and start recursion from that subclass
// until base case reaches
Node temp = children.next();
helperToJson(temp, hash);
// remove unnecessary substrings from class name
String key = nodeToString(temp);
// if statement: only adds the map from class to subclass if class name does
// not exist in the overall hashmap
if (!hash.containsKey(key)) {
// Iterates through the subclass list and removes unnecessary substrings from
// class name --> adds that new value into varibale, root, containig all it's subclasses
Iterator<Node> val = temp.getChildren();
Set<String> root = new HashSet<>();
while (val.hasNext()) {
Node tempVal = val.next();
String newVal = nodeToString(tempVal);
root.add(newVal);
}
// insert into hashmap containing all class to subclass pairs
hash.put(key, root);
aakgna marked this conversation as resolved.
Show resolved Hide resolved
}
}
} else {
// remove unnecessary substring from class name and map that to an empty
// list to add into the hashmap
aakgna marked this conversation as resolved.
Show resolved Hide resolved
String key = nodeToString(n);
if (!hash.containsKey(key)) {
Set<String> root = new HashSet<>();
hash.put(key, root);
}
}
}

/*
* Removed unnecessary part of Node by turning it into a String (Made for toJson and helperToJson)
*/
private String nodeToString(Node n) {
String key = n.getJavaClass().toString();
key = key.replace("<Primordial,", "");
key = key.replace("<Application,", "");
key = key.replace(">", "");
return key;
}

/**
* Number the class hierarchy tree to support efficient subclass tests. After numbering the tree,
* n1 is a child of n2 iff n2.left &lt;= n1.left ^ n1.left &lt;= n2.right. Described as "relative
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ public void testClassConstants()
// make sure call to hashCode from main
assertTrue(cg.hasEdge(mainMethodNode, hashCodeNodes.iterator().next()));
}

@Test
public void classHierarchyToJson() throws ClassHierarchyException, IOException {
AnalysisScope scope =
CallGraphTestUtil.makeJ2SEAnalysisScope(
TestConstants.WALA_TESTDATA, CallGraphTestUtil.REGRESSION_EXCLUSIONS);
ClassHierarchy cha = ClassHierarchyFactory.make(scope);
Object unused = cha.toJson();
// assertEquals("", json);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to write some actual assertions about what we expect to be in the final JSON string?

}
}
Loading