001: /*
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2005 INRIA, France Telecom
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: * 2. Redistributions in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in the
013: * documentation and/or other materials provided with the distribution.
014: * 3. Neither the name of the copyright holders nor the names of its
015: * contributors may be used to endorse or promote products derived from
016: * this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
028: * THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package org.objectweb.asm.tree;
031:
032: import org.objectweb.asm.AnnotationVisitor;
033: import org.objectweb.asm.Attribute;
034: import org.objectweb.asm.ClassVisitor;
035: import org.objectweb.asm.MethodVisitor;
036: import org.objectweb.asm.Label;
037: import org.objectweb.asm.Opcodes;
038: import org.objectweb.asm.Type;
039:
040: import java.util.List;
041: import java.util.ArrayList;
042: import java.util.Arrays;
043:
044: /**
045: * A node that represents a method.
046: *
047: * @author Eric Bruneton
048: */
049: @SuppressWarnings("unchecked")
050: public class MethodNode extends MemberNode implements MethodVisitor {
051:
052: /**
053: * The method's access flags (see {@link Opcodes}). This field also
054: * indicates if the method is synthetic and/or deprecated.
055: */
056: public int access;
057:
058: /**
059: * The method's name.
060: */
061: public String name;
062:
063: /**
064: * The method's descriptor (see {@link Type}).
065: */
066: public String desc;
067:
068: /**
069: * The method's signature. May be <tt>null</tt>.
070: */
071: public String signature;
072:
073: /**
074: * The internal names of the method's exception classes (see
075: * {@link Type#getInternalName() getInternalName}). This list is a list of
076: * {@link String} objects.
077: */
078: public List exceptions;
079:
080: /**
081: * The default value of this annotation interface method. This field must be
082: * a {@link Byte}, {@link Boolean}, {@link Character}, {@link Short},
083: * {@link Integer}, {@link Long}, {@link Float}, {@link Double},
084: * {@link String} or {@link Type}, or an two elements String array (for
085: * enumeration values), a {@link AnnotationNode}, or a {@link List} of
086: * values of one of the preceding types. May be <tt>null</tt>.
087: */
088: public Object annotationDefault;
089:
090: /**
091: * The runtime visible parameter annotations of this method. These lists are
092: * lists of {@link AnnotationNode} objects. May be <tt>null</tt>.
093: *
094: * @associates org.objectweb.asm.tree.AnnotationNode
095: * @label invisible parameters
096: */
097: public List[] visibleParameterAnnotations;
098:
099: /**
100: * The runtime invisible parameter annotations of this method. These lists
101: * are lists of {@link AnnotationNode} objects. May be <tt>null</tt>.
102: *
103: * @associates org.objectweb.asm.tree.AnnotationNode
104: * @label visible parameters
105: */
106: public List[] invisibleParameterAnnotations;
107:
108: /**
109: * The instructions of this method. This list is a list of
110: * {@link AbstractInsnNode} objects.
111: *
112: * @associates org.objectweb.asm.tree.AbstractInsnNode
113: * @label instructions
114: */
115: public InsnList instructions;
116:
117: /**
118: * The try catch blocks of this method. This list is a list of
119: * {@link TryCatchBlockNode} objects.
120: *
121: * @associates org.objectweb.asm.tree.TryCatchBlockNode
122: */
123: public List tryCatchBlocks;
124:
125: /**
126: * The maximum stack size of this method.
127: */
128: public int maxStack;
129:
130: /**
131: * The maximum number of local variables of this method.
132: */
133: public int maxLocals;
134:
135: /**
136: * The local variables of this method. This list is a list of
137: * {@link LocalVariableNode} objects. May be <tt>null</tt>
138: *
139: * @associates org.objectweb.asm.tree.LocalVariableNode
140: */
141: public List localVariables;
142:
143: /**
144: * Constructs a new {@link MethodNode}.
145: *
146: * @param access the method's access flags (see {@link Opcodes}). This
147: * parameter also indicates if the method is synthetic and/or
148: * deprecated.
149: * @param name the method's name.
150: * @param desc the method's descriptor (see {@link Type}).
151: * @param signature the method's signature. May be <tt>null</tt>.
152: * @param exceptions the internal names of the method's exception classes
153: * (see {@link Type#getInternalName() getInternalName}). May be
154: * <tt>null</tt>.
155: */
156: public MethodNode(final int access, final String name,
157: final String desc, final String signature,
158: final String[] exceptions) {
159: this .access = access;
160: this .name = name;
161: this .desc = desc;
162: this .signature = signature;
163: this .exceptions = new ArrayList(exceptions == null ? 0
164: : exceptions.length);
165: boolean isAbstract = (access & Opcodes.ACC_ABSTRACT) != 0;
166: this .instructions = new InsnList();
167: if (!isAbstract) {
168: this .localVariables = new ArrayList(5);
169: }
170: this .tryCatchBlocks = new ArrayList();
171: if (exceptions != null) {
172: this .exceptions.addAll(Arrays.asList(exceptions));
173: }
174: }
175:
176: // ------------------------------------------------------------------------
177: // Implementation of the MethodVisitor interface
178: // ------------------------------------------------------------------------
179:
180: public AnnotationVisitor visitAnnotationDefault() {
181: return new AnnotationNode(new ArrayList(0) {
182: public boolean add(final Object o) {
183: annotationDefault = o;
184: return super .add(o);
185: }
186: });
187: }
188:
189: public AnnotationVisitor visitParameterAnnotation(
190: final int parameter, final String desc,
191: final boolean visible) {
192: AnnotationNode an = new AnnotationNode(desc);
193: if (visible) {
194: if (visibleParameterAnnotations == null) {
195: int params = Type.getArgumentTypes(this .desc).length;
196: visibleParameterAnnotations = new List[params];
197: }
198: if (visibleParameterAnnotations[parameter] == null) {
199: visibleParameterAnnotations[parameter] = new ArrayList(
200: 1);
201: }
202: visibleParameterAnnotations[parameter].add(an);
203: } else {
204: if (invisibleParameterAnnotations == null) {
205: int params = Type.getArgumentTypes(this .desc).length;
206: invisibleParameterAnnotations = new List[params];
207: }
208: if (invisibleParameterAnnotations[parameter] == null) {
209: invisibleParameterAnnotations[parameter] = new ArrayList(
210: 1);
211: }
212: invisibleParameterAnnotations[parameter].add(an);
213: }
214: return an;
215: }
216:
217: public void visitCode() {
218: }
219:
220: public void visitFrame(final int type, final int nLocal,
221: final Object[] local, final int nStack, final Object[] stack) {
222: instructions.add(new FrameNode(type, nLocal,
223: local == null ? null : labelNodes(local), nStack,
224: stack == null ? null : labelNodes(stack)));
225: }
226:
227: public void visitInsn(final int opcode) {
228: instructions.add(new InsnNode(opcode));
229: }
230:
231: public void visitIntInsn(final int opcode, final int operand) {
232: instructions.add(new IntInsnNode(opcode, operand));
233: }
234:
235: public void visitVarInsn(final int opcode, final int var) {
236: instructions.add(new VarInsnNode(opcode, var));
237: }
238:
239: public void visitTypeInsn(final int opcode, final String desc) {
240: instructions.add(new TypeInsnNode(opcode, desc));
241: }
242:
243: public void visitFieldInsn(final int opcode, final String owner,
244: final String name, final String desc) {
245: instructions.add(new FieldInsnNode(opcode, owner, name, desc));
246: }
247:
248: public void visitMethodInsn(final int opcode, final String owner,
249: final String name, final String desc) {
250: instructions.add(new MethodInsnNode(opcode, owner, name, desc));
251: }
252:
253: public void visitJumpInsn(final int opcode, final Label label) {
254: instructions.add(new JumpInsnNode(opcode, labelNode(label)));
255: }
256:
257: public void visitLabel(final Label label) {
258: instructions.add(labelNode(label));
259: }
260:
261: public void visitLdcInsn(final Object cst) {
262: instructions.add(new LdcInsnNode(cst));
263: }
264:
265: public void visitIincInsn(final int var, final int increment) {
266: instructions.add(new IincInsnNode(var, increment));
267: }
268:
269: public void visitTableSwitchInsn(final int min, final int max,
270: final Label dflt, final Label[] labels) {
271: instructions.add(new TableSwitchInsnNode(min, max,
272: labelNode(dflt), labelNodes(labels)));
273: }
274:
275: public void visitLookupSwitchInsn(final Label dflt,
276: final int[] keys, final Label[] labels) {
277: instructions.add(new LookupSwitchInsnNode(labelNode(dflt),
278: keys, labelNodes(labels)));
279: }
280:
281: public void visitMultiANewArrayInsn(final String desc,
282: final int dims) {
283: instructions.add(new MultiANewArrayInsnNode(desc, dims));
284: }
285:
286: public void visitTryCatchBlock(final Label start, final Label end,
287: final Label handler, final String type) {
288: tryCatchBlocks.add(new TryCatchBlockNode(labelNode(start),
289: labelNode(end), labelNode(handler), type));
290: }
291:
292: public void visitLocalVariable(final String name,
293: final String desc, final String signature,
294: final Label start, final Label end, final int index) {
295: localVariables.add(new LocalVariableNode(name, desc, signature,
296: labelNode(start), labelNode(end), index));
297: }
298:
299: public void visitLineNumber(final int line, final Label start) {
300: instructions.add(new LineNumberNode(line, labelNode(start)));
301: }
302:
303: public void visitMaxs(final int maxStack, final int maxLocals) {
304: this .maxStack = maxStack;
305: this .maxLocals = maxLocals;
306: }
307:
308: private static LabelNode labelNode(final Label l) {
309: if (l.info == null) {
310: l.info = new LabelNode(l);
311: }
312: return (LabelNode) l.info;
313: }
314:
315: private static LabelNode[] labelNodes(final Label[] l) {
316: LabelNode[] nodes = new LabelNode[l.length];
317: for (int i = 0; i < l.length; ++i) {
318: nodes[i] = labelNode(l[i]);
319: }
320: return nodes;
321: }
322:
323: private static Object[] labelNodes(final Object[] objs) {
324: Object[] nodes = new Object[objs.length];
325: for (int i = 0; i < objs.length; ++i) {
326: Object o = objs[i];
327: if (o instanceof Label) {
328: o = labelNode((Label) o);
329: }
330: nodes[i] = o;
331: }
332: return nodes;
333: }
334:
335: // ------------------------------------------------------------------------
336: // Accept method
337: // ------------------------------------------------------------------------
338:
339: /**
340: * Makes the given class visitor visit this method.
341: *
342: * @param cv a class visitor.
343: */
344: public void accept(final ClassVisitor cv) {
345: String[] exceptions = new String[this .exceptions.size()];
346: this .exceptions.toArray(exceptions);
347: MethodVisitor mv = cv.visitMethod(access, name, desc,
348: signature, exceptions);
349: if (mv != null) {
350: accept(mv);
351: }
352: }
353:
354: /**
355: * Makes the given method visitor visit this method.
356: *
357: * @param mv a method visitor.
358: */
359: public void accept(final MethodVisitor mv) {
360: // visits the method attributes
361: int i, j, n;
362: if (annotationDefault != null) {
363: AnnotationVisitor av = mv.visitAnnotationDefault();
364: AnnotationNode.accept(av, null, annotationDefault);
365: av.visitEnd();
366: }
367: n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
368: for (i = 0; i < n; ++i) {
369: AnnotationNode an = (AnnotationNode) visibleAnnotations
370: .get(i);
371: an.accept(mv.visitAnnotation(an.desc, true));
372: }
373: n = invisibleAnnotations == null ? 0 : invisibleAnnotations
374: .size();
375: for (i = 0; i < n; ++i) {
376: AnnotationNode an = (AnnotationNode) invisibleAnnotations
377: .get(i);
378: an.accept(mv.visitAnnotation(an.desc, false));
379: }
380: n = visibleParameterAnnotations == null ? 0
381: : visibleParameterAnnotations.length;
382: for (i = 0; i < n; ++i) {
383: List l = visibleParameterAnnotations[i];
384: if (l == null) {
385: continue;
386: }
387: for (j = 0; j < l.size(); ++j) {
388: AnnotationNode an = (AnnotationNode) l.get(j);
389: an
390: .accept(mv.visitParameterAnnotation(i, an.desc,
391: true));
392: }
393: }
394: n = invisibleParameterAnnotations == null ? 0
395: : invisibleParameterAnnotations.length;
396: for (i = 0; i < n; ++i) {
397: List l = invisibleParameterAnnotations[i];
398: if (l == null) {
399: continue;
400: }
401: for (j = 0; j < l.size(); ++j) {
402: AnnotationNode an = (AnnotationNode) l.get(j);
403: an.accept(mv
404: .visitParameterAnnotation(i, an.desc, false));
405: }
406: }
407: n = attrs == null ? 0 : attrs.size();
408: for (i = 0; i < n; ++i) {
409: mv.visitAttribute((Attribute) attrs.get(i));
410: }
411: // visits the method's code
412: if (instructions.size() > 0) {
413: mv.visitCode();
414: // visits try catch blocks
415: for (i = 0; i < tryCatchBlocks.size(); ++i) {
416: ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv);
417: }
418: // visits instructions
419: instructions.accept(mv);
420: // visits local variables
421: n = localVariables == null ? 0 : localVariables.size();
422: for (i = 0; i < n; ++i) {
423: ((LocalVariableNode) localVariables.get(i)).accept(mv);
424: }
425: // visits maxs
426: mv.visitMaxs(maxStack, maxLocals);
427: }
428: mv.visitEnd();
429: }
430: }
|