001: /***
002: * ASM: a very small and fast Java bytecode manipulation framework
003: * Copyright (c) 2000-2007 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: */package org.objectweb.asm.tree;
030:
031: import org.objectweb.asm.AnnotationVisitor;
032: import org.objectweb.asm.Attribute;
033: import org.objectweb.asm.ClassVisitor;
034: import org.objectweb.asm.MethodVisitor;
035: import org.objectweb.asm.Label;
036: import org.objectweb.asm.Opcodes;
037: import org.objectweb.asm.Type;
038:
039: import java.util.List;
040: import java.util.ArrayList;
041: import java.util.Arrays;
042:
043: /**
044: * A node that represents a method.
045: *
046: * @author Eric Bruneton
047: */
048: public class MethodNode extends MemberNode implements MethodVisitor {
049:
050: /**
051: * The method's access flags (see {@link Opcodes}). This field also
052: * indicates if the method is synthetic and/or deprecated.
053: */
054: public int access;
055:
056: /**
057: * The method's name.
058: */
059: public String name;
060:
061: /**
062: * The method's descriptor (see {@link Type}).
063: */
064: public String desc;
065:
066: /**
067: * The method's signature. May be <tt>null</tt>.
068: */
069: public String signature;
070:
071: /**
072: * The internal names of the method's exception classes (see
073: * {@link Type#getInternalName() getInternalName}). This list is a list of
074: * {@link String} objects.
075: */
076: public List exceptions;
077:
078: /**
079: * The default value of this annotation interface method. This field must be
080: * a {@link Byte}, {@link Boolean}, {@link Character}, {@link Short},
081: * {@link Integer}, {@link Long}, {@link Float}, {@link Double},
082: * {@link String} or {@link Type}, or an two elements String array (for
083: * enumeration values), a {@link AnnotationNode}, or a {@link List} of
084: * values of one of the preceding types. May be <tt>null</tt>.
085: */
086: public Object annotationDefault;
087:
088: /**
089: * The runtime visible parameter annotations of this method. These lists are
090: * lists of {@link AnnotationNode} objects. May be <tt>null</tt>.
091: *
092: * @associates org.objectweb.asm.tree.AnnotationNode
093: * @label invisible parameters
094: */
095: public List[] visibleParameterAnnotations;
096:
097: /**
098: * The runtime invisible parameter annotations of this method. These lists
099: * are lists of {@link AnnotationNode} objects. May be <tt>null</tt>.
100: *
101: * @associates org.objectweb.asm.tree.AnnotationNode
102: * @label visible parameters
103: */
104: public List[] invisibleParameterAnnotations;
105:
106: /**
107: * The instructions of this method. This list is a list of
108: * {@link AbstractInsnNode} objects.
109: *
110: * @associates org.objectweb.asm.tree.AbstractInsnNode
111: * @label instructions
112: */
113: public InsnList instructions;
114:
115: /**
116: * The try catch blocks of this method. This list is a list of
117: * {@link TryCatchBlockNode} objects.
118: *
119: * @associates org.objectweb.asm.tree.TryCatchBlockNode
120: */
121: public List tryCatchBlocks;
122:
123: /**
124: * The maximum stack size of this method.
125: */
126: public int maxStack;
127:
128: /**
129: * The maximum number of local variables of this method.
130: */
131: public int maxLocals;
132:
133: /**
134: * The local variables of this method. This list is a list of
135: * {@link LocalVariableNode} objects. May be <tt>null</tt>
136: *
137: * @associates org.objectweb.asm.tree.LocalVariableNode
138: */
139: public List localVariables;
140:
141: /**
142: * Constructs an unitialized {@link MethodNode}.
143: */
144: public MethodNode() {
145: this .instructions = new InsnList();
146: }
147:
148: /**
149: * Constructs a new {@link MethodNode}.
150: *
151: * @param access the method's access flags (see {@link Opcodes}). This
152: * parameter also indicates if the method is synthetic and/or
153: * deprecated.
154: * @param name the method's name.
155: * @param desc the method's descriptor (see {@link Type}).
156: * @param signature the method's signature. May be <tt>null</tt>.
157: * @param exceptions the internal names of the method's exception classes
158: * (see {@link Type#getInternalName() getInternalName}). May be
159: * <tt>null</tt>.
160: */
161: public MethodNode(final int access, final String name,
162: final String desc, final String signature,
163: final String[] exceptions) {
164: this ();
165: this .access = access;
166: this .name = name;
167: this .desc = desc;
168: this .signature = signature;
169: this .exceptions = new ArrayList(exceptions == null ? 0
170: : exceptions.length);
171: boolean isAbstract = (access & Opcodes.ACC_ABSTRACT) != 0;
172: if (!isAbstract) {
173: this .localVariables = new ArrayList(5);
174: }
175: this .tryCatchBlocks = new ArrayList();
176: if (exceptions != null) {
177: this .exceptions.addAll(Arrays.asList(exceptions));
178: }
179: }
180:
181: // ------------------------------------------------------------------------
182: // Implementation of the MethodVisitor interface
183: // ------------------------------------------------------------------------
184:
185: public AnnotationVisitor visitAnnotationDefault() {
186: return new AnnotationNode(new ArrayList(0) {
187: public boolean add(final Object o) {
188: annotationDefault = o;
189: return super .add(o);
190: }
191: });
192: }
193:
194: public AnnotationVisitor visitParameterAnnotation(
195: final int parameter, final String desc,
196: final boolean visible) {
197: AnnotationNode an = new AnnotationNode(desc);
198: if (visible) {
199: if (visibleParameterAnnotations == null) {
200: int params = Type.getArgumentTypes(this .desc).length;
201: visibleParameterAnnotations = new List[params];
202: }
203: if (visibleParameterAnnotations[parameter] == null) {
204: visibleParameterAnnotations[parameter] = new ArrayList(
205: 1);
206: }
207: visibleParameterAnnotations[parameter].add(an);
208: } else {
209: if (invisibleParameterAnnotations == null) {
210: int params = Type.getArgumentTypes(this .desc).length;
211: invisibleParameterAnnotations = new List[params];
212: }
213: if (invisibleParameterAnnotations[parameter] == null) {
214: invisibleParameterAnnotations[parameter] = new ArrayList(
215: 1);
216: }
217: invisibleParameterAnnotations[parameter].add(an);
218: }
219: return an;
220: }
221:
222: public void visitCode() {
223: }
224:
225: public void visitFrame(final int type, final int nLocal,
226: final Object[] local, final int nStack, final Object[] stack) {
227: instructions.add(new FrameNode(type, nLocal,
228: local == null ? null : getLabelNodes(local), nStack,
229: stack == null ? null : getLabelNodes(stack)));
230: }
231:
232: public void visitInsn(final int opcode) {
233: instructions.add(new InsnNode(opcode));
234: }
235:
236: public void visitIntInsn(final int opcode, final int operand) {
237: instructions.add(new IntInsnNode(opcode, operand));
238: }
239:
240: public void visitVarInsn(final int opcode, final int var) {
241: instructions.add(new VarInsnNode(opcode, var));
242: }
243:
244: public void visitTypeInsn(final int opcode, final String type) {
245: instructions.add(new TypeInsnNode(opcode, type));
246: }
247:
248: public void visitFieldInsn(final int opcode, final String owner,
249: final String name, final String desc) {
250: instructions.add(new FieldInsnNode(opcode, owner, name, desc));
251: }
252:
253: public void visitMethodInsn(final int opcode, final String owner,
254: final String name, final String desc) {
255: instructions.add(new MethodInsnNode(opcode, owner, name, desc));
256: }
257:
258: public void visitJumpInsn(final int opcode, final Label label) {
259: instructions.add(new JumpInsnNode(opcode, getLabelNode(label)));
260: }
261:
262: public void visitLabel(final Label label) {
263: instructions.add(getLabelNode(label));
264: }
265:
266: public void visitLdcInsn(final Object cst) {
267: instructions.add(new LdcInsnNode(cst));
268: }
269:
270: public void visitIincInsn(final int var, final int increment) {
271: instructions.add(new IincInsnNode(var, increment));
272: }
273:
274: public void visitTableSwitchInsn(final int min, final int max,
275: final Label dflt, final Label[] labels) {
276: instructions.add(new TableSwitchInsnNode(min, max,
277: getLabelNode(dflt), getLabelNodes(labels)));
278: }
279:
280: public void visitLookupSwitchInsn(final Label dflt,
281: final int[] keys, final Label[] labels) {
282: instructions.add(new LookupSwitchInsnNode(getLabelNode(dflt),
283: keys, getLabelNodes(labels)));
284: }
285:
286: public void visitMultiANewArrayInsn(final String desc,
287: final int dims) {
288: instructions.add(new MultiANewArrayInsnNode(desc, dims));
289: }
290:
291: public void visitTryCatchBlock(final Label start, final Label end,
292: final Label handler, final String type) {
293: tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(start),
294: getLabelNode(end), getLabelNode(handler), type));
295: }
296:
297: public void visitLocalVariable(final String name,
298: final String desc, final String signature,
299: final Label start, final Label end, final int index) {
300: localVariables.add(new LocalVariableNode(name, desc, signature,
301: getLabelNode(start), getLabelNode(end), index));
302: }
303:
304: public void visitLineNumber(final int line, final Label start) {
305: instructions.add(new LineNumberNode(line, getLabelNode(start)));
306: }
307:
308: public void visitMaxs(final int maxStack, final int maxLocals) {
309: this .maxStack = maxStack;
310: this .maxLocals = maxLocals;
311: }
312:
313: /**
314: * Returns the LabelNode corresponding to the given Label. Creates a new
315: * LabelNode if necessary. The default implementation of this method uses
316: * the {@link Label#info} field to store associations between labels and
317: * label nodes.
318: *
319: * @param l a Label.
320: * @return the LabelNode corresponding to l.
321: */
322: protected LabelNode getLabelNode(final Label l) {
323: if (!(l.info instanceof LabelNode)) {
324: l.info = new LabelNode(l);
325: }
326: return (LabelNode) l.info;
327: }
328:
329: private LabelNode[] getLabelNodes(final Label[] l) {
330: LabelNode[] nodes = new LabelNode[l.length];
331: for (int i = 0; i < l.length; ++i) {
332: nodes[i] = getLabelNode(l[i]);
333: }
334: return nodes;
335: }
336:
337: private Object[] getLabelNodes(final Object[] objs) {
338: Object[] nodes = new Object[objs.length];
339: for (int i = 0; i < objs.length; ++i) {
340: Object o = objs[i];
341: if (o instanceof Label) {
342: o = getLabelNode((Label) o);
343: }
344: nodes[i] = o;
345: }
346: return nodes;
347: }
348:
349: // ------------------------------------------------------------------------
350: // Accept method
351: // ------------------------------------------------------------------------
352:
353: /**
354: * Makes the given class visitor visit this method.
355: *
356: * @param cv a class visitor.
357: */
358: public void accept(final ClassVisitor cv) {
359: String[] exceptions = new String[this .exceptions.size()];
360: this .exceptions.toArray(exceptions);
361: MethodVisitor mv = cv.visitMethod(access, name, desc,
362: signature, exceptions);
363: if (mv != null) {
364: accept(mv);
365: }
366: }
367:
368: /**
369: * Makes the given method visitor visit this method.
370: *
371: * @param mv a method visitor.
372: */
373: public void accept(final MethodVisitor mv) {
374: // visits the method attributes
375: int i, j, n;
376: if (annotationDefault != null) {
377: AnnotationVisitor av = mv.visitAnnotationDefault();
378: AnnotationNode.accept(av, null, annotationDefault);
379: if (av != null) {
380: av.visitEnd();
381: }
382: }
383: n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
384: for (i = 0; i < n; ++i) {
385: AnnotationNode an = (AnnotationNode) visibleAnnotations
386: .get(i);
387: an.accept(mv.visitAnnotation(an.desc, true));
388: }
389: n = invisibleAnnotations == null ? 0 : invisibleAnnotations
390: .size();
391: for (i = 0; i < n; ++i) {
392: AnnotationNode an = (AnnotationNode) invisibleAnnotations
393: .get(i);
394: an.accept(mv.visitAnnotation(an.desc, false));
395: }
396: n = visibleParameterAnnotations == null ? 0
397: : visibleParameterAnnotations.length;
398: for (i = 0; i < n; ++i) {
399: List l = visibleParameterAnnotations[i];
400: if (l == null) {
401: continue;
402: }
403: for (j = 0; j < l.size(); ++j) {
404: AnnotationNode an = (AnnotationNode) l.get(j);
405: an
406: .accept(mv.visitParameterAnnotation(i, an.desc,
407: true));
408: }
409: }
410: n = invisibleParameterAnnotations == null ? 0
411: : invisibleParameterAnnotations.length;
412: for (i = 0; i < n; ++i) {
413: List l = invisibleParameterAnnotations[i];
414: if (l == null) {
415: continue;
416: }
417: for (j = 0; j < l.size(); ++j) {
418: AnnotationNode an = (AnnotationNode) l.get(j);
419: an.accept(mv
420: .visitParameterAnnotation(i, an.desc, false));
421: }
422: }
423: n = attrs == null ? 0 : attrs.size();
424: for (i = 0; i < n; ++i) {
425: mv.visitAttribute((Attribute) attrs.get(i));
426: }
427: // visits the method's code
428: if (instructions.size() > 0) {
429: mv.visitCode();
430: // visits try catch blocks
431: for (i = 0; i < tryCatchBlocks.size(); ++i) {
432: ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv);
433: }
434: // visits instructions
435: instructions.accept(mv);
436: // visits local variables
437: n = localVariables == null ? 0 : localVariables.size();
438: for (i = 0; i < n; ++i) {
439: ((LocalVariableNode) localVariables.get(i)).accept(mv);
440: }
441: // visits maxs
442: mv.visitMaxs(maxStack, maxLocals);
443: }
444: mv.visitEnd();
445: }
446: }
|