• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.objectweb.asm.optimizer;
31 
32 import org.objectweb.asm.AnnotationVisitor;
33 import org.objectweb.asm.Attribute;
34 import org.objectweb.asm.ClassAdapter;
35 import org.objectweb.asm.ClassVisitor;
36 import org.objectweb.asm.FieldVisitor;
37 import org.objectweb.asm.MethodVisitor;
38 import org.objectweb.asm.Opcodes;
39 
40 /**
41  * A {@link ClassVisitor} that collects the {@link Constant}s of the classes it
42  * visits.
43  *
44  * @author Eric Bruneton
45  */
46 public class ClassConstantsCollector extends ClassAdapter {
47 
48     private ConstantPool cp;
49 
ClassConstantsCollector(final ClassVisitor cv, final ConstantPool cp)50     public ClassConstantsCollector(final ClassVisitor cv, final ConstantPool cp)
51     {
52         super(cv);
53         this.cp = cp;
54     }
55 
visit( final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces)56     public void visit(
57         final int version,
58         final int access,
59         final String name,
60         final String signature,
61         final String superName,
62         final String[] interfaces)
63     {
64         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
65             cp.newUTF8("Deprecated");
66         }
67         if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
68             cp.newUTF8("Synthetic");
69         }
70         cp.newClass(name);
71         if (signature != null) {
72             cp.newUTF8("Signature");
73             cp.newUTF8(signature);
74         }
75         if (superName != null) {
76             cp.newClass(superName);
77         }
78         if (interfaces != null) {
79             for (int i = 0; i < interfaces.length; ++i) {
80                 cp.newClass(interfaces[i]);
81             }
82         }
83         cv.visit(version, access, name, signature, superName, interfaces);
84     }
85 
visitSource(final String source, final String debug)86     public void visitSource(final String source, final String debug) {
87         if (source != null) {
88             cp.newUTF8("SourceFile");
89             cp.newUTF8(source);
90         }
91         if (debug != null) {
92             cp.newUTF8("SourceDebugExtension");
93         }
94         cv.visitSource(source, debug);
95     }
96 
visitOuterClass( final String owner, final String name, final String desc)97     public void visitOuterClass(
98         final String owner,
99         final String name,
100         final String desc)
101     {
102         cp.newUTF8("EnclosingMethod");
103         cp.newClass(owner);
104         if (name != null && desc != null) {
105             cp.newNameType(name, desc);
106         }
107         cv.visitOuterClass(owner, name, desc);
108     }
109 
visitAnnotation( final String desc, final boolean visible)110     public AnnotationVisitor visitAnnotation(
111         final String desc,
112         final boolean visible)
113     {
114         cp.newUTF8(desc);
115         if (visible) {
116             cp.newUTF8("RuntimeVisibleAnnotations");
117         } else {
118             cp.newUTF8("RuntimeInvisibleAnnotations");
119         }
120         return new AnnotationConstantsCollector(cv.visitAnnotation(desc,
121                 visible), cp);
122     }
123 
visitAttribute(final Attribute attr)124     public void visitAttribute(final Attribute attr) {
125         // can do nothing
126         cv.visitAttribute(attr);
127     }
128 
visitInnerClass( final String name, final String outerName, final String innerName, final int access)129     public void visitInnerClass(
130         final String name,
131         final String outerName,
132         final String innerName,
133         final int access)
134     {
135         cp.newUTF8("InnerClasses");
136         if (name != null) {
137             cp.newClass(name);
138         }
139         if (outerName != null) {
140             cp.newClass(outerName);
141         }
142         if (innerName != null) {
143             cp.newClass(innerName);
144         }
145         cv.visitInnerClass(name, outerName, innerName, access);
146     }
147 
visitField( final int access, final String name, final String desc, final String signature, final Object value)148     public FieldVisitor visitField(
149         final int access,
150         final String name,
151         final String desc,
152         final String signature,
153         final Object value)
154     {
155         if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
156             cp.newUTF8("Synthetic");
157         }
158         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
159             cp.newUTF8("Deprecated");
160         }
161         cp.newUTF8(name);
162         cp.newUTF8(desc);
163         if (signature != null) {
164             cp.newUTF8("Signature");
165             cp.newUTF8(signature);
166         }
167         if (value != null) {
168             cp.newConst(value);
169         }
170         return new FieldConstantsCollector(cv.visitField(access,
171                 name,
172                 desc,
173                 signature,
174                 value), cp);
175     }
176 
visitMethod( final int access, final String name, final String desc, final String signature, final String[] exceptions)177     public MethodVisitor visitMethod(
178         final int access,
179         final String name,
180         final String desc,
181         final String signature,
182         final String[] exceptions)
183     {
184         if ((access & Opcodes.ACC_SYNTHETIC) != 0) {
185             cp.newUTF8("Synthetic");
186         }
187         if ((access & Opcodes.ACC_DEPRECATED) != 0) {
188             cp.newUTF8("Deprecated");
189         }
190         cp.newUTF8(name);
191         cp.newUTF8(desc);
192         if (signature != null) {
193             cp.newUTF8("Signature");
194             cp.newUTF8(signature);
195         }
196         if (exceptions != null) {
197             cp.newUTF8("Exceptions");
198             for (int i = 0; i < exceptions.length; ++i) {
199                 cp.newClass(exceptions[i]);
200             }
201         }
202         return new MethodConstantsCollector(cv.visitMethod(access,
203                 name,
204                 desc,
205                 signature,
206                 exceptions), cp);
207     }
208 
visitEnd()209     public void visitEnd() {
210         cv.visitEnd();
211     }
212 }
213