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 "/".