1 /***
2 * BlockVertex
3 *
4 * This is a vertex in a Control Flow Graph.
5 * It represents a BasicBlock.
6 *
7 * These vertexes DO NOT CHANGE as the underlying
8 * bytecode changes.
9 */
10
11 package junit.quilt.cover.generic;
12
13 import org.apache.commons.graph.*;
14 import org.apache.bcel.generic.*;
15 import org.apache.bcel.classfile.*;
16
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.ArrayList;
20 import java.util.Map;
21 import java.util.HashMap;
22
23 public class BlockVertex
24 implements Vertex
25 {
26 private List instructions = new ArrayList();
27 private LineNumberTable lnt = null;
28 private InstructionList copy = null;
29 private boolean isPrefix = false;
30 private boolean isCopy = false;
31
32 /***
33 * BlockVertex
34 *
35 * Creates an empty basic block.
36 */
37 public BlockVertex() {
38 }
39
40 /***
41 * BlockVertex
42 *
43 * Creates a full basic block.
44 */
45 public BlockVertex( InstructionList il )
46 {
47 instructions.add( il.getInstructionHandles() );
48 }
49
50 /***
51 * BlockVertex
52 *
53 * Creates an empty block vertex with a line number
54 * table.
55 */
56 public BlockVertex( LineNumberTable lnt )
57 {
58 this.lnt = lnt;
59 }
60
61 /***
62 * BlockVertex
63 *
64 * Creates a full block vertex with a line number table.
65 */
66 public BlockVertex( InstructionList il,
67 LineNumberTable lnt )
68 {
69 instructions.add( il.getInstructionHandles() );
70 this.lnt = lnt;
71 }
72
73 /***
74 * addInstruction
75 *
76 * Appends an instruction to this basic block.
77 */
78 public void addInstruction( InstructionHandle instruction )
79 {
80 instructions.add( instruction );
81 }
82
83 /***
84 * getFirstInst
85 *
86 * Returns the first instruction.
87 */
88 public InstructionHandle getFirstInst() {
89 return (InstructionHandle) instructions.get(0);
90 }
91
92 /***
93 * getLastInst
94 *
95 * Returns the last instruction.
96 */
97 public InstructionHandle getLastInst() {
98 return (InstructionHandle) instructions.get(instructions.size() - 1);
99 }
100
101 /***
102 * getBlockStart
103 *
104 * Returns the first line of this block, if available.
105 */
106 public int getBlockStart()
107 {
108 if (lnt == null) return -1;
109 return lnt.getSourceLine( getFirstInst().getPosition() );
110 }
111
112 /***
113 * getBlockEnd
114 *
115 * Returns the last line of this block, if available.
116 */
117 public int getBlockEnd()
118 {
119 if (lnt == null) return -1;
120 return lnt.getSourceLine( getLastInst().getPosition() );
121 }
122
123 protected void setInstructionList( InstructionList il ) {
124 this.instructions = new ArrayList();
125 this.instructions.add( il.getInstructionHandles() );
126 this.copy = il;
127 this.isCopy = true;
128 }
129
130 public void prefixInstructionList(InstructionList prefix) {
131 if (copy != null) {
132 if (copy.getStart() == null) {
133 copy.append( new NOP() );
134 }
135 copy.insert( copy.getStart(), prefix );
136 } else {
137 copy = prefix;
138 }
139 isPrefix = true;
140 }
141
142 public int length() {
143 if (isCopy) return copy.size();
144
145 return instructions.size();
146 }
147
148 public InstructionList copyInstructionList() {
149 if (isCopy) return copy;
150
151 InstructionList RC = new InstructionList();
152 if (instructions.size() == 0) {
153 RC.append( new NOP() );
154 }
155
156 Iterator ihs = instructions.iterator();
157
158 while (ihs.hasNext()) {
159 InstructionHandle ih = (InstructionHandle) ihs.next();
160 if (ih.getInstruction() instanceof BranchInstruction) {
161 RC.append( (BranchInstruction) ih.getInstruction() );
162 } else {
163 RC.append( ih.getInstruction() );
164 }
165 }
166
167 if (isPrefix) {
168 RC.insert( RC.getStart(), copy );
169 }
170 return RC;
171 }
172
173 public String toString() {
174 if (getBlockStart() == getBlockEnd())
175 return "Line: " + getBlockStart();
176 return "Lines: " + getBlockStart() + " to " + getBlockEnd();
177 }
178 }
179
180
181
182
183
This page was automatically generated by Maven