KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > 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  * Contact: Eric.Bruneton@rd.francetelecom.com
31  *
32  * Author: Eric Bruneton
33  */

34
35 package org.logicalcobwebs.asm.tree;
36
37 import org.logicalcobwebs.asm.ClassVisitor;
38 import org.logicalcobwebs.asm.CodeVisitor;
39 import org.logicalcobwebs.asm.Label;
40 import org.logicalcobwebs.asm.Attribute;
41
42 import java.util.List JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Arrays JavaDoc;
45
46 /**
47  * A node that represents a method.
48  */

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

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

62
63   public String JavaDoc name;
64
65   /**
66    * The method's descriptor (see {@link org.logicalcobwebs.asm.Type Type}).
67    */

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

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

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

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

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

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

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

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

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

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

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

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