KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > reflection > ClassUtil


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.reflection;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  *
28  *
29  * Copyright 2003 Sapient
30  * @since carbon 2.0
31  * @author Greg Hinkle, March 2003
32  * @version $Revision: 1.5 $($Author: dvoet $ / $Date: 2003/05/05 21:21:23 $)
33  */

34 public class ClassUtil {
35
36     /**
37      * Retrieves all interfaces implemented by a specified interface
38      * including all recursively extended interfaces and the classes supplied
39      * int the parameter.
40      * @param childInterfaces a set of interfaces
41      * @return Class[] an array of interfaces that includes those specifed
42      * in childInterfaces plus all of those interfaces' super interfaces
43      */

44     public static Class JavaDoc[] getSuperInterfaces(Class JavaDoc[] childInterfaces) {
45
46         List JavaDoc allInterfaces = new ArrayList JavaDoc();
47
48         for (int i = 0; i < childInterfaces.length; i++) {
49             allInterfaces.add(childInterfaces[i]);
50             allInterfaces.addAll(
51                 Arrays.asList(
52                     getSuperInterfaces(childInterfaces[i].getInterfaces())));
53         }
54
55         return (Class JavaDoc[]) allInterfaces.toArray(new Class JavaDoc[allInterfaces.size()]);
56     }
57
58     /**
59      * Builds an <b>unordered</b> set of all interface and object classes that
60      * are generalizations of the provided class.
61      * @param classObject the class to find generalization of.
62      * @return a Set of class objects.
63      */

64     public static Set JavaDoc getGeneralizations(Class JavaDoc classObject) {
65         Set JavaDoc generalizations = new HashSet JavaDoc();
66
67         generalizations.add(classObject);
68
69         Class JavaDoc superClass = classObject.getSuperclass();
70         if (superClass != null) {
71             generalizations.addAll(getGeneralizations(superClass));
72         }
73
74         Class JavaDoc[] superInterfaces = classObject.getInterfaces();
75         for (int i = 0; i < superInterfaces.length; i++) {
76             Class JavaDoc superInterface = superInterfaces[i];
77             generalizations.addAll(getGeneralizations(superInterface));
78         }
79
80         return generalizations;
81     }
82
83 }
84
Popular Tags