KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > common > ClassUtil


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * ClassUtil.java
20  *
21  * This class supplies some methods for performing tests on classes.
22  */

23
24 package com.rift.coad.lib.common;
25
26 import com.rift.coad.lib.common.*;
27
28 /**
29  * This class supplies some methods for performing tests on classes.
30  *
31  * @author Brett Chaldecott
32  */

33 public class ClassUtil {
34     
35     /**
36      * Creates a new instance of ClassUtil
37      */

38     private ClassUtil() {
39     }
40     
41     /**
42      * This method performs the test on a class for a given parent interface
43      * or object.
44      *
45      * @return TRUE if found, FALSE if not.
46      * @param ref The reference to the class to check.
47      * @param parentName The name of the parent to perform the check for.
48      */

49     public static boolean testForParent(Class JavaDoc ref, Class JavaDoc parentName) {
50         return testForParent(ref, parentName.getName());
51     }
52     
53     /**
54      * This method performs the test on a class for a given parent interface
55      * or object.
56      *
57      * @return TRUE if found, FALSE if not.
58      * @param ref The reference to the class to check.
59      * @param parentName The name of the parent to perform the check for.
60      */

61     public static boolean testForParent(Class JavaDoc ref, String JavaDoc parentName) {
62         if (ref == null) {
63             return false;
64         } else if (ref.getName().equals(parentName)) {
65             return true;
66         } else if (ref.getName().equals(java.lang.Object JavaDoc.class.getName())) {
67             return false;
68         }
69         Class JavaDoc[] interfaces = ref.getInterfaces();
70         for (int index = 0; index < interfaces.length; index++) {
71             if (testForParent(interfaces[index],parentName)) {
72                 return true;
73             }
74         }
75         return testForParent(ref.getSuperclass(),parentName);
76     }
77 }
78
Popular Tags