1 /***
2 * BytecodeLayout
3 *
4 * This class is designed to determine the
5 * proper order of the BlockVertex.
6 */
7
8 package junit.quilt.cover.generic;
9
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.HashMap;
13 import java.util.HashSet;
14
15 import junit.quilt.exception.QuiltException;
16 import junit.quilt.exception.InvalidTransitionException;
17
18 import org.apache.commons.graph.*;
19 import org.apache.commons.graph.algorithm.search.*;
20
21 import org.apache.bcel.classfile.*;
22 import org.apache.bcel.generic.*;
23
24 public class BytecodeLayout
25 implements org.apache.commons.graph.algorithm.search.Visitor
26 {
27 private Map startHandle = new HashMap(); // BV X START HANDLE
28 private Map endHandle = new HashMap(); // BV X END HANDLE
29
30 private MethodGen method = null;
31 private DirectedGraph graph = null;
32 private InstructionList il = new InstructionList();
33
34 private InvalidTransitionException err = null;
35 private boolean isOK = false;
36
37 public BytecodeLayout( MethodGen method ) {
38 this.method = method;
39 method.removeExceptionHandlers();
40 }
41
42 public void discoverGraph( Graph g ) {
43 this.graph = (DirectedGraph) g;
44 }
45
46 public void discoverVertex(Vertex v) {
47 BlockVertex bv = (BlockVertex) v;
48
49 InstructionHandle blockStart =
50 il.append( bv.copyInstructionList() );
51 InstructionHandle blockEnd =
52 il.getEnd();
53
54 startHandle.put( bv, blockStart );
55 endHandle.put( bv, blockEnd );
56 }
57
58 public void discoverEdge( Edge e ) { }
59
60
61 public void finishEdge( Edge e ) {
62 FlowControlEdge fce = (FlowControlEdge) e;
63
64 BlockVertex source = (BlockVertex) graph.getSource( fce );
65 BlockVertex target = (BlockVertex) graph.getTarget( fce );
66
67 try {
68 fce.connect( method,
69 il,
70 (InstructionHandle) endHandle.get( source ),
71 (InstructionHandle) startHandle.get( target ) );
72 } catch (InvalidTransitionException ex) {
73 System.err.println("Warning: Instrumenation not completed.");
74 ex.printStackTrace();
75 this.err = ex;
76 }
77 }
78
79 public void finishVertex( Vertex v ) { }
80 public void finishGraph( Graph g ) {
81 isOK = true;
82 }
83
84 public void correctLocals( MethodGen method ) {
85 LocalVariableGen lvg[] = method.getLocalVariables();
86 // Map indexLVG = new HashMap(); // INDEX X LVG
87
88 InstructionList il = method.getInstructionList();
89 // InstructionHandle curr = il.getStart();
90 // Set visitedLVG = new HashSet();
91
92 for (int i = 0; i < lvg.length; i++) {
93 lvg[i].setStart( il.getStart() );
94 lvg[i].setEnd( il.getEnd() );
95 }
96
97 // for (int i = 0; i < lvg.length; i++) {
98 // indexLVG.put( new Integer( lvg[i].getIndex() ),
99 // lvg[i] );
100 // }
101
102 // while (curr.getNext() != null) {
103 // if (curr.getInstruction() instanceof LocalVariableInstruction) {
104 // LocalVariableInstruction lvi =
105 // (LocalVariableInstruction) curr.getInstruction();
106
107 // LocalVariableGen currGen =
108 // (LocalVariableGen) indexLVG.get(new Integer(lvi.getIndex()));
109
110 // if (currGen != null) {
111 // if (!visitedLVG.contains( currGen )) {
112 // visitedLVG.add( currGen );
113
114 // currGen.setStart( curr );
115 // }
116 // currGen.setEnd( curr );
117 // }
118 // }
119 // curr = curr.getNext();
120 // }
121 }
122
123 public Method getMethod()
124 throws QuiltException
125 {
126 if (err != null)
127 throw err;
128
129 if (!isOK)
130 throw new QuiltException("Method Not Ready.");
131
132 method.setInstructionList( il );
133 method.removeNOPs();
134
135 correctLocals( method );
136
137 method.setMaxStack();
138 method.setMaxLocals();
139
140 return method.getMethod();
141 }
142 }
143
144
145
This page was automatically generated by Maven