001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ScanClassVisitor.java 2059 2007-11-22 17:22:33Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.analyzer;
025:
026: import org.ow2.easybeans.asm.ClassVisitor;
027: import org.ow2.easybeans.asm.FieldVisitor;
028: import org.ow2.easybeans.asm.MethodVisitor;
029: import org.ow2.easybeans.deployment.annotations.JField;
030: import org.ow2.easybeans.deployment.annotations.JMethod;
031: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxAnnotationResourcesVisitor;
032: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxAnnotationSecurityDeclareRolesVisitor;
033: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxAnnotationSecurityRunAs;
034: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbApplicationExceptionVisitor;
035: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbEJBsVisitor;
036: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbLocalHomeVisitor;
037: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbLocalVisitor;
038: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbMessageDrivenVisitor;
039: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbRemoteHomeVisitor;
040: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbRemoteVisitor;
041: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbStatefulVisitor;
042: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbStatelessVisitor;
043: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxEjbTransactionManagementVisitor;
044: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxPersistencePersistenceContextsVisitor;
045: import org.ow2.easybeans.deployment.annotations.analyzer.classes.JavaxPersistencePersistenceUnitsVisitor;
046: import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
047: import org.ow2.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
048:
049: /**
050: * This classes analyses a given class and build/fill meta data information.
051: * @author Florent Benoit
052: */
053: public class ScanClassVisitor extends
054: ScanCommonVisitor<ClassAnnotationMetadata> implements
055: ClassVisitor {
056:
057: /**
058: * Class generated by the visitor which correspond to meta data contained in.
059: * the parsed class
060: */
061: private ClassAnnotationMetadata classAnnotationMetadata = null;
062:
063: /**
064: * Parent of classannotation meta data that are built by this visitor.
065: */
066: private EjbJarAnnotationMetadata ejbJarAnnotationMetadata = null;
067:
068: /**
069: * Constructor.
070: * @param ejbJarAnnotationMetadata the parent object on which add generated
071: * meta-data
072: */
073: public ScanClassVisitor(
074: final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
075: this .ejbJarAnnotationMetadata = ejbJarAnnotationMetadata;
076: }
077:
078: /**
079: * Build the meta-data class with the given name (given by asm).
080: * @param className name of the class that is visited
081: * @param superName the internal of name of the super class (see
082: * {@link org.ow2.easybeans.asm.Type#getInternalName() getInternalName}). For interfaces,
083: * the super class is {@link Object}. May be <tt>null</tt>, but
084: * only for the {@link Object} class.
085: * @param interfaces the internal names of the class's interfaces (see
086: * {@link org.ow2.easybeans.asm.Type#getInternalName() getInternalName}). May be
087: * <tt>null</tt>.
088: */
089: private void init(final String className, final String super Name,
090: final String[] interfaces) {
091: classAnnotationMetadata = new ClassAnnotationMetadata(
092: className, ejbJarAnnotationMetadata);
093: classAnnotationMetadata.setSuperName(super Name);
094: classAnnotationMetadata.setInterfaces(interfaces);
095: initVisitors();
096: }
097:
098: /**
099: * Build visitors used by this one.
100: */
101: protected void initVisitors() {
102: super .initVisitors(classAnnotationMetadata);
103: // add @Local
104: getAnnotationVisitors().put(JavaxEjbLocalVisitor.TYPE,
105: new JavaxEjbLocalVisitor(classAnnotationMetadata));
106:
107: // add @Remote
108: getAnnotationVisitors().put(JavaxEjbRemoteVisitor.TYPE,
109: new JavaxEjbRemoteVisitor(classAnnotationMetadata));
110:
111: // add @Stateless
112: getAnnotationVisitors().put(JavaxEjbStatelessVisitor.TYPE,
113: new JavaxEjbStatelessVisitor(classAnnotationMetadata));
114:
115: // add @Stateful
116: getAnnotationVisitors().put(JavaxEjbStatefulVisitor.TYPE,
117: new JavaxEjbStatefulVisitor(classAnnotationMetadata));
118:
119: // add @MessageDriven
120: getAnnotationVisitors().put(
121: JavaxEjbMessageDrivenVisitor.TYPE,
122: new JavaxEjbMessageDrivenVisitor(
123: classAnnotationMetadata));
124:
125: // add @LocalHome
126: getAnnotationVisitors().put(JavaxEjbLocalHomeVisitor.TYPE,
127: new JavaxEjbLocalHomeVisitor(classAnnotationMetadata));
128:
129: // add @RemoteHome
130: getAnnotationVisitors().put(JavaxEjbRemoteHomeVisitor.TYPE,
131: new JavaxEjbRemoteHomeVisitor(classAnnotationMetadata));
132:
133: // add @TransactionManagement
134: getAnnotationVisitors().put(
135: JavaxEjbTransactionManagementVisitor.TYPE,
136: new JavaxEjbTransactionManagementVisitor(
137: classAnnotationMetadata));
138:
139: // add @TransactionAttribute
140: getAnnotationVisitors()
141: .put(
142: JavaxEjbTransactionAttributeVisitor.TYPE,
143: new JavaxEjbTransactionAttributeVisitor<ClassAnnotationMetadata>(
144: classAnnotationMetadata));
145:
146: // add @Interceptors
147: getAnnotationVisitors()
148: .put(
149: JavaxInterceptorInterceptorsVisitor.TYPE,
150: new JavaxInterceptorInterceptorsVisitor<ClassAnnotationMetadata>(
151: classAnnotationMetadata));
152:
153: // add @ApplicationException
154: getAnnotationVisitors().put(
155: JavaxEjbApplicationExceptionVisitor.TYPE,
156: new JavaxEjbApplicationExceptionVisitor(
157: classAnnotationMetadata));
158:
159: // add @EJBs
160: getAnnotationVisitors().put(JavaxEjbEJBsVisitor.TYPE,
161: new JavaxEjbEJBsVisitor(classAnnotationMetadata));
162:
163: // add @Resources
164: getAnnotationVisitors().put(
165: JavaxAnnotationResourcesVisitor.TYPE,
166: new JavaxAnnotationResourcesVisitor(
167: classAnnotationMetadata));
168:
169: // add @PersistenceContexts
170: getAnnotationVisitors().put(
171: JavaxPersistencePersistenceContextsVisitor.TYPE,
172: new JavaxPersistencePersistenceContextsVisitor(
173: classAnnotationMetadata));
174:
175: // add @PersistenceUnits
176: getAnnotationVisitors().put(
177: JavaxPersistencePersistenceUnitsVisitor.TYPE,
178: new JavaxPersistencePersistenceUnitsVisitor(
179: classAnnotationMetadata));
180:
181: // add @DeclareRoles
182: getAnnotationVisitors().put(
183: JavaxAnnotationSecurityDeclareRolesVisitor.TYPE,
184: new JavaxAnnotationSecurityDeclareRolesVisitor(
185: classAnnotationMetadata));
186:
187: // add @RolesAllowed
188: getAnnotationVisitors()
189: .put(
190: JavaxAnnotationSecurityRolesAllowedVisitor.TYPE,
191: new JavaxAnnotationSecurityRolesAllowedVisitor<ClassAnnotationMetadata>(
192: classAnnotationMetadata));
193:
194: // add @PermitAll
195: getAnnotationVisitors()
196: .put(
197: JavaxAnnotationSecurityPermitAllVisitor.TYPE,
198: new JavaxAnnotationSecurityPermitAllVisitor<ClassAnnotationMetadata>(
199: classAnnotationMetadata));
200:
201: // add @RunAs
202: getAnnotationVisitors().put(
203: JavaxAnnotationSecurityRunAs.TYPE,
204: new JavaxAnnotationSecurityRunAs(
205: classAnnotationMetadata));
206:
207: // add @ExcludeDefaultInterceptors
208: getAnnotationVisitors()
209: .put(
210: JavaxInterceptorExcludeDefaultInterceptorsVisitor.TYPE,
211: new JavaxInterceptorExcludeDefaultInterceptorsVisitor<ClassAnnotationMetadata>(
212: classAnnotationMetadata));
213: }
214:
215: /**
216: * Visits the header of the class.
217: * @param version the class version.
218: * @param access the class's access flags (see {@link org.ow2.easybeans.asm.Opcodes}). This
219: * parameter also indicates if the class is deprecated.
220: * @param name the internal name of the class (see
221: * {@link org.ow2.easybeans.asm.Type#getInternalName() getInternalName}).
222: * @param signature the signature of this class. May be <tt>null</tt> if
223: * the class is not a generic one, and does not extend or implement
224: * generic classes or interfaces.
225: * @param superName the internal of name of the super class (see
226: * {@link org.ow2.easybeans.asm.Type#getInternalName() getInternalName}). For interfaces,
227: * the super class is {@link Object}. May be <tt>null</tt>, but
228: * only for the {@link Object} class.
229: * @param interfaces the internal names of the class's interfaces (see
230: * {@link org.ow2.easybeans.asm.Type#getInternalName() getInternalName}). May be
231: * <tt>null</tt>.
232: */
233: @Override
234: public void visit(final int version, final int access,
235: final String name, final String signature,
236: final String super Name, final String[] interfaces) {
237: init(name, super Name, interfaces);
238: }
239:
240: /**
241: * Visits a method of the class. This method <i>must</i> return a new
242: * {@link org.ow2.easybeans.asm.MethodVisitor} instance (or <tt>null</tt>) each time it is
243: * called, i.e., it should not return a previously returned visitor.
244: * @param access the method's access flags (see {@link org.ow2.easybeans.asm.Opcodes}). This
245: * parameter also indicates if the method is synthetic and/or
246: * deprecated.
247: * @param name the method's name.
248: * @param desc the method's descriptor (see {@link org.ow2.easybeans.asm.Type}).
249: * @param signature the method's signature. May be <tt>null</tt> if the
250: * method parameters, return type and exceptions do not use generic
251: * types.
252: * @param exceptions the internal names of the method's exception classes
253: * (see {@link org.ow2.easybeans.asm.Type#getInternalName() getInternalName}). May be
254: * <tt>null</tt>.
255: * @return an object to visit the byte code of the method, or <tt>null</tt>
256: * if this class visitor is not interested in visiting the code of
257: * this method.
258: */
259: @Override
260: public MethodVisitor visitMethod(final int access,
261: final String name, final String desc,
262: final String signature, final String[] exceptions) {
263: JMethod jMethod = new JMethod(access, name, desc, signature,
264: exceptions);
265: return new ScanMethodVisitor(jMethod, classAnnotationMetadata);
266: }
267:
268: /**
269: * Visits the end of the class. This method, which is the last one to be
270: * called, is used to inform the visitor that all the fields and methods of
271: * the class have been visited.
272: */
273: @Override
274: public void visitEnd() {
275: ejbJarAnnotationMetadata
276: .addClassAnnotationMetadata(classAnnotationMetadata);
277: }
278:
279: /**
280: * Visits a field of the class.
281: * @param access the field's access flags (see {@link org.ow2.easybeans.asm.Opcodes}). This
282: * parameter also indicates if the field is synthetic and/or
283: * deprecated.
284: * @param name the field's name.
285: * @param desc the field's descriptor (see {@link org.ow2.easybeans.asm.Type}).
286: * @param signature the field's signature. May be <tt>null</tt> if the
287: * field's type does not use generic types.
288: * @param value the field's initial value. This parameter, which may be
289: * <tt>null</tt> if the field does not have an initial value, must
290: * be an {@link Integer}, a {@link Float}, a {@link Long}, a
291: * {@link Double} or a {@link String} (for <tt>int</tt>,
292: * <tt>float</tt>, <tt>long</tt> or <tt>String</tt> fields
293: * respectively). <i>This parameter is only used for static fields</i>.
294: * Its value is ignored for non static fields, which must be
295: * initialized through bytecode instructions in constructors or
296: * methods.
297: * @return a visitor to visit field annotations and attributes, or
298: * <tt>null</tt> if this class visitor is not interested in
299: * visiting these annotations and attributes.
300: */
301: @Override
302: public FieldVisitor visitField(final int access, final String name,
303: final String desc, final String signature,
304: final Object value) {
305: JField jField = new JField(access, name, desc, signature, value);
306: return new ScanFieldVisitor(jField, classAnnotationMetadata);
307: }
308: }
|