KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > asm > optimizer > MethodConstantsCollector


1 /***
2  * ASM: a very small and fast Java bytecode manipulation framework
3  * Copyright (c) 2000-2005 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 package com.tc.asm.optimizer;
31
32 import com.tc.asm.AnnotationVisitor;
33 import com.tc.asm.Label;
34 import com.tc.asm.MethodAdapter;
35 import com.tc.asm.MethodVisitor;
36 import com.tc.asm.Opcodes;
37
38 /**
39  * An {@link MethodVisitor} that collects the {@link Constant}s of the methods
40  * it visits.
41  *
42  * @author Eric Bruneton
43  */

44 public class MethodConstantsCollector extends MethodAdapter {
45
46     private ConstantPool cp;
47
48     public MethodConstantsCollector(
49         final MethodVisitor mv,
50         final ConstantPool cp)
51     {
52         super(mv);
53         this.cp = cp;
54     }
55
56     public AnnotationVisitor visitAnnotationDefault() {
57         cp.newUTF8("AnnotationDefault");
58         return new AnnotationConstantsCollector(mv.visitAnnotationDefault(), cp);
59     }
60
61     public AnnotationVisitor visitAnnotation(
62         final String JavaDoc desc,
63         final boolean visible)
64     {
65         cp.newUTF8(desc);
66         if (visible) {
67             cp.newUTF8("RuntimeVisibleAnnotations");
68         } else {
69             cp.newUTF8("RuntimeInvisibleAnnotations");
70         }
71         return new AnnotationConstantsCollector(mv.visitAnnotation(desc,
72                 visible), cp);
73     }
74
75     public AnnotationVisitor visitParameterAnnotation(
76         final int parameter,
77         final String JavaDoc desc,
78         final boolean visible)
79     {
80         cp.newUTF8(desc);
81         if (visible) {
82             cp.newUTF8("RuntimeVisibleParameterAnnotations");
83         } else {
84             cp.newUTF8("RuntimeInvisibleParameterAnnotations");
85         }
86         return new AnnotationConstantsCollector(mv.visitParameterAnnotation(parameter,
87                 desc,
88                 visible),
89                 cp);
90     }
91
92     public void visitTypeInsn(final int opcode, final String JavaDoc desc) {
93         cp.newClass(desc);
94         mv.visitTypeInsn(opcode, desc);
95     }
96
97     public void visitFieldInsn(
98         final int opcode,
99         final String JavaDoc owner,
100         final String JavaDoc name,
101         final String JavaDoc desc)
102     {
103         cp.newField(owner, name, desc);
104         mv.visitFieldInsn(opcode, owner, name, desc);
105     }
106
107     public void visitMethodInsn(
108         final int opcode,
109         final String JavaDoc owner,
110         final String JavaDoc name,
111         final String JavaDoc desc)
112     {
113         boolean itf = opcode == Opcodes.INVOKEINTERFACE;
114         cp.newMethod(owner, name, desc, itf);
115         mv.visitMethodInsn(opcode, owner, name, desc);
116     }
117
118     public void visitLdcInsn(final Object JavaDoc cst) {
119         cp.newConst(cst);
120         mv.visitLdcInsn(cst);
121     }
122
123     public void visitMultiANewArrayInsn(final String JavaDoc desc, final int dims) {
124         cp.newClass(desc);
125         mv.visitMultiANewArrayInsn(desc, dims);
126     }
127
128     public void visitTryCatchBlock(
129         final Label start,
130         final Label end,
131         final Label handler,
132         final String JavaDoc type)
133     {
134         if (type != null) {
135             cp.newClass(type);
136         }
137         mv.visitTryCatchBlock(start, end, handler, type);
138     }
139
140     public void visitLocalVariable(
141         final String JavaDoc name,
142         final String JavaDoc desc,
143         final String JavaDoc signature,
144         final Label start,
145         final Label end,
146         final int index)
147     {
148         if (signature != null) {
149             cp.newUTF8("LocalVariableTypeTable");
150             cp.newUTF8(name);
151             cp.newUTF8(signature);
152         }
153         cp.newUTF8("LocalVariableTable");
154         cp.newUTF8(name);
155         cp.newUTF8(desc);
156         mv.visitLocalVariable(name, desc, signature, start, end, index);
157     }
158
159     public void visitLineNumber(final int line, final Label start) {
160         cp.newUTF8("LineNumberTable");
161         mv.visitLineNumber(line, start);
162     }
163
164     public void visitMaxs(final int maxStack, final int maxLocals) {
165         cp.newUTF8("Code");
166         mv.visitMaxs(maxStack, maxLocals);
167     }
168 }
169
Popular Tags