Skip to content

Commit

Permalink
Rename 'graph' variables to disambiguate between call/inheritance graphs
Browse files Browse the repository at this point in the history
Mostly for better project-wide text search
  • Loading branch information
Col-E committed Dec 6, 2024
1 parent ed2d66e commit f0e9437
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void transform(@Nonnull JvmTransformerContext context, @Nonnull Workspace
@Nonnull WorkspaceResource resource, @Nonnull JvmClassBundle bundle,
@Nonnull JvmClassInfo classInfo) throws TransformationException {
// TODO: Instead of a map, we should make a workspace setup call first
InheritanceGraph graph = graphCache.computeIfAbsent(workspace, w -> w == workspaceManager.getCurrent() ?
InheritanceGraph inheritanceGraph = graphCache.computeIfAbsent(workspace, w -> w == workspaceManager.getCurrent() ?
graphService.getCurrentWorkspaceInheritanceGraph() :
graphService.newInheritanceGraph(workspace));

Expand Down Expand Up @@ -140,7 +140,7 @@ else if (field.hasPrivateModifier())

// Only analyze if we see static setters
if (clinit != null && hasStaticSetters(clinit)) {
ReInterpreter interpreter = new ReInterpreter(graph);
ReInterpreter interpreter = new ReInterpreter(inheritanceGraph);
ReAnalyzer analyzer = new ReAnalyzer(interpreter);
try {
Frame<ReValue>[] frames = analyzer.analyze(className, clinit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public TransformationApplier newApplier(@Nonnull Workspace workspace) {
return newApplier(workspace, Objects.requireNonNull(graphService.getCurrentWorkspaceInheritanceGraph(), "Graph not created"));

// Need to make a new graph for the given workspace
InheritanceGraph graph = graphService.newInheritanceGraph(workspace);
return newApplier(workspace, graph);
InheritanceGraph inheritanceGraph = graphService.newInheritanceGraph(workspace);
return newApplier(workspace, inheritanceGraph);
}

/**
Expand All @@ -67,8 +67,8 @@ public TransformationApplier newApplierForCurrentWorkspace() {
Workspace workspace = workspaceManager.getCurrent();
if (workspace == null)
return null;
InheritanceGraph graph = Objects.requireNonNull(graphService.getCurrentWorkspaceInheritanceGraph(), "Graph not created");
return newApplier(workspace, graph);
InheritanceGraph inheritanceGraph = Objects.requireNonNull(graphService.getCurrentWorkspaceInheritanceGraph(), "Graph not created");
return newApplier(workspace, inheritanceGraph);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@
@Dependent
public class ThrowablePropertyAssigningProcessor implements WorkspaceProcessor, ResourceJvmClassListener, ResourceAndroidClassListener {
private static final String THROWABLE = "java/lang/Throwable";
private final InheritanceGraph graph;
private final InheritanceGraph inheritanceGraph;

@Inject
public ThrowablePropertyAssigningProcessor(@Nonnull InheritanceGraphService graphService) {
this.graph = Objects.requireNonNull(graphService.getCurrentWorkspaceInheritanceGraph(), "Graph not created");
this.inheritanceGraph = Objects.requireNonNull(graphService.getCurrentWorkspaceInheritanceGraph(), "Graph not created");
}

@Override
public void onWorkspaceOpened(@Nonnull Workspace workspace) {
graph.getVertex(THROWABLE).allChildren().forEach(vertex -> {
inheritanceGraph.getVertex(THROWABLE).allChildren().forEach(vertex -> {
ClassInfo classInfo = vertex.getValue();
ThrowableProperty.set(classInfo);
});
Expand Down Expand Up @@ -70,7 +70,7 @@ public String name() {
}

private void handle(@Nonnull ClassInfo cls) {
InheritanceVertex vertex = graph.getVertex(cls.getName());
InheritanceVertex vertex = inheritanceGraph.getVertex(cls.getName());
if (vertex != null && vertex.hasParent(THROWABLE))
ThrowableProperty.set(cls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@
*/
public class ReInterpreter extends Interpreter<ReValue> implements Opcodes {
private static final Logger logger = Logging.get(ReInterpreter.class);
private final InheritanceGraph graph;
private final InheritanceGraph inheritanceGraph;
private GetStaticLookup getStaticLookup;
private GetFieldLookup getFieldLookup;
private InvokeStaticLookup invokeStaticLookup;
private InvokeVirtualLookup invokeVirtualLookup;

public ReInterpreter(@Nonnull InheritanceGraph graph) {
public ReInterpreter(@Nonnull InheritanceGraph inheritanceGraph) {
super(RecafConstants.getAsmVersion());
this.graph = graph;
this.inheritanceGraph = inheritanceGraph;
}

public void setGetStaticLookup(@Nullable GetStaticLookup getStaticLookup) {
Expand Down Expand Up @@ -627,7 +627,7 @@ public ReValue merge(@Nonnull ReValue value1, @Nonnull ReValue value2) {
@Nonnull
private Type getSuperClass(@Nonnull Type type) {
String name = type.getInternalName();
InheritanceVertex vertex = graph.getVertex(name);
InheritanceVertex vertex = inheritanceGraph.getVertex(name);
if (vertex == null)
return Types.OBJECT_TYPE;
String superName = vertex.getValue().getSuperName();
Expand All @@ -636,7 +636,7 @@ private Type getSuperClass(@Nonnull Type type) {

private boolean isInterface(@Nonnull Type type) {
String name = type.getInternalName();
InheritanceVertex vertex = graph.getVertex(name);
InheritanceVertex vertex = inheritanceGraph.getVertex(name);
if (vertex == null)
return false;
return vertex.getValue().hasInterfaceModifier();
Expand All @@ -645,6 +645,6 @@ private boolean isInterface(@Nonnull Type type) {
private boolean isAssignableFrom(@Nonnull Type type1, @Nonnull Type type2) {
String name1 = type1.getInternalName();
String name2 = type2.getInternalName();
return graph.isAssignableFrom(name1, name2);
return inheritanceGraph.isAssignableFrom(name1, name2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,29 @@
* @author Matt Coley
*/
public class WorkspaceClassWriter extends ClassWriter {
private final InheritanceGraph graph;
private final InheritanceGraph inheritanceGraph;

/**
* @param graph
* Graph to pull inheritance relations from.
* @param inheritanceGraph
* Inheritance graph to pull inheritance relations from.
* @param flags
* Writer flags.
*/
public WorkspaceClassWriter(@Nonnull InheritanceGraph graph, int flags) {
this(graph, null, flags);
public WorkspaceClassWriter(@Nonnull InheritanceGraph inheritanceGraph, int flags) {
this(inheritanceGraph, null, flags);
}

/**
* @param graph
* Graph to pull inheritance relations from.
* @param inheritanceGraph
* Inheritance graph to pull inheritance relations from.
* @param reader
* Reader to pre-populate the constant pool with. Speeds up writing process a bit.
* @param flags
* Writer flags.
*/
public WorkspaceClassWriter(@Nonnull InheritanceGraph graph, @Nullable ClassReader reader, int flags) {
public WorkspaceClassWriter(@Nonnull InheritanceGraph inheritanceGraph, @Nullable ClassReader reader, int flags) {
super(reader, flags);
this.graph = graph;
this.inheritanceGraph = inheritanceGraph;
}

@Override
Expand All @@ -44,6 +44,6 @@ protected String getCommonSuperClass(String type1, String type2) {
return "java/lang/Object";

// Find common parent in workspace
return graph.getCommon(type1, type2);
return inheritanceGraph.getCommon(type1, type2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ void testCalleeCallerRelation() throws IOException {
JvmClassInfo mainClass = pathUser.getValue().asJvmClass();
JvmClassInfo functionClass = pathFunc.getValue().asJvmClass();

CallGraph graph = graph(workspace);
CallGraph callGraph = newCallGraph(workspace);

ClassMethodsContainer containerMain = graph.getClassMethodsContainer(mainClass);
ClassMethodsContainer containerFunction = graph.getClassMethodsContainer(functionClass);
ClassMethodsContainer containerMain = callGraph.getClassMethodsContainer(mainClass);
ClassMethodsContainer containerFunction = callGraph.getClassMethodsContainer(functionClass);

// Get outbound calls for main. Should just be to 'new StringConsumer()' and 'StringConsumer.accept(String)'
MethodVertex mainVertex = containerMain.getVertex("main", "([Ljava/lang/String;)V");
Expand Down Expand Up @@ -73,40 +73,40 @@ void testUnresolvedCall() {
assertNotNull(pathUser, "Missing FooCaller class");
JvmClassInfo mainClass = pathUser.getValue().asJvmClass();

CallGraph graph = graph(workspace);
CallGraph callGraph = newCallGraph(workspace);

// Get outbound calls for call(Foo). Should just be to 'foo.bar()' which is unresolved
ClassMethodsContainer fooCaller = graph.getClassMethodsContainer(mainClass);
ClassMethodsContainer fooCaller = callGraph.getClassMethodsContainer(mainClass);
MethodVertex callVertex = fooCaller.getVertex("call", "(LFoo;)V");
assertNotNull(callVertex, "Missing method vertex for 'call'");
assertEquals(0, callVertex.getCalls().size());
assertEquals(1, graph.getUnresolvedDeclarations().get("Foo").size(), "Expected to have unresolved call to Foo.bar()");
assertEquals(1, callGraph.getUnresolvedDeclarations().get("Foo").size(), "Expected to have unresolved call to Foo.bar()");

// Add the missing Foo class to the workspace
workspace.getPrimaryResource().getJvmClassBundle().put(new JvmClassInfoBuilder(fooBytes).build());

// The call to Foo.bar() should be resolved now
assertEquals(1, callVertex.getCalls().size());
assertEquals(0, graph.getUnresolvedDeclarations().size(), "Expected to have resolved unresolved call to Foo.bar()");
assertEquals(0, callGraph.getUnresolvedDeclarations().size(), "Expected to have resolved unresolved call to Foo.bar()");

// TODO: Make a similar test case, but in reverse.
// We probably want to prune the call-graph model when things get removed.
}

@Nonnull
static CallGraph graph(@Nonnull Workspace workspace) {
CallGraph graph = new CallGraph(workspace);
graph.initialize();
static CallGraph newCallGraph(@Nonnull Workspace workspace) {
CallGraph callGraph = new CallGraph(workspace);
callGraph.initialize();

// Need to wait until async population of graph contents is done.
ObservableBoolean ready = graph.isReady();
ObservableBoolean ready = callGraph.isReady();
assertDoesNotThrow(() -> {
while (!ready.getValue()) {
Thread.sleep(100);
}
});

return graph;
return callGraph;
}

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
class InheritanceAndRenamingTest extends TestBase {
static Workspace workspace;
static InheritanceGraph graph;
static InheritanceGraph inheritanceGraph;
static MappingApplier mappingApplier;
static JvmClassInfo[] generatedClasses;

Expand All @@ -44,8 +44,8 @@ static void setup() {
workspaceManager.setCurrent(workspace);

// Get graph
graph = recaf.get(InheritanceGraphService.class).getCurrentWorkspaceInheritanceGraph();
graph.toString(); // Force immediate init.
inheritanceGraph = recaf.get(InheritanceGraphService.class).getCurrentWorkspaceInheritanceGraph();
inheritanceGraph.toString(); // Force immediate init.

// Get mapping applier
mappingApplier = recaf.get(MappingApplier.class);
Expand All @@ -56,7 +56,7 @@ void test() {
// Verify initial state
for (int i = 1; i <= 5; i++) {
String name = "I" + i;
InheritanceVertex vertex = graph.getVertex(name);
InheritanceVertex vertex = inheritanceGraph.getVertex(name);
assertNotNull(vertex, "Graph missing '" + name + "'");
}

Expand All @@ -70,15 +70,15 @@ void test() {
// Very old classes are removed from the graph
for (int i = 1; i <= 5; i++) {
String name = "I" + i;
InheritanceVertex vertex = graph.getVertex(name);
InheritanceVertex vertex = inheritanceGraph.getVertex(name);
assertNull(vertex, "Graph contains pre-mapped '" + name + "'");
}

// Verify the new classes are added to the graph
InheritanceVertex objectVertex = graph.getVertex("java/lang/Object");
InheritanceVertex objectVertex = inheritanceGraph.getVertex("java/lang/Object");
for (int i = 1; i <= 5; i++) {
String name = "R" + i;
InheritanceVertex vertex = graph.getVertex(name);
InheritanceVertex vertex = inheritanceGraph.getVertex(name);
assertNotNull(vertex, "Graph missing post-mapped '" + name + "'");
if (i > 1) {
Set<InheritanceVertex> parents = vertex.getParents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class InheritanceGraphTest extends TestBase {
static Workspace workspace;
static InheritanceGraph graph;
static InheritanceGraph inheritanceGraph;

@BeforeAll
static void setup() throws IOException {
Expand All @@ -38,15 +38,15 @@ static void setup() throws IOException {
workspaceManager.setCurrent(workspace);

// Get graph
graph = recaf.get(InheritanceGraphService.class).getCurrentWorkspaceInheritanceGraph();
inheritanceGraph = recaf.get(InheritanceGraphService.class).getCurrentWorkspaceInheritanceGraph();
}

@Test
void getVertex() {
String appleName = Inheritance.Apple.class.getName().replace('.', '/');

// Check vertex
InheritanceVertex vertex = graph.getVertex(appleName);
InheritanceVertex vertex = inheritanceGraph.getVertex(appleName);
assertNotNull(vertex, "Could not get Apple vertex from workspace");
assertEquals(appleName, vertex.getName(), "Vertex should have same name as lookup");

Expand All @@ -70,7 +70,7 @@ void getVertex() {
"Apple missing parent: Edible");

// Check awareness of library methods
vertex = graph.getVertex(StringConsumer.class.getName().replace('.', '/'));
vertex = inheritanceGraph.getVertex(StringConsumer.class.getName().replace('.', '/'));
assertTrue(vertex.hasMethod("accept", "(Ljava/lang/Object;)V"));
assertTrue(vertex.hasMethod("accept", "(Ljava/lang/String;)V")); // Redirects to Object method
assertTrue(vertex.isLibraryMethod("accept", "(Ljava/lang/Object;)V")); // From Consumer<T>
Expand All @@ -86,7 +86,7 @@ void getVertexFamily() {
appleName.replace("Apple", "Edible"), // parent of apple
appleName.replace("Apple", "Grape") // shared parent edible
);
Set<InheritanceVertex> family = graph.getVertexFamily(appleName, false);
Set<InheritanceVertex> family = inheritanceGraph.getVertexFamily(appleName, false);
assertEquals(5, family.size());
assertEquals(names, family.stream().map(InheritanceVertex::getName).collect(Collectors.toSet()));
}
Expand All @@ -98,11 +98,11 @@ void getCommon() {
String grapeName = Inheritance.Grape.class.getName().replace('.', '/');

// Compare obvious case --> edible
String commonType = graph.getCommon(appleName, grapeName);
String commonType = inheritanceGraph.getCommon(appleName, grapeName);
assertEquals(edibleName, commonType, "Common type of Apple/Grape should be Edible");

// Compare with bogus --> object
commonType = graph.getCommon(appleName, UUID.randomUUID().toString());
commonType = inheritanceGraph.getCommon(appleName, UUID.randomUUID().toString());
assertEquals(Types.OBJECT_TYPE.getInternalName(), commonType,
"Common type of two unrelated classes should be Object");
}
Expand All @@ -114,12 +114,12 @@ void isAssignableFrom() {
String grapeName = Inheritance.Grape.class.getName().replace('.', '/');

// Edible.class.isAssignableFrom(Apple.class) --> true
assertTrue(graph.isAssignableFrom(edibleName, appleName), "Edible should be assignable from Apple");
assertTrue(graph.isAssignableFrom(edibleName, grapeName), "Edible should be assignable from Grape");
assertTrue(inheritanceGraph.isAssignableFrom(edibleName, appleName), "Edible should be assignable from Apple");
assertTrue(inheritanceGraph.isAssignableFrom(edibleName, grapeName), "Edible should be assignable from Grape");

// Apple.class.isAssignableFrom(Edible.class) --> false
assertFalse(graph.isAssignableFrom(appleName, edibleName), "Apple should not be assignable from Edible");
assertFalse(graph.isAssignableFrom(grapeName, edibleName), "Grape should not be assignable from Edible");
assertFalse(inheritanceGraph.isAssignableFrom(appleName, edibleName), "Apple should not be assignable from Edible");
assertFalse(inheritanceGraph.isAssignableFrom(grapeName, edibleName), "Grape should not be assignable from Edible");
}

@Test
Expand All @@ -131,11 +131,11 @@ void getFamilyOfThrowable() {
// Assert that looking at child types of throwable finds NotFoodException.
// Our class extends Exception, which extends Throwable. So there should be a vertex between Throwable and our type.
JvmClassInfo notFoodException = classPath.getValue().asJvmClass();
List<ClassInfo> exceptionClasses = graph.getVertex("java/lang/Exception").getAllChildren().stream()
List<ClassInfo> exceptionClasses = inheritanceGraph.getVertex("java/lang/Exception").getAllChildren().stream()
.map(InheritanceVertex::getValue)
.toList();
assertTrue(exceptionClasses.contains(notFoodException), "Subtypes of 'Exception' did not yield 'NotFoodException'");
List<ClassInfo> throwableClasses = graph.getVertex("java/lang/Throwable").getAllChildren().stream()
List<ClassInfo> throwableClasses = inheritanceGraph.getVertex("java/lang/Throwable").getAllChildren().stream()
.map(InheritanceVertex::getValue)
.toList();
assertTrue(throwableClasses.contains(notFoodException), "Subtypes of 'Throwable' did not yield 'NotFoodException'");
Expand Down
Loading

0 comments on commit f0e9437

Please sign in to comment.