KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > Location


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.ba;
21
22 import org.apache.bcel.generic.InstructionHandle;
23
24 import edu.umd.cs.findbugs.annotations.NonNull;
25
26 /**
27  * A class representing a location in the CFG for a method.
28  * Essentially, it represents a static instruction, <em>with the important caveat</em>
29  * that CFGs have inlined JSR subroutines, meaning that a single InstructionHandle
30  * in a CFG may represent several static locations. To this end, a Location
31  * is comprised of both an InstructionHandle and the BasicBlock that
32  * contains it.
33  * <p/>
34  * <p> Location objects may be compared with each other using the equals() method,
35  * and may be used as keys in tree and hash maps and sets.
36  * Note that <em>it is only valid to compare Locations produced from the same CFG</em>.
37  *
38  * @author David Hovemeyer
39  * @see CFG
40  */

41 public class Location implements Comparable JavaDoc<Location> {
42     private final InstructionHandle handle;
43     private final BasicBlock basicBlock;
44
45     /**
46      * Constructor.
47      *
48      * @param handle the instruction
49      * @param basicBlock the basic block containing the instruction
50      */

51     public Location(@NonNull InstructionHandle handle, @NonNull BasicBlock basicBlock) {
52         if (handle == null) throw new NullPointerException JavaDoc("handle cannot be null");
53         if (basicBlock == null) throw new NullPointerException JavaDoc("basicBlock cannot be null");
54         this.handle = handle;
55         this.basicBlock = basicBlock;
56     }
57
58     public static Location getFirstLocation(@NonNull BasicBlock basicBlock) {
59         InstructionHandle location = basicBlock.getFirstInstruction();
60         if (location == null)
61             return null;
62         return new Location(location, basicBlock);
63     }
64     public static Location getLastLocation(@NonNull BasicBlock basicBlock) {
65         InstructionHandle lastInstruction = basicBlock.getLastInstruction();
66         /*
67         if (lastInstruction == null)
68             lastInstruction = basicBlock.getExceptionThrower();
69         if (lastInstruction == null)
70             lastInstruction = basicBlock.getFirstInstruction();
71         */

72         if (lastInstruction == null)
73             return null;
74         return new Location(lastInstruction, basicBlock);
75     }
76     /**
77      * Get the instruction handle.
78      */

79     public InstructionHandle getHandle() {
80         return handle;
81     }
82
83     /**
84      * Get the basic block.
85      */

86     public BasicBlock getBasicBlock() {
87         return basicBlock;
88     }
89     
90     /**
91      * Return whether or not the Location is positioned at the
92      * first instruction in the basic block.
93      */

94     public boolean isFirstInstructionInBasicBlock() {
95         return !basicBlock.isEmpty() && handle == basicBlock.getFirstInstruction();
96     }
97     
98     /**
99      * Return whether or not the Location is positioned at the
100      * last instruction in the basic block.
101      */

102     public boolean isLastInstructionInBasicBlock() {
103         return !basicBlock.isEmpty() && handle == basicBlock.getLastInstruction();
104     }
105
106     public int compareTo(Location other) {
107         int cmp = basicBlock.getId() - other.basicBlock.getId();
108         if (false && cmp != 0)
109             return cmp;
110
111         int pos = handle.getPosition() - other.handle.getPosition();
112         if (pos != 0) return pos;
113         return cmp;
114     }
115
116     @Override JavaDoc
117          public int hashCode() {
118         return System.identityHashCode(basicBlock) + handle.getPosition();
119     }
120
121     @Override JavaDoc
122          public boolean equals(Object JavaDoc o) {
123         if (!(o instanceof Location))
124             return false;
125         Location other = (Location) o;
126         return basicBlock == other.basicBlock && handle == other.handle;
127     }
128
129     @Override JavaDoc
130          public String JavaDoc toString() {
131         return handle.toString() + " in basic block " + basicBlock.getId();
132     }
133 }
134
135 // vim:ts=4
136
Popular Tags