• 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.tree;
31 
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.List;
35 
36 import org.objectweb.asm.Attribute;
37 import org.objectweb.asm.ClassVisitor;
38 import org.objectweb.asm.TypeAnnotationVisitor;
39 import org.objectweb.asm.FieldVisitor;
40 import org.objectweb.asm.MethodVisitor;
41 
42 /**
43  * A node that represents a class.
44  *
45  * @author Eric Bruneton
46  */
47 public class ClassNode extends MemberNode implements ClassVisitor {
48 
visitTypeAnnotation( String desc, boolean visible, boolean inCode)49     public TypeAnnotationVisitor visitTypeAnnotation(
50         String desc, boolean visible, boolean inCode) {
51         throw new RuntimeException("Jaime did not implement yet");
52     }
53 
54     /**
55      * The class version.
56      */
57     public int version;
58 
59     /**
60      * The class's access flags (see {@link org.objectweb.asm.Opcodes}). This
61      * field also indicates if the class is deprecated.
62      */
63     public int access;
64 
65     /**
66      * The internal name of the class (see
67      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}).
68      */
69     public String name;
70 
71     /**
72      * The signature of the class. Mayt be <tt>null</tt>.
73      */
74     public String signature;
75 
76     /**
77      * The internal of name of the super class (see
78      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}). For
79      * interfaces, the super class is {@link Object}. May be <tt>null</tt>,
80      * but only for the {@link Object} class.
81      */
82     public String superName;
83 
84     /**
85      * The internal names of the class's interfaces (see
86      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}). This
87      * list is a list of {@link String} objects.
88      */
89     public List interfaces;
90 
91     /**
92      * The name of the source file from which this class was compiled. May be
93      * <tt>null</tt>.
94      */
95     public String sourceFile;
96 
97     /**
98      * Debug information to compute the correspondance between source and
99      * compiled elements of the class. May be <tt>null</tt>.
100      */
101     public String sourceDebug;
102 
103     /**
104      * The internal name of the enclosing class of the class. May be
105      * <tt>null</tt>.
106      */
107     public String outerClass;
108 
109     /**
110      * The name of the method that contains the class, or <tt>null</tt> if the
111      * class is not enclosed in a method.
112      */
113     public String outerMethod;
114 
115     /**
116      * The descriptor of the method that contains the class, or <tt>null</tt>
117      * if the class is not enclosed in a method.
118      */
119     public String outerMethodDesc;
120 
121     /**
122      * Informations about the inner classes of this class. This list is a list
123      * of {@link InnerClassNode} objects.
124      *
125      * @associates org.objectweb.asm.tree.InnerClassNode
126      */
127     public List innerClasses;
128 
129     /**
130      * The fields of this class. This list is a list of {@link FieldNode}
131      * objects.
132      *
133      * @associates org.objectweb.asm.tree.FieldNode
134      */
135     public List fields;
136 
137     /**
138      * The methods of this class. This list is a list of {@link MethodNode}
139      * objects.
140      *
141      * @associates org.objectweb.asm.tree.MethodNode
142      */
143     public List methods;
144 
145     /**
146      * Constructs a new {@link ClassNode}.
147      */
ClassNode()148     public ClassNode() {
149         this.interfaces = new ArrayList();
150         this.innerClasses = new ArrayList();
151         this.fields = new ArrayList();
152         this.methods = new ArrayList();
153     }
154 
155     // ------------------------------------------------------------------------
156     // Implementation of the ClassVisitor interface
157     // ------------------------------------------------------------------------
158 
visit( final int version, final int access, final String name, final String signature, final String superName, final String[] interfaces)159     public void visit(
160         final int version,
161         final int access,
162         final String name,
163         final String signature,
164         final String superName,
165         final String[] interfaces)
166     {
167         this.version = version;
168         this.access = access;
169         this.name = name;
170         this.signature = signature;
171         this.superName = superName;
172         if (interfaces != null) {
173             this.interfaces.addAll(Arrays.asList(interfaces));
174         }
175     }
176 
visitSource(final String file, final String debug)177     public void visitSource(final String file, final String debug) {
178         sourceFile = file;
179         sourceDebug = debug;
180     }
181 
visitOuterClass( final String owner, final String name, final String desc)182     public void visitOuterClass(
183         final String owner,
184         final String name,
185         final String desc)
186     {
187         outerClass = owner;
188         outerMethod = name;
189         outerMethodDesc = desc;
190     }
191 
visitInnerClass( final String name, final String outerName, final String innerName, final int access)192     public void visitInnerClass(
193         final String name,
194         final String outerName,
195         final String innerName,
196         final int access)
197     {
198         InnerClassNode icn = new InnerClassNode(name,
199                 outerName,
200                 innerName,
201                 access);
202         innerClasses.add(icn);
203     }
204 
visitField( final int access, final String name, final String desc, final String signature, final Object value)205     public FieldVisitor visitField(
206         final int access,
207         final String name,
208         final String desc,
209         final String signature,
210         final Object value)
211     {
212         FieldNode fn = new FieldNode(access, name, desc, signature, value);
213         fields.add(fn);
214         return fn;
215     }
216 
visitMethod( final int access, final String name, final String desc, final String signature, final String[] exceptions)217     public MethodVisitor visitMethod(
218         final int access,
219         final String name,
220         final String desc,
221         final String signature,
222         final String[] exceptions)
223     {
224         MethodNode mn = new MethodNode(access,
225                 name,
226                 desc,
227                 signature,
228                 exceptions);
229         methods.add(mn);
230         return mn;
231     }
232 
visitEnd()233     public void visitEnd() {
234     }
235 
236     // ------------------------------------------------------------------------
237     // Accept method
238     // ------------------------------------------------------------------------
239 
240     /**
241      * Makes the given class visitor visit this class.
242      *
243      * @param cv a class visitor.
244      */
accept(final ClassVisitor cv)245     public void accept(final ClassVisitor cv) {
246         // visits header
247         String[] interfaces = new String[this.interfaces.size()];
248         this.interfaces.toArray(interfaces);
249         cv.visit(version, access, name, signature, superName, interfaces);
250         // visits source
251         if (sourceFile != null || sourceDebug != null) {
252             cv.visitSource(sourceFile, sourceDebug);
253         }
254         // visits outer class
255         if (outerClass != null) {
256             cv.visitOuterClass(outerClass, outerMethod, outerMethodDesc);
257         }
258         // visits attributes
259         int i, n;
260         n = visibleAnnotations == null ? 0 : visibleAnnotations.size();
261         for (i = 0; i < n; ++i) {
262             AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i);
263             an.accept(cv.visitAnnotation(an.desc, true));
264         }
265         n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size();
266         for (i = 0; i < n; ++i) {
267             AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i);
268             an.accept(cv.visitAnnotation(an.desc, false));
269         }
270         n = attrs == null ? 0 : attrs.size();
271         for (i = 0; i < n; ++i) {
272             cv.visitAttribute((Attribute) attrs.get(i));
273         }
274         // visits inner classes
275         for (i = 0; i < innerClasses.size(); ++i) {
276             ((InnerClassNode) innerClasses.get(i)).accept(cv);
277         }
278         // visits fields
279         for (i = 0; i < fields.size(); ++i) {
280             ((FieldNode) fields.get(i)).accept(cv);
281         }
282         // visits methods
283         for (i = 0; i < methods.size(); ++i) {
284             ((MethodNode) methods.get(i)).accept(cv);
285         }
286         // visits end
287         cv.visitEnd();
288     }
289 }
290