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 all 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
52 changes: 52 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 All @@ -24,6 +25,7 @@
import com.ibm.wala.core.util.ref.CacheReference;
import com.ibm.wala.core.util.ref.ReferenceCleanser;
import com.ibm.wala.core.util.strings.Atom;
import com.ibm.wala.core.util.strings.StringStuff;
import com.ibm.wala.core.util.warnings.Warning;
import com.ibm.wala.core.util.warnings.Warnings;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
Expand Down Expand Up @@ -706,6 +708,56 @@ private void recursiveStringify(Node n, StringBuilder buffer) {
}
}

/**
* Converts ClassHierarchy to a JSON String, mapping each class name to a list of subclass names
*/
public String toJson() {
// Initialize the map to store the <class, subclass> pairs
HashMap<String, Set<String>> classNameToSubclassNames = new HashMap<>();

// Start the recursive function from the root node
helperToJson(root, classNameToSubclassNames);

// Use Gson to convert the map to a JSON string
Gson gson = new Gson();
return gson.toJson(classNameToSubclassNames);
}

/** 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
String key = nodeToString(n);

// If the node is already processed, return to avoid infinite recursion
if (hash.containsKey(key)) {
return;
}

// Initialize a set to store the names of the subclasses
Set<String> subclassNames = new HashSet<>();

// Process all children of the current node
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.

Iterator<Node> children = n.getChildren();
while (children.hasNext()) {
Node temp = children.next();
helperToJson(temp, hash);

String childKey = nodeToString(temp);
subclassNames.add(childKey);
}
}

// Put the current node and its subclasses in the map
hash.put(key, subclassNames);
}

/**
* Removed unnecessary part of Node by turning it into a String (Made for toJson and helperToJson)
*/
private String nodeToString(Node n) {
return StringStuff.jvmToBinaryName(n.getJavaClass().getName().toString());
}

/**
* 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 @@ -14,8 +14,12 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.core.tests.util.TestConstants;
import com.ibm.wala.core.tests.util.WalaTestCase;
import com.ibm.wala.core.util.strings.StringStuff;
import com.ibm.wala.ipa.callgraph.AnalysisCacheImpl;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
Expand All @@ -31,6 +35,10 @@
import com.ibm.wala.types.TypeReference;
import com.ibm.wala.util.CancelException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -79,4 +87,30 @@ 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);
Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, Set<String>>>() {}.getType();
String json = cha.toJson();
System.err.println(json);
HashMap<String, Set<String>> list = gson.fromJson(json, type);
assertTrue(list.containsKey(nodeToString(cha.getRootClass())));

Set<String> subclassNames = new HashSet<>();
Iterator<IClass> children = cha.computeSubClasses(cha.getRootClass().getReference()).iterator();
while (children.hasNext()) {
String temp = nodeToString(children.next());
subclassNames.add(temp);
}
assertTrue(subclassNames.containsAll(list.get(nodeToString(cha.getRootClass()))));
}

private String nodeToString(IClass klass) {
return StringStuff.jvmToBinaryName(klass.getName().toString());
}
}
Loading