KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > bcp > PatternElementMatch


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.bcp;
21
22 import org.apache.bcel.generic.InstructionHandle;
23
24 import edu.umd.cs.findbugs.ba.BasicBlock;
25
26 /**
27  * PatternElementMatch represents matching a PatternElement against
28  * a single instruction. The "prev" field points to the previous
29  * PatternElementMatch. By building up sequences of PatternElementMatch objects
30  * in this way, we can implement nondeterministic matching without
31  * having to copy anything.
32  */

33 public class PatternElementMatch {
34     private final PatternElement patternElement;
35     private final InstructionHandle matchedInstruction;
36     private final BasicBlock basicBlock;
37     private final int matchCount;
38     private final PatternElementMatch prev;
39
40     /**
41      * Constructor.
42      *
43      * @param patternElement the PatternElement being matched
44      * @param matchedInstruction the instruction which matched the PatternElement
45      * @param basicBlock the basic block containing the matched instruction
46      * @param matchCount the index (starting at zero) of the instructions
47      * matching the PatternElement; multiple instructions can match the
48      * same PatternElement
49      * @param prev the previous PatternElementMatch
50      */

51     public PatternElementMatch(PatternElement patternElement, InstructionHandle matchedInstruction,
52                                BasicBlock basicBlock,
53                                int matchCount, PatternElementMatch prev) {
54         this.patternElement = patternElement;
55         this.matchedInstruction = matchedInstruction;
56         this.basicBlock = basicBlock;
57         this.matchCount = matchCount;
58         this.prev = prev;
59     }
60
61     /**
62      * Get the PatternElement.
63      */

64     public PatternElement getPatternElement() {
65         return patternElement;
66     }
67
68     /**
69      * Get the matched instruction.
70      */

71     public InstructionHandle getMatchedInstructionInstructionHandle() {
72         return matchedInstruction;
73     }
74
75     /**
76      * Get the basic block containing the matched instruction.
77      */

78     public BasicBlock getBasicBlock() {
79         return basicBlock;
80     }
81
82     /*
83      * Get the index of this instruction in terms of how many instructions
84      * have matched this PatternElement. (0 for the first instruction to
85      * match the PatternElement, etc.)
86      */

87     public int getMatchCount() {
88         return matchCount;
89     }
90
91     /**
92      * Get the previous PatternMatchElement.
93      */

94     public PatternElementMatch getPrev() {
95         return prev;
96     }
97
98     /**
99      * Get the <em>first</em> instruction matched by the PatternElement with given label.
100      */

101     public InstructionHandle getLabeledInstruction(String JavaDoc label) {
102         PatternElementMatch first = getFirstLabeledMatch(label);
103         return first != null ? first.getMatchedInstructionInstructionHandle() : null;
104     }
105
106     /**
107      * Get <em>first</em> match element with given label,
108      * if any.
109      */

110     public PatternElementMatch getFirstLabeledMatch(String JavaDoc label) {
111         PatternElementMatch cur = this, result = null;
112         while (cur != null) {
113             String JavaDoc elementLabel = cur.patternElement.getLabel();
114             if (elementLabel != null && elementLabel.equals(label))
115                 result = cur;
116             cur = cur.prev;
117         }
118         return result;
119     }
120
121     /**
122      * Get <em>last</em> match element with given label,
123      * if any.
124      */

125     public PatternElementMatch getLastLabeledMatch(String JavaDoc label) {
126         PatternElementMatch cur = this;
127         while (cur != null) {
128             String JavaDoc elementLabel = cur.patternElement.getLabel();
129             if (elementLabel != null && elementLabel.equals(label))
130                 return cur;
131             cur = cur.prev;
132         }
133         return null;
134     }
135
136     /**
137      * Return whether or not the most recently matched instruction
138      * allows trailing edges.
139      */

140     public boolean allowTrailingEdges() {
141         return patternElement.allowTrailingEdges();
142     }
143
144     @Override JavaDoc
145          public String JavaDoc toString() {
146         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
147         PatternElementMatch cur = this;
148         buf.append(cur.patternElement.toString());
149         buf.append(", ");
150         buf.append(cur.matchedInstruction.toString());
151         buf.append(", ");
152         buf.append(cur.matchCount);
153         return buf.toString();
154     }
155
156     @Override JavaDoc
157          public int hashCode() {
158         // Do the simplest thing possible that works
159
throw new UnsupportedOperationException JavaDoc();
160     }
161
162     @Override JavaDoc
163          public boolean equals(Object JavaDoc o) {
164         if (!(o instanceof PatternElementMatch))
165             return false;
166         PatternElementMatch lhs = this;
167         PatternElementMatch rhs = (PatternElementMatch) o;
168
169         while (lhs != null && rhs != null) {
170             if (lhs.patternElement != rhs.patternElement ||
171                     lhs.matchedInstruction != rhs.matchedInstruction ||
172                     lhs.matchCount != rhs.matchCount)
173                 return false;
174
175             lhs = lhs.prev;
176             rhs = rhs.prev;
177         }
178
179         return lhs == rhs;
180     }
181 }
182
183 // vim:ts=4
184
Popular Tags