1. /*
  2. * @(#)Class.java 1.152 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. import java.lang.reflect.Member;
  9. import java.lang.reflect.Field;
  10. import java.lang.reflect.Method;
  11. import java.lang.reflect.Constructor;
  12. import java.lang.reflect.Modifier;
  13. import java.lang.reflect.InvocationTargetException;
  14. import java.lang.ref.SoftReference;
  15. import java.io.InputStream;
  16. import java.io.ObjectStreamClass;
  17. import java.io.ObjectStreamField;
  18. import java.security.AccessController;
  19. import java.security.PrivilegedAction;
  20. import java.util.ArrayList;
  21. import java.util.Collection;
  22. import java.util.HashSet;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import java.util.LinkedList;
  26. import java.util.LinkedHashSet;
  27. import java.util.Set;
  28. import sun.misc.Unsafe;
  29. import sun.reflect.Reflection;
  30. import sun.reflect.ReflectionFactory;
  31. import sun.reflect.SignatureIterator;
  32. import sun.security.util.SecurityConstants;
  33. /**
  34. * Instances of the class <code>Class</code> represent classes and interfaces
  35. * in a running Java application. Every array also belongs to a class that is
  36. * reflected as a <code>Class</code> object that is shared by all arrays with
  37. * the same element type and number of dimensions. The primitive Java types
  38. * (<code>boolean</code>, <code>byte</code>, <code>char</code>,
  39. * <code>short</code>, <code>int</code>, <code>long</code>,
  40. * <code>float</code>, and <code>double</code>), and the keyword
  41. * <code>void</code> are also represented as <code>Class</code> objects.
  42. *
  43. * <p> <code>Class</code> has no public constructor. Instead <code>Class</code>
  44. * objects are constructed automatically by the Java Virtual Machine as classes
  45. * are loaded and by calls to the <code>defineClass</code> method in the class
  46. * loader.
  47. *
  48. * <p> The following example uses a <code>Class</code> object to print the
  49. * class name of an object:
  50. *
  51. * <p> <blockquote><pre>
  52. * void printClassName(Object obj) {
  53. * System.out.println("The class of " + obj +
  54. * " is " + obj.getClass().getName());
  55. * }
  56. * </pre></blockquote>
  57. *
  58. * <p> It is also possible to get the <code>Class</code> object for a named
  59. * type (or for void) using a class literal
  60. * (JLS Section <A HREF="http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#251530">15.8.2</A>).
  61. * For example:
  62. *
  63. * <p> <blockquote><pre>
  64. * System.out.println("The name of class Foo is: "+Foo.class.getName());
  65. * </pre></blockquote>
  66. *
  67. * @author unascribed
  68. * @version 1.135, 05/25/01
  69. * @see java.lang.ClassLoader#defineClass(byte[], int, int)
  70. * @since JDK1.0
  71. */
  72. public final
  73. class Class implements java.io.Serializable {
  74. private static native void registerNatives();
  75. static {
  76. registerNatives();
  77. }
  78. /*
  79. * Constructor. Only the Java Virtual Machine creates Class
  80. * objects.
  81. */
  82. private Class() {}
  83. /**
  84. * Converts the object to a string. The string representation is the
  85. * string "class" or "interface", followed by a space, and then by the
  86. * fully qualified name of the class in the format returned by
  87. * <code>getName</code>. If this <code>Class</code> object represents a
  88. * primitive type, this method returns the name of the primitive type. If
  89. * this <code>Class</code> object represents void this method returns
  90. * "void".
  91. *
  92. * @return a string representation of this class object.
  93. */
  94. public String toString() {
  95. return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
  96. + getName();
  97. }
  98. /**
  99. * Returns the <code>Class</code> object associated with the class or
  100. * interface with the given string name. Invoking this method is
  101. * equivalent to:
  102. *
  103. * <blockquote><pre>
  104. * Class.forName(className, true, currentLoader)
  105. * </pre></blockquote>
  106. *
  107. * where <code>currentLoader</code> denotes the defining class loader of
  108. * the current class.
  109. *
  110. * <p> For example, the following code fragment returns the
  111. * runtime <code>Class</code> descriptor for the class named
  112. * <code>java.lang.Thread</code>:
  113. *
  114. * <blockquote><pre>
  115. * Class t = Class.forName("java.lang.Thread")
  116. * </pre></blockquote>
  117. * <p>
  118. * A call to <tt>forName("X")</tt> causes the class named
  119. * <tt>X</tt> to be initialized.
  120. *
  121. * @param className the fully qualified name of the desired class.
  122. * @return the <code>Class</code> object for the class with the
  123. * specified name.
  124. * @exception LinkageError if the linkage fails
  125. * @exception ExceptionInInitializerError if the initialization provoked
  126. * by this method fails
  127. * @exception ClassNotFoundException if the class cannot be located
  128. */
  129. public static Class forName(String className)
  130. throws ClassNotFoundException {
  131. return forName0(className, true, ClassLoader.getCallerClassLoader());
  132. }
  133. /**
  134. * Returns the <code>Class</code> object associated with the class or
  135. * interface with the given string name, using the given class loader.
  136. * Given the fully qualified name for a class or interface (in the same
  137. * format returned by <code>getName</code>) this method attempts to
  138. * locate, load, and link the class or interface. The specified class
  139. * loader is used to load the class or interface. If the parameter
  140. * <code>loader</code> is null, the class is loaded through the bootstrap
  141. * class loader. The class is initialized only if the
  142. * <code>initialize</code> parameter is <code>true</code> and if it has
  143. * not been initialized earlier.
  144. *
  145. * <p> If <code>name</code> denotes a primitive type or void, an attempt
  146. * will be made to locate a user-defined class in the unnamed package whose
  147. * name is <code>name</code>. Therefore, this method cannot be used to
  148. * obtain any of the <code>Class</code> objects representing primitive
  149. * types or void.
  150. *
  151. * <p> If <code>name</code> denotes an array class, the component type of
  152. * the array class is loaded but not initialized.
  153. *
  154. * <p> For example, in an instance method the expression:
  155. *
  156. * <blockquote><pre>
  157. * Class.forName("Foo")
  158. * </pre></blockquote>
  159. *
  160. * is equivalent to:
  161. *
  162. * <blockquote><pre>
  163. * Class.forName("Foo", true, this.getClass().getClassLoader())
  164. * </pre></blockquote>
  165. *
  166. * Note that this method throws errors related to loading, linking or
  167. * initializing as specified in Sections 12.2, 12.3 and 12.4 of <em>The
  168. * Java Language Specification</em>.
  169. * Note that this method does not check whether the requested class
  170. * is accessible to its caller.
  171. *
  172. * <p> If the <code>loader</code> is <code>null</code>, and a security
  173. * manager is present, and the caller's class loader is not null, then this
  174. * method calls the security manager's <code>checkPermission</code> method
  175. * with a <code>RuntimePermission("getClassLoader")</code> permission to
  176. * ensure it's ok to access the bootstrap class loader.
  177. *
  178. * @param name fully qualified name of the desired class
  179. * @param initialize whether the class must be initialized
  180. * @param loader class loader from which the class must be loaded
  181. * @return class object representing the desired class
  182. *
  183. * @exception LinkageError if the linkage fails
  184. * @exception ExceptionInInitializerError if the initialization provoked
  185. * by this method fails
  186. * @exception ClassNotFoundException if the class cannot be located by
  187. * the specified class loader
  188. *
  189. * @see java.lang.Class#forName(String)
  190. * @see java.lang.ClassLoader
  191. * @since 1.2
  192. */
  193. public static Class forName(String name, boolean initialize,
  194. ClassLoader loader)
  195. throws ClassNotFoundException
  196. {
  197. if (loader == null) {
  198. SecurityManager sm = System.getSecurityManager();
  199. if (sm != null) {
  200. ClassLoader ccl = ClassLoader.getCallerClassLoader();
  201. if (ccl != null) {
  202. sm.checkPermission(
  203. SecurityConstants.GET_CLASSLOADER_PERMISSION);
  204. }
  205. }
  206. }
  207. return forName0(name, initialize, loader);
  208. }
  209. /** Called after security checks have been made. */
  210. private static native Class forName0(String name, boolean initialize,
  211. ClassLoader loader)
  212. throws ClassNotFoundException;
  213. /**
  214. * Creates a new instance of the class represented by this <tt>Class</tt>
  215. * object. The class is instantiated as if by a <code>new</code>
  216. * expression with an empty argument list. The class is initialized if it
  217. * has not already been initialized.
  218. *
  219. * <p>If there is a security manager, this method first calls the security
  220. * manager's <code>checkMemberAccess</code> method with <code>this</code>
  221. * and <code>Member.PUBLIC</code> as its arguments. If the class is in a
  222. * package, then this method also calls the security manager's
  223. * <code>checkPackageAccess</code> method with the package name as its
  224. * argument. Either of these calls could result in a SecurityException.
  225. *
  226. * @return a newly allocated instance of the class represented by this
  227. * object.
  228. * @exception IllegalAccessException if the class or its nullary
  229. * constructor is not accessible.
  230. * @exception InstantiationException
  231. * if this <code>Class</code> represents an abstract class,
  232. * an interface, an array class, a primitive type, or void;
  233. * or if the class has no nullary constructor;
  234. * or if the instantiation fails for some other reason.
  235. * @exception ExceptionInInitializerError if the initialization
  236. * provoked by this method fails.
  237. * @exception SecurityException if there is no permission to create a new
  238. * instance.
  239. *
  240. */
  241. public Object newInstance()
  242. throws InstantiationException, IllegalAccessException
  243. {
  244. if (System.getSecurityManager() != null) {
  245. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  246. }
  247. return newInstance0();
  248. }
  249. private Object newInstance0()
  250. throws InstantiationException, IllegalAccessException
  251. {
  252. // NOTE: the following code may not be strictly correct under
  253. // the current Java memory model.
  254. // Constructor lookup
  255. if (cachedConstructor == null) {
  256. if (this == Class.class) {
  257. throw new IllegalAccessException(
  258. "Can not call newInstance() on the Class for java.lang.Class"
  259. );
  260. }
  261. try {
  262. final Constructor c =
  263. getConstructor0(new Class[] {}, Member.DECLARED);
  264. // Disable accessibility checks on the constructor
  265. // since we have to do the security check here anyway
  266. // (the stack depth is wrong for the Constructor's
  267. // security check to work)
  268. java.security.AccessController.doPrivileged
  269. (new java.security.PrivilegedAction() {
  270. public Object run() {
  271. c.setAccessible(true);
  272. return null;
  273. }
  274. });
  275. cachedConstructor = c;
  276. } catch (NoSuchMethodException e) {
  277. throw new InstantiationException(getName());
  278. }
  279. }
  280. Constructor tmpConstructor = cachedConstructor;
  281. // Security check (same as in java.lang.reflect.Constructor)
  282. int modifiers = tmpConstructor.getModifiers();
  283. if (!Reflection.quickCheckMemberAccess(this, modifiers)) {
  284. Class caller = Reflection.getCallerClass(3);
  285. if (newInstanceCallerCache != caller) {
  286. Reflection.ensureMemberAccess(caller, this, null, modifiers);
  287. newInstanceCallerCache = caller;
  288. }
  289. }
  290. // Run constructor
  291. try {
  292. return tmpConstructor.newInstance(null);
  293. } catch (InvocationTargetException e) {
  294. Unsafe.getUnsafe().throwException(e.getTargetException());
  295. // Not reached
  296. return null;
  297. }
  298. }
  299. private volatile transient Constructor cachedConstructor;
  300. private volatile transient Class newInstanceCallerCache;
  301. /**
  302. * Determines if the specified <code>Object</code> is assignment-compatible
  303. * with the object represented by this <code>Class</code>. This method is
  304. * the dynamic equivalent of the Java language <code>instanceof</code>
  305. * operator. The method returns <code>true</code> if the specified
  306. * <code>Object</code> argument is non-null and can be cast to the
  307. * reference type represented by this <code>Class</code> object without
  308. * raising a <code>ClassCastException.</code> It returns <code>false</code>
  309. * otherwise.
  310. *
  311. * <p> Specifically, if this <code>Class</code> object represents a
  312. * declared class, this method returns <code>true</code> if the specified
  313. * <code>Object</code> argument is an instance of the represented class (or
  314. * of any of its subclasses); it returns <code>false</code> otherwise. If
  315. * this <code>Class</code> object represents an array class, this method
  316. * returns <code>true</code> if the specified <code>Object</code> argument
  317. * can be converted to an object of the array class by an identity
  318. * conversion or by a widening reference conversion; it returns
  319. * <code>false</code> otherwise. If this <code>Class</code> object
  320. * represents an interface, this method returns <code>true</code> if the
  321. * class or any superclass of the specified <code>Object</code> argument
  322. * implements this interface; it returns <code>false</code> otherwise. If
  323. * this <code>Class</code> object represents a primitive type, this method
  324. * returns <code>false</code>.
  325. *
  326. * @param obj the object to check
  327. * @return true if <code>obj</code> is an instance of this class
  328. *
  329. * @since JDK1.1
  330. */
  331. public native boolean isInstance(Object obj);
  332. /**
  333. * Determines if the class or interface represented by this
  334. * <code>Class</code> object is either the same as, or is a superclass or
  335. * superinterface of, the class or interface represented by the specified
  336. * <code>Class</code> parameter. It returns <code>true</code> if so;
  337. * otherwise it returns <code>false</code>. If this <code>Class</code>
  338. * object represents a primitive type, this method returns
  339. * <code>true</code> if the specified <code>Class</code> parameter is
  340. * exactly this <code>Class</code> object; otherwise it returns
  341. * <code>false</code>.
  342. *
  343. * <p> Specifically, this method tests whether the type represented by the
  344. * specified <code>Class</code> parameter can be converted to the type
  345. * represented by this <code>Class</code> object via an identity conversion
  346. * or via a widening reference conversion. See <em>The Java Language
  347. * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
  348. *
  349. * @param cls the <code>Class</code> object to be checked
  350. * @return the <code>boolean</code> value indicating whether objects of the
  351. * type <code>cls</code> can be assigned to objects of this class
  352. * @exception NullPointerException if the specified Class parameter is
  353. * null.
  354. * @since JDK1.1
  355. */
  356. public native boolean isAssignableFrom(Class cls);
  357. /**
  358. * Determines if the specified <code>Class</code> object represents an
  359. * interface type.
  360. *
  361. * @return <code>true</code> if this object represents an interface;
  362. * <code>false</code> otherwise.
  363. */
  364. public native boolean isInterface();
  365. /**
  366. * Determines if this <code>Class</code> object represents an array class.
  367. *
  368. * @return <code>true</code> if this object represents an array class;
  369. * <code>false</code> otherwise.
  370. * @since JDK1.1
  371. */
  372. public native boolean isArray();
  373. /**
  374. * Determines if the specified <code>Class</code> object represents a
  375. * primitive type.
  376. *
  377. * <p> There are nine predefined <code>Class</code> objects to represent
  378. * the eight primitive types and void. These are created by the Java
  379. * Virtual Machine, and have the same names as the primitive types that
  380. * they represent, namely <code>boolean</code>, <code>byte</code>,
  381. * <code>char</code>, <code>short</code>, <code>int</code>,
  382. * <code>long</code>, <code>float</code>, and <code>double</code>.
  383. *
  384. * <p> These objects may only be accessed via the following public static
  385. * final variables, and are the only <code>Class</code> objects for which
  386. * this method returns <code>true</code>.
  387. *
  388. * @return true if and only if this class represents a primitive type
  389. *
  390. * @see java.lang.Boolean#TYPE
  391. * @see java.lang.Character#TYPE
  392. * @see java.lang.Byte#TYPE
  393. * @see java.lang.Short#TYPE
  394. * @see java.lang.Integer#TYPE
  395. * @see java.lang.Long#TYPE
  396. * @see java.lang.Float#TYPE
  397. * @see java.lang.Double#TYPE
  398. * @see java.lang.Void#TYPE
  399. * @since JDK1.1
  400. */
  401. public native boolean isPrimitive();
  402. /**
  403. * Returns the name of the entity (class, interface, array class,
  404. * primitive type, or void) represented by this <tt>Class</tt> object,
  405. * as a <tt>String</tt>.
  406. *
  407. * <p> If this class object represents a reference type that is not an
  408. * array type then the binary name of the class is returned, as specified
  409. * by the Java Language Specification, Second Edition.
  410. *
  411. * <p> If this class object represents a primitive type or void, then the
  412. * name returned is a <tt>String</tt> equal to the Java language
  413. * keyword corresponding to the primitive type or void.
  414. *
  415. * <p> If this class object represents a class of arrays, then the internal
  416. * form of the name consists of the name of the element type preceded by
  417. * one or more '<tt>[</tt>' characters representing the depth of the array
  418. * nesting. The encoding of element type names is as follows:
  419. *
  420. * <blockquote><table summary="Element types and encodings">
  421. * <tr><th> Element Type <th> Encoding
  422. * <tr><td> boolean <td align=center> Z
  423. * <tr><td> byte <td align=center> B
  424. * <tr><td> char <td align=center> C
  425. * <tr><td> class or interface <td align=center> L<i>classname;</i>
  426. * <tr><td> double <td align=center> D
  427. * <tr><td> float <td align=center> F
  428. * <tr><td> int <td align=center> I
  429. * <tr><td> long <td align=center> J
  430. * <tr><td> short <td align=center> S
  431. * </table></blockquote>
  432. *
  433. * <p> The class or interface name <i>classname</i> is the binary name of
  434. * the class specified above.
  435. *
  436. * <p> Examples:
  437. * <blockquote><pre>
  438. * String.class.getName()
  439. * returns "java.lang.String"
  440. * byte.class.getName()
  441. * returns "byte"
  442. * (new Object[3]).getClass().getName()
  443. * returns "[Ljava.lang.Object;"
  444. * (new int[3][4][5][6][7][8][9]).getClass().getName()
  445. * returns "[[[[[[[I"
  446. * </pre></blockquote>
  447. *
  448. * @return the name of the class or interface
  449. * represented by this object.
  450. */
  451. public native String getName();
  452. /**
  453. * Returns the class loader for the class. Some implementations may use
  454. * null to represent the bootstrap class loader. This method will return
  455. * null in such implementations if this class was loaded by the bootstrap
  456. * class loader.
  457. *
  458. * <p> If a security manager is present, and the caller's class loader is
  459. * not null and the caller's class loader is not the same as or an ancestor of
  460. * the class loader for the class whose class loader is requested, then
  461. * this method calls the security manager's <code>checkPermission</code>
  462. * method with a <code>RuntimePermission("getClassLoader")</code>
  463. * permission to ensure it's ok to access the class loader for the class.
  464. *
  465. * <p>If this object
  466. * represents a primitive type or void, null is returned.
  467. *
  468. * @return the class loader that loaded the class or interface
  469. * represented by this object.
  470. * @throws SecurityException
  471. * if a security manager exists and its
  472. * <code>checkPermission</code> method denies
  473. * access to the class loader for the class.
  474. * @see java.lang.ClassLoader
  475. * @see SecurityManager#checkPermission
  476. * @see java.lang.RuntimePermission
  477. */
  478. public ClassLoader getClassLoader() {
  479. ClassLoader cl = getClassLoader0();
  480. if (cl == null)
  481. return null;
  482. SecurityManager sm = System.getSecurityManager();
  483. if (sm != null) {
  484. ClassLoader ccl = ClassLoader.getCallerClassLoader();
  485. if (ccl != null && ccl != cl && !cl.isAncestor(ccl)) {
  486. sm.checkPermission(SecurityConstants.GET_CLASSLOADER_PERMISSION);
  487. }
  488. }
  489. return cl;
  490. }
  491. // Package-private to allow ClassLoader access
  492. native ClassLoader getClassLoader0();
  493. /**
  494. * Returns the <code>Class</code> representing the superclass of the entity
  495. * (class, interface, primitive type or void) represented by this
  496. * <code>Class</code>. If this <code>Class</code> represents either the
  497. * <code>Object</code> class, an interface, a primitive type, or void, then
  498. * null is returned. If this object represents an array class then the
  499. * <code>Class</code> object representing the <code>Object</code> class is
  500. * returned.
  501. *
  502. * @return the superclass of the class represented by this object.
  503. */
  504. public native Class getSuperclass();
  505. /**
  506. * Gets the package for this class. The class loader of this class is used
  507. * to find the package. If the class was loaded by the bootstrap class
  508. * loader the set of packages loaded from CLASSPATH is searched to find the
  509. * package of the class. Null is returned if no package object was created
  510. * by the class loader of this class.
  511. *
  512. * <p> Packages have attributes for versions and specifications only if the
  513. * information was defined in the manifests that accompany the classes, and
  514. * if the class loader created the package instance with the attributes
  515. * from the manifest.
  516. *
  517. * @return the package of the class, or null if no package
  518. * information is available from the archive or codebase.
  519. */
  520. public Package getPackage() {
  521. return Package.getPackage(this);
  522. }
  523. /**
  524. * Determines the interfaces implemented by the class or interface
  525. * represented by this object.
  526. *
  527. * <p> If this object represents a class, the return value is an array
  528. * containing objects representing all interfaces implemented by the
  529. * class. The order of the interface objects in the array corresponds to
  530. * the order of the interface names in the <code>implements</code> clause
  531. * of the declaration of the class represented by this object. For
  532. * example, given the declaration:
  533. * <blockquote><pre>
  534. * class Shimmer implements FloorWax, DessertTopping { ... }
  535. * </pre></blockquote>
  536. * suppose the value of <code>s</code> is an instance of
  537. * <code>Shimmer</code> the value of the expression:
  538. * <blockquote><pre>
  539. * s.getClass().getInterfaces()[0]
  540. * </pre></blockquote>
  541. * is the <code>Class</code> object that represents interface
  542. * <code>FloorWax</code> and the value of:
  543. * <blockquote><pre>
  544. * s.getClass().getInterfaces()[1]
  545. * </pre></blockquote>
  546. * is the <code>Class</code> object that represents interface
  547. * <code>DessertTopping</code>.
  548. *
  549. * <p> If this object represents an interface, the array contains objects
  550. * representing all interfaces extended by the interface. The order of the
  551. * interface objects in the array corresponds to the order of the interface
  552. * names in the <code>extends</code> clause of the declaration of the
  553. * interface represented by this object.
  554. *
  555. * <p> If this object represents a class or interface that implements no
  556. * interfaces, the method returns an array of length 0.
  557. *
  558. * <p> If this object represents a primitive type or void, the method
  559. * returns an array of length 0.
  560. *
  561. * @return an array of interfaces implemented by this class.
  562. */
  563. public native Class[] getInterfaces();
  564. /**
  565. * Returns the <code>Class</code> representing the component type of an
  566. * array. If this class does not represent an array class this method
  567. * returns null.
  568. *
  569. * @return the <code>Class</code> representing the component type of this
  570. * class if this class is an array
  571. * @see java.lang.reflect.Array
  572. * @since JDK1.1
  573. */
  574. public native Class getComponentType();
  575. /**
  576. * Returns the Java language modifiers for this class or interface, encoded
  577. * in an integer. The modifiers consist of the Java Virtual Machine's
  578. * constants for <code>public</code>, <code>protected</code>,
  579. * <code>private</code>, <code>final</code>, <code>static</code>,
  580. * <code>abstract</code> and <code>interface</code> they should be decoded
  581. * using the methods of class <code>Modifier</code>.
  582. *
  583. * <p> If the underlying class is an array class, then its
  584. * <code>public</code>, <code>private</code> and <code>protected</code>
  585. * modifiers are the same as those of its component type. If this
  586. * <code>Class</code> represents a primitive type or void, its
  587. * <code>public</code> modifier is always <code>true</code>, and its
  588. * <code>protected</code> and <code>private</code> modifiers are always
  589. * <code>false</code>. If this object represents an array class, a
  590. * primitive type or void, then its <code>final</code> modifier is always
  591. * <code>true</code> and its interface modifier is always
  592. * <code>false</code>. The values of its other modifiers are not determined
  593. * by this specification.
  594. *
  595. * <p> The modifier encodings are defined in <em>The Java Virtual Machine
  596. * Specification</em>, table 4.1.
  597. *
  598. * @return the <code>int</code> representing the modifiers for this class
  599. * @see java.lang.reflect.Modifier
  600. * @since JDK1.1
  601. */
  602. public native int getModifiers();
  603. /**
  604. * Gets the signers of this class.
  605. *
  606. * @return the signers of this class, or null if there are no signers. In
  607. * particular, this method returns null if this object represents
  608. * a primitive type or void.
  609. * @since JDK1.1
  610. */
  611. public native Object[] getSigners();
  612. /**
  613. * Set the signers of this class.
  614. */
  615. native void setSigners(Object[] signers);
  616. /**
  617. * If the class or interface represented by this <code>Class</code> object
  618. * is a member of another class, returns the <code>Class</code> object
  619. * representing the class in which it was declared. This method returns
  620. * null if this class or interface is not a member of any other class. If
  621. * this <code>Class</code> object represents an array class, a primitive
  622. * type, or void,then this method returns null.
  623. *
  624. * @return the declaring class for this class
  625. * @since JDK1.1
  626. */
  627. public native Class getDeclaringClass();
  628. /**
  629. * Returns an array containing <code>Class</code> objects representing all
  630. * the public classes and interfaces that are members of the class
  631. * represented by this <code>Class</code> object. This includes public
  632. * class and interface members inherited from superclasses and public class
  633. * and interface members declared by the class. This method returns an
  634. * array of length 0 if this <code>Class</code> object has no public member
  635. * classes or interfaces. This method also returns an array of length 0 if
  636. * this <code>Class</code> object represents a primitive type, an array
  637. * class, or void.
  638. *
  639. * <p>For this class and each of its superclasses, the following
  640. * security checks are performed:
  641. * If there is a security manager, the security manager's
  642. * <code>checkMemberAccess</code> method is called with <code>this</code>
  643. * and <code>Member.PUBLIC</code> as its arguments, where <code>this</code>
  644. * is this class or the superclass whose members are being determined. If
  645. * the class is in a package, then the security manager's
  646. * <code>checkPackageAccess</code> method is also called with the package
  647. * name as its argument. Either of these calls could result in a
  648. * SecurityException.
  649. *
  650. * @return the array of <code>Class</code> objects representing the public
  651. * members of this class
  652. * @exception SecurityException if access to the information is denied.
  653. * @see SecurityManager#checkMemberAccess(Class, int)
  654. * @see SecurityManager#checkPackageAccess(String)
  655. *
  656. * @since JDK1.1
  657. */
  658. public Class[] getClasses() {
  659. // be very careful not to change the stack depth of this
  660. // checkMemberAccess call for security reasons
  661. // see java.lang.SecurityManager.checkMemberAccess
  662. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  663. // Privileged so this implementation can look at DECLARED classes,
  664. // something the caller might not have privilege to do. The code here
  665. // is allowed to look at DECLARED classes because (1) it does not hand
  666. // out anything other than public members and (2) public member access
  667. // has already been ok'd by the SecurityManager.
  668. Class[] result = (Class[]) java.security.AccessController.doPrivileged
  669. (new java.security.PrivilegedAction() {
  670. public Object run() {
  671. java.util.List list = new java.util.ArrayList();
  672. Class currentClass = Class.this;
  673. while (currentClass != null) {
  674. Class[] members = currentClass.getDeclaredClasses();
  675. for (int i = 0; i < members.length; i++) {
  676. if (Modifier.isPublic(members[i].getModifiers())) {
  677. list.add(members[i]);
  678. }
  679. }
  680. currentClass = currentClass.getSuperclass();
  681. }
  682. return list.toArray(new Class[0]);
  683. }
  684. });
  685. return result;
  686. }
  687. /**
  688. * Returns an array containing <code>Field</code> objects reflecting all
  689. * the accessible public fields of the class or interface represented by
  690. * this <code>Class</code> object. The elements in the array returned are
  691. * not sorted and are not in any particular order. This method returns an
  692. * array of length 0 if the class or interface has no accessible public
  693. * fields, or if it represents an array class, a primitive type, or void.
  694. *
  695. * <p> Specifically, if this <code>Class</code> object represents a class,
  696. * this method returns the public fields of this class and of all its
  697. * superclasses. If this <code>Class</code> object represents an
  698. * interface, this method returns the fields of this interface and of all
  699. * its superinterfaces.
  700. *
  701. * <p>If there is a security manager, this method first
  702. * calls the security manager's <code>checkMemberAccess</code> method
  703. * with <code>this</code> and <code>Member.PUBLIC</code>
  704. * as its arguments. If the class is in a package, then this method
  705. * also calls the security manager's <code>checkPackageAccess</code>
  706. * method with the package name as its argument. Either of these calls
  707. * could result in a SecurityException.
  708. *
  709. * <p> The implicit length field for array class is not reflected by this
  710. * method. User code should use the methods of class <code>Array</code> to
  711. * manipulate arrays.
  712. *
  713. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  714. *
  715. * @return the array of <code>Field</code> objects representing the
  716. * public fields
  717. * @exception SecurityException if access to the information is denied.
  718. * @see java.lang.reflect.Field
  719. * @see SecurityManager#checkMemberAccess(Class, int)
  720. * @see SecurityManager#checkPackageAccess(String)
  721. * @since JDK1.1
  722. */
  723. public Field[] getFields() throws SecurityException {
  724. // be very careful not to change the stack depth of this
  725. // checkMemberAccess call for security reasons
  726. // see java.lang.SecurityManager.checkMemberAccess
  727. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  728. return copyFields(privateGetPublicFields(null));
  729. }
  730. /**
  731. * Returns an array containing <code>Method</code> objects reflecting all
  732. * the public <em>member</em> methods of the class or interface represented
  733. * by this <code>Class</code> object, including those declared by the class
  734. * or interface and and those inherited from superclasses and
  735. * superinterfaces. The elements in the array returned are not sorted and
  736. * are not in any particular order. This method returns an array of length
  737. * 0 if this <code>Class</code> object represents a class or interface that
  738. * has no public member methods, or if this <code>Class</code> object
  739. * represents an array class, primitive type, or void.
  740. *
  741. * <p>If there is a security manager, this method first
  742. * calls the security manager's <code>checkMemberAccess</code> method
  743. * with <code>this</code> and <code>Member.PUBLIC</code>
  744. * as its arguments. If the class is in a package, then this method
  745. * also calls the security manager's <code>checkPackageAccess</code>
  746. * method with the package name
  747. * as its argument. Either of these calls could result in a SecurityException.
  748. *
  749. * <p> The class initialization method <code><clinit></code> is not
  750. * included in the returned array. If the class declares multiple public
  751. * member methods with the same parameter types, they are all included in
  752. * the returned array.
  753. *
  754. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
  755. *
  756. * @return the array of <code>Method</code> objects representing the
  757. * public methods of this class
  758. * @exception SecurityException if access to the information is denied.
  759. * @see java.lang.reflect.Method
  760. * @see SecurityManager#checkMemberAccess(Class, int)
  761. * @see SecurityManager#checkPackageAccess(String)
  762. * @since JDK1.1
  763. */
  764. public Method[] getMethods() throws SecurityException {
  765. // be very careful not to change the stack depth of this
  766. // checkMemberAccess call for security reasons
  767. // see java.lang.SecurityManager.checkMemberAccess
  768. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  769. return copyMethods(privateGetPublicMethods());
  770. }
  771. /**
  772. * Returns an array containing <code>Constructor</code> objects reflecting
  773. * all the public constructors of the class represented by this
  774. * <code>Class</code> object. An array of length 0 is returned if the
  775. * class has no public constructors, or if the class is an array class, or
  776. * if the class reflects a primitive type or void.
  777. *
  778. * <p>If there is a security manager, this method first
  779. * calls the security manager's <code>checkMemberAccess</code> method
  780. * with <code>this</code> and <code>Member.PUBLIC</code>
  781. * as its arguments. If the class is in a package, then this method
  782. * also calls the security manager's <code>checkPackageAccess</code>
  783. * method with the package name
  784. * as its argument. Either of these calls could result in a SecurityException.
  785. *
  786. * @return the array containing <code>Method</code> objects for all the
  787. * declared public constructors of this class matches the specified
  788. * <code>parameterTypes</code>
  789. * @exception SecurityException if access to the information is denied.
  790. * @see java.lang.reflect.Constructor
  791. * @see SecurityManager#checkMemberAccess(Class, int)
  792. * @see SecurityManager#checkPackageAccess(String)
  793. * @since JDK1.1
  794. */
  795. public Constructor[] getConstructors() throws SecurityException {
  796. // be very careful not to change the stack depth of this
  797. // checkMemberAccess call for security reasons
  798. // see java.lang.SecurityManager.checkMemberAccess
  799. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  800. return copyConstructors(privateGetDeclaredConstructors(true));
  801. }
  802. /**
  803. * Returns a <code>Field</code> object that reflects the specified public
  804. * member field of the class or interface represented by this
  805. * <code>Class</code> object. The <code>name</code> parameter is a
  806. * <code>String</code> specifying the simple name of the desired field.
  807. *
  808. * <p>If there is a security manager, this method first
  809. * calls the security manager's <code>checkMemberAccess</code> method
  810. * with <code>this</code> and <code>Member.PUBLIC</code>
  811. * as its arguments. If the class is in a package, then this method
  812. * also calls the security manager's <code>checkPackageAccess</code>
  813. * method with the package name
  814. * as its argument. Either of these calls could result in a SecurityException.
  815. *
  816. * <p> The field to be reflected is determined by the algorithm that
  817. * follows. Let C be the class represented by this object:
  818. * <OL>
  819. * <LI> If C declares a public field with the name specified, that is the
  820. * field to be reflected.</LI>
  821. * <LI> If no field was found in step 1 above, this algorithm is applied
  822. * recursively to each direct superinterface of C. The direct
  823. * superinterfaces are searched in the order they were declared.</LI>
  824. * <LI> If no field was found in steps 1 and 2 above, and C has a
  825. * superclass S, then this algorithm is invoked recursively upon S.
  826. * If C has no superclass, then a <code>NoSuchFieldException</code>
  827. * is thrown.</LI>
  828. * </OL>
  829. *
  830. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  831. *
  832. * @param name the field name
  833. * @return the <code>Field</code> object of this class specified by
  834. * <code>name</code>
  835. * @exception NoSuchFieldException if a field with the specified name is
  836. * not found.
  837. * @exception NullPointerException if <code>name</code> is <code>null</code>
  838. * @exception SecurityException if access to the information is denied.
  839. * @see java.lang.reflect.Field
  840. * @see SecurityManager#checkMemberAccess(Class, int)
  841. * @see SecurityManager#checkPackageAccess(String)
  842. * @since JDK1.1
  843. */
  844. public Field getField(String name)
  845. throws NoSuchFieldException, SecurityException {
  846. // be very careful not to change the stack depth of this
  847. // checkMemberAccess call for security reasons
  848. // see java.lang.SecurityManager.checkMemberAccess
  849. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  850. Field field = getField0(name);
  851. if (field == null) {
  852. throw new NoSuchFieldException(name);
  853. }
  854. return field;
  855. }
  856. /**
  857. * Returns a <code>Method</code> object that reflects the specified public
  858. * member method of the class or interface represented by this
  859. * <code>Class</code> object. The <code>name</code> parameter is a
  860. * <code>String</code> specifying the simple name the desired method. The
  861. * <code>parameterTypes</code> parameter is an array of <code>Class</code>
  862. * objects that identify the method's formal parameter types, in declared
  863. * order. If <code>parameterTypes</code> is <code>null</code>, it is
  864. * treated as if it were an empty array.
  865. *
  866. * <p>If there is a security manager, this method first
  867. * calls the security manager's <code>checkMemberAccess</code> method
  868. * with <code>this</code> and <code>Member.PUBLIC</code>
  869. * as its arguments. If the class is in a package, then this method
  870. * also calls the security manager's <code>checkPackageAccess</code>
  871. * method with the package name
  872. * as its argument. Either of these calls could result in a SecurityException.
  873. *
  874. * <p> If the <code>name</code> is "<init>"or "<clinit>" a
  875. * <code>NoSuchMethodException</code> is raised. Otherwise, the method to
  876. * be reflected is determined by the algorithm that follows. Let C be the
  877. * class represented by this object:
  878. * <OL>
  879. * <LI> C is searched for any <I>matching methods</I>. If no matching
  880. * method is found, the algorithm of step 1 is invoked recursively on
  881. * the superclass of C.</LI>
  882. * <LI> If no method was found in step 1 above, the superinterfaces of C
  883. * are searched for a matching method. If any such method is found, it
  884. * is reflected.</LI>
  885. * </OL>
  886. *
  887. * To find a matching method in a class C:  If C declares exactly one
  888. * public method with the specified name and exactly the same formal
  889. * parameter types, that is the method reflected. If more than one such
  890. * method is found in C, and one of these methods has a return type that is
  891. * more specific than any of the others, that method is reflected;
  892. * otherwise one of the methods is chosen arbitrarily.
  893. *
  894. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.4.
  895. *
  896. * @param name the name of the method
  897. * @param parameterTypes the list of parameters
  898. * @return the <code>Method</code> object that matches the specified
  899. * <code>name</code> and <code>parameterTypes</code>
  900. * @exception NoSuchMethodException if a matching method is not found
  901. * or if the name is "<init>"or "<clinit>".
  902. * @exception NullPointerException if <code>name</code> is <code>null</code>
  903. * @exception SecurityException if access to the information is denied.
  904. * @see java.lang.reflect.Method
  905. * @see SecurityManager#checkMemberAccess(Class, int)
  906. * @see SecurityManager#checkPackageAccess(String)
  907. * @since JDK1.1
  908. */
  909. public Method getMethod(String name, Class[] parameterTypes)
  910. throws NoSuchMethodException, SecurityException {
  911. // be very careful not to change the stack depth of this
  912. // checkMemberAccess call for security reasons
  913. // see java.lang.SecurityManager.checkMemberAccess
  914. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  915. Method method = getMethod0(name, parameterTypes);
  916. if (method == null) {
  917. throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
  918. }
  919. return method;
  920. }
  921. /**
  922. * Returns a <code>Constructor</code> object that reflects the specified
  923. * public constructor of the class represented by this <code>Class</code>
  924. * object. The <code>parameterTypes</code> parameter is an array of
  925. * <code>Class</code> objects that identify the constructor's formal
  926. * parameter types, in declared order.
  927. *
  928. * <p> The constructor to reflect is the public constructor of the class
  929. * represented by this <code>Class</code> object whose formal parameter
  930. * types match those specified by <code>parameterTypes</code>.
  931. *
  932. * <p>If there is a security manager, this method first
  933. * calls the security manager's <code>checkMemberAccess</code> method
  934. * with <code>this</code> and <code>Member.PUBLIC</code>
  935. * as its arguments. If the class is in a package, then this method
  936. * also calls the security manager's <code>checkPackageAccess</code> method
  937. * with the package name as its argument. Either of these calls could
  938. * result in a SecurityException.
  939. *
  940. * @param parameterTypes the parameter array
  941. * @return the <code>Method</code> object of the public constructor that
  942. * matches the specified <code>parameterTypes</code>
  943. * @exception NoSuchMethodException if a matching method is not found.
  944. * @exception SecurityException if access to the information is denied.
  945. * @see java.lang.reflect.Constructor
  946. * @see SecurityManager#checkMemberAccess(Class, int)
  947. * @see SecurityManager#checkPackageAccess(String)
  948. * @since JDK1.1
  949. */
  950. public Constructor getConstructor(Class[] parameterTypes)
  951. throws NoSuchMethodException, SecurityException {
  952. // be very careful not to change the stack depth of this
  953. // checkMemberAccess call for security reasons
  954. // see java.lang.SecurityManager.checkMemberAccess
  955. checkMemberAccess(Member.PUBLIC, ClassLoader.getCallerClassLoader());
  956. return getConstructor0(parameterTypes, Member.PUBLIC);
  957. }
  958. /**
  959. * Returns an array of <code>Class</code> objects reflecting all the
  960. * classes and interfaces declared as members of the class represented by
  961. * this <code>Class</code> object. This includes public, protected, default
  962. * (package) access, and private classes and interfaces declared by the
  963. * class, but excludes inherited classes and interfaces. This method
  964. * returns an array of length 0 if the class declares no classes or
  965. * interfaces as members, or if this <code>Class</code> object represents a
  966. * primitive type, an array class, or void.
  967. *
  968. * <p>If there is a security manager, this method first
  969. * calls the security manager's <code>checkMemberAccess</code> method
  970. * with <code>this</code> and <code>Member.DECLARED</code>
  971. * as its arguments. If the class is in a package, then this method also
  972. * calls the security manager's <code>checkPackageAccess</code> method with
  973. * the package name as its argument. Either of these calls could result in
  974. * a SecurityException.
  975. *
  976. * @return the array of <code>Class</code> objects representing all the
  977. * declared members of this class
  978. * @exception SecurityException if access to the information is denied.
  979. * @see SecurityManager#checkMemberAccess(Class, int)
  980. * @see SecurityManager#checkPackageAccess(String)
  981. * @since JDK1.1
  982. */
  983. public Class[] getDeclaredClasses() throws SecurityException {
  984. // be very careful not to change the stack depth of this
  985. // checkMemberAccess call for security reasons
  986. // see java.lang.SecurityManager.checkMemberAccess
  987. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  988. return getDeclaredClasses0();
  989. }
  990. /**
  991. * Returns an array of <code>Field</code> objects reflecting all the fields
  992. * declared by the class or interface represented by this
  993. * <code>Class</code> object. This includes public, protected, default
  994. * (package) access, and private fields, but excludes inherited fields.
  995. * The elements in the array returned are not sorted and are not in any
  996. * particular order. This method returns an array of length 0 if the class
  997. * or interface declares no fields, or if this <code>Class</code> object
  998. * represents a primitive type, an array class, or void.
  999. *
  1000. * <p> See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  1001. *
  1002. * <p>If there is a security manager, this method first
  1003. * calls the security manager's <code>checkMemberAccess</code> method with
  1004. * <code>this</code> and <code>Member.DECLARED</code> as its arguments. If
  1005. * the class is in a package, then this method also calls the security
  1006. * manager's <code>checkPackageAccess</code> method with the package name
  1007. * as its argument. Either of these calls could result in a
  1008. * SecurityException.
  1009. *
  1010. * @return the array of <code>Field</code> objects representing all the
  1011. * declared fields of this class
  1012. * @exception SecurityException if access to the information is denied.
  1013. * @see java.lang.reflect.Field
  1014. * @see SecurityManager#checkMemberAccess(Class, int)
  1015. * @see SecurityManager#checkPackageAccess(String)
  1016. * @since JDK1.1
  1017. */
  1018. public Field[] getDeclaredFields() throws SecurityException {
  1019. // be very careful not to change the stack depth of this
  1020. // checkMemberAccess call for security reasons
  1021. // see java.lang.SecurityManager.checkMemberAccess
  1022. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1023. return copyFields(privateGetDeclaredFields(false));
  1024. }
  1025. /**
  1026. * Returns an array of <code>Method</code> objects reflecting all the
  1027. * methods declared by the class or interface represented by this
  1028. * <code>Class</code> object. This includes public, protected, default
  1029. * (package) access, and private methods, but excludes inherited methods.
  1030. * The elements in the array returned are not sorted and are not in any
  1031. * particular order. This method returns an array of length 0 if the class
  1032. * or interface declares no methods, or if this <code>Class</code> object
  1033. * represents a primitive type, an array class, or void. The class
  1034. * initialization method <code><clinit></code> is not included in the
  1035. * returned array. If the class declares multiple public member methods
  1036. * with the same parameter types, they are all included in the returned
  1037. * array.
  1038. *
  1039. * <p> See <em>The Java Language Specification</em>, section 8.2.
  1040. *
  1041. * <p>If there is a security manager, this method first
  1042. * calls the security manager's <code>checkMemberAccess</code> method
  1043. * with <code>this</code> and <code>Member.DECLARED</code>
  1044. * as its arguments. If the class is in a package, then this method
  1045. * also calls the security manager's <code>checkPackageAccess</code> method
  1046. * with the package name as its argument. Either of these calls could
  1047. * result in a SecurityException.
  1048. *
  1049. * @return the array of <code>Method</code> objects representing all the
  1050. * declared methods of this class
  1051. * @exception SecurityException if access to the information is denied.
  1052. * @see java.lang.reflect.Method
  1053. * @see SecurityManager#checkMemberAccess(Class, int)
  1054. * @see SecurityManager#checkPackageAccess(String)
  1055. * @since JDK1.1
  1056. */
  1057. public Method[] getDeclaredMethods() throws SecurityException {
  1058. // be very careful not to change the stack depth of this
  1059. // checkMemberAccess call for security reasons
  1060. // see java.lang.SecurityManager.checkMemberAccess
  1061. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1062. return copyMethods(privateGetDeclaredMethods(false));
  1063. }
  1064. /**
  1065. * Returns an array of <code>Constructor</code> objects reflecting all the
  1066. * constructors declared by the class represented by this
  1067. * <code>Class</code> object. These are public, protected, default
  1068. * (package) access, and private constructors. The elements in the array
  1069. * returned are not sorted and are not in any particular order. If the
  1070. * class has a default constructor, it is included in the returned array.
  1071. * This method returns an array of length 0 if this <code>Class</code>
  1072. * object represents an interface, a primitive type, an array class, or
  1073. * void.
  1074. *
  1075. * <p> See <em>The Java Language Specification</em>, section 8.2.
  1076. *
  1077. * <p>If there is a security manager, this method first
  1078. * calls the security manager's <code>checkMemberAccess</code> method
  1079. * with <code>this</code> and <code>Member.DECLARED</code>
  1080. * as its arguments. If the class is in a package, then this method
  1081. * also calls the security manager's <code>checkPackageAccess</code> method
  1082. * with the package name as its argument. Either of these calls could
  1083. * result in a SecurityException.
  1084. *
  1085. * @return the array of <code>Method</code> objects representing all the
  1086. * declared constructors of this class
  1087. * @exception SecurityException if access to the information is denied.
  1088. * @see java.lang.reflect.Constructor
  1089. * @see SecurityManager#checkMemberAccess(Class, int)
  1090. * @see SecurityManager#checkPackageAccess(String)
  1091. * @since JDK1.1
  1092. */
  1093. public Constructor[] getDeclaredConstructors() throws SecurityException {
  1094. // be very careful not to change the stack depth of this
  1095. // checkMemberAccess call for security reasons
  1096. // see java.lang.SecurityManager.checkMemberAccess
  1097. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1098. return copyConstructors(privateGetDeclaredConstructors(false));
  1099. }
  1100. /**
  1101. * Returns a <code>Field</code> object that reflects the specified declared
  1102. * field of the class or interface represented by this <code>Class</code>
  1103. * object. The <code>name</code> parameter is a <code>String</code> that
  1104. * specifies the simple name of the desired field. Note that this method
  1105. * will not reflect the <code>length</code> field of an array class.
  1106. *
  1107. * <p>If there is a security manager, this method first
  1108. * calls the security manager's <code>checkMemberAccess</code> method
  1109. * with <code>this</code> and <code>Member.DECLARED</code>
  1110. * as its arguments. If the class is in a package, then this method
  1111. * also calls the security manager's <code>checkPackageAccess</code> method
  1112. * with the package name as its argument. Either of these calls could
  1113. * result in a SecurityException.
  1114. *
  1115. * @param name the name of the field
  1116. * @return the <code>Field</code> object for the specified field in this
  1117. * class
  1118. * @exception NoSuchFieldException if a field with the specified name is
  1119. * not found.
  1120. * @exception NullPointerException if <code>name</code> is <code>null</code>
  1121. * @exception SecurityException if access to the information is denied.
  1122. * @see java.lang.reflect.Field
  1123. * @see SecurityManager#checkMemberAccess(Class, int)
  1124. * @see SecurityManager#checkPackageAccess(String)
  1125. * @since JDK1.1
  1126. */
  1127. public Field getDeclaredField(String name)
  1128. throws NoSuchFieldException, SecurityException {
  1129. // be very careful not to change the stack depth of this
  1130. // checkMemberAccess call for security reasons
  1131. // see java.lang.SecurityManager.checkMemberAccess
  1132. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1133. Field field = searchFields(privateGetDeclaredFields(false), name);
  1134. if (field == null) {
  1135. throw new NoSuchFieldException(name);
  1136. }
  1137. return field;
  1138. }
  1139. /**
  1140. * Returns a <code>Method</code> object that reflects the specified
  1141. * declared method of the class or interface represented by this
  1142. * <code>Class</code> object. The <code>name</code> parameter is a
  1143. * <code>String</code> that specifies the simple name of the desired
  1144. * method, and the <code>parameterTypes</code> parameter is an array of
  1145. * <code>Class</code> objects that identify the method's formal parameter
  1146. * types, in declared order. If more than one method with the same
  1147. * parameter types is declared in a class, and one of these methods has a
  1148. * return type that is more specific than any of the others, that method is
  1149. * returned; otherwise one of the methods is chosen arbitrarily. If the
  1150. * name is "<init>"or "<clinit>" a <code>NoSuchMethodException</code>
  1151. * is raised.
  1152. *
  1153. * <p>If there is a security manager, this method first
  1154. * calls the security manager's <code>checkMemberAccess</code> method
  1155. * with <code>this</code> and <code>Member.DECLARED</code>
  1156. * as its arguments. If the class is in a package, then this method also
  1157. * calls the security manager's <code>checkPackageAccess</code> method with
  1158. * the package name as its argument. Either of these calls could result in
  1159. * a SecurityException.
  1160. *
  1161. * @param name the name of the method
  1162. * @param parameterTypes the parameter array
  1163. * @return the <code>Method</code> object for the method of this class
  1164. * matching the specified name and parameters
  1165. * @exception NoSuchMethodException if a matching method is not found.
  1166. * @exception NullPointerException if <code>name</code> is <code>null</code>
  1167. * @exception SecurityException if access to the information is denied.
  1168. * @see java.lang.reflect.Method
  1169. * @see SecurityManager#checkMemberAccess(Class, int)
  1170. * @see SecurityManager#checkPackageAccess(String)
  1171. * @since JDK1.1
  1172. */
  1173. public Method getDeclaredMethod(String name, Class[] parameterTypes)
  1174. throws NoSuchMethodException, SecurityException {
  1175. // be very careful not to change the stack depth of this
  1176. // checkMemberAccess call for security reasons
  1177. // see java.lang.SecurityManager.checkMemberAccess
  1178. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1179. Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
  1180. if (method == null) {
  1181. throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
  1182. }
  1183. return method;
  1184. }
  1185. /**
  1186. * Returns a <code>Constructor</code> object that reflects the specified
  1187. * constructor of the class or interface represented by this
  1188. * <code>Class</code> object. The <code>parameterTypes</code> parameter is
  1189. * an array of <code>Class</code> objects that identify the constructor's
  1190. * formal parameter types, in declared order.
  1191. *
  1192. * <p>If there is a security manager, this method first
  1193. * calls the security manager's <code>checkMemberAccess</code> method
  1194. * with <code>this</code> and <code>Member.DECLARED</code>
  1195. * as its arguments. If the class is in a package, then this method
  1196. * also calls the security manager's <code>checkPackageAccess</code>
  1197. * method with the package name
  1198. * as its argument. Either of these calls could result in a SecurityException.
  1199. *
  1200. * @param parameterTypes the parameter array
  1201. * @return The <code>Method</code> object for the constructor with the
  1202. * specified parameter list
  1203. * @exception NoSuchMethodException if a matching method is not found.
  1204. * @exception SecurityException if access to the information is denied.
  1205. * @see java.lang.reflect.Constructor
  1206. * @see SecurityManager#checkMemberAccess(Class, int)
  1207. * @see SecurityManager#checkPackageAccess(String)
  1208. * @since JDK1.1
  1209. */
  1210. public Constructor getDeclaredConstructor(Class[] parameterTypes)
  1211. throws NoSuchMethodException, SecurityException {
  1212. // be very careful not to change the stack depth of this
  1213. // checkMemberAccess call for security reasons
  1214. // see java.lang.SecurityManager.checkMemberAccess
  1215. checkMemberAccess(Member.DECLARED, ClassLoader.getCallerClassLoader());
  1216. return getConstructor0(parameterTypes, Member.DECLARED);
  1217. }
  1218. /**
  1219. * Finds a resource with a given name. This method returns null if no
  1220. * resource with this name is found. The rules for searching
  1221. * resources associated with a given class are implemented by the
  1222. * defining class loader of the class.
  1223. *
  1224. * <p> This method delegates the call to its class loader, after making
  1225. * these changes to the resource name: if the resource name starts with
  1226. * "/", it is unchanged; otherwise, the package name is prepended to the
  1227. * resource name after converting "." to "/". If this object was loaded by
  1228. * the bootstrap loader, the call is delegated to
  1229. * <code>ClassLoader.getSystemResourceAsStream</code>.
  1230. *
  1231. * @param name name of the desired resource
  1232. * @return a <code>java.io.InputStream</code> object.
  1233. * @throws NullPointerException if <code>name</code> is <code>null</code>.
  1234. * @see java.lang.ClassLoader
  1235. * @since JDK1.1
  1236. */
  1237. public InputStream getResourceAsStream(String name) {
  1238. name = resolveName(name);
  1239. ClassLoader cl = getClassLoader0();
  1240. if (cl==null) {
  1241. // A system class.
  1242. return ClassLoader.getSystemResourceAsStream(name);
  1243. }
  1244. return cl.getResourceAsStream(name);
  1245. }
  1246. /**
  1247. * Finds a resource with a given name. This method returns null if no
  1248. * resource with this name is found. The rules for searching resources
  1249. * associated with a given class are implemented by the * defining class
  1250. * loader of the class.
  1251. *
  1252. * <p> This method delegates the call to its class loader, after making
  1253. * these changes to the resource name: if the resource name starts with
  1254. * "/", it is unchanged; otherwise, the package name is prepended to the
  1255. * resource name after converting "." to "/". If this object was loaded by
  1256. * the bootstrap loader, the call is delegated to
  1257. * <code>ClassLoader.getSystemResource</code>.
  1258. *
  1259. * @param name name of the desired resource
  1260. * @return a <code>java.net.URL</code> object.
  1261. * @see java.lang.ClassLoader
  1262. * @since JDK1.1
  1263. */
  1264. public java.net.URL getResource(String name) {
  1265. name = resolveName(name);
  1266. ClassLoader cl = getClassLoader0();
  1267. if (cl==null) {
  1268. // A system class.
  1269. return ClassLoader.getSystemResource(name);
  1270. }
  1271. return cl.getResource(name);
  1272. }
  1273. /** protection domain returned when the internal domain is null */
  1274. private static java.security.ProtectionDomain allPermDomain;
  1275. /**
  1276. * Returns the <code>ProtectionDomain</code> of this class. If there is a
  1277. * security manager installed, this method first calls the security
  1278. * manager's <code>checkPermission</code> method with a
  1279. * <code>RuntimePermission("getProtectionDomain")</code> permission to
  1280. * ensure it's ok to get the
  1281. * <code>ProtectionDomain</code>.
  1282. *
  1283. * @return the ProtectionDomain of this class
  1284. *
  1285. * @throws SecurityException
  1286. * if a security manager exists and its
  1287. * <code>checkPermission</code> method doesn't allow
  1288. * getting the ProtectionDomain.
  1289. *
  1290. * @see java.security.ProtectionDomain
  1291. * @see SecurityManager#checkPermission
  1292. * @see java.lang.RuntimePermission
  1293. * @since 1.2
  1294. */
  1295. public java.security.ProtectionDomain getProtectionDomain() {
  1296. SecurityManager sm = System.getSecurityManager();
  1297. if (sm != null) {
  1298. sm.checkPermission(SecurityConstants.GET_PD_PERMISSION);
  1299. }
  1300. java.security.ProtectionDomain pd = getProtectionDomain0();
  1301. if (pd == null) {
  1302. if (allPermDomain == null) {
  1303. java.security.Permissions perms =
  1304. new java.security.Permissions();
  1305. perms.add(SecurityConstants.ALL_PERMISSION);
  1306. allPermDomain =
  1307. new java.security.ProtectionDomain(null, perms);
  1308. }
  1309. pd = allPermDomain;
  1310. }
  1311. return pd;
  1312. }
  1313. /**
  1314. * Returns the ProtectionDomain of this class.
  1315. */
  1316. private native java.security.ProtectionDomain getProtectionDomain0();
  1317. /**
  1318. * Set the ProtectionDomain for this class. Called by
  1319. * ClassLoader.defineClass.
  1320. */
  1321. native void setProtectionDomain0(java.security.ProtectionDomain pd);
  1322. /*
  1323. * Return the Virtual Machine's Class object for the named
  1324. * primitive type.
  1325. */
  1326. static native Class getPrimitiveClass(String name);
  1327. /*
  1328. * Check if client is allowed to access members. If access is denied,
  1329. * throw a SecurityException.
  1330. *
  1331. * Be very careful not to change the stack depth of this checkMemberAccess
  1332. * call for security reasons reasons see
  1333. * java.lang.SecurityManager.checkMemberAccess
  1334. *
  1335. * <p> Default policy: allow all clients access with normal Java access
  1336. * control.
  1337. */
  1338. private void checkMemberAccess(int which, ClassLoader ccl) {
  1339. SecurityManager s = System.getSecurityManager();
  1340. if (s != null) {
  1341. s.checkMemberAccess(this, which);
  1342. ClassLoader cl = getClassLoader0();
  1343. if ((ccl != null) && (ccl != cl) &&
  1344. ((cl == null) || !cl.isAncestor(ccl))) {
  1345. String name = this.getName();
  1346. int i = name.lastIndexOf('.');
  1347. if (i != -1) {
  1348. s.checkPackageAccess(name.substring(0, i));
  1349. }
  1350. }
  1351. }
  1352. }
  1353. /**
  1354. * Add a package name prefix if the name is not absolute Remove leading "/"
  1355. * if name is absolute
  1356. */
  1357. private String resolveName(String name) {
  1358. if (name == null) {
  1359. return name;
  1360. }
  1361. if (!name.startsWith("/")) {
  1362. Class c = this;
  1363. while (c.isArray()) {
  1364. c = c.getComponentType();
  1365. }
  1366. String baseName = c.getName();
  1367. int index = baseName.lastIndexOf('.');
  1368. if (index != -1) {
  1369. name = baseName.substring(0, index).replace('.', '/')
  1370. +"/"+name;
  1371. }
  1372. } else {
  1373. name = name.substring(1);
  1374. }
  1375. return name;
  1376. }
  1377. /**
  1378. * Reflection support.
  1379. */
  1380. // Caches for certain reflective results
  1381. private static boolean useCaches = true;
  1382. private volatile transient SoftReference declaredFields;
  1383. private volatile transient SoftReference publicFields;
  1384. private volatile transient SoftReference declaredMethods;
  1385. private volatile transient SoftReference publicMethods;
  1386. private volatile transient SoftReference declaredConstructors;
  1387. private volatile transient SoftReference publicConstructors;
  1388. // Intermediate results for getFields and getMethods
  1389. private volatile transient SoftReference declaredPublicFields;
  1390. private volatile transient SoftReference declaredPublicMethods;
  1391. //
  1392. //
  1393. // java.lang.reflect.Field handling
  1394. //
  1395. //
  1396. // Returns an array of "root" fields. These Field objects must NOT
  1397. // be propagated to the outside world, but must instead be copied
  1398. // via ReflectionFactory.copyField.
  1399. private Field[] privateGetDeclaredFields(boolean publicOnly) {
  1400. checkInitted();
  1401. Field[] res = null;
  1402. if (useCaches) {
  1403. if (publicOnly) {
  1404. if (declaredPublicFields != null) {
  1405. res = (Field[]) declaredPublicFields.get();
  1406. }
  1407. } else {
  1408. if (declaredFields != null) {
  1409. res = (Field[]) declaredFields.get();
  1410. }
  1411. }
  1412. if (res != null) return res;
  1413. }
  1414. // No cached value available; request value from VM
  1415. res = getDeclaredFields0(publicOnly);
  1416. if (useCaches) {
  1417. if (publicOnly) {
  1418. declaredPublicFields = new SoftReference(res);
  1419. } else {
  1420. declaredFields = new SoftReference(res);
  1421. }
  1422. }
  1423. return res;
  1424. }
  1425. // Returns an array of "root" fields. These Field objects must NOT
  1426. // be propagated to the outside world, but must instead be copied
  1427. // via ReflectionFactory.copyField.
  1428. private Field[] privateGetPublicFields(Set traversedInterfaces) {
  1429. checkInitted();
  1430. Field[] res = null;
  1431. if (useCaches) {
  1432. if (publicFields != null) {
  1433. res = (Field[]) publicFields.get();
  1434. }
  1435. if (res != null) return res;
  1436. }
  1437. // No cached value available; compute value recursively.
  1438. // Traverse in correct order for getField().
  1439. List fields = new ArrayList();
  1440. if (traversedInterfaces == null) {
  1441. traversedInterfaces = new HashSet();
  1442. }
  1443. // Local fields
  1444. Field[] tmp = privateGetDeclaredFields(true);
  1445. addAll(fields, tmp);
  1446. // Direct superinterfaces, recursively
  1447. Class[] interfaces = getInterfaces();
  1448. for (int i = 0; i < interfaces.length; i++) {
  1449. Class c = interfaces[i];
  1450. if (!traversedInterfaces.contains(c)) {
  1451. traversedInterfaces.add(c);
  1452. addAll(fields, c.privateGetPublicFields(traversedInterfaces));
  1453. }
  1454. }
  1455. // Direct superclass, recursively
  1456. if (!isInterface()) {
  1457. Class c = getSuperclass();
  1458. if (c != null) {
  1459. addAll(fields, c.privateGetPublicFields(traversedInterfaces));
  1460. }
  1461. }
  1462. res = new Field[fields.size()];
  1463. fields.toArray(res);
  1464. if (useCaches) {
  1465. publicFields = new SoftReference(res);
  1466. }
  1467. return res;
  1468. }
  1469. private static void addAll(Collection c, Field[] o) {
  1470. for (int i = 0; i < o.length; i++) {
  1471. c.add(o[i]);
  1472. }
  1473. }
  1474. //
  1475. //
  1476. // java.lang.reflect.Constructor handling
  1477. //
  1478. //
  1479. // Returns an array of "root" constructors. These Constructor
  1480. // objects must NOT be propagated to the outside world, but must
  1481. // instead be copied via ReflectionFactory.copyConstructor.
  1482. private Constructor[] privateGetDeclaredConstructors(boolean publicOnly) {
  1483. checkInitted();
  1484. Constructor[] res = null;
  1485. if (useCaches) {
  1486. if (publicOnly) {
  1487. if (publicConstructors != null) {
  1488. res = (Constructor[]) publicConstructors.get();
  1489. }
  1490. } else {
  1491. if (declaredConstructors != null) {
  1492. res = (Constructor[]) declaredConstructors.get();
  1493. }
  1494. }
  1495. if (res != null) return res;
  1496. }
  1497. // No cached value available; request value from VM
  1498. if (isInterface()) {
  1499. res = new Constructor[0];
  1500. } else {
  1501. res = getDeclaredConstructors0(publicOnly);
  1502. }
  1503. if (useCaches) {
  1504. if (publicOnly) {
  1505. publicConstructors = new SoftReference(res);
  1506. } else {
  1507. declaredConstructors = new SoftReference(res);
  1508. }
  1509. }
  1510. return res;
  1511. }
  1512. //
  1513. //
  1514. // java.lang.reflect.Method handling
  1515. //
  1516. //
  1517. // Returns an array of "root" methods. These Method objects must NOT
  1518. // be propagated to the outside world, but must instead be copied
  1519. // via ReflectionFactory.copyMethod.
  1520. private Method[] privateGetDeclaredMethods(boolean publicOnly) {
  1521. checkInitted();
  1522. Method[] res = null;
  1523. if (useCaches) {
  1524. if (publicOnly) {
  1525. if (declaredPublicMethods != null) {
  1526. res = (Method[]) declaredPublicMethods.get();
  1527. }
  1528. } else {
  1529. if (declaredMethods != null) {
  1530. res = (Method[]) declaredMethods.get();
  1531. }
  1532. }
  1533. if (res != null) return res;
  1534. }
  1535. // No cached value available; request value from VM
  1536. res = getDeclaredMethods0(publicOnly);
  1537. if (useCaches) {
  1538. if (publicOnly) {
  1539. declaredPublicMethods = new SoftReference(res);
  1540. } else {
  1541. declaredMethods = new SoftReference(res);
  1542. }
  1543. }
  1544. return res;
  1545. }
  1546. static class MethodArray {
  1547. private Method[] methods;
  1548. private int length;
  1549. MethodArray() {
  1550. methods = new Method[20];
  1551. length = 0;
  1552. }
  1553. void add(Method m) {
  1554. if (length == methods.length) {
  1555. Method[] newMethods = new Method[2 * methods.length];
  1556. System.arraycopy(methods, 0, newMethods, 0, methods.length);
  1557. methods = newMethods;
  1558. }
  1559. methods[length++] = m;
  1560. }
  1561. void addAll(Method[] ma) {
  1562. for (int i = 0; i < ma.length; i++) {
  1563. add(ma[i]);
  1564. }
  1565. }
  1566. void addAll(MethodArray ma) {
  1567. for (int i = 0; i < ma.length(); i++) {
  1568. add(ma.get(i));
  1569. }
  1570. }
  1571. void addIfNotPresent(Method newMethod) {
  1572. for (int i = 0; i < length; i++) {
  1573. Method m = methods[i];
  1574. if (m == newMethod || (m != null && m.equals(newMethod))) {
  1575. return;
  1576. }
  1577. }
  1578. add(newMethod);
  1579. }
  1580. void addAllIfNotPresent(MethodArray newMethods) {
  1581. for (int i = 0; i < newMethods.length(); i++) {
  1582. Method m = newMethods.get(i);
  1583. if (m != null) {
  1584. addIfNotPresent(m);
  1585. }
  1586. }
  1587. }
  1588. int length() {
  1589. return length;
  1590. }
  1591. Method get(int i) {
  1592. return methods[i];
  1593. }
  1594. void removeByNameAndSignature(Method toRemove) {
  1595. for (int i = 0; i < length; i++) {
  1596. Method m = methods[i];
  1597. if (m != null &&
  1598. m.getReturnType() == toRemove.getReturnType() &&
  1599. m.getName() == toRemove.getName() &&
  1600. arrayContentsEq(m.getParameterTypes(),
  1601. toRemove.getParameterTypes())) {
  1602. methods[i] = null;
  1603. }
  1604. }
  1605. }
  1606. void compactAndTrim() {
  1607. int newPos = 0;
  1608. // Get rid of null slots
  1609. for (int pos = 0; pos < length; pos++) {
  1610. Method m = methods[pos];
  1611. if (m != null) {
  1612. if (pos != newPos) {
  1613. methods[newPos] = m;
  1614. }
  1615. newPos++;
  1616. }
  1617. }
  1618. if (newPos != methods.length) {
  1619. Method[] newMethods = new Method[newPos];
  1620. System.arraycopy(methods, 0, newMethods, 0, newPos);
  1621. methods = newMethods;
  1622. }
  1623. }
  1624. Method[] getArray() {
  1625. return methods;
  1626. }
  1627. }
  1628. // Returns an array of "root" methods. These Method objects must NOT
  1629. // be propagated to the outside world, but must instead be copied
  1630. // via ReflectionFactory.copyMethod.
  1631. private Method[] privateGetPublicMethods() {
  1632. checkInitted();
  1633. Method[] res = null;
  1634. if (useCaches) {
  1635. if (publicMethods != null) {
  1636. res = (Method[]) publicMethods.get();
  1637. }
  1638. if (res != null) return res;
  1639. }
  1640. // No cached value available; compute value recursively.
  1641. // Start by fetching public declared methods
  1642. MethodArray methods = new MethodArray();
  1643. {
  1644. Method[] tmp = privateGetDeclaredMethods(true);
  1645. methods.addAll(tmp);
  1646. }
  1647. // Now recur over superclass and direct superinterfaces.
  1648. // Go over superinterfaces first so we can more easily filter
  1649. // out concrete implementations inherited from superclasses at
  1650. // the end.
  1651. MethodArray inheritedMethods = new MethodArray();
  1652. Class[] interfaces = getInterfaces();
  1653. for (int i = 0; i < interfaces.length; i++) {
  1654. inheritedMethods.addAll(interfaces[i].privateGetPublicMethods());
  1655. }
  1656. if (!isInterface()) {
  1657. Class c = getSuperclass();
  1658. if (c != null) {
  1659. MethodArray supers = new MethodArray();
  1660. supers.addAll(c.privateGetPublicMethods());
  1661. // Filter out concrete implementations of any
  1662. // interface methods
  1663. for (int i = 0; i < supers.length(); i++) {
  1664. Method m = supers.get(i);
  1665. if (m != null && !Modifier.isAbstract(m.getModifiers())) {
  1666. inheritedMethods.removeByNameAndSignature(m);
  1667. }
  1668. }
  1669. // Insert superclass's inherited methods before
  1670. // superinterfaces' to satisfy getMethod's search
  1671. // order
  1672. supers.addAll(inheritedMethods);
  1673. inheritedMethods = supers;
  1674. }
  1675. }
  1676. // Filter out all local methods from inherited ones
  1677. for (int i = 0; i < methods.length(); i++) {
  1678. Method m = methods.get(i);
  1679. inheritedMethods.removeByNameAndSignature(m);
  1680. }
  1681. methods.addAllIfNotPresent(inheritedMethods);
  1682. methods.compactAndTrim();
  1683. res = methods.getArray();
  1684. if (useCaches) {
  1685. publicMethods = new SoftReference(res);
  1686. }
  1687. return res;
  1688. }
  1689. //
  1690. // Helpers for fetchers of one field, method, or constructor
  1691. //
  1692. private Field searchFields(Field[] fields, String name) {
  1693. String internedName = name.intern();
  1694. for (int i = 0; i < fields.length; i++) {
  1695. if (fields[i].getName() == internedName) {
  1696. return getReflectionFactory().copyField(fields[i]);
  1697. }
  1698. }
  1699. return null;
  1700. }
  1701. private Field getField0(String name) throws NoSuchFieldException {
  1702. // Note: the intent is that the search algorithm this routine
  1703. // uses be equivalent to the ordering imposed by
  1704. // privateGetPublicFields(). It fetches only the declared
  1705. // public fields for each class, however, to reduce the number
  1706. // of Field objects which have to be created for the common
  1707. // case where the field being requested is declared in the
  1708. // class which is being queried.
  1709. Field res = null;
  1710. // Search declared public fields
  1711. if ((res = searchFields(privateGetDeclaredFields(true), name)) != null) {
  1712. return res;
  1713. }
  1714. // Direct superinterfaces, recursively
  1715. Class[] interfaces = getInterfaces();
  1716. for (int i = 0; i < interfaces.length; i++) {
  1717. Class c = interfaces[i];
  1718. if ((res = c.getField0(name)) != null) {
  1719. return res;
  1720. }
  1721. }
  1722. // Direct superclass, recursively
  1723. if (!isInterface()) {
  1724. Class c = getSuperclass();
  1725. if (c != null) {
  1726. if ((res = c.getField0(name)) != null) {
  1727. return res;
  1728. }
  1729. }
  1730. }
  1731. return null;
  1732. }
  1733. private static Method searchMethods(Method[] methods,
  1734. String name,
  1735. Class[] parameterTypes)
  1736. {
  1737. Method res = null;
  1738. String internedName = name.intern();
  1739. for (int i = 0; i < methods.length; i++) {
  1740. Method m = methods[i];
  1741. if (m.getName() == internedName
  1742. && arrayContentsEq(parameterTypes, m.getParameterTypes())
  1743. && (res == null
  1744. || res.getReturnType().isAssignableFrom(m.getReturnType())))
  1745. res = m;
  1746. }
  1747. return (res == null ? res : getReflectionFactory().copyMethod(res));
  1748. }
  1749. private Method getMethod0(String name, Class[] parameterTypes) {
  1750. // Note: the intent is that the search algorithm this routine
  1751. // uses be equivalent to the ordering imposed by
  1752. // privateGetPublicMethods(). It fetches only the declared
  1753. // public methods for each class, however, to reduce the
  1754. // number of Method objects which have to be created for the
  1755. // common case where the method being requested is declared in
  1756. // the class which is being queried.
  1757. Method res = null;
  1758. // Search declared public methods
  1759. if ((res = searchMethods(privateGetDeclaredMethods(true),
  1760. name,
  1761. parameterTypes)) != null) {
  1762. return res;
  1763. }
  1764. // Search superclass's methods
  1765. if (!isInterface()) {
  1766. Class c = getSuperclass();
  1767. if (c != null) {
  1768. if ((res = c.getMethod0(name, parameterTypes)) != null) {
  1769. return res;
  1770. }
  1771. }
  1772. }
  1773. // Search superinterfaces' methods
  1774. Class[] interfaces = getInterfaces();
  1775. for (int i = 0; i < interfaces.length; i++) {
  1776. Class c = interfaces[i];
  1777. if ((res = c.getMethod0(name, parameterTypes)) != null) {
  1778. return res;
  1779. }
  1780. }
  1781. // Not found
  1782. return null;
  1783. }
  1784. private Constructor getConstructor0(Class[] parameterTypes,
  1785. int which) throws NoSuchMethodException
  1786. {
  1787. Constructor[] constructors = privateGetDeclaredConstructors((which == Member.PUBLIC));
  1788. for (int i = 0; i < constructors.length; i++) {
  1789. if (arrayContentsEq(parameterTypes,
  1790. constructors[i].getParameterTypes())) {
  1791. return getReflectionFactory().copyConstructor(constructors[i]);
  1792. }
  1793. }
  1794. throw new NoSuchMethodException(getName() + ".<init>" + argumentTypesToString(parameterTypes));
  1795. }
  1796. //
  1797. // Other helpers and base implementation
  1798. //
  1799. private static boolean arrayContentsEq(Object[] a1, Object[] a2) {
  1800. if (a1 == null) {
  1801. return a2 == null || a2.length == 0;
  1802. }
  1803. if (a2 == null) {
  1804. return a1.length == 0;
  1805. }
  1806. if (a1.length != a2.length) {
  1807. return false;
  1808. }
  1809. for (int i = 0; i < a1.length; i++) {
  1810. if (a1[i] != a2[i]) {
  1811. return false;
  1812. }
  1813. }
  1814. return true;
  1815. }
  1816. private static Field[] copyFields(Field[] arg) {
  1817. Field[] out = new Field[arg.length];
  1818. ReflectionFactory fact = getReflectionFactory();
  1819. for (int i = 0; i < arg.length; i++) {
  1820. out[i] = fact.copyField(arg[i]);
  1821. }
  1822. return out;
  1823. }
  1824. private static Method[] copyMethods(Method[] arg) {
  1825. Method[] out = new Method[arg.length];
  1826. ReflectionFactory fact = getReflectionFactory();
  1827. for (int i = 0; i < arg.length; i++) {
  1828. out[i] = fact.copyMethod(arg[i]);
  1829. }
  1830. return out;
  1831. }
  1832. private static Constructor[] copyConstructors(Constructor[] arg) {
  1833. Constructor[] out = new Constructor[arg.length];
  1834. ReflectionFactory fact = getReflectionFactory();
  1835. for (int i = 0; i < arg.length; i++) {
  1836. out[i] = fact.copyConstructor(arg[i]);
  1837. }
  1838. return out;
  1839. }
  1840. private native Field[] getDeclaredFields0(boolean publicOnly);
  1841. private native Method[] getDeclaredMethods0(boolean publicOnly);
  1842. private native Constructor[] getDeclaredConstructors0(boolean publicOnly);
  1843. private native Class[] getDeclaredClasses0();
  1844. private static String argumentTypesToString(Class[] argTypes) {
  1845. StringBuffer buf = new StringBuffer();
  1846. buf.append("(");
  1847. if (argTypes != null) {
  1848. for (int i = 0; i < argTypes.length; i++) {
  1849. if (i > 0) {
  1850. buf.append(", ");
  1851. }
  1852. Class c = argTypes[i];
  1853. buf.append((c == null) ? "null" : c.getName());
  1854. }
  1855. }
  1856. buf.append(")");
  1857. return buf.toString();
  1858. }
  1859. /** use serialVersionUID from JDK 1.1 for interoperability */
  1860. private static final long serialVersionUID = 3206093459760846163L;
  1861. /**
  1862. * Class Class is special cased within the Serialization Stream Protocol.
  1863. *
  1864. * A Class instance is written intially into an ObjectOutputStream in the
  1865. * following format:
  1866. * <pre>
  1867. * <code>TC_CLASS</code> ClassDescriptor
  1868. * A ClassDescriptor is a special cased serialization of
  1869. * a <code>java.io.ObjectStreamClass</code> instance.
  1870. * </pre>
  1871. * A new handle is generated for the initial time the class descriptor
  1872. * is written into the stream. Future references to the class descriptor
  1873. * are written as references to the initial class descriptor instance.
  1874. *
  1875. * @see java.io.ObjectStreamClass
  1876. */
  1877. private static final ObjectStreamField[] serialPersistentFields =
  1878. ObjectStreamClass.NO_FIELDS;
  1879. /**
  1880. * Returns the assertion status that would be assigned to this
  1881. * class if it were to be initialized at the time this method is invoked.
  1882. * If this class has had its assertion status set, the most recent
  1883. * setting will be returned; otherwise, if any package default assertion
  1884. * status pertains to this class, the most recent setting for the most
  1885. * specific pertinent package default assertion status is returned;
  1886. * otherwise, if this class is not a system class (i.e., it has a
  1887. * class loader) its class loader's default assertion status is returned;
  1888. * otherwise, the system class default assertion status is returned.
  1889. * <p>
  1890. * Few programmers will have any need for this method; it is provided
  1891. * for the benefit of the JRE itself. (It allows a class to determine at
  1892. * the time that it is initialized whether assertions should be enabled.)
  1893. * Note that this method is not guaranteed to return the actual
  1894. * assertion status that was (or will be) associated with the specified
  1895. * class when it was (or will be) initialized.
  1896. *
  1897. * @return the desired assertion status of the specified class.
  1898. * @see java.lang.ClassLoader#setClassAssertionStatus
  1899. * @see java.lang.ClassLoader#setPackageAssertionStatus
  1900. * @see java.lang.ClassLoader#setDefaultAssertionStatus
  1901. * @since 1.4
  1902. */
  1903. public boolean desiredAssertionStatus() {
  1904. ClassLoader loader = getClassLoader();
  1905. // If the loader is null this is a system class, so ask the VM
  1906. if (loader == null)
  1907. return desiredAssertionStatus0(this);
  1908. synchronized(loader) {
  1909. // If the classloader has been initialized with
  1910. // the assertion directives, ask it. Otherwise,
  1911. // ask the VM.
  1912. return (loader.classAssertionStatus == null ?
  1913. desiredAssertionStatus0(this) :
  1914. loader.desiredAssertionStatus(getName()));
  1915. }
  1916. }
  1917. // Retrieves the desired assertion status of this class from the VM
  1918. private static native boolean desiredAssertionStatus0(Class clazz);
  1919. // Fetches the factory for reflective objects
  1920. private static ReflectionFactory getReflectionFactory() {
  1921. if (reflectionFactory == null) {
  1922. reflectionFactory = (ReflectionFactory)
  1923. java.security.AccessController.doPrivileged
  1924. (new sun.reflect.ReflectionFactory.GetReflectionFactoryAction());
  1925. }
  1926. return reflectionFactory;
  1927. }
  1928. private static ReflectionFactory reflectionFactory;
  1929. // To be able to query system properties as soon as they're available
  1930. private static boolean initted = false;
  1931. private static void checkInitted() {
  1932. if (initted) return;
  1933. AccessController.doPrivileged(new PrivilegedAction() {
  1934. public Object run() {
  1935. // Tests to ensure the system properties table is fully
  1936. // initialized. This is needed because reflection code is
  1937. // called very early in the initialization process (before
  1938. // command-line arguments have been parsed and therefore
  1939. // these user-settable properties installed.) We assume that
  1940. // if System.out is non-null then the System class has been
  1941. // fully initialized and that the bulk of the startup code
  1942. // has been run.
  1943. if (System.out == null) {
  1944. // java.lang.System not yet fully initialized
  1945. return null;
  1946. }
  1947. String val =
  1948. System.getProperty("sun.reflect.noCaches");
  1949. if (val != null && val.equals("true")) {
  1950. useCaches = false;
  1951. }
  1952. initted = true;
  1953. return null;
  1954. }
  1955. });
  1956. }
  1957. }