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