2023-11-15 17:32:20 +00:00
|
|
|
package ch.usi.inf.sp.callgraph;
|
|
|
|
|
2023-11-15 23:13:33 +00:00
|
|
|
import org.objectweb.asm.Opcodes;
|
|
|
|
|
2023-11-15 17:32:20 +00:00
|
|
|
import java.io.FileWriter;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintWriter;
|
2023-11-15 23:13:33 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.stream.Collectors;
|
2023-11-15 17:32:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dump out information about the given ClassHierarchy.
|
|
|
|
*
|
2023-11-15 21:51:30 +00:00
|
|
|
* @author maggicl@usi.ch
|
2023-11-15 17:32:20 +00:00
|
|
|
* @author Matthias.Hauswirth@usi.ch
|
|
|
|
*/
|
|
|
|
public final class CallGraphRenderer {
|
|
|
|
|
2023-11-15 23:13:33 +00:00
|
|
|
private final Map<String, Integer> identifiers = new HashMap<>();
|
|
|
|
private final Map<Integer, Boolean> nodeBuilt = new HashMap<>();
|
|
|
|
|
|
|
|
private String identifier(String what, boolean fromNode) {
|
|
|
|
return "node" + identifiers.computeIfAbsent(what, (k) -> {
|
|
|
|
int key = identifiers.size() + 1;
|
|
|
|
nodeBuilt.compute(key, (ke, v) -> v == null ? fromNode : (v || fromNode));
|
|
|
|
return key;
|
|
|
|
});
|
2023-11-15 21:51:30 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 23:13:33 +00:00
|
|
|
private String classNodeName(ClassType type) {
|
|
|
|
return identifier(type.getInternalName(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String methodDescription(Method e) {
|
|
|
|
return (e.getVerboseModifiers() + " " +
|
|
|
|
e.getName().replaceAll("<", "<").replaceAll(">", ">") +
|
|
|
|
e.getDescriptor()).trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String classNodeDescriptor(ClassType type) {
|
|
|
|
final String className = type.getInternalName();
|
|
|
|
|
|
|
|
if (!type.isResolved()) {
|
|
|
|
return "[shape=ellipse,style=dashed,label=\"" + className + "\"]";
|
|
|
|
}
|
|
|
|
|
|
|
|
final String methods = type.getMethods().stream()
|
|
|
|
.map(CallGraphRenderer::methodDescription)
|
|
|
|
.collect(Collectors.joining("\\n"));
|
|
|
|
|
|
|
|
final String style = type.isAbstract() ? "dashed" : "solid";
|
|
|
|
|
|
|
|
return "[shape=record,style=" + style + ",label=\"{" + className + "|" + methods + "}\"]";
|
|
|
|
}
|
|
|
|
|
|
|
|
private String methodNodeName(ClassHierarchy hierarchy, String className, String name, String descriptor, boolean fromNode) {
|
|
|
|
try {
|
|
|
|
if (!hierarchy.getOrCreateClass(className).isResolved()) {
|
|
|
|
return identifier(className, true);
|
|
|
|
}
|
|
|
|
} catch (TypeInconsistencyException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return identifier(className + " " + name + " " + descriptor, fromNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String methodNodeDescriptor(Method method) {
|
|
|
|
return "[shape=rectangle,style=filled,fillcolor=lightgreen,label=\"" + method.getDeclaringClassName() +
|
|
|
|
"\\n" + methodDescription(method) + "\"]";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static String getEdge(String from, String to, int opCode, String color) {
|
|
|
|
String style;
|
|
|
|
switch (opCode) {
|
|
|
|
case Opcodes.INVOKEVIRTUAL: style = "dashed"; break;
|
|
|
|
case Opcodes.INVOKEINTERFACE: style = "dotted"; break;
|
|
|
|
default: style = "solid";
|
|
|
|
}
|
|
|
|
|
|
|
|
return from + " -> " + to + " [style=" + style + ",color=" + color + "]";
|
2023-11-15 21:51:30 +00:00
|
|
|
}
|
|
|
|
|
2023-11-15 17:32:20 +00:00
|
|
|
public void dumpDot(final ClassHierarchy hierarchy, final String fileName) throws IOException {
|
|
|
|
final PrintWriter pw = new PrintWriter(new FileWriter(fileName));
|
|
|
|
pw.println("digraph CallGraph {");
|
|
|
|
pw.println(" rankdir=\"BT\"");
|
2023-11-15 23:13:33 +00:00
|
|
|
|
|
|
|
final ArrayList<String> edges = new ArrayList<>();
|
|
|
|
|
2023-11-15 17:32:20 +00:00
|
|
|
for (final Type type : hierarchy.getTypes()) {
|
|
|
|
if (type instanceof ClassType) {
|
2023-11-15 21:51:30 +00:00
|
|
|
final ClassType classType = (ClassType) type;
|
2023-11-15 23:13:33 +00:00
|
|
|
final ClassType superClassType = classType.getSuperClass();
|
|
|
|
|
|
|
|
pw.println(classNodeName(classType) + " " + classNodeDescriptor(classType) + ";");
|
|
|
|
|
|
|
|
if (superClassType != null) {
|
|
|
|
edges.add(getEdge(classNodeName(classType), classNodeName(superClassType), 0, "black"));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final ClassType c : classType.getInterfaces()) {
|
|
|
|
edges.add(getEdge(classNodeName(classType), classNodeName(c), Opcodes.INVOKEVIRTUAL, "black"));
|
|
|
|
}
|
2023-11-15 21:51:30 +00:00
|
|
|
|
2023-11-15 23:13:33 +00:00
|
|
|
for (final Method m : classType.getMethods()) {
|
|
|
|
final String mName = methodNodeName(hierarchy, m.getDeclaringClassName(), m.getName(), m.getDescriptor(), true);
|
2023-11-15 17:32:20 +00:00
|
|
|
|
2023-11-15 23:13:33 +00:00
|
|
|
pw.println(mName + " " + methodNodeDescriptor(m) + ";");
|
|
|
|
edges.add(getEdge(classNodeName(classType), mName, 0, "lightgreen"));
|
|
|
|
|
|
|
|
for (final CallSite c : m.getCallSites()) {
|
|
|
|
final String cName = methodNodeName(hierarchy,
|
|
|
|
c.getDeclaredTargetClassName(),
|
|
|
|
c.getTargetMethodName(),
|
|
|
|
c.getTargetMethodDescriptor(), false);
|
|
|
|
|
|
|
|
edges.add(getEdge(mName, cName, c.getOpcode(), "blue"));
|
|
|
|
|
|
|
|
for (final ClassType p : c.getPossibleTargetClasses()) {
|
|
|
|
final String pName = methodNodeName(hierarchy,
|
|
|
|
p.getInternalName(),
|
|
|
|
c.getTargetMethodName(),
|
|
|
|
c.getTargetMethodDescriptor(), false);
|
|
|
|
|
|
|
|
edges.add(getEdge(mName, pName, c.getOpcode(), "red"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-15 17:32:20 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-15 23:13:33 +00:00
|
|
|
|
|
|
|
// Add missing node declarations (happens with methods in classes not scanned)
|
|
|
|
final Map<Integer, String> reverseIdentifiers = identifiers.entrySet().stream()
|
|
|
|
.collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
|
|
|
|
for (Map.Entry<Integer, Boolean> e : nodeBuilt.entrySet()) {
|
|
|
|
if (!e.getValue()) {
|
|
|
|
pw.println("node" + e.getKey() + " [label=\"" + reverseIdentifiers.get(e.getKey()) + "\"]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pw.append('\n');
|
|
|
|
for (final String edge : edges) {
|
|
|
|
pw.println(edge);
|
|
|
|
}
|
2023-11-15 17:32:20 +00:00
|
|
|
pw.println("}");
|
|
|
|
pw.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|