KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > bytecode > JClass


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.bytecode;
31
32 /**
33  * Represents an introspected java class.
34  */

35 abstract public class JClass extends JAnnotationObject implements JType {
36   public static final JClass VOID = new JClassWrapper(void.class);
37   public static final JClass BOOLEAN = new JClassWrapper(boolean.class);
38   public static final JClass BYTE = new JClassWrapper(byte.class);
39   public static final JClass SHORT = new JClassWrapper(short.class);
40   public static final JClass INT = new JClassWrapper(int.class);
41   public static final JClass LONG = new JClassWrapper(long.class);
42   public static final JClass FLOAT = new JClassWrapper(float.class);
43   public static final JClass DOUBLE = new JClassWrapper(double.class);
44   public static final JClass CHAR = new JClassWrapper(char.class);
45   public static final JClass STRING = new JClassWrapper(String JavaDoc.class);
46   public static final JClass OBJECT = new JClassWrapper(Object JavaDoc.class);
47   
48   /**
49    * Returns the class name.
50    */

51   abstract public String JavaDoc getName();
52
53   /**
54    * Returns the Java class.
55    */

56   public Class JavaDoc getJavaClass()
57   {
58     try {
59       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
60       
61       return Class.forName(getName(), false, loader);
62     } catch (Exception JavaDoc e) {
63       throw new RuntimeException JavaDoc(e);
64     }
65   }
66
67   /**
68    * Returns the parameter types.
69    */

70   public JType []getActualTypeArguments()
71   {
72     return new JType[0];
73   }
74
75   /**
76    * Returns the raw type.
77    */

78   public JClass getRawType()
79   {
80     return this;
81   }
82   
83   /**
84    * Returns true for a primitive class.
85    */

86   abstract public boolean isPrimitive();
87   
88   /**
89    * Returns true for a public class.
90    */

91   abstract public boolean isPublic();
92   
93   /**
94    * Returns true for an abstract class
95    */

96   abstract public boolean isAbstract();
97   
98   /**
99    * Returns true for a final class
100    */

101   abstract public boolean isFinal();
102   
103   /**
104    * Returns true for an interface
105    */

106   abstract public boolean isInterface();
107
108   /**
109    * Returns the superclass.
110    */

111   abstract public JClass getSuperClass();
112
113   /**
114    * Returns the interfaces.
115    */

116   abstract public JClass []getInterfaces();
117
118   /**
119    * Returns true for an array class.
120    */

121   abstract public boolean isArray();
122
123   /**
124    * Returns the component for a class.
125    */

126   public JClass getComponentType()
127   {
128     return null;
129   }
130
131   /**
132    * Returns true if the jClass is assignable to the class.
133    */

134   abstract public boolean isAssignableTo(Class JavaDoc cl);
135
136   /**
137    * Returns true if the jClass is assignable to the class.
138    */

139   abstract public boolean isAssignableFrom(Class JavaDoc cl);
140
141   /**
142    * Returns true if the jClass is assignable to the class.
143    */

144   abstract public boolean isAssignableFrom(JClass cl);
145
146   /**
147    * Returns the declared methods
148    */

149   abstract public JMethod []getDeclaredMethods();
150
151   /**
152    * Returns the public methods
153    */

154   abstract public JMethod []getMethods();
155
156   /**
157    * Returns the constructors.
158    */

159   abstract public JMethod []getConstructors();
160
161   /**
162    * Returns a matching constructor.
163    */

164   public JMethod getConstructor(JClass []param)
165   {
166     JMethod []ctors = getConstructors();
167
168     loop:
169     for (int i = 0; i < ctors.length; i++) {
170       JClass []args = ctors[i].getParameterTypes();
171
172       if (args.length != param.length)
173     continue loop;
174
175       for (int j = 0; j < args.length; j++)
176     if (! args[i].equals(param[j]))
177       continue loop;
178
179       return ctors[i];
180     }
181
182     return null;
183   }
184
185   /**
186    * Returns the matching method.
187    */

188   abstract public JMethod getMethod(String JavaDoc name, JClass []param);
189
190   /**
191    * Returns the declared fields
192    */

193   abstract public JField []getDeclaredFields();
194
195   /**
196    * Returns the fields
197    */

198   abstract public JField []getFields();
199
200   /**
201    * Returns the printable name.
202    */

203   public String JavaDoc getPrintName()
204   {
205     if (isArray())
206       return getComponentType().getPrintName() + "[]";
207     else
208       return getName().replace('$', '.');
209   }
210
211   /**
212    * Returns a printable version of a class.
213    */

214   public String JavaDoc getShortName()
215   {
216     if (isArray())
217       return getComponentType().getShortName() + "[]";
218     else {
219       String JavaDoc name = getName().replace('$', '.');
220       
221       int p = name.lastIndexOf('.');
222
223       if (p >= 0)
224     return name.substring(p + 1);
225       else
226     return name;
227     }
228   }
229   
230   /**
231    * Returns the hash code
232    */

233   public int hashCode()
234   {
235     return getName().hashCode();
236   }
237   
238   /**
239    * Returns true if equals.
240    */

241   public boolean equals(Object JavaDoc o)
242   {
243     if (o == this)
244       return true;
245     else if (o == null || getClass() != o.getClass())
246       return false;
247
248     JClass jClass = (JClass) o;
249
250     // note that the equality test doesn't include the class loader
251
return getName().equals(jClass.getName());
252   }
253
254   public String JavaDoc toString()
255   {
256     return "JClass[" + getName() + "]";
257   }
258 }
259
Popular Tags