Skip to content

Commit

Permalink
multiple layouts and more formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
GraxCode committed Jun 2, 2020
1 parent 38f87af commit 9654049
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
47 changes: 29 additions & 18 deletions src/me/nov/threadtear/graph/CFGPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import org.objectweb.asm.tree.*;

import com.mxgraph.layout.mxCompactTreeLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.util.mxCellRenderer;

Expand All @@ -29,6 +30,7 @@ public class CFGPanel extends JPanel {
private CFGraph graph;
private CFGComponent graphComponent;
private JScrollPane scrollPane;
public boolean useTreeLayout = false;

public CFGPanel(ClassNode cn) {
this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
Expand All @@ -41,6 +43,7 @@ public CFGPanel(ClassNode cn) {

leftActionPanel.add(new JLabel("Control flow graph"));
JComboBox<Object> methodSelection = new JComboBox<>(cn.methods.stream().map(m -> m.name + m.desc).toArray());
methodSelection.setPreferredSize(new Dimension(Math.min(400, methodSelection.getPreferredSize().width), methodSelection.getPreferredSize().height));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 4, 0, 0);
leftActionPanel.add(methodSelection, gbc);
Expand All @@ -58,8 +61,14 @@ public CFGPanel(ClassNode cn) {

JPanel rs = new JPanel();
rs.setLayout(new GridLayout(1, 5));
for (int i = 0; i < 3; i++)
for (int i = 0; i < 2; i++)
rs.add(new JPanel());
JComboBox<String> layout = new JComboBox<>(new String[] { "Hierarchial", "Compact" });
layout.addActionListener(a -> {
useTreeLayout = layout.getSelectedIndex() == 1;
generateGraph();
});
rs.add(layout);
JButton save = new JButton("Save");
save.addActionListener(l -> {
File parentDir = new File(System.getProperty("user.home") + File.separator + "Desktop");
Expand Down Expand Up @@ -143,22 +152,24 @@ public void generateGraph() {
}
}
graph.getView().setScale(1);
// new mxCircleLayout(graph).execute(graph.getDefaultParent());
// mxCompactTreeLayout layout = new mxCompactTreeLayout(graph);
// layout.setResetEdges(true);
// layout.setEdgeRouting(true);
// layout.setHorizontal(false);
// layout.setMoveTree(true);
// layout.setUseBoundingBox(true);
// layout.execute(graph.getDefaultParent());
PatchedHierarchicalLayout layout = new PatchedHierarchicalLayout(graph);
layout.setFineTuning(true);
layout.setIntraCellSpacing(25d);
layout.setInterRankCellSpacing(80d);
layout.setDisableEdgeStyle(true);
layout.setParallelEdgeSpacing(100d);
layout.setUseBoundingBox(true);
layout.execute(graph.getDefaultParent());
if (useTreeLayout) {
mxCompactTreeLayout layout = new mxCompactTreeLayout(graph);
layout.setResetEdges(true);
layout.setEdgeRouting(true);
layout.setHorizontal(false);
layout.setMoveTree(true);
layout.setUseBoundingBox(true);
layout.execute(graph.getDefaultParent());
} else {
PatchedHierarchicalLayout layout = new PatchedHierarchicalLayout(graph);
layout.setFineTuning(true);
layout.setIntraCellSpacing(20d);
layout.setInterRankCellSpacing(50d);
layout.setDisableEdgeStyle(true);
layout.setParallelEdgeSpacing(100d);
layout.setUseBoundingBox(true);
layout.execute(graph.getDefaultParent());
}
} finally {
graph.getModel().endUpdate();
}
Expand Down Expand Up @@ -192,7 +203,7 @@ private mxCell addBlock(mxCell parent, Block b, BlockVertex input) {
for (int i = 0; i < next.size(); i++) {
Block out = next.get(i);
if (out.equals(b)) {
graph.insertEdge(parent, null, null, v1, v1, "strokeColor=" + getEdgeColor(b, i) + ";");
graph.insertEdge(parent, null, "Infinite loop", v1, v1, "strokeColor=" + getEdgeColor(b, i) + ";fontColor=" + Strings.hexColor(getForeground().brighter()));
} else {
mxCell vertexOut = addBlock(parent, out, vertex);
graph.insertEdge(parent, null, null, v1, vertexOut, "strokeColor=" + getEdgeColor(b, i) + ";");
Expand Down
8 changes: 6 additions & 2 deletions src/me/nov/threadtear/graph/vertex/BlockVertex.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ public String toString() {
if (text == null) {
StringBuilder sb = new StringBuilder();
if (block.getTCB() != null) {
String type = block.getTCB().type;
TryCatchBlockNode tcb = block.getTCB();
String type = tcb.type;
sb.append("<html>");
sb.append(Html.mono("tcb-handler " + block.getTCBIndex()) + ": (" + (type == null || type.equals("java/lang/Throwable") ? "all throwables" : type.replace('/', '.')) + ")");
sb.append(Html.mono("tcb-handler " + OpFormat.labelToString(tcb.start) + " to " + OpFormat.labelToString(tcb.end) + " - N" + block.getTCBIndex()));
sb.append(": (");
sb.append((type == null || type.equals("java/lang/Throwable") ? "catch all" : type.replace('/', '.')));
sb.append(")");
sb.append("\n");
}
for (AbstractInsnNode ain : nodes) {
Expand Down
12 changes: 8 additions & 4 deletions src/me/nov/threadtear/util/format/OpFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,15 +598,15 @@ public static String toString(AbstractInsnNode ain) {
return s + " " + descToString(mnin.desc) + Strings.repeat("[]", mnin.dims * 2);
case AbstractInsnNode.TABLESWITCH_INSN:
TableSwitchInsnNode ts = (TableSwitchInsnNode) ain;
return s + " [Keys: " + ts.min + " to " + ts.max + "], [Labels: " + ts.labels.stream().map(ln -> getLabelString(getLabelIndex(ln))).collect(Collectors.joining(", ")) + ", d: "
return s + " [Keys: " + ts.min + " to " + ts.max + "], [Labels: " + ts.labels.stream().map(OpFormat::labelToString).collect(Collectors.joining(", ")) + ", d: "
+ getLabelString(getLabelIndex(ts.dflt)) + "]";
case AbstractInsnNode.LOOKUPSWITCH_INSN:
LookupSwitchInsnNode ls = (LookupSwitchInsnNode) ain;
return s + " [Keys: " + ls.keys.stream().map(String::valueOf).collect(Collectors.joining(", ")) + "], [Labels: "
+ ls.labels.stream().map(ln -> getLabelString(getLabelIndex(ln))).collect(Collectors.joining(", ")) + ", d: " + getLabelString(getLabelIndex(ls.dflt)) + "]";
+ ls.labels.stream().map(OpFormat::labelToString).collect(Collectors.joining(", ")) + ", d: " + labelToString(ls.dflt) + "]";
case AbstractInsnNode.JUMP_INSN:
JumpInsnNode jin = (JumpInsnNode) ain;
return s + " " + getLabelString(getLabelIndex(jin.label));
return s + " " + labelToString(jin.label);
case AbstractInsnNode.LDC_INSN:
LdcInsnNode ldc = (LdcInsnNode) ain;
if (ldc.cst instanceof String) {
Expand Down Expand Up @@ -634,13 +634,17 @@ public static String toString(AbstractInsnNode ain) {
case AbstractInsnNode.LABEL:
LabelNode ln = (LabelNode) ain;
boolean hasLine = (ln.getNext() != null && ln.getNext().getType() == AbstractInsnNode.LINE);
return Html.mono("label " + getLabelString(getLabelIndex(ln)) + (hasLine ? ", line " + ((LineNumberNode) ln.getNext()).line : "") + ":");
return Html.mono("label " + labelToString(ln) + (hasLine ? ", line " + ((LineNumberNode) ln.getNext()).line : "") + ":");
case AbstractInsnNode.INSN:
return s;
}
return "";
}

public static String labelToString(LabelNode ln) {
return getLabelString(getLabelIndex(ln));
}

public static String descToString(String desc) {
if (desc.startsWith("(")) {
return "(" + Arrays.stream(Type.getArgumentTypes(desc)).map(OpFormat::getTypeClassName).collect(Collectors.joining(", ")) + ") " + getTypeClassName(Type.getReturnType(desc));
Expand Down

0 comments on commit 9654049

Please sign in to comment.