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: */package com.tc.asm.tree;
030:
031: import com.tc.asm.AnnotationVisitor;
032: import com.tc.asm.Attribute;
033: import com.tc.asm.ClassVisitor;
034: import com.tc.asm.Label;
035: import com.tc.asm.MethodVisitor;
036: import com.tc.asm.Opcodes;
037: import com.tc.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 com.tc.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 com.tc.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 com.tc.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 com.tc.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 com.tc.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 : labelNodes(local), nStack,
229: stack == null ? null : labelNodes(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 desc) {
245: instructions.add(new TypeInsnNode(opcode, desc));
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, labelNode(label)));
260: }
261:
262: public void visitLabel(final Label label) {
263: instructions.add(labelNode(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: labelNode(dflt), labelNodes(labels)));
278: }
279:
280: public void visitLookupSwitchInsn(final Label dflt,
281: final int[] keys, final Label[] labels) {
282: instructions.add(new LookupSwitchInsnNode(labelNode(dflt),
283: keys, labelNodes(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(labelNode(start),
294: labelNode(end), labelNode(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: labelNode(start), labelNode(end), index));
302: }
303:
304: public void visitLineNumber(final int line, final Label start) {
305: instructions.add(new LineNumberNode(line, labelNode(start)));
306: }
307:
308: public void visitMaxs(final int maxStack, final int maxLocals) {
309: this .maxStack = maxStack;
310: this .maxLocals = maxLocals;
311: }
312:
313: private static LabelNode labelNode(final Label l) {
314: if (l.info == null) {
315: l.info = new LabelNode(l);
316: }
317: return (LabelNode) l.info;
318: }
319:
320: private static LabelNode[] labelNodes(final Label[] l) {
321: LabelNode[] nodes = new LabelNode[l.length];
322: for (int i = 0; i < l.length; ++i) {
323: nodes[i] = labelNode(l[i]);
324: }
325: return nodes;
326: }
327:
328: private static Object[] labelNodes(final Object[] objs) {
329: Object[] nodes = new Object[objs.length];
330: for (int i = 0; i < objs.length; ++i) {
331: Object o = objs[i];
332: if (o instanceof Label) {
333: o = labelNode((Label) o);
334: }
335: nodes[i] = o;
336: }
337: return nodes;
338: }
339:
340: // ------------------------------------------------------------------------
341: // Accept method
342: // ------------------------------------------------------------------------
343:
344: /**
345: * Makes the given class visitor visit this method.
346: *
347: * @param cv a class visitor.
348: */
349: public void accept(final ClassVisitor cv) {
350: String[] exceptions = new String[this .exceptions.size()];
351: this .exceptions.toArray(exceptions);
352: MethodVisitor mv = cv.visitMethod(access, name, desc,
353: signature, exceptions);
354: if (mv != null) {
355: accept(mv);
356: }
357: }
358:
359: /**
360: * Makes the given method visitor visit this method.
361: *
362: * @param mv a method visitor.
363: */
364: public void accept(final MethodVisitor mv) {
365: // visits the method attributes
366: int i, j, n;
367: if (annotationDefault != null) {
368: AnnotationVisitor av = mv.visitAnnotationDefault();
369: AnnotationNode.accept(av, null, annotationDefault);
370: if (av != null) {
371: av.visitEnd();
372: }
373: }
374: n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
375: for (i = 0; i < n; ++i) {
376: AnnotationNode an = (AnnotationNode) visibleAnnotations
377: .get(i);
378: an.accept(mv.visitAnnotation(an.desc, true));
379: }
380: n = invisibleAnnotations == null ? 0 : invisibleAnnotations
381: .size();
382: for (i = 0; i < n; ++i) {
383: AnnotationNode an = (AnnotationNode) invisibleAnnotations
384: .get(i);
385: an.accept(mv.visitAnnotation(an.desc, false));
386: }
387: n = visibleParameterAnnotations == null ? 0
388: : visibleParameterAnnotations.length;
389: for (i = 0; i < n; ++i) {
390: List l = visibleParameterAnnotations[i];
391: if (l == null) {
392: continue;
393: }
394: for (j = 0; j < l.size(); ++j) {
395: AnnotationNode an = (AnnotationNode) l.get(j);
396: an
397: .accept(mv.visitParameterAnnotation(i, an.desc,
398: true));
399: }
400: }
401: n = invisibleParameterAnnotations == null ? 0
402: : invisibleParameterAnnotations.length;
403: for (i = 0; i < n; ++i) {
404: List l = invisibleParameterAnnotations[i];
405: if (l == null) {
406: continue;
407: }
408: for (j = 0; j < l.size(); ++j) {
409: AnnotationNode an = (AnnotationNode) l.get(j);
410: an.accept(mv
411: .visitParameterAnnotation(i, an.desc, false));
412: }
413: }
414: n = attrs == null ? 0 : attrs.size();
415: for (i = 0; i < n; ++i) {
416: mv.visitAttribute((Attribute) attrs.get(i));
417: }
418: // visits the method's code
419: if (instructions.size() > 0) {
420: mv.visitCode();
421: // visits try catch blocks
422: for (i = 0; i < tryCatchBlocks.size(); ++i) {
423: ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv);
424: }
425: // visits instructions
426: instructions.accept(mv);
427: // visits local variables
428: n = localVariables == null ? 0 : localVariables.size();
429: for (i = 0; i < n; ++i) {
430: ((LocalVariableNode) localVariables.get(i)).accept(mv);
431: }
432: // visits maxs
433: mv.visitMaxs(maxStack, maxLocals);
434: }
435: mv.visitEnd();
436: }
437: }
|