KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > ojb > constraints > InheritanceHelper


1 package xdoclet.modules.ojb.constraints;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import xdoclet.modules.ojb.model.*;
7 import xjavadoc.XClass;
8
9 /* Copyright 2004-2005 The Apache Software Foundation
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */

23
24 /**
25  * Helper class for functionality related to inheritance between classes/interfaces.
26  *
27  * @author <a HREF="mailto:tomdz@users.sourceforge.net">Thomas Dudziak (tomdz@users.sourceforge.net)</a>
28  */

29 public class InheritanceHelper
30 {
31     /**
32      * Retrieves the class object for the class with the given name.
33      *
34      * @param name The class name
35      * @return The class object
36      * @throws ClassNotFoundException If the class is not on the classpath (the exception message contains the class name)
37      */

38     public static Class JavaDoc getClass(String JavaDoc name) throws ClassNotFoundException JavaDoc
39     {
40         try
41         {
42             return Class.forName(name);
43         }
44         catch (ClassNotFoundException JavaDoc ex)
45         {
46             throw new ClassNotFoundException JavaDoc(name);
47         }
48     }
49     /**
50      * Determines whether the given type is the same or a sub type of the other type.
51      *
52      * @param type The type
53      * @param baseType The possible base type
54      * @param checkActualClasses Whether to use the actual classes for the test
55      * @return <code>true</code> If <code>type</code> specifies the same or a sub type of <code>baseType</code>
56      * @throws ClassNotFoundException If the two classes are not on the classpath
57      */

58     public boolean isSameOrSubTypeOf(XClass type, String JavaDoc baseType, boolean checkActualClasses) throws ClassNotFoundException JavaDoc
59     {
60         String JavaDoc qualifiedBaseType = baseType.replace('$', '.');
61
62         if (type.getQualifiedName().equals(qualifiedBaseType))
63         {
64             return true;
65         }
66
67         // first search via XDoclet
68
ArrayList JavaDoc queue = new ArrayList JavaDoc();
69         boolean canSpecify = false;
70         XClass curType;
71
72         queue.add(type);
73         while (!queue.isEmpty())
74         {
75             curType = (XClass)queue.get(0);
76             queue.remove(0);
77             if (qualifiedBaseType.equals(curType.getQualifiedName()))
78             {
79                 return true;
80             }
81             if (curType.getInterfaces() != null)
82             {
83                 for (Iterator JavaDoc it = curType.getInterfaces().iterator(); it.hasNext(); )
84                 {
85                     queue.add(it.next());
86                 }
87             }
88             if (!curType.isInterface())
89             {
90                 if (curType.getSuperclass() != null)
91                 {
92                     queue.add(curType.getSuperclass());
93                 }
94             }
95         }
96
97         // if not found, we try via actual classes
98
return checkActualClasses ? isSameOrSubTypeOf(type.getQualifiedName(), qualifiedBaseType) : false;
99     }
100
101     /**
102      * Determines whether the given type is the same or a sub type of the other type.
103      *
104      * @param type The type
105      * @param baseType The possible base type
106      * @param checkActualClasses Whether to use the actual classes for the test
107      * @return <code>true</code> If <code>type</code> specifies the same or a sub type of <code>baseType</code>
108      * @throws ClassNotFoundException If the two classes are not on the classpath
109      */

110     public boolean isSameOrSubTypeOf(ClassDescriptorDef type, String JavaDoc baseType, boolean checkActualClasses) throws ClassNotFoundException JavaDoc
111     {
112         if (type.getQualifiedName().equals(baseType.replace('$', '.')))
113         {
114             return true;
115         }
116         else if (type.getOriginalClass() != null)
117         {
118             return isSameOrSubTypeOf(type.getOriginalClass(), baseType, checkActualClasses);
119         }
120         else
121         {
122             return checkActualClasses ? isSameOrSubTypeOf(type.getName(), baseType) : false;
123         }
124     }
125
126     /**
127      * Determines whether the given type is the same or a sub type of the other type.
128      *
129      * @param type The type
130      * @param baseType The possible base type
131      * @return <code>true</code> If <code>type</code> specifies the same or a sub type of <code>baseType</code>
132      * @throws ClassNotFoundException If the two classes are not on the classpath
133      */

134     public boolean isSameOrSubTypeOf(String JavaDoc type, String JavaDoc baseType) throws ClassNotFoundException JavaDoc
135     {
136         return type.replace('$', '.').equals(baseType.replace('$', '.')) ? true : isSameOrSubTypeOf(getClass(type), baseType);
137     }
138
139     /**
140      * Determines whether the given type is the same or a sub type of the other type.
141      *
142      * @param type The type
143      * @param baseType The possible base type
144      * @return <code>true</code> If <code>type</code> specifies the same or a sub type of <code>baseType</code>
145      * @throws ClassNotFoundException If the two classes are not on the classpath
146      */

147     public boolean isSameOrSubTypeOf(Class JavaDoc type, String JavaDoc baseType) throws ClassNotFoundException JavaDoc
148     {
149         return type.getName().equals(baseType.replace('$', '.')) ? true : getClass(baseType).isAssignableFrom(type);
150     }
151 }
152
Popular Tags