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