KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > annotations > analyzer > ScanClassVisitor


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ScanClassVisitor.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.annotations.analyzer;
27
28 import org.objectweb.asm.ClassVisitor;
29 import org.objectweb.asm.FieldVisitor;
30 import org.objectweb.asm.MethodVisitor;
31 import org.objectweb.easybeans.deployment.annotations.JField;
32 import org.objectweb.easybeans.deployment.annotations.JMethod;
33 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxAnnotationResourcesVisitor;
34 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxAnnotationSecurityDeclareRolesVisitor;
35 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxAnnotationSecurityRunAs;
36 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbApplicationExceptionVisitor;
37 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbEJBsVisitor;
38 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbLocalHomeVisitor;
39 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbLocalVisitor;
40 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbMessageDrivenVisitor;
41 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbRemoteHomeVisitor;
42 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbRemoteVisitor;
43 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbStatefulVisitor;
44 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbStatelessVisitor;
45 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxEjbTransactionManagementVisitor;
46 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxPersistencePersistenceContextsVisitor;
47 import org.objectweb.easybeans.deployment.annotations.analyzer.classes.JavaxPersistencePersistenceUnitsVisitor;
48 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
49 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
50
51 /**
52  * This classes analyses a given class and build/fill meta data information.
53  * @author Florent Benoit
54  */

55 public class ScanClassVisitor extends ScanCommonVisitor<ClassAnnotationMetadata> implements ClassVisitor {
56
57     /**
58      * Class generated by the visitor which correspond to meta data contained in.
59      * the parsed class
60      */

61     private ClassAnnotationMetadata classAnnotationMetadata = null;
62
63     /**
64      * Parent of classannotation meta data that are built by this visitor.
65      */

66     private EjbJarAnnotationMetadata ejbJarAnnotationMetadata = null;
67
68     /**
69      * Constructor.
70      * @param ejbJarAnnotationMetadata the parent object on which add generated
71      * meta-data
72      */

73     public ScanClassVisitor(final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
74         this.ejbJarAnnotationMetadata = ejbJarAnnotationMetadata;
75     }
76
77     /**
78      * Build the meta-data class with the given name (given by asm).
79      * @param className name of the class that is visited
80      * @param superName the internal of name of the super class (see
81      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}). For interfaces,
82      * the super class is {@link Object}. May be <tt>null</tt>, but
83      * only for the {@link Object} class.
84      * @param interfaces the internal names of the class's interfaces (see
85      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}). May be
86      * <tt>null</tt>.
87      */

88     private void init(final String JavaDoc className, final String JavaDoc superName, final String JavaDoc[] interfaces) {
89         classAnnotationMetadata = new ClassAnnotationMetadata(className, ejbJarAnnotationMetadata);
90         classAnnotationMetadata.setSuperName(superName);
91         classAnnotationMetadata.setInterfaces(interfaces);
92         initVisitors();
93     }
94
95     /**
96      * Build visitors used by this one.
97      */

98     protected void initVisitors() {
99         super.initVisitors(classAnnotationMetadata);
100         // add @Local
101
getAnnotationVisitors().put(JavaxEjbLocalVisitor.TYPE, new JavaxEjbLocalVisitor(classAnnotationMetadata));
102
103         // add @Remote
104
getAnnotationVisitors().put(JavaxEjbRemoteVisitor.TYPE, new JavaxEjbRemoteVisitor(classAnnotationMetadata));
105
106         // add @Stateless
107
getAnnotationVisitors().put(JavaxEjbStatelessVisitor.TYPE,
108                 new JavaxEjbStatelessVisitor(classAnnotationMetadata));
109
110         // add @Stateful
111
getAnnotationVisitors().put(JavaxEjbStatefulVisitor.TYPE, new JavaxEjbStatefulVisitor(classAnnotationMetadata));
112
113         // add @MessageDriven
114
getAnnotationVisitors().put(JavaxEjbMessageDrivenVisitor.TYPE, new JavaxEjbMessageDrivenVisitor(
115                 classAnnotationMetadata));
116
117         // add @LocalHome
118
getAnnotationVisitors().put(JavaxEjbLocalHomeVisitor.TYPE,
119                 new JavaxEjbLocalHomeVisitor(classAnnotationMetadata));
120
121         // add @RemoteHome
122
getAnnotationVisitors().put(JavaxEjbRemoteHomeVisitor.TYPE, new JavaxEjbRemoteHomeVisitor(
123                 classAnnotationMetadata));
124
125         // add @TransactionManagement
126
getAnnotationVisitors().put(JavaxEjbTransactionManagementVisitor.TYPE,
127                 new JavaxEjbTransactionManagementVisitor(classAnnotationMetadata));
128
129         // add @TransactionAttribute
130
getAnnotationVisitors().put(JavaxEjbTransactionAttributeVisitor.TYPE,
131                 new JavaxEjbTransactionAttributeVisitor<ClassAnnotationMetadata>(classAnnotationMetadata));
132
133         // add @Interceptors
134
getAnnotationVisitors().put(JavaxInterceptorInterceptorsVisitor.TYPE,
135                 new JavaxInterceptorInterceptorsVisitor<ClassAnnotationMetadata>(classAnnotationMetadata));
136
137         // add @ApplicationException
138
getAnnotationVisitors().put(JavaxEjbApplicationExceptionVisitor.TYPE, new JavaxEjbApplicationExceptionVisitor(
139                 classAnnotationMetadata));
140
141         // add @EJBs
142
getAnnotationVisitors().put(JavaxEjbEJBsVisitor.TYPE,
143                 new JavaxEjbEJBsVisitor(classAnnotationMetadata));
144
145         // add @Resources
146
getAnnotationVisitors().put(JavaxAnnotationResourcesVisitor.TYPE,
147                 new JavaxAnnotationResourcesVisitor(classAnnotationMetadata));
148
149         // add @PersistenceContexts
150
getAnnotationVisitors().put(JavaxPersistencePersistenceContextsVisitor.TYPE,
151                 new JavaxPersistencePersistenceContextsVisitor(classAnnotationMetadata));
152
153         // add @PersistenceUnits
154
getAnnotationVisitors().put(JavaxPersistencePersistenceUnitsVisitor.TYPE,
155                 new JavaxPersistencePersistenceUnitsVisitor(classAnnotationMetadata));
156
157         // add @DeclareRoles
158
getAnnotationVisitors().put(JavaxAnnotationSecurityDeclareRolesVisitor.TYPE,
159                 new JavaxAnnotationSecurityDeclareRolesVisitor(classAnnotationMetadata));
160
161         // add @RolesAllowed
162
getAnnotationVisitors().put(JavaxAnnotationSecurityRolesAllowedVisitor.TYPE,
163                 new JavaxAnnotationSecurityRolesAllowedVisitor<ClassAnnotationMetadata>(classAnnotationMetadata));
164
165         // add @PermitAll
166
getAnnotationVisitors().put(JavaxAnnotationSecurityPermitAllVisitor.TYPE,
167                 new JavaxAnnotationSecurityPermitAllVisitor<ClassAnnotationMetadata>(classAnnotationMetadata));
168
169         // add @RunAs
170
getAnnotationVisitors().put(JavaxAnnotationSecurityRunAs.TYPE,
171                 new JavaxAnnotationSecurityRunAs(classAnnotationMetadata));
172
173     }
174
175     /**
176      * Visits the header of the class.
177      * @param version the class version.
178      * @param access the class's access flags (see {@link org.objectweb.asm.Opcodes}). This
179      * parameter also indicates if the class is deprecated.
180      * @param name the internal name of the class (see
181      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}).
182      * @param signature the signature of this class. May be <tt>null</tt> if
183      * the class is not a generic one, and does not extend or implement
184      * generic classes or interfaces.
185      * @param superName the internal of name of the super class (see
186      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}). For interfaces,
187      * the super class is {@link Object}. May be <tt>null</tt>, but
188      * only for the {@link Object} class.
189      * @param interfaces the internal names of the class's interfaces (see
190      * {@link org.objectweb.asm.Type#getInternalName() getInternalName}). May be
191      * <tt>null</tt>.
192      */

193     @Override JavaDoc
194     public void visit(final int version,
195                       final int access,
196                       final String JavaDoc name,
197                       final String JavaDoc signature,
198                       final String JavaDoc superName,
199                       final String JavaDoc[] interfaces) {
200         init(name, superName, interfaces);
201     }
202
203
204
205     /**
206      * Visits a method of the class. This method <i>must</i> return a new
207      * {@link org.objectweb.asm.MethodVisitor} instance (or <tt>null</tt>) each time it is
208      * called, i.e., it should not return a previously returned visitor.
209      * @param access the method's access flags (see {@link org.objectweb.asm.Opcodes}). This
210      * parameter also indicates if the method is synthetic and/or
211      * deprecated.
212      * @param name the method's name.
213      * @param desc the method's descriptor (see {@link org.objectweb.asm.Type}).
214      * @param signature the method's signature. May be <tt>null</tt> if the
215      * method parameters, return type and exceptions do not use generic
216      * types.
217      * @param exceptions the internal names of the method's exception classes
218      * (see {@link org.objectweb.asm.Type#getInternalName() getInternalName}). May be
219      * <tt>null</tt>.
220      * @return an object to visit the byte code of the method, or <tt>null</tt>
221      * if this class visitor is not interested in visiting the code of
222      * this method.
223      */

224     @Override JavaDoc
225     public MethodVisitor visitMethod(final int access, final String JavaDoc name, final String JavaDoc desc, final String JavaDoc signature,
226             final String JavaDoc[] exceptions) {
227         JMethod jMethod = new JMethod(access, name, desc, signature, exceptions);
228         return new ScanMethodVisitor(jMethod, classAnnotationMetadata);
229     }
230
231     /**
232      * Visits the end of the class. This method, which is the last one to be
233      * called, is used to inform the visitor that all the fields and methods of
234      * the class have been visited.
235      */

236     @Override JavaDoc
237     public void visitEnd() {
238         ejbJarAnnotationMetadata.addClassAnnotationMetadata(classAnnotationMetadata);
239     }
240
241     /**
242      * Visits a field of the class.
243      * @param access the field's access flags (see {@link org.objectweb.asm.Opcodes}). This
244      * parameter also indicates if the field is synthetic and/or
245      * deprecated.
246      * @param name the field's name.
247      * @param desc the field's descriptor (see {@link org.objectweb.asm.Type}).
248      * @param signature the field's signature. May be <tt>null</tt> if the
249      * field's type does not use generic types.
250      * @param value the field's initial value. This parameter, which may be
251      * <tt>null</tt> if the field does not have an initial value, must
252      * be an {@link Integer}, a {@link Float}, a {@link Long}, a
253      * {@link Double} or a {@link String} (for <tt>int</tt>,
254      * <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
255      * respectively). <i>This parameter is only used for static fields</i>.
256      * Its value is ignored for non static fields, which must be
257      * initialized through bytecode instructions in constructors or
258      * methods.
259      * @return a visitor to visit field annotations and attributes, or
260      * <tt>null</tt> if this class visitor is not interested in
261      * visiting these annotations and attributes.
262      */

263     @Override JavaDoc
264     public FieldVisitor visitField(final int access, final String JavaDoc name, final String JavaDoc desc, final String JavaDoc signature,
265             final Object JavaDoc value) {
266         JField jField = new JField(access, name, desc, signature, value);
267         return new ScanFieldVisitor(jField, classAnnotationMetadata);
268     }
269 }
270
Popular Tags