diff options
Diffstat (limited to 'src/test/util')
-rw-r--r-- | src/test/util/DotExporter.java | 183 | ||||
-rw-r--r-- | src/test/util/MemoryMonitor.java | 25 | ||||
-rw-r--r-- | src/test/util/Timer.java | 31 | ||||
-rw-r--r-- | src/test/util/ZipStripper.java | 71 |
4 files changed, 310 insertions, 0 deletions
diff --git a/src/test/util/DotExporter.java b/src/test/util/DotExporter.java new file mode 100644 index 0000000..88b9320 --- /dev/null +++ b/src/test/util/DotExporter.java @@ -0,0 +1,183 @@ +package test.util; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; + + +import de.fernflower.code.cfg.BasicBlock; +import de.fernflower.code.cfg.ControlFlowGraph; +import de.fernflower.modules.decompiler.StatEdge; +import de.fernflower.modules.decompiler.sforms.DirectGraph; +import de.fernflower.modules.decompiler.sforms.DirectNode; +import de.fernflower.modules.decompiler.stats.Statement; +import de.fernflower.modules.decompiler.vars.VarVersionEdge; +import de.fernflower.modules.decompiler.vars.VarVersionNode; +import de.fernflower.modules.decompiler.vars.VarVersionsGraph; + +public class DotExporter { + + + public static String toDotFormat(Statement stat) { + + StringBuffer buffer = new StringBuffer(); + + buffer.append("digraph G {\r\n"); + + for(Statement st : stat.getStats()) { + + String sourceid = st.id + (st.getSuccessorEdges(StatEdge.TYPE_EXCEPTION).isEmpty()?"":"000000"); + + buffer.append(sourceid+" [shape=box,label=\""+sourceid+"\"];\r\n"); + + for(StatEdge edge : st.getSuccessorEdges(Statement.STATEDGE_DIRECT_ALL)) { + String destid = edge.getDestination().id + (edge.getDestination().getSuccessorEdges(StatEdge.TYPE_EXCEPTION).isEmpty()?"":"000000"); + + buffer.append(sourceid+"->"+destid+";\r\n"); + + if(!stat.getStats().contains(edge.getDestination())) { + buffer.append(destid+" [label=\""+destid+"\"];\r\n"); + } + } + + for(StatEdge edge : st.getSuccessorEdges(StatEdge.TYPE_EXCEPTION)) { + String destid = edge.getDestination().id + (edge.getDestination().getSuccessorEdges(StatEdge.TYPE_EXCEPTION).isEmpty()?"":"000000"); + + buffer.append(sourceid+" -> "+destid+" [style=dotted];\r\n"); + + if(!stat.getStats().contains(edge.getDestination())) { + buffer.append(destid+" [label=\""+destid+"\"];\r\n"); + } + } + } + + buffer.append("}"); + + return buffer.toString(); + } + + + public static String toDotFormat(ControlFlowGraph graph, boolean showMultipleEdges) { + + StringBuffer buffer = new StringBuffer(); + + buffer.append("digraph G {\r\n"); + + List<BasicBlock> blocks = graph.getBlocks(); + for(int i=0;i<blocks.size();i++) { + BasicBlock block = (BasicBlock)blocks.get(i); + + buffer.append(block.id+" [shape=box,label=\""+block.id+"\"];\r\n"); + + + List<BasicBlock> suc = block.getSuccs(); + if(!showMultipleEdges) { + HashSet<BasicBlock> set = new HashSet<BasicBlock>(); + set.addAll(suc); + suc = Collections.list(Collections.enumeration(set)); + } + for(int j=0;j<suc.size();j++) { + buffer.append(block.id+"->"+((BasicBlock)suc.get(j)).id+";\r\n"); + } + + + suc = block.getSuccExceptions(); + if(!showMultipleEdges) { + HashSet<BasicBlock> set = new HashSet<BasicBlock>(); + set.addAll(suc); + suc = Collections.list(Collections.enumeration(set)); + } + for(int j=0;j<suc.size();j++) { + buffer.append(block.id+" -> "+((BasicBlock)suc.get(j)).id+" [style=dotted];\r\n"); + } + } + + buffer.append("}"); + + return buffer.toString(); + } + + public static String toDotFormat(VarVersionsGraph graph) { + + StringBuffer buffer = new StringBuffer(); + + buffer.append("digraph G {\r\n"); + + List<VarVersionNode> blocks = graph.nodes; + for(int i=0;i<blocks.size();i++) { + VarVersionNode block = blocks.get(i); + + buffer.append((block.var*1000+block.version)+" [shape=box,label=\""+block.var+"_"+block.version+"\"];\r\n"); + + for(VarVersionEdge edge: block.succs) { + VarVersionNode dest = edge.dest; + buffer.append((block.var*1000+block.version)+"->"+(dest.var*1000+dest.version)+(edge.type==VarVersionEdge.EDGE_PHANTOM?" [style=dotted]":"")+";\r\n"); + } + } + + buffer.append("}"); + + return buffer.toString(); + } + + public static String toDotFormat(DirectGraph graph) { + + StringBuffer buffer = new StringBuffer(); + + buffer.append("digraph G {\r\n"); + + List<DirectNode> blocks = graph.nodes; + for(int i=0;i<blocks.size();i++) { + DirectNode block = blocks.get(i); + + buffer.append(directBlockIdToDot(block.id)+" [shape=box,label=\""+directBlockIdToDot(block.id)+"\"];\r\n"); + + for(DirectNode dest: block.succs) { + buffer.append(directBlockIdToDot(block.id)+"->"+directBlockIdToDot(dest.id)+";\r\n"); + } + } + + buffer.append("}"); + + return buffer.toString(); + } + + private static String directBlockIdToDot(String id) { + id = id.replaceAll("_try", "999"); + id = id.replaceAll("_tail", "888"); + return id; + } + + public static void toDotFile(ControlFlowGraph graph, File file, boolean showMultipleEdges) throws FileNotFoundException, IOException { + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); + out.write(toDotFormat(graph, showMultipleEdges).getBytes()); + out.close(); + } + + public static void toDotFile(VarVersionsGraph graph, File file) throws FileNotFoundException, IOException { + + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); + out.write(toDotFormat(graph).getBytes()); + out.close(); + } + + public static void toDotFile(DirectGraph graph, File file) throws FileNotFoundException, IOException { + + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); + out.write(toDotFormat(graph).getBytes()); + out.close(); + } + + public static void toDotFile(Statement stat, File file) throws FileNotFoundException, IOException { + + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); + out.write(toDotFormat(stat).getBytes()); + out.close(); + } + +} diff --git a/src/test/util/MemoryMonitor.java b/src/test/util/MemoryMonitor.java new file mode 100644 index 0000000..4030383 --- /dev/null +++ b/src/test/util/MemoryMonitor.java @@ -0,0 +1,25 @@ +package test.util; + +public class MemoryMonitor implements Runnable { + + public static boolean run = false; + + + public void run() { + + while(run) { + try { + Thread.sleep(500); + } catch(InterruptedException ex) { + ex.printStackTrace(); + } + + //Runtime.getRuntime().gc(); + System.err.println((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024)); + + } + + } + + +} diff --git a/src/test/util/Timer.java b/src/test/util/Timer.java new file mode 100644 index 0000000..5a45cc0 --- /dev/null +++ b/src/test/util/Timer.java @@ -0,0 +1,31 @@ +package test.util; + +import java.util.HashMap; + +public class Timer { + + private static HashMap<String, Double> mapValue = new HashMap<String, Double>(); + + public static void addTime(int index, long value) { + addTime(String.valueOf(index), value); + } + + public static double getTime(int index) { + return mapValue.get(String.valueOf(index)); + } + + public static void addTime(String index, double value) { + Double val = mapValue.get(index); + if(val != null) { + value+=val.doubleValue(); + } + mapValue.put(index, value); + } + + public static double getTime(String index) { + Double value = mapValue.get(index); + return value==null?0:value.doubleValue(); + } + +} + diff --git a/src/test/util/ZipStripper.java b/src/test/util/ZipStripper.java new file mode 100644 index 0000000..2eeb2c0 --- /dev/null +++ b/src/test/util/ZipStripper.java @@ -0,0 +1,71 @@ +package test.util; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Enumeration; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import java.util.zip.ZipOutputStream; + +public class ZipStripper { + + public static void main(String[] args) { + + try { + String sourceFileName = args[0]; + File sourceFile = new File(sourceFileName); + + File tempFile = new File(sourceFile.getParentFile(), "tmp31415926535.zip"); + tempFile.createNewFile(); + + ZipOutputStream outTemp = new ZipOutputStream(new FileOutputStream(tempFile)); + + ZipFile archive = new ZipFile(sourceFile); + + Enumeration<? extends ZipEntry> en = archive.entries(); + while(en.hasMoreElements()) { + ZipEntry entr = en.nextElement(); + + outTemp.putNextEntry(new ZipEntry(entr.getName())); + + if(!entr.isDirectory()) { + InputStream in = archive.getInputStream(entr); + + copyInputStream(in, outTemp); + in.close(); + } + } + + outTemp.flush(); + outTemp.close(); + + archive.close(); + + String destFileName = args[1]; + + if(sourceFileName.equals(destFileName)) { + sourceFile.delete(); + } + + tempFile.renameTo(new File(destFileName)); + + } catch(Exception ex) { + ex.printStackTrace(); + } + + } + + public static void copyInputStream(InputStream in, OutputStream out)throws IOException { + + byte[] buffer = new byte[1024]; + int len; + + while((len = in.read(buffer)) >= 0) { + out.write(buffer, 0, len); + } + } + +} |