KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > common > ClassesInheritance


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2002 USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.common;
28
29 /** The java API's imports */
30 import java.util.Vector JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * This class gives the inheritance tree of a given classe ordered by interface first and by level.
36  *
37  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
38  * @version 0.1
39  */

40 public class ClassesInheritance {
41
42     // ==================================================================
43
//
44
// Internal state.
45
//
46
// ==================================================================
47

48     /** Contains the list of classes and interfaces */
49     protected List JavaDoc classes_;
50
51     // ==================================================================
52
//
53
// Constructors.
54
//
55
// ==================================================================
56

57     /**
58      * Default constructor.
59      * @param theClass The given you want to obtain the inherit classes
60      */

61     public ClassesInheritance(Class JavaDoc theClass) {
62         classes_ = new Vector JavaDoc();
63         if (theClass != null)
64             build(theClass);
65     }
66
67     // ==================================================================
68
//
69
// Internal methods.
70
//
71
// ==================================================================
72

73     /**
74      * Indicates if the given class name already exists in the list.
75      * @param className The class to check
76      * @return The classe which is in the list or null
77      */

78     protected Class JavaDoc getClass(String JavaDoc className) {
79         Iterator JavaDoc it = classes_.iterator();
80         while (it.hasNext()) {
81             Class JavaDoc c = (Class JavaDoc) it.next();
82             if (className.equals(c.getName()))
83                 return c;
84         }
85         return null;
86     }
87
88     /**
89      * Adds the given class to the list.
90      * @param c The class to add
91      */

92     protected void buildClass(Class JavaDoc c) {
93         // Adds the class to the given vector
94
if (c != null) {
95             classes_.add(c);
96         }
97     }
98
99     /**
100      * Adds the given interfaces to the list.
101      * If the map previously contained a mapping for this class, the highest level class is kept.
102      * <ul>
103      * <li>1. Adds the interfaces to the list</li>
104      * <li>2. Creates an array containing all the interface extended by the given interfaces</li>
105      * <li>3. Recursive call with the created array</li>
106      * </ul>
107      * @param interfaces List of interfaces
108      */

109     protected void buildInterfaces(Class JavaDoc[] interfaces) {
110         // Adds the interfaces to the given vector
111
for (int i = 0; i < interfaces.length; i++) {
112             Class JavaDoc oldClass = getClass(interfaces[i].getName());
113             if (oldClass != null)
114                 classes_.remove(oldClass);
115             classes_.add(interfaces[i]);
116         }
117         // Creates an array containing all the interface extended by the given interface's array
118
Vector JavaDoc allExtendedInterfaces = new Vector JavaDoc();
119         for (int i = 0; i < interfaces.length; i++) {
120             Class JavaDoc[] extendedInterfaces = interfaces[i].getInterfaces();
121             for (int j = 0; j < extendedInterfaces.length; j++) {
122                 allExtendedInterfaces.add(extendedInterfaces[j]);
123             }
124         }
125         // Recursive call
126
if (allExtendedInterfaces.size() != 0)
127             buildInterfaces(
128                 (Class JavaDoc[]) allExtendedInterfaces.toArray(new Class JavaDoc[0]));
129     }
130
131     /**
132      * Adds the implemented interfaces and the subclasses of the given class. It also adds the given class.
133      * <ul>
134      * <li>1. Call to buildClass with te given class</li>
135      * <li>2. Call to buildInterfaces with the implemented interface of the given class</li>
136      * <li>3. Recursive call with the superclass of the given class</li>
137      * </ul>
138      * @param c The class to analyse
139      */

140     protected void build(Class JavaDoc c) {
141         if (c != null) {
142             buildClass(c);
143             buildInterfaces(c.getInterfaces());
144             build(c.getSuperclass());
145         }
146     }
147
148     // ==================================================================
149
//
150
// Public methods.
151
//
152
// ==================================================================
153

154     /**
155      * Gives the list of inherit classes and interfaces.
156      * @return The list containing the implemented interfaces and the subclasses of the class given to the construvtor
157      */

158     public List JavaDoc getInheritClasses() {
159         return classes_;
160     }
161
162     // ==================================================================
163
//
164
// Test class.
165
//
166
// ==================================================================
167

168     /**
169      * Test the class with a java.util.ArrayList object.
170      */

171     public static void main(String JavaDoc[] args) {
172         ClassesInheritance ci = new ClassesInheritance((new java.util.ArrayList JavaDoc()).getClass());
173         List JavaDoc list = ci.getInheritClasses();
174         Iterator JavaDoc it = list.iterator();
175         while (it.hasNext()) {
176             System.out.println("=> " + it.next());
177         }
178     }
179
180 }
181
Popular Tags