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 org.ejb3unit.asm.optimizer;
030:
031: import org.ejb3unit.asm.AnnotationVisitor;
032: import org.ejb3unit.asm.Label;
033: import org.ejb3unit.asm.MethodAdapter;
034: import org.ejb3unit.asm.MethodVisitor;
035: import org.ejb3unit.asm.Opcodes;
036:
037: /**
038: * An {@link MethodVisitor} that collects the {@link Constant}s of the methods
039: * it visits.
040: *
041: * @author Eric Bruneton
042: */
043: public class MethodConstantsCollector extends MethodAdapter {
044:
045: private ConstantPool cp;
046:
047: public MethodConstantsCollector(final MethodVisitor mv,
048: final ConstantPool cp) {
049: super (mv);
050: this .cp = cp;
051: }
052:
053: public AnnotationVisitor visitAnnotationDefault() {
054: cp.newUTF8("AnnotationDefault");
055: return new AnnotationConstantsCollector(mv
056: .visitAnnotationDefault(), cp);
057: }
058:
059: public AnnotationVisitor visitAnnotation(final String desc,
060: final boolean visible) {
061: cp.newUTF8(desc);
062: if (visible) {
063: cp.newUTF8("RuntimeVisibleAnnotations");
064: } else {
065: cp.newUTF8("RuntimeInvisibleAnnotations");
066: }
067: return new AnnotationConstantsCollector(mv.visitAnnotation(
068: desc, visible), cp);
069: }
070:
071: public AnnotationVisitor visitParameterAnnotation(
072: final int parameter, final String desc,
073: final boolean visible) {
074: cp.newUTF8(desc);
075: if (visible) {
076: cp.newUTF8("RuntimeVisibleParameterAnnotations");
077: } else {
078: cp.newUTF8("RuntimeInvisibleParameterAnnotations");
079: }
080: return new AnnotationConstantsCollector(mv
081: .visitParameterAnnotation(parameter, desc, visible), cp);
082: }
083:
084: public void visitTypeInsn(final int opcode, final String desc) {
085: cp.newClass(desc);
086: mv.visitTypeInsn(opcode, desc);
087: }
088:
089: public void visitFieldInsn(final int opcode, final String owner,
090: final String name, final String desc) {
091: cp.newField(owner, name, desc);
092: mv.visitFieldInsn(opcode, owner, name, desc);
093: }
094:
095: public void visitMethodInsn(final int opcode, final String owner,
096: final String name, final String desc) {
097: boolean itf = opcode == Opcodes.INVOKEINTERFACE;
098: cp.newMethod(owner, name, desc, itf);
099: mv.visitMethodInsn(opcode, owner, name, desc);
100: }
101:
102: public void visitLdcInsn(final Object cst) {
103: cp.newConst(cst);
104: mv.visitLdcInsn(cst);
105: }
106:
107: public void visitMultiANewArrayInsn(final String desc,
108: final int dims) {
109: cp.newClass(desc);
110: mv.visitMultiANewArrayInsn(desc, dims);
111: }
112:
113: public void visitTryCatchBlock(final Label start, final Label end,
114: final Label handler, final String type) {
115: if (type != null) {
116: cp.newClass(type);
117: }
118: mv.visitTryCatchBlock(start, end, handler, type);
119: }
120:
121: public void visitLocalVariable(final String name,
122: final String desc, final String signature,
123: final Label start, final Label end, final int index) {
124: if (signature != null) {
125: cp.newUTF8("LocalVariableTypeTable");
126: cp.newUTF8(name);
127: cp.newUTF8(signature);
128: }
129: cp.newUTF8("LocalVariableTable");
130: cp.newUTF8(name);
131: cp.newUTF8(desc);
132: mv.visitLocalVariable(name, desc, signature, start, end, index);
133: }
134:
135: public void visitLineNumber(final int line, final Label start) {
136: cp.newUTF8("LineNumberTable");
137: mv.visitLineNumber(line, start);
138: }
139:
140: public void visitMaxs(final int maxStack, final int maxLocals) {
141: cp.newUTF8("Code");
142: mv.visitMaxs(maxStack, maxLocals);
143: }
144: }
|