1. /*
  2. * @(#)Class.java 1.107 00/02/02
  3. *
  4. * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. import java.lang.reflect.Member;
  12. import java.lang.reflect.Field;
  13. import java.lang.reflect.Method;
  14. import java.lang.reflect.Constructor;
  15. import java.lang.reflect.Modifier;
  16. import java.io.InputStream;
  17. import java.io.ObjectStreamClass;
  18. import java.io.ObjectStreamField;
  19. /**
  20. * Instances of the class <code>Class</code> represent classes and interfaces
  21. * in a running Java application. Every array also belongs to a class that is
  22. * reflected as a <code>Class</code> object that is shared by all arrays with
  23. * the same element type and number of dimensions. The primitive Java types
  24. * (<code>boolean</code>, <code>byte</code>, <code>char</code>,
  25. * <code>short</code>, <code>int</code>, <code>long</code>,
  26. * <code>float</code>, and <code>double</code>), and the keyword
  27. * <code>void</code> are also represented as <code>Class</code> objects.
  28. *
  29. * <p> <code>Class</code> has no public constructor. Instead <code>Class</code>
  30. * objects are constructed automatically by the Java Virtual Machine as classes
  31. * are loaded and by calls to the <code>defineClass</code> method in the class
  32. * loader.
  33. *
  34. * <p> The following example uses a <code>Class</code> object to print the
  35. * class name of an object:
  36. *
  37. * <p> <blockquote><pre>
  38. * void printClassName(Object obj) {
  39. * System.out.println("The class of " + obj +
  40. * " is " + obj.getClass().getName());
  41. * }
  42. * </pre></blockquote>
  43. *
  44. * @author unascribed
  45. * @version 1.107, 02/02/00
  46. * @see java.lang.ClassLoader#defineClass(byte[], int, int)
  47. * @since JDK1.0
  48. */
  49. public final
  50. class Class implements java.io.Serializable {
  51. private static native void registerNatives();
  52. static {
  53. registerNatives();
  54. }
  55. /*
  56. * Constructor. Only the Java Virtual Machine creates Class
  57. * objects.
  58. */
  59. private Class() {}
  60. /**
  61. * Converts the object to a string. The string representation is the
  62. * string "class" or "interface", followed by a space, and then by the
  63. * fully qualified name of the class in the format returned by
  64. * <code>getName</code>. If this <code>Class</code> object represents a
  65. * primitive type, this method returns the name of the primitive type. If
  66. * this <code>Class</code> object represents void this method returns
  67. * "void".
  68. *
  69. * @return a string representation of this class object.
  70. */
  71. public String toString() {
  72. return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
  73. + getName();
  74. }
  75. /**
  76. * Returns the <code>Class</code> object associated with the class or
  77. * interface with the given string name. Invoking this method is
  78. * equivalent to:
  79. *
  80. * <blockquote><pre>
  81. * Class.forName(className, true, currentLoader)
  82. * </pre></blockquote>
  83. *
  84. * where <code>currentLoader</code> denotes the defining class loader of
  85. * the current class.
  86. *
  87. * <p> For example, the following code fragment returns the
  88. * runtime <code>Class</code> descriptor for the class named
  89. * <code>java.lang.Thread</code>:
  90. *
  91. * <blockquote><pre>
  92. * Class t = Class.forName("java.lang.Thread")
  93. * </pre></blockquote>
  94. * <p>
  95. * A call to <tt>forName("X")</tt> causes the class named
  96. * <tt>X</tt> to be initialized.
  97. *
  98. * @param className the fully qualified name of the desired class.
  99. * @return the <code>Class</code> object for the class with the
  100. * specified name.
  101. * @exception LinkageError if the linkage fails
  102. * @exception ExceptionInInitializerError if the initialization provoked
  103. * by this method fails
  104. * @exception ClassNotFoundException if the class cannot be located
  105. */
  106. public static Class forName(String className)
  107. throws ClassNotFoundException {
  108. return forName0(className, true, ClassLoader.getCallerClassLoader());
  109. }
  110. /**
  111. * Returns the <code>Class</code> object associated with the class or
  112. * interface with the given string name, using the given class loader.
  113. * Given the fully qualified name for a class or interface (in the same
  114. * format returned by <code>getName</code>) this method attempts to
  115. * locate, load, and link the class or interface. The specified class
  116. * loader is used to load the class or interface. If the parameter
  117. * <code>loader</code> is null, the class is loaded through the bootstrap
  118. * class loader. The class is initialized only if the
  119. * <code>initialize</code> parameter is <code>true</code> and if it has
  120. * not been initialized earlier.
  121. *
  122. * <p> If <code>name</code> denotes a primitive type or void, an attempt
  123. * will be made to locate a user-defined class in the unnamed package whose
  124. * name is <code>name</code>. Therefore, this method cannot be used to
  125. * obtain any of the <code>Class</code> objects representing primitive
  126. * types or void.
  127. *
  128. * <p> If <code>name</code> denotes an array class, the component type of
  129. * the array class is loaded but not initialized.
  130. *
  131. * <p> For example, in an instance method the expression:
  132. *
  133. * <blockquote><pre>
  134. * Class.forName("Foo")
  135. * </pre></blockquote>
  136. *
  137. * is equivalent to:
  138. *
  139. * <blockquote><pre>
  140. * Class.forName("Foo", true, this.getClass().getClassLoader())
  141. * </pre></blockquote>
  142. *
  143. * Note that this method throws errors related to loading, linking or
  144. * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
  145. * Java Language Specification</em>.
  146. *
  147. * <p> If the <code>loader</code> is <code>null</code>, and a security
  148. * manager is present, and the caller's class loader is not null, then this
  149. * method calls the security manager's <code>checkPermission</code> method
  150. * with a <code>RuntimePermission("getClassLoader")</code> permission to
  151. * ensure it's ok to access the bootstrap class loader.
  152. *
  153. * @param name fully qualified name of the desired class
  154. * @param initialize whether the class must be initialized
  155. * @param loader class loader from which the class must be loaded
  156. * @return class object representing the desired class
  157. *
  158. * @exception LinkageError if the linkage fails
  159. * @exception ExceptionInInitializerError if the initialization provoked
  160. * by this method fails
  161. * @exception ClassNotFoundException if the class cannot be located by
  162. * the specified class loader
  163. *
  164. * @see java.lang.Class#forName(String)
  165. * @see java.lang.ClassLoader
  166. * @since 1.2
  167. */
  168. public static Class forName(String name, boolean initialize,
  169. ClassLoader loader)
  170. throws ClassNotFoundException
  171. {
  172. if (loader == null) {
  173. SecurityManager sm = System.getSecurityManager();
  174. if (sm != null) {
  175. ClassLoader ccl = ClassLoader.getCallerClassLoader();
  176. if (ccl != null) {
  177. sm.checkPermission(ClassLoader.getGetClassLoaderPerm());
  178. }
  179. }
  180. }
  181. return forName0(name, initialize, loader);
  182. }
  183. /** Called after security checks have been made. */
  184. private static native Class forName0(String name, boolean initialize,
  185. ClassLoader loader)
  186. throws ClassNotFoundException;
  187. /**
  188. * Creates a new instance of the class represented by this <tt>Class</tt>
  189. * object. The class is instantiatied as if by a <code>new</code>
  190. * expression with an empty argument list. The class is initialized if it
  191. * has not already been initialized.
  192. *
  193. * <p>If there is a security manager, this method first calls the security
  194. * manager's <code>checkMemberAccess</code> method with <code>this</code>
  195. * and <code>Member.PUBLIC</code> as its arguments. If the class is in a
  196. * package, then this method also calls the security manager's
  197. * <code>checkPackageAccess</code> method with the package name as its
  198. * argument. Either of these calls could result in a SecurityException.
  199. *
  200. * @return a newly allocated instance of the class represented by this
  201. * object.
  202. * @exception IllegalAccessException if the class or initializer is
  203. * not accessible.
  204. * @exception InstantiationException
  205. * if this <code>Class</code> represents an abstract class,
  206. * an interface, an array class,
  207. * a primitive type, or void;
  208. * or if the instantiation fails for some other reason.
  209. * @exception ExceptionInInitializerError if the initialization
  210. * provoked by this method fails.
  211. * @exception SecurityException if there is no permission to create a new
  212. * instance.
  213. *
  214. */
  215. public Object newInstance()
  216. throws InstantiationException, IllegalAccessException
  217. {
  218. if (System.getSecurityManager() != null) {
  219. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  220. }
  221. return newInstance0();
  222. }
  223. private native Object newInstance0()
  224. throws InstantiationException, IllegalAccessException;
  225. /**
  226. * Determines if the specified <code>Object</code> is assignment-compatible
  227. * with the object represented by this <code>Class</code>. This method is
  228. * the dynamic equivalent of the Java language <code>instanceof</code>
  229. * operator. The method returns <code>true</code> if the specified
  230. * <code>Object</code> argument is non-null and can be cast to the
  231. * reference type represented by this <code>Class</code> object without
  232. * raising a <code>ClassCastException.</code> It returns <code>false</code>
  233. * otherwise.
  234. *
  235. * <p> Specifically, if this <code>Class</code> object represents a
  236. * declared class, this method returns <code>true</code> if the specified
  237. * <code>Object</code> argument is an instance of the represented class (or
  238. * of any of its subclasses); it returns <code>false</code> otherwise. If
  239. * this <code>Class</code> object represents an array class, this method
  240. * returns <code>true</code> if the specified <code>Object</code> argument
  241. * can be converted to an object of the array class by an identity
  242. * conversion or by a widening reference conversion; it returns
  243. * <code>false</code> otherwise. If this <code>Class</code> object
  244. * represents an interface, this method returns <code>true</code> if the
  245. * class or any superclass of the specified <code>Object</code> argument
  246. * implements this interface; it returns <code>false</code> otherwise. If
  247. * this <code>Class</code> object represents a primitive type, this method
  248. * returns <code>false</code>.
  249. *
  250. * @param obj the object to check
  251. * @return true if <code>obj</code> is an instance of this class
  252. *
  253. * @since JDK1.1
  254. */
  255. public native boolean isInstance(Object obj);
  256. /**
  257. * Determines if the class or interface represented by this
  258. * <code>Class</code> object is either the same as, or is a superclass or
  259. * superinterface of, the class or interface represented by the specified
  260. * <code>Class</code> parameter. It returns <code>true</code> if so;
  261. * otherwise it returns <code>false</code>. If this <code>Class</code>
  262. * object represents a primitive type, this method returns
  263. * <code>true</code> if the specified <code>Class</code> parameter is
  264. * exactly this <code>Class</code> object; otherwise it returns
  265. * <code>false</code>.
  266. *
  267. * <p> Specifically, this method tests whether the type represented by the
  268. * specified <code>Class</code> parameter can be converted to the type
  269. * represented by this <code>Class</code> object via an identity conversion
  270. * or via a widening reference conversion. See <em>The Java Language
  271. * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
  272. *
  273. * @param cls the <code>Class</code> object to be checked
  274. * @return the <code>boolean</code> value indicating whether objects of the
  275. * type <code>cls</code> can be assigned to objects of this class
  276. * @exception NullPointerException if the specified Class parameter is
  277. * null.
  278. * @since JDK1.1
  279. */
  280. public native boolean isAssignableFrom(Class cls);
  281. /**
  282. * Determines if the specified <code>Class</code> object represents an
  283. * interface type.
  284. *
  285. * @return <code>true</code> if this object represents an interface;
  286. * <code>false</code> otherwise.
  287. */
  288. public native boolean isInterface();
  289. /**
  290. * Determines if this <code>Class</code> object represents an array class.
  291. *
  292. * @return <code>true</code> if this object represents an array class;
  293. * <code>false</code> otherwise.
  294. * @since JDK1.1
  295. */
  296. public native boolean isArray();
  297. /**
  298. * Determines if the specified <code>Class</code> object represents a
  299. * primitive type.
  300. *
  301. * <p> There are nine predefined <code>Class</code> objects to represent
  302. * the eight primitive types and void. These are created by the Java
  303. * Virtual Machine, and have the same names as the primitive types that
  304. * they represent, namely <code>boolean</code>, <code>byte</code>,
  305. * <code>char</code>, <code>short</code>, <code>int</code>,
  306. * <code>long</code>, <code>float</code>, and <code>double</code>.
  307. *
  308. * <p> These objects may only be accessed via the following public static
  309. * final variables, and are the only <code>Class</code> objects for which
  310. * this method returns <code>true</code>.
  311. *
  312. * @return true if and only if this class represents a primitive type
  313. *
  314. * @see java.lang.Boolean#TYPE
  315. * @see java.lang.Character#TYPE
  316. * @see java.lang.Byte#TYPE
  317. * @see java.lang.Short#TYPE
  318. * @see java.lang.Integer#TYPE
  319. * @see java.lang.Long#TYPE
  320. * @see java.lang.Float#TYPE
  321. * @see java.lang.Double#TYPE
  322. * @see java.lang.Void#TYPE
  323. * @since JDK1.1
  324. */
  325. public native boolean isPrimitive();
  326. /**
  327. * Returns the fully-qualified name of the entity (class, interface, array
  328. * class, primitive type, or void) represented by this <code>Class</code>
  329. * object, as a <code>String</code>.
  330. *
  331. * <p> If this <code>Class</code> object represents a class of arrays, then
  332. * the internal form of the name consists of the name of the element type
  333. * in Java signature format, preceded by one or more "<tt>[</tt>"
  334. * characters representing the depth of array nesting. Thus:
  335. *
  336. * <blockquote><pre>
  337. * (new Object[3]).getClass().getName()
  338. * </pre></blockquote>
  339. *
  340. * returns "<code>[Ljava.lang.Object;</code>" and:
  341. *
  342. * <blockquote><pre>
  343. * (new int[3][4][5][6][7][8][9]).getClass().getName()
  344. * </pre></blockquote>
  345. *
  346. * returns "<code>[[[[[[[I</code>". The encoding of element type names
  347. * is as follows:
  348. *
  349. * <blockquote><pre>
  350. * B byte
  351. * C char
  352. * D double
  353. * F float
  354. * I int
  355. * J long
  356. * L<i>classname;</i> class or interface
  357. * S short
  358. * Z boolean
  359. * </pre></blockquote>
  360. *
  361. * The class or interface name <tt><i>classname</i></tt> is given in fully
  362. * qualified form as shown in the example above.
  363. *
  364. * @return the fully qualified name of the class or interface
  365. * represented by this object.
  366. */
  367. public native String getName();
  368. /**
  369. * Returns the class loader for the class. Some implementations may use
  370. * null to represent the bootstrap class loader. This method will return
  371. * null in such implementations if this class was loaded by the bootstrap
  372. * class loader.
  373. *
  374. * <p> If a security manager is present, and the caller's class loader is
  375. * not null and the caller's class loader is not the same as or an ancestor of
  376. * the class loader for the class whose class loader is requested, then
  377. * this method calls the security manager's <code>checkPermission</code>
  378. * method with a <code>RuntimePermission("getClassLoader")</code>
  379. * permission to ensure it's ok to access the class loader for the class.
  380. *
  381. * <p>If this object
  382. * represents a primitive type or void, null is returned.
  383. *
  384. * @return the class loader that loaded the class or interface
  385. * represented by this object.
  386. * @throws SecurityException
  387. * if a security manager exists and its
  388. * <code>checkPermission</code> method denies
  389. * access to the class loader for the class.
  390. * @see java.lang.ClassLoader
  391. * @see SecurityManager#checkPermission
  392. * @see java.lang.RuntimePermission
  393. */
  394. public ClassLoader getClassLoader() {
  395. ClassLoader cl = getClassLoader0();
  396. if (cl == null)
  397. return null;
  398. SecurityManager sm = System.getSecurityManager();
  399. if (sm != null) {
  400. ClassLoader ccl = ClassLoader.getCallerClassLoader();
  401. if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
  402. sm.checkPermission(ClassLoader.getGetClassLoaderPerm());
  403. }
  404. }
  405. return cl;
  406. }
  407. private native ClassLoader getClassLoader0();
  408. /**
  409. * Returns the <code>Class</code> representing the superclass of the entity
  410. * (class, interface, primitive type or void) represented by this
  411. * <code>Class</code>. If this <code>Class</code> represents either the
  412. * <code>Object</code> class, an interface, a primitive type, or void, then
  413. * null is returned. If this object represents an array class then the
  414. * <code>Class</code> object representing the <code>Object</code> class is
  415. * returned.
  416. *
  417. * @return the superclass of the class represented by this object.
  418. */
  419. public native Class getSuperclass();
  420. /**
  421. * Gets the package for this class. The class loader of this class is used
  422. * to find the package. If the class was loaded by the bootstrap class
  423. * loader the set of packages loaded from CLASSPATH is searched to find the
  424. * package of the class. Null is returned if no package object was created
  425. * by the class loader of this class.
  426. *
  427. * <p> Packages have attributes for versions and specifications only if the
  428. * information was defined in the manifests that accompany the classes, and
  429. * if the class loader created the package instance with the attributes
  430. * from the manifest.
  431. *
  432. * @return the package of the class, or null if no package
  433. * information is available from the archive or codebase.
  434. */
  435. public Package getPackage() {
  436. return Package.getPackage(this);
  437. }
  438. /**
  439. * Determines the interfaces implemented by the class or interface
  440. * represented by this object.
  441. *
  442. * <p> If this object represents a class, the return value is an array
  443. * containing objects representing all interfaces implemented by the
  444. * class. The order of the interface objects in the array corresponds to
  445. * the order of the interface names in the <code>implements</code> clause
  446. * of the declaration of the class represented by this object. For
  447. * example, given the declaration:
  448. * <blockquote><pre>
  449. * class Shimmer implements FloorWax, DessertTopping { ... }
  450. * </pre></blockquote>
  451. * suppose the value of <code>s</code> is an instance of
  452. * <code>Shimmer</code> the value of the expression:
  453. * <blockquote><pre>
  454. * s.getClass().getInterfaces()[0]
  455. * </pre></blockquote>
  456. * is the <code>Class</code> object that represents interface
  457. * <code>FloorWax</code> and the value of:
  458. * <blockquote><pre>
  459. * s.getClass().getInterfaces()[1]
  460. * </pre></blockquote>
  461. * is the <code>Class</code> object that represents interface
  462. * <code>DessertTopping</code>.
  463. *
  464. * <p> If this object represents an interface, the array contains objects
  465. * representing all interfaces extended by the interface. The order of the
  466. * interface objects in the array corresponds to the order of the interface
  467. * names in the <code>extends</code> clause of the declaration of the
  468. * interface represented by this object.
  469. *
  470. * <p> If this object represents a class or interface that implements no
  471. * interfaces, the method returns an array of length 0.
  472. *
  473. * <p> If this object represents a primitive type or void, the method
  474. * returns an array of length 0.
  475. *
  476. * @return an array of interfaces implemented by this class.
  477. */
  478. public native Class[] getInterfaces();
  479. /**
  480. * Returns the <code>Class</code> representing the component type of an
  481. * array. If this class does not represent an array class this method
  482. * returns null.
  483. *
  484. * @return the <code>Class</code> representing the component type of this
  485. * class if this class is an array
  486. * @see java.lang.reflect.Array
  487. * @since JDK1.1
  488. */
  489. public native Class getComponentType();
  490. /**
  491. * Returns the Java language modifiers for this class or interface, encoded
  492. * in an integer. The modifiers consist of the Java Virtual Machine's
  493. * constants for <code>public</code>, <code>protected</code>,
  494. * <code>private</code>, <code>final</code>, <code>static</code>,
  495. * <code>abstract</code> and <code>interface</code> they should be decoded
  496. * using the methods of class <code>Modifier</code>.
  497. *
  498. * <p> If the underlying class is an array class, then its
  499. * <code>public</code>, <code>private</code> and <code>protected</code>
  500. * modifiers are the same as those of its component type. If this
  501. * <code>Class</code> represents a primitive type or void, its
  502. * <code>public</code> modifier is always <code>true</code>, and its
  503. * <code>protected</code> and <code>private</code> modifers are always
  504. * <code>false</code>. If this object represents an array class, a
  505. * primitive type or void, then its <code>final</code> modifier is always
  506. * <code>true</code> and its interface modifer is always
  507. * <code>false</code>. The values of its other modifiers are not determined
  508. * by this specification.
  509. *
  510. * <p> The modifier encodings are defined in <em>The Java Virtual Machine
  511. * Specification</em>, table 4.1.
  512. *
  513. * @return the <code>int</code> representing the modifiers for this class
  514. * @see java.lang.reflect.Modifier
  515. * @since JDK1.1
  516. */
  517. public native int getModifiers();
  518. /**
  519. * Gets the signers of this class.
  520. *
  521. * @return the signers of this class, or null if there are no signers. In
  522. * particular, this method returns null if this object represents
  523. * a primitive type or void.
  524. * @since JDK1.1
  525. */
  526. public native Object[] getSigners();
  527. /**
  528. * Set the signers of this class.
  529. */
  530. native void setSigners(Object[] signers);
  531. /**
  532. * If the class or interface represented by this <code>Class</code> object
  533. * is a member of another class, returns the <code>Class</code> object
  534. * representing the class in which it was declared. This method returns
  535. * null if this class or interface is not a member of any other class. If
  536. * this <code>Class</code> object represents an array class, a primitive
  537. * type, or void,then this method returns null.
  538. *
  539. * @return the declaring class for this class
  540. * @since JDK1.1
  541. */
  542. public native Class getDeclaringClass();
  543. /**
  544. * Returns an array containing <code>Class</code> objects representing all
  545. * the public classes and interfaces that are members of the class
  546. * represented by this <code>Class</code> object. This includes public
  547. * class and interface members inherited from superclasses and public class
  548. * and interface members declared by the class. This method returns an
  549. * array of length 0 if this <code>Class</code> object has no public member
  550. * classes or interfaces. This method also returns an array of length 0 if
  551. * this <code>Class</code> object represents a primitive type, an array
  552. * class, or void.
  553. *
  554. * <p>For this class and each of its superclasses, the following
  555. * security checks are performed:
  556. * If there is a security manager, the security manager's
  557. * <code>checkMemberAccess</code> method is called with <code>this</code>
  558. * and <code>Member.PUBLIC</code> as its arguments, where <code>this</code>
  559. * is this class or the superclass whose members are being determined. If
  560. * the class is in a package, then the security manager's
  561. * <code>checkPackageAccess</code> method is also called with the package
  562. * name as its argument. Either of these calls could result in a
  563. * SecurityException.
  564. *
  565. * @return the array of <code>Class</code> objects representing the public
  566. * members of this class
  567. * @exception SecurityException if access to the information is denied.
  568. * @see SecurityManager#checkMemberAccess(Class, int)
  569. * @see SecurityManager#checkPackageAccess(String)
  570. *
  571. * @since JDK1.1
  572. */
  573. public Class[] getClasses() {
  574. // be very careful not to change the stack depth of this
  575. // checkMemberAccess call for security reasons
  576. // see java.lang.SecurityManager.checkMemberAccess
  577. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  578. // Privileged so this implementation can look at DECLARED classes,
  579. // something the caller might not have privilege to do. The code here
  580. // is allowed to look at DECLARED classes because (1) it does not hand
  581. // out anything other than public members and (2) public member access
  582. // has already been ok'd by the SecurityManager.
  583. Class[] result = (Class[]) java.security.AccessController.doPrivileged
  584. (new java.security.PrivilegedAction() {
  585. public Object run() {
  586. java.util.List list = new java.util.ArrayList();
  587. Class currentClass = Class.this;
  588. while (currentClass != null) {
  589. Class[] members = currentClass.getDeclaredClasses();
  590. for (int i = 0; i < members.length; i++) {
  591. if (Modifier.isPublic(members[i].getModifiers())) {
  592. list.add(members[i]);
  593. }
  594. }
  595. currentClass = currentClass.getSuperclass();
  596. }
  597. return list.toArray(new Class[0]);
  598. }
  599. });
  600. return result;
  601. }
  602. /**
  603. * Returns an array containing <code>Field</code> objects reflecting all
  604. * the accessible public fields of the class or interface represented by
  605. * this <code>Class</code> object. The elements in the array returned are
  606. * not sorted and are not in any particular order. This method returns an
  607. * array of length 0 if the class or interface has no accessible public
  608. * fields, or if it represents an array class, a primitive type, or void.
  609. *
  610. * <p> Specifically, if this <code>Class</code> object represents a class,
  611. * this method returns the public fields of this class and of all its
  612. * superclasses. If this <code>Class</code> object represents an
  613. * interface, this method returns the fields of this interface and of all
  614. * its superinterfaces.
  615. *
  616. * <p>If there is a security manager, this method first
  617. * calls the security manager's <code>checkMemberAccess</code> method
  618. * with <code>this</code> and <code>Member.PUBLIC</code>
  619. * as its arguments. If the class is in a package, then this method
  620. * also calls the security manager's <code>checkPackageAccess</code>
  621. * method with the package name as its argument. Either of these calls
  622. * could result in a SecurityException.
  623. *
  624. * <p> The implicit length field for array classs is not reflected by this
  625. * method. User code should use the methods of class <code>Array</code> to
  626. * manipulate arrays.
  627. *
  628. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  629. *
  630. * @return the array of <code>Field</code> objects representing the
  631. * public fields
  632. * @exception SecurityException if access to the information is denied.
  633. * @see java.lang.reflect.Field
  634. * @see SecurityManager#checkMemberAccess(Class, int)
  635. * @see SecurityManager#checkPackageAccess(String)
  636. * @since JDK1.1
  637. */
  638. public Field[] getFields() throws SecurityException {
  639. // be very careful not to change the stack depth of this
  640. // checkMemberAccess call for security reasons
  641. // see java.lang.SecurityManager.checkMemberAccess
  642. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  643. return getFields0(Member.PUBLIC);
  644. }
  645. /**
  646. * Returns an array containing <code>Method</code> objects reflecting all
  647. * the public <em>member</em> methods of the class or interface represented
  648. * by this <code>Class</code> object, including those declared by the class
  649. * or interface and and those inherited from superclasses and
  650. * superinterfaces. The elements in the array returned are not sorted and
  651. * are not in any particular order. This method returns an array of length
  652. * 0 if this <code>Class</code> object represents a class or interface that
  653. * has no public member methods, or if this <code>Class</code> object
  654. * represents an array class, primitive type, or void.
  655. *
  656. * <p>If there is a security manager, this method first
  657. * calls the security manager's <code>checkMemberAccess</code> method
  658. * with <code>this</code> and <code>Member.PUBLIC</code>
  659. * as its arguments. If the class is in a package, then this method
  660. * also calls the security manager's <code>checkPackageAccess</code>
  661. * method with the package name
  662. * as its argument. Either of these calls could result in a SecurityException.
  663. *
  664. * <p> The class initialization method <code><clinit></code> is not
  665. * included in the returned array. If the class declares multiple public
  666. * member methods with the same parameter types, they are all included in
  667. * the returned array.
  668. *
  669. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
  670. *
  671. * @return the array of <code>Method</code> objects representing the
  672. * public methods of this class
  673. * @exception SecurityException if access to the information is denied.
  674. * @see java.lang.reflect.Method
  675. * @see SecurityManager#checkMemberAccess(Class, int)
  676. * @see SecurityManager#checkPackageAccess(String)
  677. * @since JDK1.1
  678. */
  679. public Method[] getMethods() throws SecurityException {
  680. // be very careful not to change the stack depth of this
  681. // checkMemberAccess call for security reasons
  682. // see java.lang.SecurityManager.checkMemberAccess
  683. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  684. return getMethods0(Member.PUBLIC);
  685. }
  686. /**
  687. * Returns an array containing <code>Constructor</code> objects reflecting
  688. * all the public constructors of the class represented by this
  689. * <code>Class</code> object. An array of length 0 is returned if the
  690. * class has no public constructors, or if the class is an array class, or
  691. * if the class reflects a primitive type or void.
  692. *
  693. * <p>If there is a security manager, this method first
  694. * calls the security manager's <code>checkMemberAccess</code> method
  695. * with <code>this</code> and <code>Member.PUBLIC</code>
  696. * as its arguments. If the class is in a package, then this method
  697. * also calls the security manager's <code>checkPackageAccess</code>
  698. * method with the package name
  699. * as its argument. Either of these calls could result in a SecurityException.
  700. *
  701. * @return the array containing <code>Method</code> objects for all the
  702. * declared public constructors of this class matches the specified
  703. * <code>parameterTypes</code>
  704. * @exception SecurityException if access to the information is denied.
  705. * @see java.lang.reflect.Constructor
  706. * @see SecurityManager#checkMemberAccess(Class, int)
  707. * @see SecurityManager#checkPackageAccess(String)
  708. * @since JDK1.1
  709. */
  710. public Constructor[] getConstructors() throws SecurityException {
  711. // be very careful not to change the stack depth of this
  712. // checkMemberAccess call for security reasons
  713. // see java.lang.SecurityManager.checkMemberAccess
  714. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  715. return getConstructors0(Member.PUBLIC);
  716. }
  717. /**
  718. * Returns a <code>Field</code> object that reflects the specified public
  719. * member field of the class or interface represented by this
  720. * <code>Class</code> object. The <code>name</code> parameter is a
  721. * <code>String</code> specifying the simple name of the desired field.
  722. *
  723. * <p>If there is a security manager, this method first
  724. * calls the security manager's <code>checkMemberAccess</code> method
  725. * with <code>this</code> and <code>Member.PUBLIC</code>
  726. * as its arguments. If the class is in a package, then this method
  727. * also calls the security manager's <code>checkPackageAccess</code>
  728. * method with the package name
  729. * as its argument. Either of these calls could result in a SecurityException.
  730. *
  731. * <p> The field to be reflected is determined by the algorithm that
  732. * follows. Let C be the class represented by this object:
  733. * <OL>
  734. * <LI> If C declares a public field with the name specified, that is the
  735. * field to be reflected.</LI>
  736. * <LI> If no field was found in step 1 above, this algorithm is applied
  737. * recursively to each direct superinterface of C. The direct
  738. * superinterfaces are searched in the order they were declared.</LI>
  739. * <LI> If no field was found in steps 1 and 2 above, and C has a
  740. * superclass S, then this algorithm is invoked recursively upon S.
  741. * If C has no superclass, then a <code>NoSuchFieldException</code>
  742. * is thrown.</LI>
  743. * </OL>
  744. *
  745. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  746. *
  747. * @param name the field name
  748. * @return the <code>Field</code> object of this class specified by
  749. * <code>name</code>
  750. * @exception NoSuchFieldException if a field with the specified name is
  751. * not found.
  752. * @exception SecurityException if access to the information is denied.
  753. * @see java.lang.reflect.Field
  754. * @see SecurityManager#checkMemberAccess(Class, int)
  755. * @see SecurityManager#checkPackageAccess(String)
  756. * @since JDK1.1
  757. */
  758. public Field getField(String name)
  759. throws NoSuchFieldException, SecurityException {
  760. // be very careful not to change the stack depth of this
  761. // checkMemberAccess call for security reasons
  762. // see java.lang.SecurityManager.checkMemberAccess
  763. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  764. return getField0(name, Member.PUBLIC);
  765. }
  766. /**
  767. * Returns a <code>Method</code> object that reflects the specified public
  768. * member method of the class or interface represented by this
  769. * <code>Class</code> object. The <code>name</code> parameter is a
  770. * <code>String</code> specifying the simple name the desired method. The
  771. * <code>parameterTypes</code> parameter is an array of <code>Class</code>
  772. * objects that identify the method's formal parameter types, in declared
  773. * order. If <code>parameterTypes</code> is <code>null</code>, it is
  774. * treated as if it were an empty array.
  775. *
  776. * <p>If there is a security manager, this method first
  777. * calls the security manager's <code>checkMemberAccess</code> method
  778. * with <code>this</code> and <code>Member.PUBLIC</code>
  779. * as its arguments. If the class is in a package, then this method
  780. * also calls the security manager's <code>checkPackageAccess</code>
  781. * method with the package name
  782. * as its argument. Either of these calls could result in a SecurityException.
  783. *
  784. * <p> If the <code>name</code> is "<init>"or "<clinit>" a
  785. * <code>NoSuchMethodException</code> is raised. Otherwise, the method to
  786. * be reflected is determined by the algorithm that follows. Let C be the
  787. * class represented by this object:
  788. * <OL>
  789. * <LI> C is searched for any <I>matching methods</I>. If no matching
  790. * method is found, the algorithm of step 1 is invoked recursively on
  791. * the superclass of C.</LI>
  792. * <LI> If no method was found in step 1 above, the superinterfaces of C
  793. * are searched for a matching method. If any such method is found, it
  794. * is reflected.</LI>
  795. * </OL>
  796. *
  797. * To find a matching method in a class C:  If C declares exactly one
  798. * public method with the specified name and exactly the same formal
  799. * parameter types, that is the method reflected. If more than one such
  800. * method is found in C, and one of these methods has a return type that is
  801. * more specific than any of the others, that method is reflected;
  802. * otherwise one of the methods is chosen arbitrarily.
  803. *
  804. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
  805. *
  806. * @param name the name of the method
  807. * @param parameterTypes the list of parameters
  808. * @return the <code>Method</code> object that matches the specified
  809. * <code>name</code> and <code>parameterTypes</code>
  810. * @exception NoSuchMethodException if a matching method is not found
  811. * or if then name is "<init>"or "<clinit>".
  812. * @exception SecurityException if access to the information is denied.
  813. * @see java.lang.reflect.Method
  814. * @see SecurityManager#checkMemberAccess(Class, int)
  815. * @see SecurityManager#checkPackageAccess(String)
  816. * @since JDK1.1
  817. */
  818. public Method getMethod(String name, Class[] parameterTypes)
  819. throws NoSuchMethodException, SecurityException {
  820. // be very careful not to change the stack depth of this
  821. // checkMemberAccess call for security reasons
  822. // see java.lang.SecurityManager.checkMemberAccess
  823. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  824. return getMethod0(name, parameterTypes, Member.PUBLIC);
  825. }
  826. /**
  827. * Returns a <code>Constructor</code> object that reflects the specified
  828. * public constructor of the class represented by this <code>Class</code>
  829. * object. The <code>parameterTypes</code> parameter is an array of
  830. * <code>Class</code> objects that identify the constructor's formal
  831. * parameter types, in declared order.
  832. *
  833. * <p> The constructor to reflect is the public constructor of the class
  834. * represented by this <code>Class</code> object whose formal parameter
  835. * types match those specified by <code>parameterTypes</code>.
  836. *
  837. * <p>If there is a security manager, this method first
  838. * calls the security manager's <code>checkMemberAccess</code> method
  839. * with <code>this</code> and <code>Member.PUBLIC</code>
  840. * as its arguments. If the class is in a package, then this method
  841. * also calls the security manager's <code>checkPackageAccess</code> method
  842. * with the package name as its argument. Either of these calls could
  843. * result in a SecurityException.
  844. *
  845. * @param parameterTypes the parameter array
  846. * @return the <code>Method</code> object of the public constructor that
  847. * matches the specified <code>parameterTypes</code>
  848. * @exception NoSuchMethodException if a matching method is not found.
  849. * @exception SecurityException if access to the information is denied.
  850. * @see java.lang.reflect.Constructor
  851. * @see SecurityManager#checkMemberAccess(Class, int)
  852. * @see SecurityManager#checkPackageAccess(String)
  853. * @since JDK1.1
  854. */
  855. public Constructor getConstructor(Class[] parameterTypes)
  856. throws NoSuchMethodException, SecurityException {
  857. // be very careful not to change the stack depth of this
  858. // checkMemberAccess call for security reasons
  859. // see java.lang.SecurityManager.checkMemberAccess
  860. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  861. return getConstructor0(parameterTypes, Member.PUBLIC);
  862. }
  863. /**
  864. * Returns an array of <code>Class</code> objects reflecting all the
  865. * classes and interfaces declared as members of the class represented by
  866. * this <code>Class</code> object. This includes public, protected, default
  867. * (package) access, and private classes and interfaces declared by the
  868. * class, but excludes inherited classes and interfaces. This method
  869. * returns an array of length 0 if the class declares no classes or
  870. * interfaces as members, or if this <code>Class</code> object represents a
  871. * primitive type, an array class, or void.
  872. *
  873. * <p>If there is a security manager, this method first
  874. * calls the security manager's <code>checkMemberAccess</code> method
  875. * with <code>this</code> and <code>Member.DECLARED</code>
  876. * as its arguments. If the class is in a package, then this method also
  877. * calls the security manager's <code>checkPackageAccess</code> method with
  878. * the package name as its argument. Either of these calls could result in
  879. * a SecurityException.
  880. *
  881. * @return the array of <code>Class</code> objects representing all the
  882. * declared members of this class
  883. * @exception SecurityException if access to the information is denied.
  884. * @see SecurityManager#checkMemberAccess(Class, int)
  885. * @see SecurityManager#checkPackageAccess(String)
  886. * @since JDK1.1
  887. */
  888. public Class[] getDeclaredClasses() throws SecurityException {
  889. // be very careful not to change the stack depth of this
  890. // checkMemberAccess call for security reasons
  891. // see java.lang.SecurityManager.checkMemberAccess
  892. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  893. return getDeclaredClasses0();
  894. }
  895. /**
  896. * Returns an array of <code>Field</code> objects reflecting all the fields
  897. * declared by the class or interface represented by this
  898. * <code>Class</code> object. This includes public, protected, default
  899. * (package) access, and private fields, but excludes inherited fields.
  900. * The elements in the array returned are not sorted and are not in any
  901. * particular order. This method returns an array of length 0 if the class
  902. * or interface declares no fields, or if this <code>Class</code> object
  903. * represents a primitive type, an array class, or void.
  904. *
  905. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  906. *
  907. * <p>If there is a security manager, this method first
  908. * calls the security manager's <code>checkMemberAccess</code> method with
  909. * <code>this</code> and <code>Member.DECLARED</code> as its arguments. If
  910. * the class is in a package, then this method also calls the security
  911. * manager's <code>checkPackageAccess</code> method with the package name
  912. * as its argument. Either of these calls could result in a
  913. * SecurityException.
  914. *
  915. * @return the array of <code>Field</code> objects representing all the
  916. * declared fields of this class
  917. * @exception SecurityException if access to the information is denied.
  918. * @see java.lang.reflect.Field
  919. * @see SecurityManager#checkMemberAccess(Class, int)
  920. * @see SecurityManager#checkPackageAccess(String)
  921. * @since JDK1.1
  922. */
  923. public Field[] getDeclaredFields() throws SecurityException {
  924. // be very careful not to change the stack depth of this
  925. // checkMemberAccess call for security reasons
  926. // see java.lang.SecurityManager.checkMemberAccess
  927. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  928. return getFields0(Member.DECLARED);
  929. }
  930. /**
  931. * Returns an array of <code>Method</code> objects reflecting all the
  932. * methods declared by the class or interface represented by this
  933. * <code>Class</code> object. This includes public, protected, default
  934. * (package) access, and private methods, but excludes inherited methods.
  935. * The elements in the array returned are not sorted and are not in any
  936. * particular order. This method returns an array of length 0 if the class
  937. * or interface declares no methods, or if this <code>Class</code> object
  938. * represents a primitive type, an array class, or void. The class
  939. * initialization method <code><clinit></code> is not included in the
  940. * returned array. If the class declares multiple public member methods
  941. * with the same parameter types, they are all included in the returned
  942. * array.
  943. *
  944. * <p> See <em>The Java Language Specification</em>, section 8.2.
  945. *
  946. * <p>If there is a security manager, this method first
  947. * calls the security manager's <code>checkMemberAccess</code> method
  948. * with <code>this</code> and <code>Member.DECLARED</code>
  949. * as its arguments. If the class is in a package, then this method
  950. * also calls the security manager's <code>checkPackageAccess</code> method
  951. * with the package name as its argument. Either of these calls could
  952. * result in a SecurityException.
  953. *
  954. * @return the array of <code>Method</code> objects representing all the
  955. * declared methods of this class
  956. * @exception SecurityException if access to the information is denied.
  957. * @see java.lang.reflect.Method
  958. * @see SecurityManager#checkMemberAccess(Class, int)
  959. * @see SecurityManager#checkPackageAccess(String)
  960. * @since JDK1.1
  961. */
  962. public Method[] getDeclaredMethods() throws SecurityException {
  963. // be very careful not to change the stack depth of this
  964. // checkMemberAccess call for security reasons
  965. // see java.lang.SecurityManager.checkMemberAccess
  966. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  967. return getMethods0(Member.DECLARED);
  968. }
  969. /**
  970. * Returns an array of <code>Constructor</code> objects reflecting all the
  971. * constructors declared by the class represented by this
  972. * <code>Class</code> object. These are public, protected, default
  973. * (package) access, and private constructors. The elements in the array
  974. * returned are not sorted and are not in any particular order. If the
  975. * class has a default constructor, it is included in the returned array.
  976. * This method returns an array of length 0 if this <code>Class</code>
  977. * object represents an interface, a primitive type, an array class, or
  978. * void.
  979. *
  980. * <p> See <em>The Java Language Specification</em>, section 8.2.
  981. *
  982. * <p>If there is a security manager, this method first
  983. * calls the security manager's <code>checkMemberAccess</code> method
  984. * with <code>this</code> and <code>Member.DECLARED</code>
  985. * as its arguments. If the class is in a package, then this method
  986. * also calls the security manager's <code>checkPackageAccess</code> method
  987. * with the package name as its argument. Either of these calls could
  988. * result in a SecurityException.
  989. *
  990. * @return the array of <code>Method</code> objects representing all the
  991. * declared constructors of this class
  992. * @exception SecurityException if access to the information is denied.
  993. * @see java.lang.reflect.Constructor
  994. * @see SecurityManager#checkMemberAccess(Class, int)
  995. * @see SecurityManager#checkPackageAccess(String)
  996. * @since JDK1.1
  997. */
  998. public Constructor[] getDeclaredConstructors() throws SecurityException {
  999. // be very careful not to change the stack depth of this
  1000. // checkMemberAccess call for security reasons
  1001. // see java.lang.SecurityManager.checkMemberAccess
  1002. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1003. return getConstructors0(Member.DECLARED);
  1004. }
  1005. /**
  1006. * Returns a <code>Field</code> object that reflects the specified declared
  1007. * field of the class or interface represented by this <code>Class</code>
  1008. * object. The <code>name</code> parameter is a <code>String</code> that
  1009. * specifies the simple name of the desired field. Note that this method
  1010. * will not reflect the <code>length</code> field of an array class.
  1011. *
  1012. * <p>If there is a security manager, this method first
  1013. * calls the security manager's <code>checkMemberAccess</code> method
  1014. * with <code>this</code> and <code>Member.DECLARED</code>
  1015. * as its arguments. If the class is in a package, then this method
  1016. * also calls the security manager's <code>checkPackageAccess</code> method
  1017. * with the package name as its argument. Either of these calls could
  1018. * result in a SecurityException.
  1019. *
  1020. * @param name the name of the field
  1021. * @return the <code>Field</code> object for the specified field in this
  1022. * class
  1023. * @exception NoSuchFieldException if a field with the specified name is
  1024. * not found.
  1025. * @exception SecurityException if access to the information is denied.
  1026. * @see java.lang.reflect.Field
  1027. * @see SecurityManager#checkMemberAccess(Class, int)
  1028. * @see SecurityManager#checkPackageAccess(String)
  1029. * @since JDK1.1
  1030. */
  1031. public Field getDeclaredField(String name)
  1032. throws NoSuchFieldException, SecurityException {
  1033. // be very careful not to change the stack depth of this
  1034. // checkMemberAccess call for security reasons
  1035. // see java.lang.SecurityManager.checkMemberAccess
  1036. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1037. return getField0(name, Member.DECLARED);
  1038. }
  1039. /**
  1040. * Returns a <code>Method</code> object that reflects the specified
  1041. * declared method of the class or interface represented by this
  1042. * <code>Class</code> object. The <code>name</code> parameter is a
  1043. * <code>String</code> that specifies the simple name of the desired
  1044. * method, and the <code>parameterTypes</code> parameter is an array of
  1045. * <code>Class</code> objects that identify the method's formal parameter
  1046. * types, in declared order. If more than one method with the same
  1047. * parameter types is declared in a class, and one of these methods has a
  1048. * return type that is more specific than any of the others, that method is
  1049. * returned; otherwise one of the methods is chosen arbitrarily. If the
  1050. * name is "<init>"or "<clinit>" a <code>NoSuchMethodException</code>
  1051. * is raised.
  1052. *
  1053. * <p>If there is a security manager, this method first
  1054. * calls the security manager's <code>checkMemberAccess</code> method
  1055. * with <code>this</code> and <code>Member.DECLARED</code>
  1056. * as its arguments. If the class is in a package, then this method also
  1057. * calls the security manager's <code>checkPackageAccess</code> method with
  1058. * the package name as its argument. Either of these calls could result in
  1059. * a SecurityException.
  1060. *
  1061. * @param name the name of the method
  1062. * @param parameterTypes the parameter array
  1063. * @return the <code>Method</code> object for the method of this class
  1064. * matching the specified name and parameters
  1065. * @exception NoSuchMethodException if a matching method is not found.
  1066. * @exception SecurityException if access to the information is denied.
  1067. * @see java.lang.reflect.Method
  1068. * @see SecurityManager#checkMemberAccess(Class, int)
  1069. * @see SecurityManager#checkPackageAccess(String)
  1070. * @since JDK1.1
  1071. */
  1072. public Method getDeclaredMethod(String name, Class[] parameterTypes)
  1073. throws NoSuchMethodException, SecurityException {
  1074. // be very careful not to change the stack depth of this
  1075. // checkMemberAccess call for security reasons
  1076. // see java.lang.SecurityManager.checkMemberAccess
  1077. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1078. return getMethod0(name, parameterTypes, Member.DECLARED);
  1079. }
  1080. /**
  1081. * Returns a <code>Constructor</code> object that reflects the specified
  1082. * constructor of the class or interface represented by this
  1083. * <code>Class</code> object. The <code>parameterTypes</code> parameter is
  1084. * an array of <code>Class</code> objects that identify the constructor's
  1085. * formal parameter types, in declared order.
  1086. *
  1087. * <p>If there is a security manager, this method first
  1088. * calls the security manager's <code>checkMemberAccess</code> method
  1089. * with <code>this</code> and <code>Member.DECLARED</code>
  1090. * as its arguments. If the class is in a package, then this method
  1091. * also calls the security manager's <code>checkPackageAccess</code>
  1092. * method with the package name
  1093. * as its argument. Either of these calls could result in a SecurityException.
  1094. *
  1095. * @param parameterTypes the parameter array
  1096. * @return The <code>Method</code> object for the constructor with the
  1097. * specified parameter list
  1098. * @exception NoSuchMethodException if a matching method is not found.
  1099. * @exception SecurityException if access to the information is denied.
  1100. * @see java.lang.reflect.Constructor
  1101. * @see SecurityManager#checkMemberAccess(Class, int)
  1102. * @see SecurityManager#checkPackageAccess(String)
  1103. * @since JDK1.1
  1104. */
  1105. public Constructor getDeclaredConstructor(Class[] parameterTypes)
  1106. throws NoSuchMethodException, SecurityException {
  1107. // be very careful not to change the stack depth of this
  1108. // checkMemberAccess call for security reasons
  1109. // see java.lang.SecurityManager.checkMemberAccess
  1110. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1111. return getConstructor0(parameterTypes, Member.DECLARED);
  1112. }
  1113. /**
  1114. * Finds a resource with a given name. This method returns null if no
  1115. * resource with this name is found. The rules for searching
  1116. * resources associated with a given class are implemented by the
  1117. * defining class loader of the class.
  1118. *
  1119. * <p> This method delegates the call to its class loader, after making
  1120. * these changes to the resource name: if the resource name starts with
  1121. * "/", it is unchanged; otherwise, the package name is prepended to the
  1122. * resource name after converting "." to "/". If this object was loaded by
  1123. * the bootstrap loader, the call is delegated to
  1124. * <code>ClassLoader.getSystemResourceAsStream</code>.
  1125. *
  1126. * @param name name of the desired resource
  1127. * @return a <code>java.io.InputStream</code> object.
  1128. * @see java.lang.ClassLoader
  1129. * @since JDK1.1
  1130. */
  1131. public InputStream getResourceAsStream(String name) {
  1132. name = resolveName(name);
  1133. ClassLoader cl = getClassLoader0();
  1134. if (cl==null) {
  1135. // A system class.
  1136. return ClassLoader.getSystemResourceAsStream(name);
  1137. }
  1138. return cl.getResourceAsStream(name);
  1139. }
  1140. /**
  1141. * Finds a resource with a given name. This method returns null if no
  1142. * resource with this name is found. The rules for searching resources
  1143. * associated with a given class are implemented by the * defining class
  1144. * loader of the class.
  1145. *
  1146. * <p> This method delegates the call to its class loader, after making
  1147. * these changes to the resource name: if the resource name starts with
  1148. * "/", it is unchanged; otherwise, the package name is prepended to the
  1149. * resource name after converting "." to "/". If this object was loaded by
  1150. * the bootstrap loader, the call is delegated to
  1151. * <code>ClassLoader.getSystemResource</code>.
  1152. *
  1153. * @param name name of the desired resource
  1154. * @return a <code>java.net.URL</code> object.
  1155. * @see java.lang.ClassLoader
  1156. * @since JDK1.1
  1157. */
  1158. public java.net.URL getResource(String name) {
  1159. name = resolveName(name);
  1160. ClassLoader cl = getClassLoader0();
  1161. if (cl==null) {
  1162. // A system class.
  1163. return ClassLoader.getSystemResource(name);
  1164. }
  1165. return cl.getResource(name);
  1166. }
  1167. /** permission required to get a protection domain */
  1168. private static RuntimePermission getPDperm;
  1169. /** protection domain returned when the internal domain is null */
  1170. private static java.security.ProtectionDomain allPermDomain;
  1171. /**
  1172. * Returns the <code>ProtectionDomain</code> of this class. If there is a
  1173. * security manager installed, this method first calls the security
  1174. * manager's <code>checkPermission</code> method with a
  1175. * <code>RuntimePermission("getProtectionDomain")</code> permission to
  1176. * ensure it's ok to get the
  1177. * <code>ProtectionDomain</code>.
  1178. *
  1179. * @return the ProtectionDomain of this class
  1180. *
  1181. * @throws SecurityException
  1182. * if a security manager exists and its
  1183. * <code>checkPermission</code> method doesn't allow
  1184. * geting the ProtectionDomain.
  1185. *
  1186. * @see java.security.ProtectionDomain
  1187. * @see SecurityManager#checkPermission
  1188. * @see java.lang.RuntimePermission
  1189. * @since 1.2
  1190. */
  1191. public java.security.ProtectionDomain getProtectionDomain() {
  1192. SecurityManager sm = System.getSecurityManager();
  1193. if (sm != null) {
  1194. if (getPDperm == null)
  1195. getPDperm = new RuntimePermission("getProtectionDomain");
  1196. sm.checkPermission(getPDperm);
  1197. }
  1198. java.security.ProtectionDomain pd = getProtectionDomain0();
  1199. if (pd == null) {
  1200. if (allPermDomain == null) {
  1201. java.security.Permissions perms =
  1202. new java.security.Permissions();
  1203. perms.add(new java.security.AllPermission());
  1204. allPermDomain =
  1205. new java.security.ProtectionDomain(null, perms);
  1206. }
  1207. pd = allPermDomain;
  1208. }
  1209. return pd;
  1210. }
  1211. /**
  1212. * Returns the ProtectionDomain of this class.
  1213. */
  1214. private native java.security.ProtectionDomain getProtectionDomain0();
  1215. /**
  1216. * Set the ProtectionDomain for this class. Called by
  1217. * ClassLoader.defineClass.
  1218. */
  1219. native void setProtectionDomain0(java.security.ProtectionDomain pd);
  1220. /*
  1221. * Return the Virtual Machine's Class object for the named
  1222. * primitive type.
  1223. */
  1224. static native Class getPrimitiveClass(String name);
  1225. /*
  1226. * Check if client is allowed to access members. If access is denied,
  1227. * throw a SecurityException.
  1228. *
  1229. * Be very careful not to change the stack depth of this checkMemberAccess
  1230. * call for security reasons reasons see
  1231. * java.lang.SecurityManager.checkMemberAccess
  1232. *
  1233. * <p> Default policy: allow all clients access with normal Java access
  1234. * control.
  1235. */
  1236. private void checkMemberAccess(int which, ClassLoader ccl) {
  1237. SecurityManager s = System.getSecurityManager();
  1238. if (s != null) {
  1239. s.checkMemberAccess(this, which);
  1240. ClassLoader cl = getClassLoader0();
  1241. if ((ccl != null) && (ccl != cl) &&
  1242. ((cl == null) || !cl.isAncestor(ccl))) {
  1243. String name = this.getName();
  1244. int i = name.lastIndexOf('.');
  1245. if (i != -1) {
  1246. s.checkPackageAccess(name.substring(0, i));
  1247. }
  1248. }
  1249. }
  1250. }
  1251. /**
  1252. * Add a package name prefix if the name is not absolute Remove leading "/"
  1253. * if name is absolute
  1254. */
  1255. private String resolveName(String name) {
  1256. if (name == null) {
  1257. return name;
  1258. }
  1259. if (!name.startsWith("/")) {
  1260. Class c = this;
  1261. while (c.isArray()) {
  1262. c = c.getComponentType();
  1263. }
  1264. String baseName = c.getName();
  1265. int index = baseName.lastIndexOf('.');
  1266. if (index != -1) {
  1267. name = baseName.substring(0, index).replace('.', '/')
  1268. +"/"+name;
  1269. }
  1270. } else {
  1271. name = name.substring(1);
  1272. }
  1273. return name;
  1274. }
  1275. private native Field[] getFields0(int which);
  1276. private native Method[] getMethods0(int which);
  1277. private native Constructor[] getConstructors0(int which);
  1278. private native Field getField0(String name, int which);
  1279. private native Method getMethod0(String name, Class[] parameterTypes,
  1280. int which);
  1281. private native Constructor getConstructor0(Class[] parameterTypes,
  1282. int which);
  1283. private native Class[] getDeclaredClasses0();
  1284. /** use serialVersionUID from JDK 1.1 for interoperability */
  1285. private static final long serialVersionUID = 3206093459760846163L;
  1286. /**
  1287. * Class Class is special cased within the Serialization Stream Protocol.
  1288. *
  1289. * A Class instance is written intially into an ObjectOutputStream in the
  1290. * following format:
  1291. * <pre>
  1292. * <code>TC_CLASS</code> ClassDescriptor
  1293. * A ClassDescriptor is a special cased serialization of
  1294. * a <code>java.io.ObjectStreamClass</code> instance.
  1295. * </pre>
  1296. * A new handle is generated for the initial time the class descriptor
  1297. * is written into the stream. Future references to the class descriptor
  1298. * are written as references to the initial class descriptor instance.
  1299. *
  1300. * @see java.io.ObjectStreamClass
  1301. */
  1302. private static final ObjectStreamField[] serialPersistentFields =
  1303. ObjectStreamClass.NO_FIELDS;
  1304. }