KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > libraries > asm > tree > MethodNode


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000,2002,2003 INRIA, France Telecom
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28  * THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package oracle.toplink.libraries.asm.tree;
32
33 import oracle.toplink.libraries.asm.ClassVisitor;
34 import oracle.toplink.libraries.asm.CodeVisitor;
35 import oracle.toplink.libraries.asm.Label;
36 import oracle.toplink.libraries.asm.Attribute;
37
38 import java.util.List JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Arrays JavaDoc;
41
42 /**
43  * A node that represents a method.
44  *
45  * @author Eric Bruneton
46  */

47
48 public class MethodNode {
49
50   /**
51    * The method's access flags (see {@link oracle.toplink.libraries.asm.Constants}). This
52    * field also indicates if the method is synthetic and/or deprecated.
53    */

54
55   public int access;
56
57   /**
58    * The method's name.
59    */

60
61   public String JavaDoc name;
62
63   /**
64    * The method's descriptor (see {@link oracle.toplink.libraries.asm.Type Type}).
65    */

66
67   public String JavaDoc desc;
68
69   /**
70    * The internal names of the method's exception classes (see {@link
71    * oracle.toplink.libraries.asm.Type#getInternalName() getInternalName}). This list is a
72    * list of {@link String} objects.
73    */

74
75   public final List JavaDoc exceptions;
76
77   /**
78    * The non standard attributes of the method.
79    */

80
81   public Attribute attrs;
82
83   /**
84    * The instructions of this method. This list is a list of {@link
85    * AbstractInsnNode AbstractInsnNode} and {@link Label Label} objects.
86    */

87
88   public final List JavaDoc instructions;
89
90   /**
91    * The try catch blocks of this method. This list is a list of {@link
92    * TryCatchBlockNode TryCatchBlockNode} objects.
93    */

94
95   public final List JavaDoc tryCatchBlocks;
96
97   /**
98    * The maximum stack size of this method.
99    */

100
101   public int maxStack;
102
103   /**
104    * The maximum number of local variables of this method.
105    */

106
107   public int maxLocals;
108
109   /**
110    * The local variables of this method. This list is a list of {@link
111    * LocalVariableNode LocalVariableNode} objects.
112    */

113
114   public final List JavaDoc localVariables;
115
116   /**
117    * The line numbers of this method. This list is a list of {@link
118    * LineNumberNode LineNumberNode} objects.
119    */

120
121   public final List JavaDoc lineNumbers;
122
123   /**
124    * The non standard attributes of the method's code.
125    */

126
127   public Attribute codeAttrs;
128
129   /**
130    * Constructs a new {@link MethodNode MethodNode} object.
131    *
132    * @param access the method's access flags (see {@link
133    * oracle.toplink.libraries.asm.Constants}). This parameter also indicates if the
134    * method is synthetic and/or deprecated.
135    * @param name the method's name.
136    * @param desc the method's descriptor (see {@link oracle.toplink.libraries.asm.Type
137    * Type}).
138    * @param exceptions the internal names of the method's exception
139    * classes (see {@link oracle.toplink.libraries.asm.Type#getInternalName()
140    * getInternalName}). May be <tt>null</tt>.
141    * @param attrs the non standard attributes of the method.
142    */

143
144   public MethodNode (
145     final int access,
146     final String JavaDoc name,
147     final String JavaDoc desc,
148     final String JavaDoc[] exceptions,
149     final Attribute attrs)
150   {
151     this.access = access;
152     this.name = name;
153     this.desc = desc;
154     this.exceptions = new ArrayList JavaDoc();
155     this.instructions = new ArrayList JavaDoc();
156     this.tryCatchBlocks = new ArrayList JavaDoc();
157     this.localVariables = new ArrayList JavaDoc();
158     this.lineNumbers = new ArrayList JavaDoc();
159     if (exceptions != null) {
160       this.exceptions.addAll(Arrays.asList(exceptions));
161     }
162     this.attrs = attrs;
163   }
164
165   /**
166    * Makes the given class visitor visit this method.
167    *
168    * @param cv a class visitor.
169    */

170
171   public void accept (final ClassVisitor cv) {
172     String JavaDoc[] exceptions = new String JavaDoc[this.exceptions.size()];
173     this.exceptions.toArray(exceptions);
174     CodeVisitor mv = cv.visitMethod(access, name, desc, exceptions, attrs);
175     if (mv != null && instructions.size() > 0) {
176       int i;
177       // visits instructions
178
for (i = 0; i < instructions.size(); ++i) {
179         Object JavaDoc insn = instructions.get(i);
180         if (insn instanceof Label) {
181           mv.visitLabel((Label)insn);
182         } else {
183           ((AbstractInsnNode)insn).accept(mv);
184         }
185       }
186       // visits try catch blocks
187
for (i = 0; i < tryCatchBlocks.size(); ++i) {
188         ((TryCatchBlockNode)tryCatchBlocks.get(i)).accept(mv);
189       }
190       // visits maxs
191
mv.visitMaxs(maxStack, maxLocals);
192       // visits local variables
193
for (i = 0; i < localVariables.size(); ++i) {
194         ((LocalVariableNode)localVariables.get(i)).accept(mv);
195       }
196       // visits line numbers
197
for (i = 0; i < lineNumbers.size(); ++i) {
198         ((LineNumberNode)lineNumbers.get(i)).accept(mv);
199       }
200       // visits the code attributes
201
Attribute attrs = codeAttrs;
202       while (attrs != null) {
203         mv.visitAttribute(attrs);
204         attrs = attrs.next;
205       }
206     }
207   }
208 }
209
Popular Tags