1 |
/* |
/* Class.java -- Reference implementation of access to object metadata |
2 |
* java.lang.Class: part of the Java Class Libraries project. |
Copyright (C) 1998, 2002 Free Software Foundation |
3 |
* Copyright (C) 1998 John Keiser |
|
4 |
* |
This file is part of GNU Classpath. |
5 |
* This library is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU Library General Public |
GNU Classpath is free software; you can redistribute it and/or modify |
7 |
* License as published by the Free Software Foundation; either |
it under the terms of the GNU General Public License as published by |
8 |
* version 2 of the License, or (at your option) any later version. |
the Free Software Foundation; either version 2, or (at your option) |
9 |
* |
any later version. |
10 |
* This library is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
GNU Classpath is distributed in the hope that it will be useful, but |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* Library General Public License for more details. |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
* |
General Public License for more details. |
15 |
* You should have received a copy of the GNU Library General Public |
|
16 |
* License along with this library; if not, write to the |
You should have received a copy of the GNU General Public License |
17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
along with GNU Classpath; see the file COPYING. If not, write to the |
18 |
* Boston, MA 02111-1307, USA. |
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
*/ |
02111-1307 USA. |
20 |
|
|
21 |
package java.lang; |
Linking this library statically or dynamically with other modules is |
22 |
|
making a combined work based on this library. Thus, the terms and |
23 |
import java.lang.reflect.*; |
conditions of the GNU General Public License cover the whole |
24 |
import gnu.java.lang.*; |
combination. |
25 |
|
|
26 |
/** |
As a special exception, the copyright holders of this library give you |
27 |
** A Class represents a Java type. There will never be |
permission to link this library with independent modules to produce an |
28 |
** multiple Class objects with identical names and |
executable, regardless of the license terms of these independent |
29 |
** ClassLoaders.<P> |
modules, and to copy and distribute the resulting executable under |
30 |
** |
terms of your choice, provided that you also meet, for each linked |
31 |
** Arrays with identical type and number of dimensions |
independent module, the terms and conditions of the license of that |
32 |
** share the same class (and null "system" ClassLoader, |
module. An independent module is a module which is not derived from |
33 |
** incidentally). The name of an array class is |
or based on this library. If you modify this library, you may extend |
34 |
** <CODE>[<type name></CODE> ... for example, |
this exception to your version of the library, but you are not |
35 |
** String[]'s class is <CODE>[java.lang.String</CODE>. |
obligated to do so. If you do not wish to do so, delete this |
36 |
** boolean, byte, short, char, int, long, float and double |
exception statement from your version. */ |
37 |
** have the "type name" of Z,B,S,C,I,J,F,D for the |
|
38 |
** purposes of array classes. If it's a multidimensioned |
package java.lang; |
39 |
** array, the same principle applies: |
|
40 |
** <CODE>int[][][]</CODE> == <CODE>[[[I</CODE>.<P> |
import java.io.Serializable; |
41 |
** |
import java.io.InputStream; |
42 |
** As of 1.1, this class represents primitive types as |
import java.lang.reflect.Constructor; |
43 |
** well. You can get to those by looking at |
import java.lang.reflect.Field; |
44 |
** java.lang.Integer.TYPE, java.lang.Boolean.TYPE, etc. |
import java.lang.reflect.InvocationTargetException; |
45 |
** |
import java.lang.reflect.Member; |
46 |
** @author John Keiser |
import java.lang.reflect.Method; |
47 |
** @version 1.1.0, Aug 6 1998 |
import java.net.URL; |
48 |
** @since JDK1.0 |
import java.security.AllPermission; |
49 |
**/ |
import java.security.Permissions; |
50 |
|
import java.security.ProtectionDomain; |
51 |
public class Class { |
import java.util.ArrayList; |
52 |
Class superclass; |
import java.util.Arrays; |
53 |
String name; |
import gnu.java.lang.ClassHelper; |
54 |
Object[] signers; |
|
55 |
|
/* |
56 |
/** Return the human-readable form of this Object. For |
* This class is a reference version, mainly for compiling a class library |
57 |
** class, that means "interface " or "class " plus the |
* jar. It is likely that VM implementers replace this with their own |
58 |
** classname. |
* version that can communicate effectively with the VM. |
59 |
** @return the human-readable form of this Object. |
*/ |
60 |
** @since JDK1.0 |
|
61 |
**/ |
/** |
62 |
public String toString() { |
* A Class represents a Java type. There will never be multiple Class |
63 |
return (isInterface() ? "interface " : "class ") + getName(); |
* objects with identical names and ClassLoaders. Primitive types, array |
64 |
} |
* types, and void also have a Class object. |
65 |
|
* |
66 |
/** Get the name of this class, separated by dots for |
* <p>Arrays with identical type and number of dimensions share the same |
67 |
** package separators. |
* class (and null "system" ClassLoader, incidentally). The name of an |
68 |
** @return the name of this class. |
* array class is <code>[<signature format>;</code> ... for example, |
69 |
** @since JDK1.0 |
* String[]'s class is <code>[Ljava.lang.String;</code>. boolean, byte, |
70 |
**/ |
* short, char, int, long, float and double have the "type name" of |
71 |
public String getName() { |
* Z,B,S,C,I,J,F,D for the purposes of array classes. If it's a |
72 |
if(name != null) |
* multidimensioned array, the same principle applies: |
73 |
return name; |
* <code>int[][][]</code> == <code>[[[I</code>. |
74 |
else |
* |
75 |
return VMClass.getName(this); |
* <p>There is no public constructor - Class objects are obtained only through |
76 |
} |
* the virtual machine, as defined in ClassLoaders. |
77 |
|
* |
78 |
/** Get whether this class is an interface or not. Array |
* @serialData Class objects serialize specially: |
79 |
** types are not interfaces. |
* <code>TC_CLASS ClassDescriptor</code>. For more serialization information, |
80 |
** @return whether this class is an interface or not. |
* see {@link ObjectStreamClass}. |
81 |
** @since JDK1.0 |
* |
82 |
**/ |
* @author John Keiser |
83 |
public boolean isInterface() { |
* @author Eric Blake <ebb9@email.byu.edu> |
84 |
return VMClass.isInterface(this); |
* @since 1.0 |
85 |
} |
* @see ClassLoader |
86 |
|
*/ |
87 |
/** Get the direct superclass of this class. If this is |
public final class Class implements Serializable |
88 |
** an interface, it will get the direct superinterface. |
{ |
89 |
** @return the direct superclass of this class. |
/** |
90 |
** @since JDK1.0 |
* Compatible with JDK 1.0+. |
91 |
**/ |
*/ |
92 |
public Class getSuperclass() { |
private static final long serialVersionUID = 3206093459760846163L; |
93 |
if(superclass != null) |
|
94 |
return superclass; |
/** The class signers. */ |
95 |
else |
private Object[] signers = null; |
96 |
return VMClass.getSuperclass(this); |
/** The class protection domain. */ |
97 |
} |
private ProtectionDomain pd = null; |
98 |
|
|
99 |
/** Get the interfaces this class <EM>directly</EM> |
/** The unknown protection domain. */ |
100 |
** implements, in the order that they were declared. |
private final static ProtectionDomain unknownProtectionDomain; |
101 |
** This method may return an empty array, but will |
static |
102 |
** never return null. |
{ |
103 |
** @return the interfaces this class directly implements. |
Permissions permissions = new Permissions(); |
104 |
** @since JDK1.0 |
permissions.add(new AllPermission()); |
105 |
**/ |
unknownProtectionDomain = new ProtectionDomain(null, permissions); |
106 |
public Class[] getInterfaces() { |
} |
107 |
return VMClass.getInterfaces(this); |
|
108 |
} |
private transient final VMClass vmClass; |
109 |
|
|
110 |
/** Get a new instance of this class by calling the |
/** |
111 |
** no-argument constructor. |
* Class is non-instantiable from Java code; only the VM can create |
112 |
** @return a new instance of this class. |
* instances of this class. |
113 |
** @exception InstantiationException if there is not a |
*/ |
114 |
** no-arg constructor for this class, or if |
private Class() |
115 |
** an exception occurred during instantiation, |
{ |
116 |
** or if the target constructor throws an |
this.vmClass = VMClass.getInstance (); |
117 |
** exception. |
} |
118 |
** @exception IllegalAccessException if you are not |
|
119 |
** allowed to access the no-arg constructor of |
/** |
120 |
** this Class for whatever reason. |
* Return the human-readable form of this Object. For an object, this |
121 |
** @since JDK1.0 |
* is either "interface " or "class " followed by <code>getName()</code>, |
122 |
**/ |
* for primitive types and void it is just <code>getName()</code>. |
123 |
public Object newInstance() throws InstantiationException, IllegalAccessException { |
* |
124 |
try { |
* @return the human-readable form of this Object |
125 |
return getConstructor(new Class[0]).newInstance(new Object[0]); |
*/ |
126 |
} catch(SecurityException e) { |
public String toString() |
127 |
throw new IllegalAccessException("Cannot access no-arg constructor"); |
{ |
128 |
} catch(IllegalArgumentException e) { |
if (isPrimitive()) |
129 |
throw new UnknownError("IllegalArgumentException thrown from Constructor.newInstance(). Something is rotten in Denmark."); |
return getName(); |
130 |
} catch(InvocationTargetException e) { |
return (isInterface() ? "interface " : "class ") + getName(); |
131 |
throw new InstantiationException("Target threw an exception."); |
} |
132 |
} catch(NoSuchMethodException e) { |
|
133 |
throw new InstantiationException("Method not found"); |
/** |
134 |
} |
* Use the classloader of the current class to load, link, and initialize |
135 |
} |
* a class. This is equivalent to your code calling |
136 |
|
* <code>Class.forName(name, true, getClass().getClassLoader())</code>. |
137 |
/** Get the ClassLoader that loaded this class. If it was |
* |
138 |
** loaded by the system classloader, this method will |
* @param name the name of the class to find |
139 |
** return null. |
* @return the Class object representing the class |
140 |
** @return the ClassLoader that loaded this class. |
* @throws ClassNotFoundException if the class was not found by the |
141 |
** @since JDK1.0 |
* classloader |
142 |
**/ |
* @throws LinkageError if linking the class fails |
143 |
public ClassLoader getClassLoader() { |
* @throws ExceptionInInitializerError if the class loads, but an exception |
144 |
return VMClass.getClassLoader(this); |
* occurs during initialization |
145 |
} |
*/ |
146 |
|
public static Class forName(String name) throws ClassNotFoundException |
147 |
/** Use the system classloader to load and link a class. |
{ |
148 |
** @param name the name of the class to find. |
Class result = vmClass.forName (name); |
149 |
** @return the Class object representing the class. |
if (result == null) |
150 |
** @exception ClassNotFoundException if the class was not |
result = Class.forName(name, true, |
151 |
** found by the system classloader. |
VMSecurityManager.getClassContext()[1].getClassLoader()); |
152 |
** @since JDK1.0 |
return result; |
153 |
**/ |
} |
154 |
public static Class forName(String name) throws ClassNotFoundException { |
|
155 |
return VMClass.forName(name); |
/** |
156 |
} |
* Use the specified classloader to load and link a class. If the loader |
157 |
|
* is null, this uses the bootstrap class loader (provide the security |
158 |
/** Discover whether an Object is an instance of this |
* check succeeds). Unfortunately, this method cannot be used to obtain |
159 |
** Class. Think of it as almost like |
* the Class objects for primitive types or for void, you have to use |
160 |
** <CODE>o instanceof (this class)</CODE>. |
* the fields in the appropriate java.lang wrapper classes. |
161 |
** @param o the Object to check |
* |
162 |
** @return whether o is an instance of this class. |
* <p>Calls <code>classloader.loadclass(name, initialize)</code>. |
163 |
** @since JDK1.1 |
* |
164 |
**/ |
* @param name the name of the class to find |
165 |
public boolean isInstance(Object o) { |
* @param initialize whether or not to initialize the class at this time |
166 |
return VMClass.isInstance(this,o); |
* @param classloader the classloader to use to find the class; null means |
167 |
} |
* to use the bootstrap class loader |
168 |
|
* @throws ClassNotFoundException if the class was not found by the |
169 |
/** Discover whether an instance of the Class parameter |
* classloader |
170 |
** would be an instance of this Class as well. Think of |
* @throws LinkageError if linking the class fails |
171 |
** doing <CODE>isInstance(c.newInstance())</CODE> or even |
* @throws ExceptionInInitializerError if the class loads, but an exception |
172 |
** <CODE>c instanceof (this class)</CODE>. |
* occurs during initialization |
173 |
** @param c the class to check |
* @throws SecurityException if the <code>classloader</code> argument |
174 |
** @return whether an instance of c would be an instance |
* is <code>null</code> and the caller does not have the |
175 |
** of this class as well. |
* <code>RuntimePermission("getClassLoader")</code> permission |
176 |
** @since JDK1.1 |
* @see ClassLoader |
177 |
**/ |
* @since 1.2 |
178 |
public boolean isAssignableFrom(Class c) { |
*/ |
179 |
return VMClass.isAssignableFrom(this,c); |
public static Class forName(String name, boolean initialize, |
180 |
} |
ClassLoader classloader) |
181 |
|
throws ClassNotFoundException |
182 |
/** Return whether this class is an array type. |
{ |
183 |
** @return whether this class is an array type. |
if (classloader == null) |
184 |
** @since JDK1.1 |
{ |
185 |
**/ |
// Check if we may access the bootstrap classloader |
186 |
public boolean isArray() { |
SecurityManager sm = System.getSecurityManager(); |
187 |
return name.charAt(0) == '['; |
if (sm != null) |
188 |
} |
{ |
189 |
|
// Get the calling class and classloader |
190 |
/** Return whether this class is a primitive type. A |
Class c = VMSecurityManager.getClassContext()[1]; |
191 |
** primitive type class is a class representing a kind of |
ClassLoader cl = c.getClassLoader(); |
192 |
** "placeholder" for the various primitive types. You |
if (cl != null) |
193 |
** can access the various primitive type classes through |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
194 |
** java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc. |
} |
195 |
** @return whether this class is a primitive type. |
Class c = VMClassLoader.loadClass(name, initialize); |
196 |
** @since JDK1.1 |
if (c != null) |
197 |
**/ |
return c; |
198 |
public boolean isPrimitive() { |
throw new ClassNotFoundException(name); |
199 |
return VMClass.isPrimitive(this); |
} |
200 |
} |
return classloader.loadClass(name, initialize); |
201 |
|
} |
202 |
/** If this is an array, get the Class representing the |
|
203 |
** type of array. Examples: [[java.lang.String would |
/** |
204 |
** return [java.lang.String, and calling getComponentType |
* Get a new instance of this class by calling the no-argument constructor. |
205 |
** on that would give java.lang.String. If this is not |
* The class is initialized if it has not been already. A security check |
206 |
** an array, returns null. |
* may be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code> |
207 |
** @return the array type of this class, or null. |
* as well as <code>checkPackageAccess</code> both having to succeed. |
208 |
** @since JDK1.1 |
* |
209 |
**/ |
* @return a new instance of this class |
210 |
public Class getComponentType() { |
* @throws InstantiationException if there is not a no-arg constructor |
211 |
if(isArray()) { |
* for this class, including interfaces, abstract classes, arrays, |
212 |
try { |
* primitive types, and void; or if an exception occurred during |
213 |
return Class.forName(name.substring(1)); |
* the constructor |
214 |
} catch(ClassNotFoundException e) { |
* @throws IllegalAccessException if you are not allowed to access the |
215 |
return null; |
* no-arg constructor because of scoping reasons |
216 |
} |
* @throws SecurityException if the security check fails |
217 |
} else { |
* @throws ExceptionInInitializerError if class initialization caused by |
218 |
return null; |
* this call fails with an exception |
219 |
} |
*/ |
220 |
} |
public Object newInstance() |
221 |
|
throws InstantiationException, IllegalAccessException |
222 |
/** Get the signers of this class. |
{ |
223 |
** @return the signers of this class. |
try |
224 |
** @since JDK1.1 |
{ |
225 |
**/ |
return getConstructor(null).newInstance(null); |
226 |
public Object[] getSigners() { |
} |
227 |
return signers; |
catch (IllegalArgumentException e) |
228 |
} |
{ |
229 |
|
throw (Error) new InternalError("Should not happen").initCause(e); |
230 |
/** Set the signers of this class. |
} |
231 |
** @param signers the signers of this class. |
catch (InvocationTargetException e) |
232 |
** @since JDK1.1 |
{ |
233 |
**/ |
throw (InstantiationException) |
234 |
public void setSigners(Object[] signers) { |
new InstantiationException(e.toString()).initCause(e); |
235 |
this.signers = signers; |
} |
236 |
} |
catch (NoSuchMethodException e) |
237 |
|
{ |
238 |
/** Get a resource URL using this class's package using |
throw (InstantiationException) |
239 |
** the getClassLoader().getResource() method. If this |
new InstantiationException(e.toString()).initCause(e); |
240 |
** class was loaded using the system classloader, |
} |
241 |
** ClassLoader.getSystemResource() is used instead.<P> |
} |
242 |
** |
|
243 |
** If the name you supply is absolute (it starts with a |
/** |
244 |
** <CODE>/</CODE>), then it is passed on to getResource() |
* Discover whether an Object is an instance of this Class. Think of it |
245 |
** as is. If it is relative, the package name is |
* as almost like <code>o instanceof (this class)</code>. |
246 |
** prepended, with <CODE>.</CODE>'s replaced with |
* |
247 |
** <CODE>/</CODE> slashes.<P> |
* @param o the Object to check |
248 |
** |
* @return whether o is an instance of this class |
249 |
** The URL returned is system- and classloader- |
* @since 1.1 |
250 |
** dependent, and could change across implementations. |
*/ |
251 |
** @param name the name of the resource, generally a |
public boolean isInstance(Object o) |
252 |
** path. |
{ |
253 |
** @return the URL to the resource. |
return vmClass.isInstance (o); |
254 |
**/ |
} |
255 |
public java.net.URL getResource(String name) { |
|
256 |
if(name.length() > 0 && name.charAt(0) != '/') { |
/** |
257 |
name = ClassHelper.getPackagePortion(getName()).replace('.','/') + "/" + name; |
* Discover whether an instance of the Class parameter would be an |
258 |
} |
* instance of this Class as well. Think of doing |
259 |
ClassLoader c = getClassLoader(); |
* <code>isInstance(c.newInstance())</code> or even |
260 |
if(c == null) { |
* <code>c.newInstance() instanceof (this class)</code>. While this |
261 |
return ClassLoader.getSystemResource(name); |
* checks widening conversions for objects, it must be exact for primitive |
262 |
} else { |
* types. |
263 |
return c.getResource(name); |
* |
264 |
} |
* @param c the class to check |
265 |
} |
* @return whether an instance of c would be an instance of this class |
266 |
|
* as well |
267 |
/** Get a resource using this class's package using the |
* @throws NullPointerException if c is null |
268 |
** getClassLoader().getResource() method. If this class |
* @since 1.1 |
269 |
** was loaded using the system classloader, |
*/ |
270 |
** ClassLoader.getSystemResource() is used instead.<P> |
public boolean isAssignableFrom(Class c) |
271 |
** |
{ |
272 |
** If the name you supply is absolute (it starts with a |
return vmClass.isAssignableFrom (c); |
273 |
** <CODE>/</CODE>), then it is passed on to getResource() |
} |
274 |
** as is. If it is relative, the package name is |
|
275 |
** prepended, with <CODE>.</CODE>'s replaced with |
/** |
276 |
** <CODE>/</CODE> slashes.<P> |
* Check whether this class is an interface or not. Array types are not |
277 |
** |
* interfaces. |
278 |
** The URL returned is system- and classloader- |
* |
279 |
** dependent, and could change across implementations. |
* @return whether this class is an interface or not |
280 |
** @param name the name of the resource, generally a |
*/ |
281 |
** path. |
public boolean isInterface() |
282 |
** @return An InputStream with the contents of the |
{ |
283 |
** resource in it. |
return vmClass.isInterface (); |
284 |
**/ |
} |
285 |
public java.io.InputStream getResourceAsStream(String name) { |
|
286 |
if(name.length() > 0 && name.charAt(0) != '/') { |
/** |
287 |
name = ClassHelper.getPackagePortion(getName()).replace('.','/') + "/" + name; |
* Return whether this class is an array type. |
288 |
} |
* |
289 |
ClassLoader c = getClassLoader(); |
* @return whether this class is an array type |
290 |
if(c == null) { |
* @since 1.1 |
291 |
return ClassLoader.getSystemResourceAsStream(name); |
*/ |
292 |
} else { |
public boolean isArray() |
293 |
return c.getResourceAsStream(name); |
{ |
294 |
} |
int result = -1; |
295 |
} |
if ((result = vmClass.isArray ()) < 0) |
296 |
|
return getName().charAt(0) == '['; |
297 |
/** Get the modifiers of this class. These can be checked |
|
298 |
** against using java.lang.reflect.Modifier. |
return (result == 1) ? true : false; |
299 |
** @return the modifiers of this class. |
} |
300 |
** @see java.lang.reflect.Modifer |
|
301 |
** @since JDK1.1 |
/** |
302 |
**/ |
* Return whether this class is a primitive type. A primitive type class |
303 |
public int getModifiers() { |
* is a class representing a kind of "placeholder" for the various |
304 |
return VMClass.getModifiers(this); |
* primitive types, or void. You can access the various primitive type |
305 |
} |
* classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc., |
306 |
|
* or through boolean.class, int.class, etc. |
307 |
/** If this is an inner class, return the class that |
* |
308 |
** declared it. If not, return null. |
* @return whether this class is a primitive type |
309 |
** @return the declaring class of this class. |
* @see Boolean#TYPE |
310 |
** @since JDK1.1 |
* @see Byte#TYPE |
311 |
**/ |
* @see Character#TYPE |
312 |
public Class getDeclaringClass() { |
* @see Short#TYPE |
313 |
return VMClass.getDeclaringClass(this); |
* @see Integer#TYPE |
314 |
} |
* @see Long#TYPE |
315 |
|
* @see Float#TYPE |
316 |
/** Get all the public inner classes, declared in this |
* @see Double#TYPE |
317 |
** class or inherited from superclasses, that are |
* @see Void#TYPE |
318 |
** members of this class. |
* @since 1.1 |
319 |
** @return all public inner classes in this class. |
*/ |
320 |
**/ |
public boolean isPrimitive() |
321 |
public Class[] getClasses() { |
{ |
322 |
System.getSecurityManager().checkMemberAccess(this,Member.PUBLIC); |
return vmClass.isPrimitive (); |
323 |
return VMClass.getClasses(this); |
} |
324 |
} |
|
325 |
|
/** |
326 |
/** Get all the inner classes declared in this class. |
* Get the name of this class, separated by dots for package separators. |
327 |
** @return all inner classes declared in this class. |
* Primitive types and arrays are encoded as: |
328 |
** @exception SecurityException if you do not have access |
* <pre> |
329 |
** to non-public inner classes of this class. |
* boolean Z |
330 |
**/ |
* byte B |
331 |
public Class[] getDeclaredClasses() throws SecurityException { |
* char C |
332 |
System.getSecurityManager().checkMemberAccess(this,Member.DECLARED); |
* short S |
333 |
return VMClass.getDeclaredClasses(this); |
* int I |
334 |
} |
* long J |
335 |
|
* float F |
336 |
/** Get a public constructor from this class. |
* double D |
337 |
** @param args the argument types for the constructor. |
* void V |
338 |
** @return the constructor. |
* array type [<em>element type</em> |
339 |
** @exception NoSuchMethodException if the constructor does |
* class or interface, alone: <dotted name> |
340 |
** not exist. |
* class or interface, as element type: L<dotted name>; |
341 |
** @exception SecurityException if you do not have access to public |
* |
342 |
** members of this class. |
* @return the name of this class |
343 |
**/ |
*/ |
344 |
public Constructor getConstructor(Class[] args) throws NoSuchMethodException, SecurityException { |
public String getName() |
345 |
System.getSecurityManager().checkMemberAccess(this,Member.PUBLIC); |
{ |
346 |
return VMClass.getConstructor(this,args); |
return vmClass.getName (); |
347 |
} |
} |
348 |
|
|
349 |
/** Get a constructor declared in this class. |
/** |
350 |
** @param args the argument types for the constructor. |
* Get the ClassLoader that loaded this class. If it was loaded by the |
351 |
** @return the constructor. |
* system classloader, this method will return null. If there is a security |
352 |
** @exception NoSuchMethodException if the constructor does |
* manager, and the caller's class loader does not match the requested |
353 |
** not exist in this class. |
* one, a security check of <code>RuntimePermission("getClassLoader")</code> |
354 |
** @exception SecurityException if you do not have access to |
* must first succeed. Primitive types and void return null. |
355 |
** non-public members of this class. |
* |
356 |
**/ |
* @return the ClassLoader that loaded this class |
357 |
public Constructor getDeclaredConstructor(Class[] args) throws NoSuchMethodException, SecurityException { |
* @throws SecurityException if the security check fails |
358 |
System.getSecurityManager().checkMemberAccess(this,Member.DECLARED); |
* @see ClassLoader |
359 |
return VMClass.getDeclaredConstructor(this,args); |
* @see RuntimePermission |
360 |
} |
*/ |
361 |
|
public ClassLoader getClassLoader() |
362 |
/** Get all public constructors from this class. |
{ |
363 |
** @return all public constructors in this class. |
// Check some common cases. |
364 |
** @exception SecurityException if you do not have access to public |
if (isPrimitive()) |
365 |
** members of this class. |
return null; |
366 |
**/ |
String name = getName(); |
367 |
public Constructor[] getConstructors() throws SecurityException { |
if (name.startsWith("java.") || name.startsWith("gnu.java.")) |
368 |
System.getSecurityManager().checkMemberAccess(this,Member.PUBLIC); |
return null; |
369 |
return VMClass.getConstructors(this); |
|
370 |
} |
ClassLoader loader = vmClass.getClassLoader(); |
371 |
|
// Check if we may get the classloader |
372 |
/** Get all constructors declared in this class. |
SecurityManager sm = System.getSecurityManager(); |
373 |
** @return all constructors declared in this class. |
if (sm != null) |
374 |
** @exception SecurityException if you do not have access to |
{ |
375 |
** non-public members of this class. |
// Get the calling class and classloader |
376 |
**/ |
Class c = VMSecurityManager.getClassContext()[1]; |
377 |
public Constructor[] getDeclaredConstructors() throws SecurityException { |
ClassLoader cl = c.getClassLoader(); |
378 |
System.getSecurityManager().checkMemberAccess(this,Member.DECLARED); |
if (cl != null && cl != ClassLoader.systemClassLoader) |
379 |
return VMClass.getDeclaredConstructors(this); |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
380 |
} |
} |
381 |
|
return loader; |
382 |
|
} |
383 |
/** Get a public method from this class. |
|
384 |
** @param name the name of the method. |
/** |
385 |
** @param args the argument types for the method. |
* Get the direct superclass of this class. If this is an interface, |
386 |
** @return the method. |
* Object, a primitive type, or void, it will return null. If this is an |
387 |
** @exception NoSuchMethodException if the method does |
* array type, it will return Object. |
388 |
** not exist. |
* |
389 |
** @exception SecurityException if you do not have access to public |
* @return the direct superclass of this class |
390 |
** members of this class. |
*/ |
391 |
**/ |
public Class getSuperclass() |
392 |
public Method getMethod(String name, Class[] args) throws NoSuchMethodException, SecurityException { |
{ |
393 |
System.getSecurityManager().checkMemberAccess(this,Member.PUBLIC); |
return vmClass.getSuperClass (); |
394 |
return VMClass.getMethod(this,name,args); |
} |
395 |
} |
|
396 |
|
/** |
397 |
/** Get a method declared in this class. |
* Returns the <code>Package</code> in which this class is defined |
398 |
** @param name the name of the method. |
* Returns null when this information is not available from the |
399 |
** @param args the argument types for the method. |
* classloader of this class or when the classloader of this class |
400 |
** @return the method. |
* is null. |
401 |
** @exception NoSuchMethodException if the method does |
* |
402 |
** not exist in this class. |
* @return the package for this class, if it is available |
403 |
** @exception SecurityException if you do not have access to |
* @since 1.2 |
404 |
** non-public members of this class. |
*/ |
405 |
**/ |
public Package getPackage() |
406 |
public Method getDeclaredMethod(String name, Class[] args) throws NoSuchMethodException, SecurityException { |
{ |
407 |
System.getSecurityManager().checkMemberAccess(this,Member.DECLARED); |
ClassLoader cl = getClassLoader(); |
408 |
return VMClass.getDeclaredMethod(this,name,args); |
if (cl != null) |
409 |
} |
return cl.getPackage(ClassHelper.getPackagePortion(getName())); |
410 |
|
return null; |
411 |
/** Get all public methods from this class. |
} |
412 |
** @return all public methods in this class. |
|
413 |
** @exception SecurityException if you do not have access to public |
/** |
414 |
** members of this class. |
* Get the interfaces this class <EM>directly</EM> implements, in the |
415 |
**/ |
* order that they were declared. This returns an empty array, not null, |
416 |
public Method[] getMethods() throws SecurityException { |
* for Object, primitives, void, and classes or interfaces with no direct |
417 |
System.getSecurityManager().checkMemberAccess(this,Member.PUBLIC); |
* superinterface. Array types return Cloneable and Serializable. |
418 |
return VMClass.getMethods(this); |
* |
419 |
} |
* @return the interfaces this class directly implements |
420 |
|
*/ |
421 |
/** Get all methods declared in this class. |
public Class[] getInterfaces() |
422 |
** @return all methods declared in this class. |
{ |
423 |
** @exception SecurityException if you do not have access to |
return vmClass.getInterfaces (); |
424 |
** non-public members of this class. |
} |
425 |
**/ |
|
426 |
public Method[] getDeclaredMethods() throws SecurityException { |
/** |
427 |
System.getSecurityManager().checkMemberAccess(this,Member.DECLARED); |
* If this is an array, get the Class representing the type of array. |
428 |
return VMClass.getDeclaredMethods(this); |
* Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and |
429 |
} |
* calling getComponentType on that would give "java.lang.String". If |
430 |
|
* this is not an array, returns null. |
431 |
|
* |
432 |
/** Get a public field from this class. |
* @return the array type of this class, or null |
433 |
** @param name the name of the field. |
* @see Array |
434 |
** @return the field. |
* @since 1.1 |
435 |
** @exception NoSuchFieldException if the field does |
*/ |
436 |
** not exist. |
public Class getComponentType() |
437 |
** @exception SecurityException if you do not have access to public |
{ |
438 |
** members of this class. |
return vmClass.getComponentType (); |
439 |
**/ |
} |
440 |
public Field getField(String name) throws NoSuchFieldException, SecurityException { |
|
441 |
System.getSecurityManager().checkMemberAccess(this,Member.PUBLIC); |
/** |
442 |
return VMClass.getField(this,name); |
* Get the modifiers of this class. These can be decoded using Modifier, |
443 |
} |
* and is limited to one of public, protected, or private, and any of |
444 |
|
* final, static, abstract, or interface. An array class has the same |
445 |
/** Get a field declared in this class. |
* public, protected, or private modifier as its component type, and is |
446 |
** @param name the name of the field. |
* marked final but not an interface. Primitive types and void are marked |
447 |
** @return the field. |
* public and final, but not an interface. |
448 |
** @exception NoSuchFieldException if the field does |
* |
449 |
** not exist in this class. |
* @return the modifiers of this class |
450 |
** @exception SecurityException if you do not have access to |
* @see Modifer |
451 |
** non-public members of this class. |
* @since 1.1 |
452 |
**/ |
*/ |
453 |
public Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException { |
public int getModifiers() |
454 |
System.getSecurityManager().checkMemberAccess(this,Member.DECLARED); |
{ |
455 |
return VMClass.getDeclaredField(this,name); |
return vmClass.getModifiers (); |
456 |
} |
} |
457 |
|
|
458 |
/** Get all public fields from this class. |
/** |
459 |
** @return all public fields in this class. |
* Get the signers of this class. This returns null if there are no signers, |
460 |
** @exception SecurityException if you do not have access to public |
* such as for primitive types or void. |
461 |
** members of this class. |
* |
462 |
**/ |
* @return the signers of this class |
463 |
public Field[] getFields() throws SecurityException { |
* @since 1.1 |
464 |
System.getSecurityManager().checkMemberAccess(this,Member.PUBLIC); |
*/ |
465 |
return VMClass.getFields(this); |
public Object[] getSigners() |
466 |
} |
{ |
467 |
|
return signers.clone (); |
468 |
/** Get all fields declared in this class. |
} |
469 |
** @return all fieilds declared in this class. |
|
470 |
** @exception SecurityException if you do not have access to |
/** |
471 |
** non-public members of this class. |
* Set the signers of this class. |
472 |
**/ |
* |
473 |
public Field[] getDeclaredFields() throws SecurityException { |
* @param signers the signers of this class |
474 |
System.getSecurityManager().checkMemberAccess(this,Member.DECLARED); |
*/ |
475 |
return VMClass.getDeclaredFields(this); |
void setSigners(Object[] signers) |
476 |
} |
{ |
477 |
} |
this.signers = signers; |
478 |
|
} |
479 |
|
|
480 |
|
/** |
481 |
|
* Perform security checks common to all of the methods that |
482 |
|
* get members of this Class. |
483 |
|
*/ |
484 |
|
private void memberAccessCheck(int which) { |
485 |
|
SecurityManager sm = System.getSecurityManager(); |
486 |
|
if (sm != null) { |
487 |
|
sm.checkMemberAccess(this, which); |
488 |
|
Package pkg = getPackage(); |
489 |
|
if (pkg != null) |
490 |
|
sm.checkPackageAccess(pkg.getName()); |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
|
* If this is a nested or inner class, return the class that declared it. |
496 |
|
* If not, return null. |
497 |
|
* |
498 |
|
* @return the declaring class of this class |
499 |
|
* @since 1.1 |
500 |
|
*/ |
501 |
|
public Class getDeclaringClass() |
502 |
|
{ |
503 |
|
return vmClass.getDeclaringClass (); |
504 |
|
} |
505 |
|
|
506 |
|
/** |
507 |
|
* Get all the public member classes and interfaces declared in this |
508 |
|
* class or inherited from superclasses. This returns an array of length |
509 |
|
* 0 if there are no member classes, including for primitive types. A |
510 |
|
* security check may be performed, with |
511 |
|
* <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as |
512 |
|
* <code>checkPackageAccess</code> both having to succeed. |
513 |
|
* |
514 |
|
* @return all public member classes in this class |
515 |
|
* @throws SecurityException if the security check fails |
516 |
|
* @since 1.1 |
517 |
|
*/ |
518 |
|
public Class[] getClasses() { |
519 |
|
memberAccessCheck(Member.PUBLIC); |
520 |
|
return internalGetClasses(); |
521 |
|
} |
522 |
|
|
523 |
|
/** |
524 |
|
* Like <code>getClasses()</code> but without the security checks. |
525 |
|
*/ |
526 |
|
private Class[] internalGetClasses() { |
527 |
|
ArrayList list = new ArrayList(); |
528 |
|
list.add(Arrays.asList(getDeclaredClasses(true))); |
529 |
|
Class superClass = getSuperclass(); |
530 |
|
if (superClass != null) |
531 |
|
list.add(Arrays.asList(superClass.internalGetClasses())); |
532 |
|
return (Class[])list.toArray(new Class[list.size()]); |
533 |
|
} |
534 |
|
|
535 |
|
/** |
536 |
|
* Get all the public fields declared in this class or inherited from |
537 |
|
* superclasses. This returns an array of length 0 if there are no fields, |
538 |
|
* including for primitive types. This does not return the implicit length |
539 |
|
* field of arrays. A security check may be performed, with |
540 |
|
* <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as |
541 |
|
* <code>checkPackageAccess</code> both having to succeed. |
542 |
|
* |
543 |
|
* @return all public fields in this class |
544 |
|
* @throws SecurityException if the security check fails |
545 |
|
* @since 1.1 |
546 |
|
*/ |
547 |
|
public Field[] getFields() { |
548 |
|
memberAccessCheck(Member.PUBLIC); |
549 |
|
return internalGetFields(); |
550 |
|
} |
551 |
|
|
552 |
|
/** |
553 |
|
* Like <code>getFields()</code> but without the security checks. |
554 |
|
*/ |
555 |
|
private Field[] internalGetFields() { |
556 |
|
ArrayList list = new ArrayList(); |
557 |
|
list.add(Arrays.asList(getDeclaredFields(true))); |
558 |
|
if (isInterface()) { |
559 |
|
Class[] interfaces = getInterfaces(); |
560 |
|
for (int i = 0; i < interfaces.length; i++) |
561 |
|
list.add(Arrays.asList(interfaces[i].internalGetFields())); |
562 |
|
} else { |
563 |
|
Class superClass = getSuperclass(); |
564 |
|
if (superClass != null) |
565 |
|
list.add(Arrays.asList(superClass.internalGetFields())); |
566 |
|
} |
567 |
|
return (Field[])list.toArray(new Field[list.size()]); |
568 |
|
} |
569 |
|
|
570 |
|
/** |
571 |
|
* Get all the public methods declared in this class or inherited from |
572 |
|
* superclasses. This returns an array of length 0 if there are no methods, |
573 |
|
* including for primitive types. This does include the implicit methods of |
574 |
|
* arrays and interfaces which mirror methods of Object, nor does it |
575 |
|
* include constructors or the class initialization methods. The Virtual |
576 |
|
* Machine allows multiple methods with the same signature but differing |
577 |
|
* return types; all such methods are in the returned array. A security |
578 |
|
* check may be performed, with |
579 |
|
* <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as |
580 |
|
* <code>checkPackageAccess</code> both having to succeed. |
581 |
|
* |
582 |
|
* @return all public methods in this class |
583 |
|
* @throws SecurityException if the security check fails |
584 |
|
* @since 1.1 |
585 |
|
*/ |
586 |
|
public Method[] getMethods() { |
587 |
|
memberAccessCheck(Member.PUBLIC); |
588 |
|
return internalGetMethods(); |
589 |
|
} |
590 |
|
|
591 |
|
/** |
592 |
|
* Like <code>getMethods()</code> but without the security checks. |
593 |
|
*/ |
594 |
|
private Method[] internalGetMethods() |
595 |
|
{ |
596 |
|
java.util.HashMap map = new java.util.HashMap(); |
597 |
|
Method[] methods; |
598 |
|
Class[] interfaces = getInterfaces(); |
599 |
|
for(int i = 0; i < interfaces.length; i++) |
600 |
|
{ |
601 |
|
methods = interfaces[i].internalGetMethods(); |
602 |
|
for(int j = 0; j < methods.length; j++) |
603 |
|
{ |
604 |
|
map.put(new MethodKey(methods[j]), methods[j]); |
605 |
|
} |
606 |
|
} |
607 |
|
Class superClass = getSuperclass(); |
608 |
|
if(superClass != null) |
609 |
|
{ |
610 |
|
methods = superclass.internalGetMethods(); |
611 |
|
for(int i = 0; i < methods.length; i++) |
612 |
|
{ |
613 |
|
map.put(new MethodKey(methods[i]), methods[i]); |
614 |
|
} |
615 |
|
} |
616 |
|
methods = getDeclaredMethods(true); |
617 |
|
for(int i = 0; i < methods.length; i++) |
618 |
|
{ |
619 |
|
map.put(new MethodKey(methods[i]), methods[i]); |
620 |
|
} |
621 |
|
return (Method[])map.values().toArray(new Method[map.size()]); |
622 |
|
} |
623 |
|
|
624 |
|
private static final class MethodKey |
625 |
|
{ |
626 |
|
private String name; |
627 |
|
private Class[] params; |
628 |
|
private Class returnType; |
629 |
|
private int hash; |
630 |
|
|
631 |
|
MethodKey(Method m) |
632 |
|
{ |
633 |
|
name = m.getName(); |
634 |
|
params = m.getParameterTypes(); |
635 |
|
returnType = m.getReturnType(); |
636 |
|
hash = name.hashCode() ^ returnType.hashCode(); |
637 |
|
for(int i = 0; i < params.length; i++) |
638 |
|
{ |
639 |
|
hash ^= params[i].hashCode(); |
640 |
|
} |
641 |
|
} |
642 |
|
|
643 |
|
public boolean equals(Object o) |
644 |
|
{ |
645 |
|
if(o instanceof MethodKey) |
646 |
|
{ |
647 |
|
MethodKey m = (MethodKey)o; |
648 |
|
if(m.name.equals(name) && m.params.length == params.length && m.returnType == returnType) |
649 |
|
{ |
650 |
|
for(int i = 0; i < params.length; i++) |
651 |
|
{ |
652 |
|
if(m.params[i] != params[i]) |
653 |
|
{ |
654 |
|
return false; |
655 |
|
} |
656 |
|
} |
657 |
|
return true; |
658 |
|
} |
659 |
|
} |
660 |
|
return false; |
661 |
|
} |
662 |
|
|
663 |
|
public int hashCode() |
664 |
|
{ |
665 |
|
return hash; |
666 |
|
} |
667 |
|
} |
668 |
|
|
669 |
|
|
670 |
|
/** |
671 |
|
* Get all the public constructors of this class. This returns an array of |
672 |
|
* length 0 if there are no constructors, including for primitive types, |
673 |
|
* arrays, and interfaces. It does, however, include the default |
674 |
|
* constructor if one was supplied by the compiler. A security check may |
675 |
|
* be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code> |
676 |
|
* as well as <code>checkPackageAccess</code> both having to succeed. |
677 |
|
* |
678 |
|
* @return all public constructors in this class |
679 |
|
* @throws SecurityException if the security check fails |
680 |
|
* @since 1.1 |
681 |
|
*/ |
682 |
|
public Constructor[] getConstructors() { |
683 |
|
memberAccessCheck(Member.PUBLIC); |
684 |
|
return getDeclaredConstructors(true); |
685 |
|
} |
686 |
|
|
687 |
|
/** |
688 |
|
* Get a public field declared or inherited in this class, where name is |
689 |
|
* its simple name. If the class contains multiple accessible fields by |
690 |
|
* that name, an arbitrary one is returned. The implicit length field of |
691 |
|
* arrays is not available. A security check may be performed, with |
692 |
|
* <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as |
693 |
|
* <code>checkPackageAccess</code> both having to succeed. |
694 |
|
* |
695 |
|
* @param name the name of the field |
696 |
|
* @return the field |
697 |
|
* @throws NoSuchFieldException if the field does not exist |
698 |
|
* @throws SecurityException if the security check fails |
699 |
|
* @see #getFields() |
700 |
|
* @since 1.1 |
701 |
|
*/ |
702 |
|
public Field getField(String name) throws NoSuchFieldException { |
703 |
|
memberAccessCheck(Member.PUBLIC); |
704 |
|
Field[] fields = getDeclaredFields(true); |
705 |
|
for (int i = 0; i < fields.length; i++) { |
706 |
|
Field field = fields[i]; |
707 |
|
if (field.getName().equals(name)) |
708 |
|
return field; |
709 |
|
} |
710 |
|
Class[] interfaces = getInterfaces(); |
711 |
|
for (int i = 0; i < interfaces.length; i++) { |
712 |
|
try { |
713 |
|
return interfaces[i].getField(name); |
714 |
|
} catch (NoSuchFieldException e) { |
715 |
|
} |
716 |
|
} |
717 |
|
Class superclass = getSuperclass(); |
718 |
|
if (superclass != null) |
719 |
|
return superclass.getField(name); |
720 |
|
throw new NoSuchFieldException(); |
721 |
|
} |
722 |
|
|
723 |
|
/** |
724 |
|
* Get a public method declared or inherited in this class, where name is |
725 |
|
* its simple name. The implicit methods of Object are not available from |
726 |
|
* arrays or interfaces. Constructors (named "<init>" in the class file) |
727 |
|
* and class initializers (name "<clinit>") are not available. The Virtual |
728 |
|
* Machine allows multiple methods with the same signature but differing |
729 |
|
* return types, and the class can inherit multiple methods of the same |
730 |
|
* return type; in such a case the most specific return types are favored, |
731 |
|
* then the final choice is arbitrary. If the method takes no argument, an |
732 |
|
* array of zero elements and null are equivalent for the types argument. |
733 |
|
* A security check may be performed, with |
734 |
|
* <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as |
735 |
|
* <code>checkPackageAccess</code> both having to succeed. |
736 |
|
* |
737 |
|
* @param name the name of the method |
738 |
|
* @param types the type of each parameter |
739 |
|
* @return the method |
740 |
|
* @throws NoSuchMethodException if the method does not exist |
741 |
|
* @throws SecurityException if the security check fails |
742 |
|
* @see #getMethods() |
743 |
|
* @since 1.1 |
744 |
|
*/ |
745 |
|
public Method getMethod(String name, Class[] args) |
746 |
|
throws NoSuchMethodException { |
747 |
|
memberAccessCheck(Member.PUBLIC); |
748 |
|
for (Class c = this; c != null; c = c.getSuperclass()) { |
749 |
|
Method match = matchMethod(c.getDeclaredMethods(true), name, args); |
750 |
|
if (match != null) |
751 |
|
return match; |
752 |
|
} |
753 |
|
throw new NoSuchMethodException(); |
754 |
|
} |
755 |
|
|
756 |
|
/** |
757 |
|
* Find the best matching method in <code>list</code> according to |
758 |
|
* the definition of ``best matching'' used by <code>getMethod()</code> |
759 |
|
* |
760 |
|
* <p> |
761 |
|
* Returns the method if any, otherwise <code>null</code>. |
762 |
|
* |
763 |
|
* @param list List of methods to search |
764 |
|
* @param name Name of method |
765 |
|
* @param args Method parameter types |
766 |
|
* @see #getMethod() |
767 |
|
*/ |
768 |
|
private static Method matchMethod(Method[] list, String name, Class[] args) { |
769 |
|
Method match = null; |
770 |
|
for (int i = 0; i < list.length; i++) { |
771 |
|
Method method = list[i]; |
772 |
|
if (!method.getName().equals(name)) |
773 |
|
continue; |
774 |
|
if (!matchParameters(args, method.getParameterTypes())) |
775 |
|
continue; |
776 |
|
if (match == null |
777 |
|
|| match.getReturnType().isAssignableFrom(method.getReturnType())) |
778 |
|
match = method; |
779 |
|
} |
780 |
|
return match; |
781 |
|
} |
782 |
|
|
783 |
|
/** |
784 |
|
* Check for an exact match between parameter type lists. |
785 |
|
* Either list may be <code>null</code> to mean a list of |
786 |
|
* length zero. |
787 |
|
*/ |
788 |
|
private static boolean matchParameters(Class[] types1, Class[] types2) { |
789 |
|
if (types1 == null) |
790 |
|
return types2 == null || types2.length == 0; |
791 |
|
if (types2 == null) |
792 |
|
return types1 == null || types1.length == 0; |
793 |
|
if (types1.length != types2.length) |
794 |
|
return false; |
795 |
|
for (int i = 0; i < types1.length; i++) { |
796 |
|
if (!types1[i].equals(types2[i])) |
797 |
|
return false; |
798 |
|
} |
799 |
|
return true; |
800 |
|
} |
801 |
|
|
802 |
|
/** |
803 |
|
* Get a public constructor declared in this class. If the constructor takes |
804 |
|
* no argument, an array of zero elements and null are equivalent for the |
805 |
|
* types argument. A security check may be performed, with |
806 |
|
* <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as |
807 |
|
* <code>checkPackageAccess</code> both having to succeed. |
808 |
|
* |
809 |
|
* @param types the type of each parameter |
810 |
|
* @return the constructor |
811 |
|
* @throws NoSuchMethodException if the constructor does not exist |
812 |
|
* @throws SecurityException if the security check fails |
813 |
|
* @see #getConstructors() |
814 |
|
* @since 1.1 |
815 |
|
*/ |
816 |
|
public Constructor getConstructor(Class[] args) throws NoSuchMethodException { |
817 |
|
memberAccessCheck(Member.PUBLIC); |
818 |
|
Constructor[] constructors = getDeclaredConstructors(true); |
819 |
|
for (int i = 0; i < constructors.length; i++) { |
820 |
|
Constructor constructor = constructors[i]; |
821 |
|
if (matchParameters(args, constructor.getParameterTypes())) |
822 |
|
return constructor; |
823 |
|
} |
824 |
|
throw new NoSuchMethodException(); |
825 |
|
} |
826 |
|
|
827 |
|
/** |
828 |
|
* Get all the declared member classes and interfaces in this class, but |
829 |
|
* not those inherited from superclasses. This returns an array of length |
830 |
|
* 0 if there are no member classes, including for primitive types. A |
831 |
|
* security check may be performed, with |
832 |
|
* <code>checkMemberAccess(this, Member.DECLARED)</code> as well as |
833 |
|
* <code>checkPackageAccess</code> both having to succeed. |
834 |
|
* |
835 |
|
* @return all declared member classes in this class |
836 |
|
* @throws SecurityException if the security check fails |
837 |
|
* @since 1.1 |
838 |
|
*/ |
839 |
|
public Class[] getDeclaredClasses() { |
840 |
|
memberAccessCheck(Member.DECLARED); |
841 |
|
return getDeclaredClasses(false); |
842 |
|
} |
843 |
|
|
844 |
|
Class[] getDeclaredClasses (boolean publicOnly) |
845 |
|
{ |
846 |
|
return vmClass.getDeclaredClasses (publicOnly); |
847 |
|
} |
848 |
|
|
849 |
|
/** |
850 |
|
* Get all the declared fields in this class, but not those inherited from |
851 |
|
* superclasses. This returns an array of length 0 if there are no fields, |
852 |
|
* including for primitive types. This does not return the implicit length |
853 |
|
* field of arrays. A security check may be performed, with |
854 |
|
* <code>checkMemberAccess(this, Member.DECLARED)</code> as well as |
855 |
|
* <code>checkPackageAccess</code> both having to succeed. |
856 |
|
* |
857 |
|
* @return all declared fields in this class |
858 |
|
* @throws SecurityException if the security check fails |
859 |
|
* @since 1.1 |
860 |
|
*/ |
861 |
|
public Field[] getDeclaredFields() { |
862 |
|
memberAccessCheck(Member.DECLARED); |
863 |
|
return getDeclaredFields(false); |
864 |
|
} |
865 |
|
|
866 |
|
Field[] getDeclaredFields (boolean publicOnly) |
867 |
|
{ |
868 |
|
return vmClass.getDeclaredFields (publicOnly); |
869 |
|
} |
870 |
|
|
871 |
|
/** |
872 |
|
* Get all the declared methods in this class, but not those inherited from |
873 |
|
* superclasses. This returns an array of length 0 if there are no methods, |
874 |
|
* including for primitive types. This does include the implicit methods of |
875 |
|
* arrays and interfaces which mirror methods of Object, nor does it |
876 |
|
* include constructors or the class initialization methods. The Virtual |
877 |
|
* Machine allows multiple methods with the same signature but differing |
878 |
|
* return types; all such methods are in the returned array. A security |
879 |
|
* check may be performed, with |
880 |
|
* <code>checkMemberAccess(this, Member.DECLARED)</code> as well as |
881 |
|
* <code>checkPackageAccess</code> both having to succeed. |
882 |
|
* |
883 |
|
* @return all declared methods in this class |
884 |
|
* @throws SecurityException if the security check fails |
885 |
|
* @since 1.1 |
886 |
|
*/ |
887 |
|
public Method[] getDeclaredMethods() { |
888 |
|
memberAccessCheck(Member.DECLARED); |
889 |
|
return getDeclaredMethods(false); |
890 |
|
} |
891 |
|
|
892 |
|
Method[] getDeclaredMethods (boolean publicOnly) |
893 |
|
{ |
894 |
|
return vmClass.getDeclaredMethods (publicOnly); |
895 |
|
} |
896 |
|
|
897 |
|
/** |
898 |
|
* Get all the declared constructors of this class. This returns an array of |
899 |
|
* length 0 if there are no constructors, including for primitive types, |
900 |
|
* arrays, and interfaces. It does, however, include the default |
901 |
|
* constructor if one was supplied by the compiler. A security check may |
902 |
|
* be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code> |
903 |
|
* as well as <code>checkPackageAccess</code> both having to succeed. |
904 |
|
* |
905 |
|
* @return all constructors in this class |
906 |
|
* @throws SecurityException if the security check fails |
907 |
|
* @since 1.1 |
908 |
|
*/ |
909 |
|
public Constructor[] getDeclaredConstructors() { |
910 |
|
memberAccessCheck(Member.DECLARED); |
911 |
|
return getDeclaredConstructors(false); |
912 |
|
} |
913 |
|
|
914 |
|
Constructor[] getDeclaredConstructors (boolean publicOnly) |
915 |
|
{ |
916 |
|
return vmClass.getDeclaredConstructors (publicOnly); |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
|
* Get a field declared in this class, where name is its simple name. The |
921 |
|
* implicit length field of arrays is not available. A security check may |
922 |
|
* be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code> |
923 |
|
* as well as <code>checkPackageAccess</code> both having to succeed. |
924 |
|
* |
925 |
|
* @param name the name of the field |
926 |
|
* @return the field |
927 |
|
* @throws NoSuchFieldException if the field does not exist |
928 |
|
* @throws SecurityException if the security check fails |
929 |
|
* @see #getDeclaredFields() |
930 |
|
* @since 1.1 |
931 |
|
*/ |
932 |
|
public Field getDeclaredField(String name) throws NoSuchFieldException { |
933 |
|
memberAccessCheck(Member.DECLARED); |
934 |
|
Field[] fields = getDeclaredFields(false); |
935 |
|
for (int i = 0; i < fields.length; i++) { |
936 |
|
if (fields[i].getName().equals(name)) |
937 |
|
return fields[i]; |
938 |
|
} |
939 |
|
throw new NoSuchFieldException(); |
940 |
|
} |
941 |
|
|
942 |
|
/** |
943 |
|
* Get a method declared in this class, where name is its simple name. The |
944 |
|
* implicit methods of Object are not available from arrays or interfaces. |
945 |
|
* Constructors (named "<init>" in the class file) and class initializers |
946 |
|
* (name "<clinit>") are not available. The Virtual Machine allows |
947 |
|
* multiple methods with the same signature but differing return types; in |
948 |
|
* such a case the most specific return types are favored, then the final |
949 |
|
* choice is arbitrary. If the method takes no argument, an array of zero |
950 |
|
* elements and null are equivalent for the types argument. A security |
951 |
|
* check may be performed, with |
952 |
|
* <code>checkMemberAccess(this, Member.DECLARED)</code> as well as |
953 |
|
* <code>checkPackageAccess</code> both having to succeed. |
954 |
|
* |
955 |
|
* @param name the name of the method |
956 |
|
* @param types the type of each parameter |
957 |
|
* @return the method |
958 |
|
* @throws NoSuchMethodException if the method does not exist |
959 |
|
* @throws SecurityException if the security check fails |
960 |
|
* @see #getDeclaredMethods() |
961 |
|
* @since 1.1 |
962 |
|
*/ |
963 |
|
public Method getDeclaredMethod(String name, Class[] args) |
964 |
|
throws NoSuchMethodException { |
965 |
|
memberAccessCheck(Member.DECLARED); |
966 |
|
Method match = matchMethod(getDeclaredMethods(false), name, args); |
967 |
|
if (match != null) |
968 |
|
return match; |
969 |
|
throw new NoSuchMethodException(); |
970 |
|
} |
971 |
|
|
972 |
|
/** |
973 |
|
* Get a constructor declared in this class. If the constructor takes no |
974 |
|
* argument, an array of zero elements and null are equivalent for the |
975 |
|
* types argument. A security check may be performed, with |
976 |
|
* <code>checkMemberAccess(this, Member.DECLARED)</code> as well as |
977 |
|
* <code>checkPackageAccess</code> both having to succeed. |
978 |
|
* |
979 |
|
* @param types the type of each parameter |
980 |
|
* @return the constructor |
981 |
|
* @throws NoSuchMethodException if the constructor does not exist |
982 |
|
* @throws SecurityException if the security check fails |
983 |
|
* @see #getDeclaredConstructors() |
984 |
|
* @since 1.1 |
985 |
|
*/ |
986 |
|
public Constructor getDeclaredConstructor(Class[] args) |
987 |
|
throws NoSuchMethodException { |
988 |
|
memberAccessCheck(Member.DECLARED); |
989 |
|
Constructor[] constructors = getDeclaredConstructors(false); |
990 |
|
for (int i = 0; i < constructors.length; i++) { |
991 |
|
Constructor constructor = constructors[i]; |
992 |
|
if (matchParameters(args, constructor.getParameterTypes())) |
993 |
|
return constructor; |
994 |
|
} |
995 |
|
throw new NoSuchMethodException(); |
996 |
|
} |
997 |
|
|
998 |
|
/** |
999 |
|
* Get a resource using this class's package using the |
1000 |
|
* getClassLoader().getResourceAsStream() method. If this class was loaded |
1001 |
|
* using the system classloader, ClassLoader.getSystemResource() is used |
1002 |
|
* instead. |
1003 |
|
* |
1004 |
|
* <p>If the name you supply is absolute (it starts with a <code>/</code>), |
1005 |
|
* then it is passed on to getResource() as is. If it is relative, the |
1006 |
|
* package name is prepended, and <code>.</code>'s are replaced with |
1007 |
|
* <code>/</code>. |
1008 |
|
* |
1009 |
|
* <p>The URL returned is system- and classloader-dependent, and could |
1010 |
|
* change across implementations. |
1011 |
|
* |
1012 |
|
* @param name the name of the resource, generally a path |
1013 |
|
* @return an InputStream with the contents of the resource in it, or null |
1014 |
|
* @throws NullPointerException if name is null |
1015 |
|
* @since 1.1 |
1016 |
|
*/ |
1017 |
|
public InputStream getResourceAsStream(String name) |
1018 |
|
{ |
1019 |
|
if (name.length() > 0 && name.charAt(0) != '/') |
1020 |
|
name = ClassHelper.getPackagePortion(getName()).replace('.','/') |
1021 |
|
+ "/" + name; |
1022 |
|
ClassLoader c = getClassLoader(); |
1023 |
|
if (c == null) |
1024 |
|
return ClassLoader.getSystemResourceAsStream(name); |
1025 |
|
return c.getResourceAsStream(name); |
1026 |
|
} |
1027 |
|
|
1028 |
|
/** |
1029 |
|
* Get a resource URL using this class's package using the |
1030 |
|
* getClassLoader().getResource() method. If this class was loaded using |
1031 |
|
* the system classloader, ClassLoader.getSystemResource() is used instead. |
1032 |
|
* |
1033 |
|
* <p>If the name you supply is absolute (it starts with a <code>/</code>), |
1034 |
|
* then it is passed on to getResource() as is. If it is relative, the |
1035 |
|
* package name is prepended, and <code>.</code>'s are replaced with |
1036 |
|
* <code>/</code>. |
1037 |
|
* |
1038 |
|
* <p>The URL returned is system- and classloader-dependent, and could |
1039 |
|
* change across implementations. |
1040 |
|
* |
1041 |
|
* @param name the name of the resource, generally a path |
1042 |
|
* @return the URL to the resource |
1043 |
|
* @throws NullPointerException if name is null |
1044 |
|
* @since 1.1 |
1045 |
|
*/ |
1046 |
|
public URL getResource(String name) |
1047 |
|
{ |
1048 |
|
if(name.length() > 0 && name.charAt(0) != '/') |
1049 |
|
name = ClassHelper.getPackagePortion(getName()).replace('.','/') |
1050 |
|
+ "/" + name; |
1051 |
|
ClassLoader c = getClassLoader(); |
1052 |
|
if (c == null) |
1053 |
|
return ClassLoader.getSystemResource(name); |
1054 |
|
return c.getResource(name); |
1055 |
|
} |
1056 |
|
|
1057 |
|
/** |
1058 |
|
* Returns the protection domain of this class. If the classloader did not |
1059 |
|
* record the protection domain when creating this class the unknown |
1060 |
|
* protection domain is returned which has a <code>null</code> code source |
1061 |
|
* and all permissions. A security check may be performed, with |
1062 |
|
* <code>RuntimePermission("getProtectionDomain")</code>. |
1063 |
|
* |
1064 |
|
* @return the protection domain |
1065 |
|
* @throws SecurityException if the security check fails |
1066 |
|
* @see RuntimePermission |
1067 |
|
* @since 1.2 |
1068 |
|
*/ |
1069 |
|
public ProtectionDomain getProtectionDomain() |
1070 |
|
{ |
1071 |
|
SecurityManager sm = System.getSecurityManager(); |
1072 |
|
if (sm != null) |
1073 |
|
sm.checkPermission(new RuntimePermission("getProtectionDomain")); |
1074 |
|
|
1075 |
|
return pd == null ? unknownProtectionDomain : pd; |
1076 |
|
} |
1077 |
|
|
1078 |
|
/** |
1079 |
|
* Returns the desired assertion status of this class, if it were to be |
1080 |
|
* initialized at this moment. The class assertion status, if set, is |
1081 |
|
* returned; the backup is the default package status; then if there is |
1082 |
|
* a class loader, that default is returned; and finally the system default |
1083 |
|
* is returned. This method seldom needs calling in user code, but exists |
1084 |
|
* for compilers to implement the assert statement. Note that there is no |
1085 |
|
* guarantee that the result of this method matches the class's actual |
1086 |
|
* assertion status. |
1087 |
|
* |
1088 |
|
* @return the desired assertion status |
1089 |
|
* @see ClassLoader#setClassAssertionStatus(String, boolean) |
1090 |
|
* @see ClassLoader#setPackageAssertionStatus(String, boolean) |
1091 |
|
* @see ClassLoader#setDefaultAssertionStatus(boolean) |
1092 |
|
* @since 1.4 |
1093 |
|
*/ |
1094 |
|
public boolean desiredAssertionStatus() |
1095 |
|
{ |
1096 |
|
ClassLoader c = getClassLoader(); |
1097 |
|
Object status; |
1098 |
|
if (c == null) |
1099 |
|
return VMClassLoader.defaultAssertionStatus(); |
1100 |
|
if (c.classAssertionStatus != null) |
1101 |
|
synchronized (c) |
1102 |
|
{ |
1103 |
|
status = c.classAssertionStatus.get(getName()); |
1104 |
|
if (status != null) |
1105 |
|
return status.equals(Boolean.TRUE); |
1106 |
|
} |
1107 |
|
else |
1108 |
|
{ |
1109 |
|
status = ClassLoader.systemClassAssertionStatus.get(getName()); |
1110 |
|
if (status != null) |
1111 |
|
return status.equals(Boolean.TRUE); |
1112 |
|
} |
1113 |
|
if (c.packageAssertionStatus != null) |
1114 |
|
synchronized (c) |
1115 |
|
{ |
1116 |
|
String name = ClassHelper.getPackagePortion(getName()); |
1117 |
|
if ("".equals(name)) |
1118 |
|
status = c.packageAssertionStatus.get(null); |
1119 |
|
else |
1120 |
|
do |
1121 |
|
{ |
1122 |
|
status = c.packageAssertionStatus.get(name); |
1123 |
|
name = ClassHelper.getPackagePortion(name); |
1124 |
|
} |
1125 |
|
while (! "".equals(name) && status == null); |
1126 |
|
if (status != null) |
1127 |
|
return status.equals(Boolean.TRUE); |
1128 |
|
} |
1129 |
|
else |
1130 |
|
{ |
1131 |
|
String name = ClassHelper.getPackagePortion(getName()); |
1132 |
|
if ("".equals(name)) |
1133 |
|
status = ClassLoader.systemPackageAssertionStatus.get(null); |
1134 |
|
else |
1135 |
|
do |
1136 |
|
{ |
1137 |
|
status = ClassLoader.systemPackageAssertionStatus.get(name); |
1138 |
|
name = ClassHelper.getPackagePortion(name); |
1139 |
|
} |
1140 |
|
while (! "".equals(name) && status == null); |
1141 |
|
if (status != null) |
1142 |
|
return status.equals(Boolean.TRUE); |
1143 |
|
} |
1144 |
|
return c.defaultAssertionStatus; |
1145 |
|
} |
1146 |
|
|
1147 |
|
} // class Class |