KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uka > ipd > coverage > recording > BasicBlock


1 /*
2  * Created on Aug 13, 2004
3  * @author Matthias Kempka
4  */

5 package de.uka.ipd.coverage.recording;
6
7 import java.io.Serializable JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11
12 /**
13  * Represents a block of assignments. Such a block is an atomic sequence of
14  * assignments
15  * which are always executed together as long as no exception is thrown. This
16  * means especially that there are no <code>if-else</code> assignments or loops
17  * or any other jumps within a basic block.<br>
18  *
19  * Being an atomic sequence means for the basic block that there can be no blocks
20  * that are sub-blocks of other blocks.
21  *
22  * @author Matthias Kempka
23  *
24  */

25 public class BasicBlock implements IBasicBlock, Serializable JavaDoc {
26
27     private List JavaDoc successors = new ArrayList JavaDoc(); // contains IBasicBlock objects
28
private List JavaDoc predecessors = new ArrayList JavaDoc(); // contains IBasicBlock objects
29
private RegisteredMethod method;
30     private int startLine; // startLine is included, bytecode instruction line
31
private int endLine; // endLine is included, bytecode instruction line
32

33     public BasicBlock(RegisteredMethod method) {
34         assert method != null : "RegisteredMethod for a BasicBlock must not be null"; //$NON-NLS-1$
35
this.method = method;
36     }
37     
38     public RegisteredMethod getRegisteredMethod() {
39         return method;
40     }
41     
42     /**
43       * This is a convenience method to get a CoverageState for two BasicBlocks
44       */

45     public CoverageState covers(IBasicBlock obj) {
46         CoverageState result = CoverageState.NO_COVERAGE;
47         if (obj.referencesSameBytecodeAs(this)) {
48             result = CoverageState.FULL_COVERAGE;
49         }
50         return result;
51     }
52
53     /**
54      * This method decides whether the compared block describes the same bytecode
55      * as the comparing block. The return values are as follows:
56      * <br>
57      * Full coverage is given if exactly the same bytecode is covered. This is
58      * the case if the method, start and end line are equal.
59      * <br>
60      * No coverage is given if the method is not the same or if the intersection
61      * is empty.
62      *
63      * @param compBlock
64      * @return returns true if full coverage is achieved.
65      */

66     public boolean referencesSameBytecodeAs(BasicBlock compBlock) {
67         boolean result = false;
68         if (method.equals(compBlock.method)) {
69             if (this.startLine == compBlock.startLine
70                     && this.endLine == compBlock.endLine) {
71                 result = true;
72             }
73             
74             /* decide partial coverage.
75              * Partial coverage is not allowed, and there is a programming
76              * error somewhere.
77              * the following possibilities are given, the -s- represents
78              * the start line, the -e- represents the end line
79              *
80              * this: | compBlock:
81              * | -s- -s-
82              * -s- |
83              * | -e- -s- -s-
84              * | -e-
85              * -e- |
86              * | -e- -e-
87              *
88              * all of the cases are covered if 2 conditions are true:
89              *
90              * 1. compBlock.endline >= this.startline
91              * 2. compBlock.startline <= this.endline
92              *
93              * checking for equality in every case is right, because start and
94              * end lines are both included in a block.
95              */

96             else if (compBlock.endLine >= this.startLine
97                     && compBlock.startLine <= this.endLine) {
98                 throw new AssertionError JavaDoc("BasicBlock detected that has non-empty intersection" //$NON-NLS-1$
99
+ " of another BasicBlock: " + this.toString() + " vs. " //$NON-NLS-1$ //$NON-NLS-2$
100
+ compBlock.toString());
101             }
102         }
103         return result;
104     }
105
106     protected void setEndLine(int endLine) {
107         this.endLine = endLine;
108     }
109     
110     protected void setStartLine(int startLine) {
111         this.startLine = startLine;
112     }
113     
114     public int getEndLine() {
115         return endLine;
116     }
117     
118     public int getStartLine() {
119         return startLine;
120     }
121     
122     /*
123      * this method is final because the construction of successors and
124      * predecessors shall not be disturbed.
125      */

126     public final void addSuccessor(BasicBlock successor) {
127         if (!successors.contains(successor)) {
128             this.successors.add(successor);
129         }
130         successor.addPredecessor(this);
131     }
132     
133     private void addPredecessor(BasicBlock predecessor) {
134         if (!predecessors.contains(predecessor)) {
135             this.predecessors.add(predecessor);
136         }
137     }
138     
139     public BasicBlock[] getSuccessors() {
140         return (BasicBlock[]) successors.toArray(
141                 new BasicBlock[successors.size()]);
142     }
143     
144     public BasicBlock[] getPredecessors() {
145         return (BasicBlock[]) predecessors.toArray(
146                 new BasicBlock[predecessors.size()]);
147     }
148     
149     public String JavaDoc toString() {
150         String JavaDoc result = "BasicBlock:"; //$NON-NLS-1$
151
result += " Method: " + getRegisteredMethod().getMethod().getName(); //$NON-NLS-1$
152
result += " PC[" + getStartLine() + ":" + getEndLine() + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
153
return result;
154     }
155     
156     /**
157      * returns whether the BasicBlocks have the same state, that is,
158      * are in the same method and refer to the same bytecode
159      */

160     public boolean equals(Object JavaDoc obj) {
161         if (obj instanceof IBasicBlock) {
162             IBasicBlock compBlock = (IBasicBlock) obj;
163             if (compBlock.getStartLine()== startLine
164                     && compBlock.getEndLine() == endLine
165                     && compBlock.getRegisteredMethod().equals(method)) {
166                 return true;
167             }
168         }
169         return false;
170     }
171     
172     public int hashCode() {
173         return method.hashCode();
174     }
175
176 }
177
Popular Tags