1   package org.apache.bcel.classfile;
2   
3   /* ====================================================================
4    * The Apache Software License, Version 1.1
5    *
6    * Copyright (c) 2001 The Apache Software Foundation.  All rights
7    * reserved.
8    *
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   * 1. Redistributions of source code must retain the above copyright
14   *    notice, this list of conditions and the following disclaimer.
15   *
16   * 2. Redistributions in binary form must reproduce the above copyright
17   *    notice, this list of conditions and the following disclaimer in
18   *    the documentation and/or other materials provided with the
19   *    distribution.
20   *
21   * 3. The end-user documentation included with the redistribution,
22   *    if any, must include the following acknowledgment:
23   *       "This product includes software developed by the
24   *        Apache Software Foundation (http://www.apache.org/)."
25   *    Alternately, this acknowledgment may appear in the software itself,
26   *    if and wherever such third-party acknowledgments normally appear.
27   *
28   * 4. The names "Apache" and "Apache Software Foundation" and
29   *    "Apache BCEL" must not be used to endorse or promote products
30   *    derived from this software without prior written permission. For
31   *    written permission, please contact apache@apache.org.
32   *
33   * 5. Products derived from this software may not be called "Apache",
34   *    "Apache BCEL", nor may "Apache" appear in their name, without
35   *    prior written permission of the Apache Software Foundation.
36   *
37   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48   * SUCH DAMAGE.
49   * ====================================================================
50   *
51   * This software consists of voluntary contributions made by many
52   * individuals on behalf of the Apache Software Foundation.  For more
53   * information on the Apache Software Foundation, please see
54   * <http://www.apache.org/>.
55   */
56  import java.util.Stack;
57  
58  /***
59   * Traverses a JavaClass with another Visitor object 'piggy-backed'
60   * that is applied to all components of a JavaClass object. I.e. this
61   * class supplies the traversal strategy, other classes can make use
62   * of it.
63   *
64   * @version $Id: DescendingVisitor.java,v 1.2 2002/02/04 13:07:30 enver Exp $
65   * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> 
66   */
67  public class DescendingVisitor implements Visitor {
68    private JavaClass clazz;
69    private Visitor   visitor;
70    private Stack     stack = new Stack();
71  
72    /*** @return container of current entitity, i.e., predecessor during traversal
73     */
74    public Object predecessor() {
75      return predecessor(0);
76    }
77  
78    /***
79     * @param level nesting level, i.e., 0 returns the direct predecessor
80     * @return container of current entitity, i.e., predecessor during traversal
81     */
82    public Object predecessor(int level) {
83      int size = stack.size();
84  
85      if((size < 2) || (level < 0))
86        return null;
87      else
88        return stack.elementAt(size - (level + 2)); // size - 1 == current
89    }
90  
91    /*** @return current object
92     */
93    public Object current() {
94      return stack.peek();
95    }
96  
97    /***
98     * @param clazz Class to traverse
99     * @param visitor visitor object to apply to all components
100    */
101   public DescendingVisitor(JavaClass clazz, Visitor visitor) {
102     this.clazz   = clazz;
103     this.visitor = visitor;
104   }
105 
106   /***
107    * Start traversal.
108    */
109   public void visit() { clazz.accept(this); }
110 
111   public void visitJavaClass(JavaClass clazz) {
112     stack.push(clazz);
113     clazz.accept(visitor);
114 
115     Field[] fields = clazz.getFields();
116     for(int i=0; i < fields.length; i++)
117       fields[i].accept(this);
118 
119     Method[] methods = clazz.getMethods();
120     for(int i=0; i < methods.length; i++)
121       methods[i].accept(this);
122 
123     Attribute[] attributes = clazz.getAttributes();
124     for(int i=0; i < attributes.length; i++)
125       attributes[i].accept(this);
126 
127     clazz.getConstantPool().accept(this);
128     stack.pop();
129   }
130 
131   public void visitField(Field field) {
132     stack.push(field);
133     field.accept(visitor);
134     
135     Attribute[] attributes = field.getAttributes();
136     for(int i=0; i < attributes.length; i++)
137       attributes[i].accept(this);
138     stack.pop();
139   }
140 
141   public void visitConstantValue(ConstantValue cv) {
142     stack.push(cv);
143     cv.accept(visitor);
144     stack.pop();
145   }
146 
147   public void visitMethod(Method method) {
148     stack.push(method);
149     method.accept(visitor);
150     
151     Attribute[] attributes = method.getAttributes();
152     for(int i=0; i < attributes.length; i++)
153       attributes[i].accept(this);
154 
155     stack.pop();
156   }
157 
158   public void visitExceptionTable(ExceptionTable table) {
159     stack.push(table);
160     table.accept(visitor);
161     stack.pop();
162   }
163 
164   public void visitCode(Code code) {
165     stack.push(code);
166     code.accept(visitor);
167 
168     CodeException[] table = code.getExceptionTable();
169     for(int i=0; i < table.length; i++)
170       table[i].accept(this);
171 
172     Attribute[] attributes = code.getAttributes();
173     for(int i=0; i < attributes.length; i++)
174       attributes[i].accept(this);
175     stack.pop();
176   }
177 
178   public void visitCodeException(CodeException ce) {
179     stack.push(ce);
180     ce.accept(visitor);
181     stack.pop();
182   }
183 
184   public void visitLineNumberTable(LineNumberTable table) {
185     stack.push(table);
186     table.accept(visitor);
187 
188     LineNumber[] numbers = table.getLineNumberTable();
189     for(int i=0; i < numbers.length; i++)
190       numbers[i].accept(this);
191     stack.pop();
192   }
193 
194   public void visitLineNumber(LineNumber number) {
195     stack.push(number);
196     number.accept(visitor);
197     stack.pop();
198   }
199 
200   public void visitLocalVariableTable(LocalVariableTable table) {
201     stack.push(table);
202     table.accept(visitor);
203 
204     LocalVariable[] vars = table.getLocalVariableTable();
205     for(int i=0; i < vars.length; i++)
206       vars[i].accept(this);
207     stack.pop();
208   }
209 
210   public void visitStackMap(StackMap table) {
211     stack.push(table);
212     table.accept(visitor);
213 
214     StackMapEntry[] vars = table.getStackMap();
215 
216     for(int i=0; i < vars.length; i++)
217       vars[i].accept(this);
218     stack.pop();
219   }
220 
221   public void visitStackMapEntry(StackMapEntry var) {
222     stack.push(var);
223     var.accept(visitor);
224     stack.pop();
225   }
226 
227   public void visitLocalVariable(LocalVariable var) {
228     stack.push(var);
229     var.accept(visitor);
230     stack.pop();
231   }
232 
233   public void visitConstantPool(ConstantPool cp) {
234     stack.push(cp);
235     cp.accept(visitor);
236 
237     Constant[] constants = cp.getConstantPool();
238     for(int i=1; i < constants.length; i++) {
239       if(constants[i] != null)
240 	constants[i].accept(this);
241     }
242 
243     stack.pop();
244   }
245 
246   public void visitConstantClass(ConstantClass constant) {
247     stack.push(constant);
248     constant.accept(visitor);
249     stack.pop();
250   }
251 
252   public void visitConstantDouble(ConstantDouble constant) {
253     stack.push(constant);
254     constant.accept(visitor);
255     stack.pop();
256   }
257 
258   public void visitConstantFieldref(ConstantFieldref constant) {
259     stack.push(constant);
260     constant.accept(visitor);
261     stack.pop();
262   }
263 
264   public void visitConstantFloat(ConstantFloat constant) {
265     stack.push(constant);
266     constant.accept(visitor);
267     stack.pop();
268  }
269 
270   public void visitConstantInteger(ConstantInteger constant) {
271     stack.push(constant);
272     constant.accept(visitor);
273     stack.pop();
274   }
275 
276   public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref constant) {
277     stack.push(constant);
278     constant.accept(visitor);
279     stack.pop();
280   }
281 
282   public void visitConstantLong(ConstantLong constant) {
283     stack.push(constant);
284     constant.accept(visitor);
285     stack.pop();
286   }
287 
288   public void visitConstantMethodref(ConstantMethodref constant) {
289     stack.push(constant);
290     constant.accept(visitor); 
291     stack.pop();
292   }
293 
294   public void visitConstantNameAndType(ConstantNameAndType constant) {
295     stack.push(constant);
296     constant.accept(visitor);
297     stack.pop();
298   }
299 
300   public void visitConstantString(ConstantString constant) {
301     stack.push(constant);
302     constant.accept(visitor);
303     stack.pop();
304   }
305 
306   public void visitConstantUtf8(ConstantUtf8 constant) {
307     stack.push(constant);
308     constant.accept(visitor);
309     stack.pop();
310   }
311 
312   public void visitInnerClasses(InnerClasses ic) {
313     stack.push(ic);
314     ic.accept(visitor);
315 
316     InnerClass[] ics = ic.getInnerClasses();
317     for(int i=0; i < ics.length; i++)
318       ics[i].accept(this);
319     stack.pop();
320   }
321 
322   public void visitInnerClass(InnerClass inner) {
323     stack.push(inner);
324     inner.accept(visitor);
325     stack.pop();
326   }
327 
328   public void visitDeprecated(Deprecated attribute) {
329     stack.push(attribute);
330     attribute.accept(visitor);
331     stack.pop();
332   }
333 
334   public void visitSignature(Signature attribute) {
335     stack.push(attribute);
336     attribute.accept(visitor);
337     stack.pop();
338   }
339   
340   public void visitSourceFile(SourceFile attribute) {
341     stack.push(attribute);
342     attribute.accept(visitor);
343     stack.pop();
344   }
345 
346   public void visitSynthetic(Synthetic attribute) {
347     stack.push(attribute);
348     attribute.accept(visitor);
349     stack.pop();
350   }
351 
352   public void visitUnknown(Unknown attribute) {
353     stack.push(attribute);
354     attribute.accept(visitor);
355     stack.pop();
356  }
357 }
This page was automatically generated by Maven