KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > enhancer > client > ClientLifeCycleAdapter


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: ClientLifeCycleAdapter.java 489 2006-05-22 15:48:37Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.enhancer.client;
27
28 import org.objectweb.asm.ClassAdapter;
29 import org.objectweb.asm.ClassVisitor;
30 import org.objectweb.asm.MethodVisitor;
31 import org.objectweb.asm.Opcodes;
32 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
33 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
34 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata;
35
36 /**
37  * This class manages the lifecycle of the client. It injects a method managing
38  * the postConstruct annotation.
39  * @author Florent Benoit
40  */

41 public class ClientLifeCycleAdapter extends ClassAdapter implements Opcodes {
42
43     /**
44      * Metadata available by this adapter for a class.
45      */

46     private ClassAnnotationMetadata classAnnotationMetadata;
47
48     /**
49      * Defines java.lang.Object class.
50      */

51     public static final String JavaDoc JAVA_LANG_OBJECT = "java/lang/Object";
52
53     /**
54      * Constructor.
55      * @param classAnnotationMetadata object containing all attributes of the
56      * class
57      * @param cv the class visitor to which this adapter must delegate calls.
58      */

59     public ClientLifeCycleAdapter(final ClassAnnotationMetadata classAnnotationMetadata, final ClassVisitor cv) {
60         super(cv);
61         this.classAnnotationMetadata = classAnnotationMetadata;
62     }
63
64     /**
65      * Visits the end of the class. This method, which is the last one to be
66      * called, is used to inform the visitor that all the fields and methods of
67      * the class have been visited.
68      */

69     @Override JavaDoc
70     public void visitEnd() {
71         super.visitEnd();
72
73         // Inject call to all postConstructs annotations
74
MethodVisitor mv = cv
75                 .visitMethod(ACC_PUBLIC + ACC_STATIC, "easyBeansLifeCyclePostConstruct", "()V", null, null);
76         mv.visitCode();
77
78         // call super method (if any)
79
String JavaDoc superNameClass = classAnnotationMetadata.getSuperName();
80         if (superNameClass != null && !superNameClass.equals(JAVA_LANG_OBJECT)) {
81             EjbJarAnnotationMetadata jarMetadata = classAnnotationMetadata.getEjbJarAnnotationMetadata();
82             ClassAnnotationMetadata superMetadata = jarMetadata.getClassAnnotationMetadata(superNameClass);
83             if (superMetadata != null) {
84                 mv.visitMethodInsn(INVOKESTATIC, superMetadata.getClassName(), "easyBeansLifeCyclePostConstruct", "()V");
85             }
86         }
87
88         // call each method annotated
89
for (MethodAnnotationMetadata method : classAnnotationMetadata.getPostConstructMethodsMetadata()) {
90             String JavaDoc clName = method.getClassAnnotationMetadata().getClassName();
91             mv.visitMethodInsn(INVOKESTATIC, clName, method.getMethodName(), method.getJMethod().getDescriptor());
92         }
93
94         mv.visitInsn(RETURN);
95         mv.visitMaxs(0, 0);
96         mv.visitEnd();
97     }
98
99 }
100
Popular Tags