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

analysisScope toJson function #1355

Merged
merged 29 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
29 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
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
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ dependencies {
api(projects.util) { because("public interface CallGraph extends interface NumberedGraph") }
testFixturesImplementation(libs.ant)
testFixturesImplementation(libs.junit.platform.launcher)
implementation(libs.gson)
testImplementation(libs.assertj.core)
testImplementation(libs.hamcrest)
testRuntimeOnly(sourceSets["testSubjects"].output.classesDirs)
Expand Down
37 changes: 37 additions & 0 deletions core/src/main/java/com/ibm/wala/ipa/callgraph/AnalysisScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package com.ibm.wala.ipa.callgraph;

import com.google.gson.Gson;
import com.ibm.wala.classLoader.ArrayClassLoader;
import com.ibm.wala.classLoader.BinaryDirectoryTreeModule;
import com.ibm.wala.classLoader.ClassFileModule;
Expand Down Expand Up @@ -345,6 +346,42 @@ public String toString() {
return result.toString();
}

/**
* An AnalysisScope is converted to a JSON formatted variable using the loaders and exclusions
* hierarchy using ToJson. (Loaders) Primordial, Extension, Application, and Synthetic are the
* loaders keys; each one contains an arraylist of Strings. The exclusions contains an arraylist
* of strings.
*
* @return json variable containing contents of the AnalysisScope
*/
public String toJson() {
aakgna marked this conversation as resolved.
Show resolved Hide resolved
LinkedHashMap<String, Object> res = new LinkedHashMap<>();
LinkedHashMap<String, ArrayList<String>> loaders = new LinkedHashMap<>();
for (ClassLoaderReference loader : loadersByName.values()) {
ArrayList<String> arr = new ArrayList<>();
for (Module m : getModules(loader)) {
arr.add(m.toString());
}
msridhar marked this conversation as resolved.
Show resolved Hide resolved
loaders.put(loader.getName().toString(), arr);
}
res.put("Loaders", loaders);
ArrayList<String> arr2 = new ArrayList<>();
if (getExclusions() == null) {
res.put("Exclusions", arr2);
} else {
String[] exclusions = getExclusions().toString().split("\\|");
for (int i = 0; i < exclusions.length; i++) {
String word = exclusions[i];
word = word.replace("(", "");
word = word.replace(")", "");
arr2.add(word);
}
res.put("Exclusions", arr2);
}
Gson gson = new Gson();
return gson.toJson(res);
}

/**
* @return a String that describes exclusions from the analysis scope.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
package com.ibm.wala.core.tests.cha;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ibm.wala.core.tests.util.TestConstants;
import com.ibm.wala.core.util.config.AnalysisScopeReader;
import com.ibm.wala.core.util.io.FileProvider;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.ipa.cha.ClassHierarchyFactory;
import com.ibm.wala.properties.WalaProperties;
import com.ibm.wala.types.ClassLoaderReference;
import com.ibm.wala.types.TypeReference;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Type;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.jar.JarFile;
import org.junit.jupiter.api.Test;

public class AnalysisScopeTest {
Expand Down Expand Up @@ -55,4 +71,61 @@ public void testBaseScope() throws IOException, ClassHierarchyException {
ClassLoaderReference.Application, "Ljava/awt/AlphaComposite")),
"found unexpected class");
}

@Test
public void testToJson() throws IOException {
AnalysisScope scope =
AnalysisScopeReader.instance.readJavaScope(
TestConstants.WALA_TESTDATA,
new FileProvider().getFile("GUIExclusions.txt"),
AnalysisScopeTest.class.getClassLoader());
Gson gson = new Gson();
Type type = new TypeToken<LinkedHashMap<String, Object>>() {}.getType();
LinkedHashMap<String, Object> map = gson.fromJson(scope.toJson(), type);
assertEquals(
List.of("java\\/awt\\/.*", "javax\\/swing\\/.*", "sun\\/awt\\/.*", "sun\\/swing\\/.*"),
map.get("Exclusions"));

@SuppressWarnings("unchecked")
Map<String, List<String>> loaders = (Map<String, List<String>>) map.get("Loaders");
Set<String> loaderKeys =
new HashSet<>(List.of("Primordial", "Extension", "Application", "Synthetic"));
assertEquals(loaders.keySet(), loaderKeys);
assertEquals(2, loaders.get("Primordial").size());
assertThat(loaders.get("Primordial"), hasItem("Nested Jar File:primordial.jar.model"));
assertEquals(1, loaders.get("Application").size());
assertThat(
loaders.get("Application").get(0), containsString("com.ibm.wala.core.testdata_1.0.0.jar"));
assertEquals(0, loaders.get("Extension").size());
assertEquals(0, loaders.get("Synthetic").size());
}

@Test
public void testToJsonCustom() throws IOException {
AnalysisScope scope;
scope = AnalysisScope.createJavaAnalysisScope();
String[] stdlibs = WalaProperties.getJ2SEJarFiles();
Arrays.sort(stdlibs);
for (String stdlib : stdlibs) {
scope.addToScope(ClassLoaderReference.Primordial, new JarFile(stdlib));
scope.addToScope(ClassLoaderReference.Application, new JarFile(stdlib));
}
scope.setExclusions(null);
Gson gson = new Gson();
Type type = new TypeToken<LinkedHashMap<String, Object>>() {}.getType();
LinkedHashMap<String, Object> map = gson.fromJson(scope.toJson(), type);
assertEquals(List.of(), map.get("Exclusions"));

@SuppressWarnings("unchecked")
Map<String, List<String>> loaders = (Map<String, List<String>>) map.get("Loaders");
Set<String> loaderKeys =
new HashSet<>(List.of("Primordial", "Extension", "Application", "Synthetic"));
assertEquals(loaders.keySet(), loaderKeys);
assertEquals(stdlibs.length, loaders.get("Primordial").size());
assertTrue(loaders.get("Primordial").get(0).contains("/Contents/Home/jmods/java.base.jmod"));
Copy link
Member

Choose a reason for hiding this comment

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

This is still wrong, and tests are still failing. You need to take the relevant strings from the stdlibs array for this comparison, rather than just doing get(0) and comparing to a constant string.

assertEquals(stdlibs.length, loaders.get("Application").size());
assertTrue(loaders.get("Application").get(0).contains("/Contents/Home/jmods/java.base.jmod"));
assertEquals(0, loaders.get("Extension").size());
assertEquals(0, loaders.get("Synthetic").size());
}
}
Loading