KickJava   Java API By Example, From Geeks To Geeks.

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


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
23
24 /**
25  * A ByteCodePattern is a pattern matching a sequence of bytecode instructions.
26  *
27  * @author David Hovemeyer
28  * @see PatternElement
29  * @see PatternMatcher
30  */

31 public class ByteCodePattern {
32     private PatternElement first, last;
33     private int interElementWild;
34     private int numElements;
35     private int dummyVariableCount;
36
37     /**
38      * Add a PatternElement to the end of the pattern.
39      *
40      * @param element the PatternElement
41      * @return this object
42      */

43     public ByteCodePattern add(PatternElement element) {
44         if (first != null)
45             addInterElementWild();
46         addElement(element);
47         return this;
48     }
49
50     /**
51      * Add a wildcard to match between 0 and given number of instructions.
52      * If there is already a wildcard at the end of the current pattern,
53      * resets its max value to that given.
54      *
55      * @param numWild maximum number of instructions to be matched by
56      * the wildcard
57      */

58     public ByteCodePattern addWild(int numWild) {
59         Wild wild = isLastWild();
60         if (wild != null)
61             wild.setMinAndMax(0, numWild);
62         else
63             addElement(new Wild(numWild));
64         return this;
65     }
66
67     /**
68      * Set number of inter-element wildcards to create between
69      * explicit PatternElements. By default, no implicit wildcards
70      * are created.
71      *
72      * @param numWild the number of wildcard instructions which
73      * may be matched between explicit PatternElements
74      * @return this object
75      */

76     public ByteCodePattern setInterElementWild(int numWild) {
77         this.interElementWild = numWild;
78         return this;
79     }
80
81     /**
82      * Get the first PatternElement in the pattern.
83      */

84     public PatternElement getFirst() {
85         return first;
86     }
87
88     /**
89      * Get a dummy variable name.
90      * The name returned will begin with the <code>'$'</code> character,
91      * and will be different than any previous dummy variable name allocated
92      * by this object. Dummy variable names are useful for creating
93      * PatternElements where you don't care whether the value it uses
94      * is the same as one used by another PatternElement.
95      */

96     public String JavaDoc dummyVariable() {
97         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
98         buf.append("$_");
99         buf.append(dummyVariableCount++);
100         return buf.toString();
101     }
102
103     private void addInterElementWild() {
104         if (interElementWild > 0 && isLastWild() == null)
105             addElement(new Wild(interElementWild));
106     }
107
108     private void addElement(PatternElement element) {
109         element.setIndex(numElements++);
110         if (first == null) {
111             first = last = element;
112         } else {
113             last.setNext(element);
114             last = element;
115         }
116     }
117
118     private Wild isLastWild() {
119         if (last != null && last instanceof Wild)
120             return (Wild) last;
121         else
122             return null;
123     }
124 }
125
126 // vim:ts=4
127
Popular Tags