01: /*
02: * CoadunationLib: The coaduntion implementation library.
03: * Copyright (C) 2006 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
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 ref, Class 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 ref, String parentName) {
62: if (ref == null) {
63: return false;
64: } else if (ref.getName().equals(parentName)) {
65: return true;
66: } else if (ref.getName().equals(
67: java.lang.Object.class.getName())) {
68: return false;
69: }
70: Class[] interfaces = ref.getInterfaces();
71: for (int index = 0; index < interfaces.length; index++) {
72: if (testForParent(interfaces[index], parentName)) {
73: return true;
74: }
75: }
76: return testForParent(ref.getSuperclass(), parentName);
77: }
78: }
|