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: InheritanceInterfacesHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.deployment.annotations.helper.bean;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import org.ow2.easybeans.deployment.annotations.exceptions.ResolverException;
030: import org.ow2.easybeans.deployment.annotations.impl.JLocal;
031: import org.ow2.easybeans.deployment.annotations.impl.JRemote;
032: import org.ow2.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
033: import org.ow2.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
034:
035: /**
036: * Analyze classes and if there are super classes, set all super interfaces to
037: * the current class.
038: * @author Florent Benoit
039: */
040: public final class InheritanceInterfacesHelper {
041:
042: /**
043: * Defines java.lang.Object class.
044: */
045: public static final String JAVA_LANG_OBJECT = "java/lang/Object";
046:
047: /**
048: * Helper class, no public constructor.
049: */
050: private InheritanceInterfacesHelper() {
051:
052: }
053:
054: /**
055: * Found all method meta data of the super class and adds them to the bean's class.
056: * Delegates to loop method.
057: * @param classAnnotationMetadata bean' class to analyze.
058: * @throws ResolverException if the super class in not in the given ejb-jar
059: */
060: public static void resolve(
061: final ClassAnnotationMetadata classAnnotationMetadata)
062: throws ResolverException {
063: loop(classAnnotationMetadata, classAnnotationMetadata);
064:
065: }
066:
067: /**
068: * Found all method meta data of the super class and adds them to the bean's class.
069: * @param beanClassAnnotationMetadata class where to report interfaces
070: * @param visitingClassAnnotationMetadata class to analyze
071: * @throws ResolverException if the super class in not in the given ejb-jar
072: */
073: public static void loop(
074: final ClassAnnotationMetadata beanClassAnnotationMetadata,
075: final ClassAnnotationMetadata visitingClassAnnotationMetadata)
076: throws ResolverException {
077: String super Class = visitingClassAnnotationMetadata
078: .getSuperName();
079: if (super Class != null) {
080: // try to see if it was analyzed (and not java.lang.Object)
081: if (!super Class.equals(JAVA_LANG_OBJECT)) {
082: EjbJarAnnotationMetadata ejbJarAnnotationMetadata = beanClassAnnotationMetadata
083: .getEjbJarAnnotationMetadata();
084: ClassAnnotationMetadata super Metadata = ejbJarAnnotationMetadata
085: .getClassAnnotationMetadata(super Class);
086:
087: if (super Metadata == null) {
088: throw new IllegalStateException(
089: "No super class named '"
090: + super Class
091: + "' was analyzed. But it is referenced from '"
092: + visitingClassAnnotationMetadata
093: .getClassName() + "'.");
094: }
095:
096: // Add in a new list the existing interfaces
097: List<String> newInterfacesLst = new ArrayList<String>();
098: String[] currentInterfaces = beanClassAnnotationMetadata
099: .getInterfaces();
100: if (currentInterfaces != null) {
101: for (String itf : currentInterfaces) {
102: newInterfacesLst.add(itf);
103: }
104: }
105:
106: // Add the interfaces of the super class only if there aren't
107: // yet present
108: String[] super Interfaces = super Metadata
109: .getInterfaces();
110: if (super Interfaces != null) {
111: for (String itf : super Interfaces) {
112: if (!newInterfacesLst.contains(itf)) {
113: newInterfacesLst.add(itf);
114: }
115: }
116: }
117:
118: // Set the updated list.
119: beanClassAnnotationMetadata
120: .setInterfaces(newInterfacesLst
121: .toArray(new String[newInterfacesLst
122: .size()]));
123:
124: // The local and remote interfaces need to be reported from the superclass to the current class.
125: // Start with the local interfaces.
126: JLocal currentLocalInterfaces = beanClassAnnotationMetadata
127: .getLocalInterfaces();
128: JLocal super LocalInterfaces = super Metadata
129: .getLocalInterfaces();
130: if (super LocalInterfaces != null) {
131: if (currentLocalInterfaces == null) {
132: currentLocalInterfaces = new JLocal();
133: beanClassAnnotationMetadata
134: .setLocalInterfaces(currentLocalInterfaces);
135: }
136: for (String itf : super LocalInterfaces
137: .getInterfaces()) {
138: if (!currentLocalInterfaces.getInterfaces()
139: .contains(itf)) {
140: currentLocalInterfaces.addInterface(itf);
141: }
142: }
143: }
144:
145: // And then, with the remote interfaces
146: JRemote currentRemoteInterfaces = beanClassAnnotationMetadata
147: .getRemoteInterfaces();
148: JRemote super RemoteInterfaces = super Metadata
149: .getRemoteInterfaces();
150: if (super RemoteInterfaces != null) {
151: if (currentRemoteInterfaces == null) {
152: currentRemoteInterfaces = new JRemote();
153: beanClassAnnotationMetadata
154: .setRemoteInterfaces(currentRemoteInterfaces);
155: }
156: for (String itf : super RemoteInterfaces
157: .getInterfaces()) {
158: if (!currentRemoteInterfaces.getInterfaces()
159: .contains(itf)) {
160: currentRemoteInterfaces.addInterface(itf);
161: }
162: }
163: }
164:
165: // Loop again until java.lang.Object super class is found
166: if (!superMetadata.getClassName().equals(
167: JAVA_LANG_OBJECT)) {
168: loop(beanClassAnnotationMetadata, superMetadata);
169: }
170: }
171: }
172: }
173: }
|