KickJava   Java API By Example, From Geeks To Geeks.

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


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: InheritanceInterfacesHelper.java 268 2006-03-24 15:09:48Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.deployment.annotations.helper.bean;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 import org.objectweb.easybeans.deployment.annotations.exceptions.ResolverException;
32 import org.objectweb.easybeans.deployment.annotations.impl.JLocal;
33 import org.objectweb.easybeans.deployment.annotations.impl.JRemote;
34 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
35 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
36
37 /**
38  * Analyze classes and if there are super classes, set all super interfaces to
39  * the current class.
40  * @author Florent Benoit
41  */

42 public final class InheritanceInterfacesHelper {
43
44     /**
45      * Defines java.lang.Object class.
46      */

47     public static final String JavaDoc JAVA_LANG_OBJECT = "java/lang/Object";
48
49     /**
50      * Helper class, no public constructor.
51      */

52     private InheritanceInterfacesHelper() {
53
54     }
55
56     /**
57      * Found all method meta data of the super class and adds them to the bean's class.
58      * Delegates to loop method.
59      * @param classAnnotationMetadata bean' class to analyze.
60      * @throws ResolverException if the super class in not in the given ejb-jar
61      */

62     public static void resolve(final ClassAnnotationMetadata classAnnotationMetadata) throws ResolverException {
63         loop(classAnnotationMetadata, classAnnotationMetadata);
64
65     }
66
67     /**
68      * Found all method meta data of the super class and adds them to the bean's class.
69      * @param beanClassAnnotationMetadata class where to report interfaces
70      * @param visitingClassAnnotationMetadata class to analyze
71      * @throws ResolverException if the super class in not in the given ejb-jar
72      */

73     public static void loop(final ClassAnnotationMetadata beanClassAnnotationMetadata,
74             final ClassAnnotationMetadata visitingClassAnnotationMetadata) throws ResolverException {
75           String JavaDoc superClass = visitingClassAnnotationMetadata.getSuperName();
76         if (superClass != null) {
77             // try to see if it was analyzed (and not java.lang.Object)
78
if (!superClass.equals(JAVA_LANG_OBJECT)) {
79                 EjbJarAnnotationMetadata ejbJarAnnotationMetadata = beanClassAnnotationMetadata
80                         .getEjbJarAnnotationMetadata();
81                 ClassAnnotationMetadata superMetadata = ejbJarAnnotationMetadata.getClassAnnotationMetadata(superClass);
82
83                 if (superMetadata == null) {
84                     throw new IllegalStateException JavaDoc("No super class named '" + superClass
85                             + "' was analyzed. But it is referenced from '" + visitingClassAnnotationMetadata.getClassName()
86                             + "'.");
87                 }
88
89                 // Add in a new list the existing interfaces
90
List JavaDoc<String JavaDoc> newInterfacesLst = new ArrayList JavaDoc<String JavaDoc>();
91                 String JavaDoc[] currentInterfaces = beanClassAnnotationMetadata.getInterfaces();
92                 if (currentInterfaces != null) {
93                     for (String JavaDoc itf : currentInterfaces) {
94                         newInterfacesLst.add(itf);
95                     }
96                 }
97
98                 // Add the interfaces of the super class only if there aren't
99
// yet present
100
String JavaDoc[] superInterfaces = superMetadata.getInterfaces();
101                 if (superInterfaces != null) {
102                     for (String JavaDoc itf : superInterfaces) {
103                         if (!newInterfacesLst.contains(itf)) {
104                             newInterfacesLst.add(itf);
105                         }
106                     }
107                 }
108
109                 // Set the updated list.
110
beanClassAnnotationMetadata.setInterfaces(newInterfacesLst.toArray(new String JavaDoc[newInterfacesLst.size()]));
111
112                 // The local and remote interfaces need to be reported from the superclass to the current class.
113
// Start with the local interfaces.
114
JLocal currentLocalInterfaces = beanClassAnnotationMetadata.getLocalInterfaces();
115                 JLocal superLocalInterfaces = superMetadata.getLocalInterfaces();
116                 if (superLocalInterfaces != null) {
117                     if (currentLocalInterfaces == null) {
118                         currentLocalInterfaces = new JLocal();
119                         beanClassAnnotationMetadata.setLocalInterfaces(currentLocalInterfaces);
120                     }
121                     for (String JavaDoc itf : superLocalInterfaces.getInterfaces()) {
122                         if (!currentLocalInterfaces.getInterfaces().contains(itf)) {
123                             currentLocalInterfaces.addInterface(itf);
124                         }
125                     }
126                 }
127
128                 // And then, with the remote interfaces
129
JRemote currentRemoteInterfaces = beanClassAnnotationMetadata.getRemoteInterfaces();
130                 JRemote superRemoteInterfaces = superMetadata.getRemoteInterfaces();
131                 if (superRemoteInterfaces != null) {
132                     if (currentRemoteInterfaces == null) {
133                         currentRemoteInterfaces = new JRemote();
134                         beanClassAnnotationMetadata.setRemoteInterfaces(currentRemoteInterfaces);
135                     }
136                     for (String JavaDoc itf : superRemoteInterfaces.getInterfaces()) {
137                         if (!currentRemoteInterfaces.getInterfaces().contains(itf)) {
138                             currentRemoteInterfaces.addInterface(itf);
139                         }
140                     }
141                 }
142
143                 // Loop again until java.lang.Object super class is found
144
if (!superMetadata.getClassName().equals(JAVA_LANG_OBJECT)) {
145                     loop(beanClassAnnotationMetadata, superMetadata);
146                 }
147             }
148         }
149     }
150 }
151
Popular Tags