KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > annotations > helper > bean > InheritanceMethodResolver


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: InheritanceMethodResolver.java 486 2006-05-22 15:46:08Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.annotations.helper.bean;
27
28 import org.objectweb.asm.Type;
29
30 import org.objectweb.easybeans.deployment.annotations.JMethod;
31 import org.objectweb.easybeans.deployment.annotations.exceptions.ResolverException;
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 adds method meta data to bean class from the super class.<br>
38  * TODO: Try to analyze super class from a super classloader if not found in the
39  * current ejb-jar
40  * @see <a HREF="http://www.jcp.org/en/jsr/detail?id=220">EJB 3.0 Spec ?4.6.2</a><br><p>
41  * A super class can't be a bean class (stateless, stateful, etc) so the
42  * method metadata don't need to be cloned</p>
43  * @author Florent Benoit
44  */

45 public final class InheritanceMethodResolver {
46
47     /**
48      * java.lang.object internal name.
49      */

50     private static final String JavaDoc JAVA_LANG_OBJECT = Type.getInternalName(Object JavaDoc.class);
51
52     /**
53      * Helper class, no public constructor.
54      */

55     private InheritanceMethodResolver() {
56
57     }
58
59     /**
60      * Found all method meta data of the super class and adds them to the class
61      * being analyzed.
62      * @param classAnnotationMetadata class to analyze
63      * @throws ResolverException if the super class in not in the given ejb-jar
64      */

65     public static void resolve(final ClassAnnotationMetadata classAnnotationMetadata) throws ResolverException {
66         addMethodMetadata(classAnnotationMetadata, classAnnotationMetadata);
67     }
68
69     /**
70      * Adds method meta data on the first class by iterating on the second given
71      * class.
72      * @param beanclassAnnotationMetadata class where to add method metadata
73      * @param visitingClassAnnotationMetadata takes method metadata from super
74      * class of the given class
75      * @throws ResolverException if a super class metadata is not found from
76      * ejb-jar
77      */

78     private static void addMethodMetadata(final ClassAnnotationMetadata beanclassAnnotationMetadata,
79             final ClassAnnotationMetadata visitingClassAnnotationMetadata) throws ResolverException {
80
81         // Analyze super classes of the given class
82
String JavaDoc superClass = visitingClassAnnotationMetadata.getSuperName();
83
84         if (superClass != null) {
85
86             // If super class is java.lang.Object, break the loop
87
if (superClass.equals(JAVA_LANG_OBJECT)) {
88                 return;
89             }
90
91             // Get parent meta-data
92
EjbJarAnnotationMetadata ejbJarAnnotationMetadata = beanclassAnnotationMetadata.getEjbJarAnnotationMetadata();
93
94             // Get meta data of the super class
95
ClassAnnotationMetadata superClassMetadata = ejbJarAnnotationMetadata.getClassAnnotationMetadata(superClass);
96
97             if (superClassMetadata == null) {
98                 // TODO : I18n
99
throw new ResolverException("The class " + beanclassAnnotationMetadata + " extends the class " + superClass
100                         + "but this class seems to be outside of the ejb-jar");
101             }
102
103             // Takes method metadata of the super class and adds them to the
104
// bean class
105
// Note : the flag inherited is set to true
106
for (MethodAnnotationMetadata methodAnnotationMetadata : superClassMetadata.getMethodAnnotationMetadataCollection()) {
107                 // check that the method has not be redefined
108
JMethod method = methodAnnotationMetadata.getJMethod();
109
110                 // Add only if it is not present.
111
if (beanclassAnnotationMetadata
112                         .getMethodAnnotationMetadata(method) == null) {
113                     // Add a clone of the method to bean class
114
MethodAnnotationMetadata clonedMethodAnnotationMetadata = (MethodAnnotationMetadata) methodAnnotationMetadata
115                             .clone();
116                     // set new class linked to this method metadata
117
clonedMethodAnnotationMetadata
118                             .setClassAnnotationMetadata(beanclassAnnotationMetadata);
119                     beanclassAnnotationMetadata
120                             .addMethodAnnotationMetadata(clonedMethodAnnotationMetadata);
121
122                     // method is inherited
123
clonedMethodAnnotationMetadata.setInherited(true, superClassMetadata);
124
125                     // lifecycle / aroundInvoke
126
if (clonedMethodAnnotationMetadata.isPostConstruct()) {
127                         beanclassAnnotationMetadata.addPostConstructMethodMetadata(clonedMethodAnnotationMetadata);
128                     }
129                     if (clonedMethodAnnotationMetadata.isPreDestroy()) {
130                         beanclassAnnotationMetadata.addPreDestroyMethodMetadata(clonedMethodAnnotationMetadata);
131                     }
132                     if (clonedMethodAnnotationMetadata.isPostActivate()) {
133                         beanclassAnnotationMetadata.addPostActivateMethodMetadata(clonedMethodAnnotationMetadata);
134                     }
135                     if (clonedMethodAnnotationMetadata.isPrePassivate()) {
136                         beanclassAnnotationMetadata.addPrePassivateMethodMetadata(clonedMethodAnnotationMetadata);
137                     }
138                     if (clonedMethodAnnotationMetadata.isAroundInvoke()) {
139                         beanclassAnnotationMetadata.addAroundInvokeMethodMetadata(clonedMethodAnnotationMetadata);
140                     }
141                 }
142             }
143
144             // Loop again
145
addMethodMetadata(beanclassAnnotationMetadata, superClassMetadata);
146
147         }
148     }
149
150 }
151
Popular Tags