home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / Class.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  30.9 KB  |  824 lines

  1. /*
  2.  * @(#)Class.java    1.66 98/03/18
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16. import java.lang.reflect.Member;
  17. import java.lang.reflect.Field;
  18. import java.lang.reflect.Method;
  19. import java.lang.reflect.Constructor;
  20. import java.io.InputStream;
  21.  
  22. /**
  23.  * Instances of the class <code>Class</code> represent classes and 
  24.  * interfaces in a running Java application.
  25.  * Every array also belongs to a class that is reflected as a Class
  26.  * object that is shared by all arrays with the same element type and
  27.  * number of dimensions.  Finally, the other primitive Java types
  28.  * (boolean, byte, char, short, int, long, float, and double) and
  29.  * the keyword void are also represented as Class objects.
  30.  * <p>
  31.  * There is no public constructor for the class <code>Class</code>. 
  32.  * <code>Class</code> objects are constructed automatically by the Java 
  33.  * Virtual Machine as classes are loaded and by calls to the 
  34.  * <code>defineClass</code> method in the class loader. 
  35.  * <p>
  36.  * The following example uses a Class object to print the Class name
  37.  * of an object:
  38.  * <p><blockquote><pre>
  39.  *     void printClassName(Object obj) {
  40.  *         System.out.println("The class of " + obj +
  41.  *                            " is " + obj.getClass().getName());
  42.  *     }
  43.  * </pre></blockquote>
  44.  *
  45.  * @author  unascribed
  46.  * @version 1.66, 03/18/98
  47.  * @see     java.lang.ClassLoader#defineClass(byte[], int, int)
  48.  * @since   JDK1.0
  49.  */
  50. public final
  51. class Class implements java.io.Serializable {
  52.  
  53.     private static native void registerNatives();
  54.     static {
  55.         registerNatives();
  56.     }
  57.  
  58.     /*
  59.      * Constructor. Only the Java Virtual Machine creates Class
  60.      * objects.
  61.      */
  62.     private Class() {}
  63.  
  64.     /**
  65.      * Converts the object to a string. The string representation is the 
  66.      * string <code>"class"</code> or <code>"interface"</code> followed 
  67.      * by a space and then the fully qualified name of the class. 
  68.      * If this Class object represents a primitive type,
  69.      * returns the name of the primitive type.
  70.      *
  71.      * @return  a string representation of this class object. 
  72.      */
  73.     public String toString() {
  74.     return (isInterface() ? "interface " : (isPrimitive() ? "" : "class "))
  75.         + getName();
  76.     }
  77.  
  78.     /**
  79.      * Returns the <code>Class</code> object associated with the class 
  80.      * with the given string name. 
  81.      * Given the fully qualified name for a class or interface, in a 
  82.      * form as returned by <a href="#getName"><tt>getName</tt></a>, this
  83.      * method attempts to locate, load and link the class.  If it
  84.      * succeeds, returns the Class object representing the class.  If
  85.      * it fails, the method throws a ClassNotFoundException.
  86.      * <p>
  87.      * For example, the following code fragment returns the runtime 
  88.      * <code>Class</code> descriptor for the class named 
  89.      * <code>java.lang.Thread</code>: 
  90.      * <blockquote><pre>
  91.      *   Class t = Class.forName("java.lang.Thread")
  92.      * </pre></blockquote>
  93.      *
  94.      * @param      className   the fully qualified name of the desired class.
  95.      * @return     the <code>Class</code> descriptor for the class with the
  96.      *             specified name.
  97.      * @exception  ClassNotFoundException  if the class could not be found.
  98.      */
  99.     public static native Class forName(String className)
  100.     throws ClassNotFoundException;
  101.  
  102.     /**
  103.      * Creates a new instance of a class. 
  104.      *
  105.      * @return     a newly allocated instance of the class represented by this
  106.      *             object. This is done exactly as if by a <code>new</code>
  107.      *             expression with an empty argument list.
  108.      * @exception  IllegalAccessException  if the class or initializer is
  109.      *               not accessible.
  110.      * @exception  InstantiationException  if an application tries to
  111.      *               instantiate an abstract class or an interface, or if the
  112.      *               instantiation fails for some other reason.
  113.      */
  114.     public native Object newInstance() 
  115.     throws InstantiationException, IllegalAccessException;
  116.  
  117.     /**
  118.      * This method is the dynamic equivalent of the Java language
  119.      * <code>instanceof</code> operator. The method returns true if
  120.      * the specified Object argument is non-null and can be cast to
  121.      * the reference type represented by this Class object without
  122.      * raising a ClassCastException. It returns false otherwise.
  123.      *
  124.      * <p>Specifically, if this Class object represents a declared
  125.      * class, returns true if the specified Object argument is an
  126.      * instance of the represented class (or of any of its
  127.      * subclasses); false otherwise. If this Class object represents
  128.      * an array class, returns true if the specified Object argument
  129.      * can be converted to an object of the array type by an identity
  130.      * conversion or by a widening reference conversion; false
  131.      * otherwise. If this Class object represents an interface,
  132.      * returns true if the class or any superclass of the
  133.      * specified Object argument implements this interface; false
  134.      * otherwise. If this Class object represents a primitive type,
  135.      * returns false.
  136.      *
  137.      * @param   obj The object to check
  138.      * @since   JDK1.1
  139.      */
  140.     public native boolean isInstance(Object obj);
  141.  
  142.     /**
  143.      * Determines if the class or interface
  144.      * represented by this Class object is either the same as, or is a
  145.      * superclass or superinterface of, the class or interface
  146.      * represented by the specified Class parameter. It returns true
  147.      * if so, false otherwise. If this Class object represents a
  148.      * primitive type, returns true if the specified Class parameter
  149.      * is exactly this Class object, false otherwise.
  150.      *
  151.      * <p>Specifically, this method tests whether the type represented
  152.      * by the specified Class parameter can be converted to the type
  153.      * represented by this Class object via an identity conversion or
  154.      * via a widening reference conversion. See <em>The Java Language
  155.      * Specification</em>, sections 5.1.1 and 5.1.4 , for details.
  156.      *
  157.      * @exception NullPointerException if the specified Class parameter is null.
  158.      * @since   JDK1.1
  159.      */
  160.     public native boolean isAssignableFrom(Class cls);
  161.  
  162.     /**
  163.      * Determines if the specified Class object represents an interface type.
  164.      *
  165.      * @return  <code>true</code> if this object represents an interface;
  166.      *          <code>false</code> otherwise.
  167.      */
  168.     public native boolean isInterface();
  169.  
  170.     /**
  171.      * If this Class object represents an array type, returns true,
  172.      * otherwise returns false.
  173.      *
  174.      * @since   JDK1.1
  175.      */
  176.     public native boolean isArray();
  177.  
  178.     /**
  179.      * Determines if the specified Class object represents a primitive Java
  180.      * type.
  181.      *
  182.      * <p>There are nine predefined Class objects to represent the eight
  183.      * primitive Java types and void.  These are created by the Java
  184.      * Virtual Machine, and have the same names as the primitive types
  185.      * that they represent, namely boolean, byte, char, short, int,
  186.      * long, float, and double, and void.
  187.      *
  188.      * <p>These objects may only be accessed via the following public
  189.      * static final variables, and are the only Class objects for
  190.      * which this method returns true.
  191.      *
  192.      * @see     java.lang.Boolean#TYPE
  193.      * @see     java.lang.Character#TYPE
  194.      * @see     java.lang.Byte#TYPE
  195.      * @see     java.lang.Short#TYPE
  196.      * @see     java.lang.Integer#TYPE
  197.      * @see     java.lang.Long#TYPE
  198.      * @see     java.lang.Float#TYPE
  199.      * @see     java.lang.Double#TYPE
  200.      * @see     java.lang.Void#TYPE
  201.      * @since   JDK1.1
  202.      */
  203.     public native boolean isPrimitive();
  204.  
  205.     /**
  206.      * Returns the fully-qualified name of the type (class, interface,
  207.      * array, or primitive) represented by this Class object, as a String.
  208.      * <p>
  209.      * If this class object represents a class of arrays, then the internal 
  210.      * form of the name consists of the name of the element type in Java 
  211.      * signature format, preceded by one or more "<tt>[</tt>" characters 
  212.      * representing the depth of array nesting. Thus:
  213.      * <blockquote><pre>
  214.      * (new Object[3]).getClass().getName()
  215.      * </pre></blockquote>
  216.      * returns "<code>[Ljava.lang.Object;</code>" and:
  217.      * <blockquote><pre>
  218.      * (new int[3][4][5][6][7][8][9]).getClass().getName()
  219.      * </pre></blockquote>
  220.      * returns "<code>[[[[[[[I</code>". The encoding of element type names 
  221.      * is as follows:
  222.      * <blockquote><pre>
  223.      * B            byte
  224.      * C            char
  225.      * D            double
  226.      * F            float
  227.      * I            int
  228.      * J            long
  229.      * L<i>classname;</i>  class or interface
  230.      * S            short
  231.      * Z            boolean
  232.      * </pre></blockquote>
  233.      * The class or interface name <tt><i>classname</i></tt> is given in 
  234.      * fully qualified form as shown in the example above.
  235.      *
  236.      * @return  the fully qualified name of the class or interface
  237.      *          represented by this object.
  238.      */
  239.     public native String getName();
  240.  
  241.     /**
  242.      * Determines the class loader for the class. 
  243.      *
  244.      * @return  the class loader that created the class or interface
  245.      *          represented by this object, or <code>null</code> if the
  246.      *          class was not created by a class loader.
  247.      * @see     java.lang.ClassLoader
  248.      */
  249.     public native ClassLoader getClassLoader();
  250.  
  251.     /**
  252.      * If this object represents any class other than the class 
  253.      * <code>Object</code>, then the object that represents the superclass 
  254.      * of that class is returned. 
  255.      * <p>
  256.      * If this object is the one that represents the class 
  257.      * <code>Object</code> or this object represents an interface, 
  258.      * <code>null</code> is returned. 
  259.      *
  260.      * @return  the superclass of the class represented by this object.
  261.      */
  262.     public native Class getSuperclass();
  263.  
  264.     /**
  265.      * Get the package for this class.
  266.      * The classes classloader is used to find the package instance
  267.      * corresponding to this class. If the class has no
  268.      * classloader the set of packages loaded from CLASSPATH is
  269.      * searched to find the classes package. Null is returned if no
  270.      * package object was created by the classloader of this class.<br>
  271.      * Packages have attributes
  272.      * for versions and specifications only if the information was defined
  273.      * in the manifests that accompany the classes and if the class
  274.      * loader created the package instance with the attributes
  275.      * from the manifest.
  276.      *
  277.      * @return The package of the class. It may be null if no package
  278.      *         information is available from the archive or codebase.
  279.      */
  280.     public Package getPackage() {
  281.     return Package.getPackage(this);
  282.     }
  283.  
  284.     /**
  285.      * Determines the interfaces implemented by the class or interface 
  286.      * represented by this object. 
  287.      * <p>
  288.      * If this object represents a class, the return value is an array 
  289.      * containing objects representing all interfaces implemented by the 
  290.      * class. The order of the interface objects in the array corresponds 
  291.      * to the order of the interface names in the <code>implements</code> 
  292.      * clause of the declaration of the class represented by this object. 
  293.      * <p>
  294.      * If this object represents an interface, the array contains 
  295.      * objects representing all interfaces extended by the interface. The 
  296.      * order of the interface objects in the array corresponds to the 
  297.      * order of the interface names in the <code>extends</code> clause of 
  298.      * the declaration of the interface represented by this object. 
  299.      * <p>
  300.      * If the class or interface implements no interfaces, the method 
  301.      * returns an array of length 0. 
  302.      *
  303.      * @return  an array of interfaces implemented by this class.
  304.      */
  305.     public native Class[] getInterfaces();
  306.  
  307.     /**
  308.      * If this class represents an array type, returns the Class
  309.      * object representing the component type of the array; otherwise
  310.      * returns null.
  311.      *
  312.      * @see     java.lang.reflect.Array
  313.      * @since   JDK1.1
  314.      */
  315.     public native Class getComponentType();
  316.  
  317.     /**
  318.      * Returns the Java language modifiers for this class or
  319.      * interface, encoded in an integer. The modifiers consist of the
  320.      * Java Virtual Machine's constants for public, protected,
  321.      * private, final, and interface; they should be decoded using the
  322.      * methods of class Modifier.
  323.      *
  324.      * <p>The modifier encodings are defined in <em>The Java Virtual
  325.      * Machine Specification</em>, table 4.1.
  326.      *
  327.      * @see     java.lang.reflect.Modifier
  328.      * @since   JDK1.1
  329.      */
  330.     public native int getModifiers();
  331.  
  332.     /**
  333.      * Get the signers of this class.
  334.      *
  335.      * @return the signers of this class, or null if there are no signers.
  336.      * @since   JDK1.1
  337.      */
  338.     public native Object[] getSigners();
  339.     
  340.     /**
  341.      * Set the signers of this class.
  342.      */
  343.     native void setSigners(Object[] signers);
  344.  
  345.     /**
  346.      * If the class or interface represented by this Class object is
  347.      * a member of another class, returns the Class object
  348.      * representing the class of which it is a member (its
  349.      * <em>declaring class</em>).  Returns null if this class or
  350.      * interface is not a member of any other class.
  351.      *
  352.      * @since   JDK1.1
  353.      */
  354.     public Class getDeclaringClass() {
  355.     return null;                /* not implemented */
  356.     }
  357.  
  358.     /**
  359.      * Returns an array containing Class objects representing all the
  360.      * public classes and interfaces that are members of the class
  361.      * represented by this Class object.  This includes public class
  362.      * and interface members inherited from superclasses and public
  363.      * class and interface members declared by the class.  Returns an
  364.      * array of length 0 if the class has no public member classes or
  365.      * interfaces, or if this Class object represents a primitive
  366.      * type.
  367.      *
  368.      * @since   JDK1.1
  369.      */
  370.     public Class[] getClasses() {
  371.     return new Class[0];            /* not implemented */
  372.     }
  373.  
  374.     /**
  375.      * Returns an array containing Field objects reflecting all the
  376.      * accessible public fields of the class or interface represented
  377.      * by this Class object.  
  378.      * The elements in the array returned are not sorted and are not
  379.      * in any particular order.
  380.      * Returns an array of length 0 if the
  381.      * class or interface has no accessible public fields, or if it
  382.      * represents an array type or a primitive type.
  383.      *
  384.      * <p>Specifically, if this Class object represents a class,
  385.      * returns the public fields of this class and of all its
  386.      * superclasses.  If this Class object represents an interface,
  387.      * returns the fields of this interface and of all its
  388.      * superinterfaces.  If this Class object represents an array type
  389.      * or a primitive type, returns an array of length 0.
  390.      *
  391.      * <p>The implicit length field for array types is not reflected
  392.      * by this method. User code should use the methods of class Array
  393.      * to manipulate arrays.
  394.      *
  395.      * <p>See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  396.      *
  397.      * @exception SecurityException    if access to the information is denied.
  398.      * @see       java.lang.reflect.Field
  399.      * @since     JDK1.1
  400.      */
  401.     public Field[] getFields() throws SecurityException {
  402.     checkMemberAccess(Member.PUBLIC);
  403.     return getFields0(Member.PUBLIC);
  404.     }
  405.  
  406.     /**
  407.      * Returns an array containing Method objects reflecting all the
  408.      * public <em>member</em> methods of the class or interface
  409.      * represented by this Class object, including those declared by
  410.      * the class or interface and and those inherited from
  411.      * superclasses and superinterfaces. 
  412.      * The elements in the array returned are not sorted and are not
  413.      * in any particular order.
  414.      * Returns an array of length 0
  415.      * if the class or interface has no public member methods.
  416.      *
  417.      * <p>See <em>The Java Language Specification</em>, sections 8.2
  418.      * and 8.4.
  419.      *
  420.      * @exception SecurityException    if access to the information is denied.
  421.      * @see       java.lang.reflect.Method
  422.      * @since     JDK1.1
  423.      */
  424.     public Method[] getMethods() throws SecurityException {
  425.     checkMemberAccess(Member.PUBLIC);
  426.     return getMethods0(Member.PUBLIC);
  427.     }
  428.  
  429.     /**
  430.      * Returns an array containing Constructor objects reflecting
  431.      * all the public constructors of the class represented by this
  432.      * Class object.  An array of length 0 is returned if the class
  433.      * has no public constructors.
  434.      *
  435.      * @exception SecurityException    if access to the information is denied.
  436.      * @see       java.lang.reflect.Constructor
  437.      * @since     JDK1.1
  438.      */
  439.     public Constructor[] getConstructors() throws SecurityException {
  440.     checkMemberAccess(Member.PUBLIC);
  441.     return getConstructors0(Member.PUBLIC);
  442.     }
  443.  
  444.     /**
  445.      * Returns a Field object that reflects the specified public
  446.      * member field of the class or interface represented by
  447.      * this Class object. The name parameter is a String specifying
  448.      * the simple name of the desired field.
  449.      *
  450.      * <p>The field to be reflected is located by searching all the
  451.      * member fields of the class or interface represented by this
  452.      * Class object for a public field with the specified name.
  453.      *
  454.      * <p>See <em>The Java Language Specification</em>, sections 8.2 and 8.3.
  455.      *
  456.      * @exception NoSuchFieldException if a field with the specified name is
  457.      *              not found.
  458.      * @exception SecurityException    if access to the information is denied.
  459.      * @see       java.lang.reflect.Field
  460.      * @since     JDK1.1
  461.      */
  462.     public Field getField(String name)
  463.     throws NoSuchFieldException, SecurityException {
  464.     checkMemberAccess(Member.PUBLIC);
  465.     return getField0(name, Member.PUBLIC);
  466.     }
  467.  
  468.     /**
  469.      * Returns a Method object that reflects the specified public
  470.      * member method of the class or interface represented by this
  471.      * Class object. The name parameter is a String specifying the
  472.      * simple name the desired method, and the parameterTypes
  473.      * parameter is an array of Class objects that identify the
  474.      * method's formal parameter types, in declared order.
  475.      *
  476.      * <p>The method to reflect is located by searching all the member
  477.      * methods of the class or interface represented by this Class
  478.      * object for a public method with the specified name and exactly
  479.      * the same formal parameter types.
  480.      *
  481.      * <p>See <em>The Java Language Specification</em>, sections 8.2
  482.      * and 8.4.
  483.      *
  484.      * @exception NoSuchMethodException if a matching method is not found.
  485.      * @exception SecurityException    if access to the information is denied.
  486.      * @see       java.lang.reflect.Method
  487.      * @since     JDK1.1
  488.      */
  489.     public Method getMethod(String name, Class[] parameterTypes)
  490.     throws NoSuchMethodException, SecurityException {
  491.     checkMemberAccess(Member.PUBLIC);
  492.     return getMethod0(name, parameterTypes, Member.PUBLIC);
  493.     }
  494.  
  495.     /**
  496.      * Returns a Constructor object that reflects the specified public
  497.      * constructor of the class represented by this Class object. The
  498.      * parameterTypes parameter is an array of Class objects that
  499.      * identify the constructor's formal parameter types, in declared
  500.      * order.
  501.      *
  502.      * <p>The constructor to reflect is located by searching all the
  503.      * constructors of the class represented by this Class object for
  504.      * a public constructor with the exactly the same formal parameter
  505.      * types.
  506.      *
  507.      * @exception NoSuchMethodException if a matching method is not found.
  508.      * @exception SecurityException     if access to the information is denied.
  509.      * @see       java.lang.reflect.Constructor
  510.      * @since     JDK1.1
  511.      */
  512.     public Constructor getConstructor(Class[] parameterTypes)
  513.     throws NoSuchMethodException, SecurityException {
  514.     checkMemberAccess(Member.PUBLIC);
  515.     return getConstructor0(parameterTypes, Member.PUBLIC);
  516.     }
  517.  
  518.     /**
  519.      * Returns an array of Class objects reflecting all the classes
  520.      * and interfaces declared as members of the class represented by
  521.      * this Class object. This includes public, protected, default
  522.      * (package) access, and private classes and interfaces declared
  523.      * by the class, but excludes inherited classes and interfaces.
  524.      * Returns an array of length 0 if the class declares no classes
  525.      * or interfaces as members, or if this Class object represents a
  526.      * primitive type.
  527.      *
  528.      * @exception SecurityException    if access to the information is denied.
  529.      * @since     JDK1.1
  530.      */
  531.     public Class[] getDeclaredClasses() throws SecurityException {
  532.     checkMemberAccess(Member.DECLARED);
  533.     return new Class[0];            /* not implemented */
  534.     }
  535.  
  536.     /**
  537.      * Returns an array of Field objects reflecting all the fields
  538.      * declared by the class or interface represented by this Class
  539.      * object. This includes public, protected, default (package)
  540.      * access, and private fields, but excludes inherited
  541.      * fields. 
  542.      * The elements in the array returned are not sorted and are not
  543.      * in any particular order.
  544.      * Returns an array of length 0 if the class or interface
  545.      * declares no fields, or if this Class object represents a
  546.      * primitive type.
  547.      *
  548.      * See <em>The Java Language Specification</em>, sections 8.2 and
  549.      * 8.3.
  550.      *
  551.      * @exception SecurityException    if access to the information is denied.
  552.      * @see       java.lang.reflect.Field
  553.      * @since     JDK1.1
  554.      */
  555.     public Field[] getDeclaredFields() throws SecurityException {
  556.     checkMemberAccess(Member.DECLARED);
  557.     return getFields0(Member.DECLARED);
  558.     }
  559.  
  560.     /**
  561.      * Returns an array of Method objects reflecting all the methods
  562.      * declared by the class or interface represented by this Class
  563.      * object. This includes public, protected, default (package)
  564.      * access, and private methods, but excludes inherited
  565.      * methods. 
  566.      * The elements in the array returned are not sorted and are not
  567.      * in any particular order.
  568.      * Returns an array of length 0 if the class or interface
  569.      * declares no methods, or if this Class object represents a
  570.      * primitive type.
  571.      *
  572.      * <p>See <em>The Java Language Specification</em>, section 8.2.
  573.      *
  574.      * @exception SecurityException    if access to the information is denied.
  575.      * @see       java.lang.reflect.Method
  576.      * @since     JDK1.1
  577.      */
  578.     public Method[] getDeclaredMethods() throws SecurityException {
  579.     checkMemberAccess(Member.DECLARED);
  580.     return getMethods0(Member.DECLARED);
  581.     }
  582.  
  583.     /**
  584.      * Returns an array of Constructor objects reflecting all the
  585.      * constructors declared by the class represented by this Class
  586.      * object. These are public, protected, default (package) access,
  587.      * and private constructors.  
  588.      * The elements in the array returned are not sorted and are not
  589.      * in any particular order.
  590.      * Returns an array of length 0 if this
  591.      * Class object represents an interface or a primitive type.
  592.      *
  593.      * <p>See <em>The Java Language Specification</em>, section 8.2.
  594.      *
  595.      * @exception SecurityException    if access to the information is denied.
  596.      * @see       java.lang.reflect.Constructor
  597.      * @since     JDK1.1
  598.      */
  599.     public Constructor[] getDeclaredConstructors() throws SecurityException {
  600.     checkMemberAccess(Member.DECLARED);
  601.     return getConstructors0(Member.DECLARED);
  602.     }
  603.  
  604.     /**
  605.      * Returns a Field object that reflects the specified declared
  606.      * field of the class or interface represented by this Class
  607.      * object. The name parameter is a String that specifies the
  608.      * simple name of the desired field.
  609.      *
  610.      * @exception NoSuchFieldException if a field with the specified name is
  611.      *              not found.
  612.      * @exception SecurityException    if access to the information is denied.
  613.      * @see       java.lang.reflect.Field
  614.      * @since     JDK1.1
  615.      */
  616.     public Field getDeclaredField(String name)
  617.     throws NoSuchFieldException, SecurityException {
  618.     checkMemberAccess(Member.DECLARED);
  619.     return getField0(name, Member.DECLARED);
  620.     }
  621.  
  622.     /**
  623.      * Returns a Method object that reflects the specified declared
  624.      * method of the class or interface represented by this Class
  625.      * object. The name parameter is a String that specifies the
  626.      * simple name of the desired method, and the parameterTypes
  627.      * parameter is an array of Class objects that identify the
  628.      * method's formal parameter types, in declared order.
  629.      *
  630.      * @exception NoSuchMethodException if a matching method is not found.
  631.      * @exception SecurityException     if access to the information is denied.
  632.      * @see       java.lang.reflect.Method
  633.      * @since     JDK1.1
  634.      */
  635.     public Method getDeclaredMethod(String name, Class[] parameterTypes)
  636.     throws NoSuchMethodException, SecurityException {
  637.     checkMemberAccess(Member.DECLARED);
  638.     return getMethod0(name, parameterTypes, Member.DECLARED);
  639.     }
  640.  
  641.     /**
  642.      * Returns a Constructor object that reflects the specified declared
  643.      * constructor of the class or interface represented by this Class
  644.      * object.  The parameterTypes parameter is an array of Class
  645.      * objects that identify the constructor's formal parameter types,
  646.      * in declared order.
  647.      *
  648.      * @exception NoSuchMethodException if a matching method is not found.
  649.      * @exception SecurityException     if access to the information is denied.
  650.      * @see       java.lang.reflect.Constructor
  651.      * @since     JDK1.1
  652.      */
  653.     public Constructor getDeclaredConstructor(Class[] parameterTypes)
  654.     throws NoSuchMethodException, SecurityException {
  655.     checkMemberAccess(Member.DECLARED);
  656.     return getConstructor0(parameterTypes, Member.DECLARED);
  657.     }
  658.  
  659.     /**
  660.      * Find a resource with a given name.  Will return null if no
  661.      * resource with this name is found.  The rules for searching a
  662.      * resources associated with a given class are implemented by the
  663.      * ClassLoader of the class.<p>
  664.      *
  665.      * The Class methods delegate to ClassLoader methods, after applying
  666.      * a naming convention: if the resource name starts with "/", it is used
  667.      * as is.  Otherwise, the name of the package is prepended, after
  668.      * converting "." to "/".
  669.      *
  670.      * @see     java.lang.ClassLoader
  671.      * @since   JDK1.1
  672.      */
  673.     public InputStream getResourceAsStream(String name) {
  674.     name = resolveName(name);
  675.     ClassLoader cl = getClassLoader();
  676.     if (cl==null) {
  677.         // A system class.
  678.         return ClassLoader.getSystemResourceAsStream(name);
  679.     }
  680.     return cl.getResourceAsStream(name);
  681.     }
  682.  
  683.     /**
  684.      * @since   JDK1.1
  685.      */
  686.     public java.net.URL getResource(String name) {
  687.     name = resolveName(name);
  688.     ClassLoader cl = getClassLoader();
  689.     if (cl==null) {
  690.         // A system class.
  691.         return ClassLoader.getSystemResource(name);
  692.     }
  693.     return cl.getResource(name);
  694.     }
  695.  
  696.     private static final RuntimePermission getPDperm = 
  697.                           new RuntimePermission("Class.getProtectionDomain");
  698.  
  699.     /**
  700.      * Returns the ProtectionDomain of this class.
  701.      *
  702.      * The <code>getProtectionDomain</code> method first calls
  703.      * <code>SecurityManager.checkPermission</code> with the
  704.      * <code>RuntimePermission("Class.getProtectionDomain")</code> permission
  705.      * to ensure the caller has permission to get the
  706.      * ProtectionDomain.
  707.      *
  708.      * @return the ProtectionDomain of this class, or <code>null</code> 
  709.      * if this class does not have a ProtectionDomain.
  710.      *
  711.      * @exception  SecurityException if the caller is not 
  712.      * allowed to get the ProtectionDomain of this class.
  713.      *
  714.      * @see java.security.ProtectionDomain
  715.      * @see java.lang.RuntimePermission
  716.      * @since JDK1.2
  717.      */
  718.     public java.security.ProtectionDomain getProtectionDomain()
  719.     {
  720.     SecurityManager sm = System.getSecurityManager();
  721.     if (sm != null) sm.checkPermission(getPDperm);
  722.     return getProtectionDomain0();
  723.     }
  724.  
  725.     /**
  726.      * Returns the ProtectionDomain of this class.
  727.      */
  728.     private native java.security.ProtectionDomain getProtectionDomain0();
  729.  
  730.     private static final RuntimePermission setPDperm =
  731.                           new RuntimePermission("Class.setProtectionDomain");
  732.  
  733.     /**
  734.      * Sets the ProtectionDomain of this class.
  735.      * <p>
  736.      * The <code>setProtectionDomain</code> method first calls
  737.      * <code>SecurityManager.checkPermission</code> with the
  738.      * <code>RuntimePermission("Class.setProtectionDomain")</code> permission
  739.      * to ensure the caller has permission to set the
  740.      * ProtectionDomain.
  741.      *
  742.      * @param domain the ProtectionDomain to be set for this class.
  743.      *
  744.      * @exception  SecurityException if the caller is not 
  745.      * allowed to set the ProtectionDomain of this class.
  746.      *
  747.      * @see java.security.ProtectionDomain
  748.      * @see java.lang.RuntimePermission
  749.      * @since JDK1.2
  750.      */
  751.  
  752.     public void setProtectionDomain(java.security.ProtectionDomain domain)
  753.     {
  754.         SecurityManager sm = System.getSecurityManager();
  755.         if (sm != null) sm.checkPermission(setPDperm);
  756.     setProtectionDomain0(domain);
  757.     }
  758.  
  759.     /**
  760.      * Set the ProtectionDomain for this class. Called by the above call
  761.      * as well as by ClassLoader.defineClass.
  762.      */
  763.  
  764.     native void setProtectionDomain0(java.security.ProtectionDomain pd);
  765.  
  766.     /*
  767.      * Return the Virtual Machine's Class object for the named
  768.      * primitive type.
  769.      */
  770.     static native Class getPrimitiveClass(String name);
  771.  
  772.     /*
  773.      * Check if client is allowed to access members.  If access is
  774.      * denied, throw a SecurityException.
  775.      *
  776.      * <p>Default policy: allow all clients access with normal Java
  777.      * access control.
  778.      */
  779.     private void checkMemberAccess(int which) {
  780.     SecurityManager s = System.getSecurityManager();
  781.     if (s != null) {
  782.         s.checkMemberAccess(this, which);
  783.     }
  784.     }
  785.  
  786.     /**
  787.      * Add a package name prefix if the name is not absolute
  788.      * Remove leading "/" if name is absolute
  789.      */
  790.     private String resolveName(String name) {
  791.     if (name == null) {
  792.         return name;
  793.     }
  794.     if (!name.startsWith("/")) {
  795.         Class c = this;
  796.         while (c.isArray()) {
  797.         c = c.getComponentType();
  798.         }
  799.         String baseName = c.getName();
  800.         int index = baseName.lastIndexOf('.');
  801.         if (index != -1) {
  802.         name = baseName.substring(0, index).replace('.', '/')
  803.             +"/"+name;
  804.         }
  805.     } else {
  806.         name = name.substring(1);
  807.     }
  808.     return name;
  809.     }
  810.  
  811.     private native Field[] getFields0(int which);
  812.     private native Method[] getMethods0(int which);
  813.     private native Constructor[] getConstructors0(int which);
  814.     private native Field getField0(String name, int which);
  815.     private native Method getMethod0(String name, Class[] parameterTypes,
  816.     int which);
  817.     private native Constructor getConstructor0(Class[] parameterTypes,
  818.     int which);
  819.  
  820.     /** use serialVersionUID from JDK 1.1 for interoperability */
  821.     private static final long serialVersionUID = 3206093459760846163L;
  822.  
  823. }
  824.