make NodeDecorator generic
This commit is contained in:
parent
de1b61db5e
commit
45fe53a8bf
@ -124,7 +124,7 @@ public class PDFControlDependenceGraph {
|
||||
String dotExe = wp.getProperty(WalaExamplesProperties.DOT_EXE);
|
||||
String gvExe = wp.getProperty(WalaExamplesProperties.PDFVIEW_EXE);
|
||||
|
||||
DotUtil.dotify(cdg, PDFViewUtil.makeIRDecorator(ir), dotFile, psFile, dotExe);
|
||||
DotUtil.<ISSABasicBlock>dotify(cdg, PDFViewUtil.makeIRDecorator(ir), dotFile, psFile, dotExe);
|
||||
|
||||
return PDFViewUtil.launchPDFView(psFile, gvExe);
|
||||
|
||||
|
||||
@ -177,11 +177,10 @@ public class PDFSDG {
|
||||
return GraphSlicer.prune(sdg, f);
|
||||
}
|
||||
|
||||
private static NodeDecorator makeNodeDecorator() {
|
||||
return new NodeDecorator() {
|
||||
private static NodeDecorator<Statement> makeNodeDecorator() {
|
||||
return new NodeDecorator<Statement>() {
|
||||
@Override
|
||||
public String getLabel(Object o) throws WalaException {
|
||||
Statement s = (Statement) o;
|
||||
public String getLabel(Statement s) throws WalaException {
|
||||
switch (s.getKind()) {
|
||||
case HEAP_PARAM_CALLEE:
|
||||
case HEAP_PARAM_CALLER:
|
||||
|
||||
@ -256,11 +256,10 @@ public class PDFSlice {
|
||||
/**
|
||||
* @return a NodeDecorator that decorates statements in a slice for a dot-ted representation
|
||||
*/
|
||||
public static NodeDecorator makeNodeDecorator() {
|
||||
return new NodeDecorator() {
|
||||
public static NodeDecorator<Statement> makeNodeDecorator() {
|
||||
return new NodeDecorator<Statement>() {
|
||||
@Override
|
||||
public String getLabel(Object o) throws WalaException {
|
||||
Statement s = (Statement) o;
|
||||
public String getLabel(Statement s) throws WalaException {
|
||||
switch (s.getKind()) {
|
||||
case HEAP_PARAM_CALLEE:
|
||||
case HEAP_PARAM_CALLER:
|
||||
|
||||
@ -51,38 +51,38 @@ public class PDFViewUtil {
|
||||
* @throws IllegalArgumentException if ir is null
|
||||
*/
|
||||
public static Process ghostviewIR(IClassHierarchy cha, IR ir, String pdfFile, String dotFile, String dotExe, String pdfViewExe,
|
||||
NodeDecorator annotations) throws WalaException {
|
||||
NodeDecorator<ISSABasicBlock> annotations) throws WalaException {
|
||||
|
||||
if (ir == null) {
|
||||
throw new IllegalArgumentException("ir is null");
|
||||
}
|
||||
Graph<? extends ISSABasicBlock> g = ir.getControlFlowGraph();
|
||||
Graph<ISSABasicBlock> g = ir.getControlFlowGraph();
|
||||
|
||||
NodeDecorator labels = makeIRDecorator(ir);
|
||||
NodeDecorator<ISSABasicBlock> labels = makeIRDecorator(ir);
|
||||
if (annotations != null) {
|
||||
labels = new ConcatenatingNodeDecorator(annotations, labels);
|
||||
labels = new ConcatenatingNodeDecorator<ISSABasicBlock>(annotations, labels);
|
||||
}
|
||||
|
||||
g = CFGSanitizer.sanitize(ir, cha);
|
||||
|
||||
DotUtil.dotify(g, labels, dotFile, pdfFile, dotExe);
|
||||
DotUtil.<ISSABasicBlock>dotify(g,labels,dotFile,pdfFile,dotExe);
|
||||
|
||||
return launchPDFView(pdfFile, pdfViewExe);
|
||||
}
|
||||
|
||||
public static NodeDecorator makeIRDecorator(IR ir) {
|
||||
public static NodeDecorator<ISSABasicBlock> makeIRDecorator(IR ir) {
|
||||
if (ir == null) {
|
||||
throw new IllegalArgumentException("ir is null");
|
||||
}
|
||||
final HashMap<BasicBlock, String> labelMap = HashMapFactory.make();
|
||||
final HashMap<ISSABasicBlock,String> labelMap = HashMapFactory.make();
|
||||
for (Iterator it = ir.getControlFlowGraph().iterator(); it.hasNext();) {
|
||||
SSACFG.BasicBlock bb = (SSACFG.BasicBlock) it.next();
|
||||
labelMap.put(bb, getNodeLabel(ir, bb));
|
||||
}
|
||||
NodeDecorator labels = new NodeDecorator() {
|
||||
NodeDecorator<ISSABasicBlock> labels = new NodeDecorator<ISSABasicBlock>() {
|
||||
@Override
|
||||
public String getLabel(Object o) {
|
||||
return labelMap.get(o);
|
||||
public String getLabel(ISSABasicBlock bb) {
|
||||
return labelMap.get(bb);
|
||||
}
|
||||
};
|
||||
return labels;
|
||||
@ -90,21 +90,22 @@ public class PDFViewUtil {
|
||||
|
||||
/**
|
||||
* A node decorator which concatenates the labels from two other node decorators
|
||||
* @param <T> the type of the node
|
||||
*/
|
||||
private final static class ConcatenatingNodeDecorator implements NodeDecorator {
|
||||
private final static class ConcatenatingNodeDecorator<T> implements NodeDecorator<T> {
|
||||
|
||||
private final NodeDecorator A;
|
||||
private final NodeDecorator<T> A;
|
||||
|
||||
private final NodeDecorator B;
|
||||
private final NodeDecorator<T> B;
|
||||
|
||||
ConcatenatingNodeDecorator(NodeDecorator A, NodeDecorator B) {
|
||||
ConcatenatingNodeDecorator(NodeDecorator<T> A, NodeDecorator<T> B) {
|
||||
this.A = A;
|
||||
this.B = B;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getLabel(Object o) throws WalaException {
|
||||
return A.getLabel(o) + B.getLabel(o);
|
||||
public String getLabel(T n) throws WalaException {
|
||||
return A.getLabel(n) + B.getLabel(n);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -73,13 +73,17 @@ public class DotUtil {
|
||||
|
||||
|
||||
/**
|
||||
* @param <T> the type of a graph node
|
||||
*/
|
||||
public static <T> void dotify(Graph<T> g, NodeDecorator labels, String dotFile, String outputFile, String dotExe)
|
||||
public static <T> void dotify(Graph<T> g, NodeDecorator<T> labels, String dotFile, String outputFile, String dotExe)
|
||||
throws WalaException {
|
||||
dotify(g, labels, null, dotFile, outputFile, dotExe);
|
||||
}
|
||||
|
||||
public static <T> void dotify(Graph<T> g, NodeDecorator labels, String title, String dotFile, String outputFile, String dotExe)
|
||||
/**
|
||||
* @param <T> the type of a graph node
|
||||
*/
|
||||
public static <T> void dotify(Graph<T> g, NodeDecorator<T> labels, String title, String dotFile, String outputFile, String dotExe)
|
||||
throws WalaException {
|
||||
if (g == null) {
|
||||
throw new IllegalArgumentException("g is null");
|
||||
@ -149,7 +153,7 @@ public class DotUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> File writeDotFile(Graph<T> g, NodeDecorator labels, String title, String dotfile) throws WalaException {
|
||||
public static <T> File writeDotFile(Graph<T> g, NodeDecorator<T> labels, String title, String dotfile) throws WalaException {
|
||||
|
||||
if (g == null) {
|
||||
throw new IllegalArgumentException("g is null");
|
||||
@ -176,7 +180,7 @@ public class DotUtil {
|
||||
* @return StringBuffer holding dot output representing G
|
||||
* @throws WalaException
|
||||
*/
|
||||
private static <T> StringBuffer dotOutput(Graph<T> g, NodeDecorator labels, String title) throws WalaException {
|
||||
private static <T> StringBuffer dotOutput(Graph<T> g, NodeDecorator<T> labels, String title) throws WalaException {
|
||||
StringBuffer result = new StringBuffer("digraph \"DirectedGraph\" {\n");
|
||||
|
||||
if (title != null) {
|
||||
@ -205,7 +209,7 @@ public class DotUtil {
|
||||
result.append(fontnameStr);
|
||||
result.append("]; \n");
|
||||
|
||||
Collection<?> dotNodes = computeDotNodes(g);
|
||||
Collection<T> dotNodes = computeDotNodes(g);
|
||||
|
||||
outputNodes(labels, result, dotNodes);
|
||||
|
||||
@ -225,13 +229,13 @@ public class DotUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void outputNodes(NodeDecorator labels, StringBuffer result, Collection<?> dotNodes) throws WalaException {
|
||||
for (Iterator<?> it = dotNodes.iterator(); it.hasNext();) {
|
||||
private static <T> void outputNodes(NodeDecorator<T> labels, StringBuffer result, Collection<T> dotNodes) throws WalaException {
|
||||
for (Iterator<T> it = dotNodes.iterator(); it.hasNext();) {
|
||||
outputNode(labels, result, it.next());
|
||||
}
|
||||
}
|
||||
|
||||
private static void outputNode(NodeDecorator labels, StringBuffer result, Object n) throws WalaException {
|
||||
private static <T> void outputNode(NodeDecorator<T> labels, StringBuffer result, T n) throws WalaException {
|
||||
result.append(" ");
|
||||
result.append("\"");
|
||||
result.append(getLabel(n, labels));
|
||||
@ -254,19 +258,19 @@ public class DotUtil {
|
||||
* @param n node to decorate
|
||||
* @param d decorating master
|
||||
*/
|
||||
private static String decorateNode(Object n, NodeDecorator d) throws WalaException {
|
||||
private static <T> String decorateNode(T n, NodeDecorator<T> d) throws WalaException {
|
||||
StringBuffer result = new StringBuffer();
|
||||
result.append(" [ ]\n");
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static String getLabel(Object o, NodeDecorator d) throws WalaException {
|
||||
private static <T> String getLabel(T n, NodeDecorator<T> d) throws WalaException {
|
||||
String result = null;
|
||||
if (d == null) {
|
||||
result = o.toString();
|
||||
result = n.toString();
|
||||
} else {
|
||||
result = d.getLabel(o);
|
||||
result = result == null ? o.toString() : result;
|
||||
result = d.getLabel(n);
|
||||
result = result == null ? n.toString() : result;
|
||||
}
|
||||
if (result.length() >= MAX_LABEL_LENGTH) {
|
||||
result = result.substring(0, MAX_LABEL_LENGTH - 3) + "...";
|
||||
@ -274,8 +278,8 @@ public class DotUtil {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String getPort(Object o, NodeDecorator d) throws WalaException {
|
||||
return "\"" + getLabel(o, d) + "\"";
|
||||
private static <T> String getPort(T n, NodeDecorator<T> d) throws WalaException {
|
||||
return "\"" + getLabel(n, d) + "\"";
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -13,19 +13,14 @@ package com.ibm.wala.viz;
|
||||
import com.ibm.wala.util.WalaException;
|
||||
|
||||
/**
|
||||
* @param <T> the node type
|
||||
*/
|
||||
public interface NodeDecorator {
|
||||
|
||||
public static final NodeDecorator DEFAULT = new NodeDecorator() {
|
||||
@Override
|
||||
public String getLabel(Object o) {
|
||||
return o.toString();
|
||||
} };
|
||||
public interface NodeDecorator<T> {
|
||||
|
||||
/**
|
||||
* @param o
|
||||
* @return the String label for node o
|
||||
* @param n
|
||||
* @return the String label for node n
|
||||
*/
|
||||
String getLabel(Object o) throws WalaException;
|
||||
String getLabel(T n) throws WalaException;
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user