/[classpath]/classpath/vm/reference/java/lang/Class.java
ViewVC logotype

Diff of /classpath/vm/reference/java/lang/Class.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.17 by mark, Tue Feb 19 22:55:30 2002 UTC revision 1.18 by ericb, Fri Feb 22 20:07:40 2002 UTC
# Line 1  Line 1 
1  /* Class.java  /* Class.java -- Reference implementation of access to object metadata
2     Copyright (C) 1998, 2002 Free Software Foundation     Copyright (C) 1998, 2002 Free Software Foundation
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
# Line 40  package java.lang; Line 40  package java.lang;
40  import java.io.Serializable;  import java.io.Serializable;
41  import java.lang.reflect.*;  import java.lang.reflect.*;
42  import java.security.*;  import java.security.*;
43    import java.net.URL;
44  import gnu.java.lang.*;  import gnu.java.lang.*;
45    
46    /*
47     * This class is a reference version, mainly for compiling a class library
48     * jar.  It is likely that VM implementers replace this with their own
49     * version that can communicate effectively with the VM.
50     */
51    
52  /**  /**
53   * A Class represents a Java type.  There will never be   * A Class represents a Java type.  There will never be multiple Class
54   * multiple Class objects with identical names and   * objects with identical names and ClassLoaders. Primitive types, array
55   * ClassLoaders.<P>   * types, and void also have a Class object.
56     *
57     * <p>Arrays with identical type and number of dimensions share the same
58     * class (and null "system" ClassLoader, incidentally).  The name of an
59     * array class is <code>[&lt;signature format&gt;;</code> ... for example,
60     * String[]'s class is <code>[Ljava.lang.String;</code>. boolean, byte,
61     * short, char, int, long, float and double have the "type name" of
62     * Z,B,S,C,I,J,F,D for the purposes of array classes.  If it's a
63     * multidimensioned array, the same principle applies:
64     * <code>int[][][]</code> == <code>[[[I</code>.
65   *   *
66   * Arrays with identical type and number of dimensions   * <p>There is no public constructor - Class objects are obtained only through
67   * share the same class (and null "system" ClassLoader,   * the virtual machine, as defined in ClassLoaders.
  * incidentally).  The name of an array class is  
  * <CODE>[&lt;signature format&gt;;</CODE> ... for example,  
  * String[]'s class is <CODE>[Ljava.lang.String;</CODE>.  
  * boolean, byte, short, char, int, long, float and double  
  * have the "type name" of Z,B,S,C,I,J,F,D for the  
  * purposes of array classes.  If it's a multidimensioned  
  * array, the same principle applies:  
  * <CODE>int[][][]</CODE> == <CODE>[[[I</CODE>.<P>  
68   *   *
69   * As of 1.1, this class represents primitive types as   * @serialData Class objects serialize specially:
70   * well.  You can get to those by looking at   * <code>TC_CLASS ClassDescriptor</code>. For more serialization information,
71   * java.lang.Integer.TYPE, java.lang.Boolean.TYPE, etc.   * see {@link ObjectStreamClass}.
72   *   *
73   * @author John Keiser   * @author John Keiser
74   * @version 1.1.0, Aug 6 1998   * @author Eric Blake <ebb9@email.byu.edu>
75   * @since JDK1.0   * @since 1.0
76     * @see ClassLoader
77   */   */
78    public final class Class implements Serializable
79  public final class Class implements Serializable {  {
80      /**
81      private static final long serialVersionUID = 3206093459760846163L;     * Compatible with JDK 1.0+.
82       */
83      private Object[] signers = null;    private static final long serialVersionUID = 3206093459760846163L;
84      private ProtectionDomain pd = null;  
85          /** The class signers. */
86      // The unknown protection domain.    private Object[] signers = null;
87      private final static ProtectionDomain unknownProtectionDomain;    /** The class protection domain. */
88      private ProtectionDomain pd = null;
89      static {  
90          Permissions permissions = new Permissions();    /** The unknown protection domain. */
91          permissions.add(new AllPermission());    private final static ProtectionDomain unknownProtectionDomain;
92          unknownProtectionDomain = new ProtectionDomain(null, permissions);    static
93      }    {
94        Permissions permissions = new Permissions();
95      // Permission needed to get the protection domain      permissions.add(new AllPermission());
96      private final static Permission protectionDomainPermission      unknownProtectionDomain = new ProtectionDomain(null, permissions);
97          = new RuntimePermission("getProtectionDomain");    }
98    
99      private Class() {    /** Permission needed to get the protection domain. */
100      }    private final static Permission protectionDomainPermission
101        = new RuntimePermission("getProtectionDomain");
102      /**  
103       * Return the human-readable form of this Object.  For    /**
104       * class, that means "interface " or "class " plus the     * Class is non-instantiable from Java code; only the VM can create
105       * classname.     * instances of this class.
106       * @return the human-readable form of this Object.     */
107       * @since JDK1.0    private Class()
108       */    {
109      public String toString() {    }
110          return (isInterface() ? "interface " : "class ") + getName();  
111      }    /**
112       * Return the human-readable form of this Object.  For an object, this
113      /**     * is either "interface " or "class " followed by <code>getName()</code>,
114       * Get the name of this class, separated by dots for     * for primitive types and void it is just <code>getName()</code>.
115       * package separators.     *
116       * @return the name of this class.     * @return the human-readable form of this Object
117       * @since JDK1.0     */
118       */    public String toString()
119      public native String getName();    {
120        //XXX Fix for primitive types.
121      /**      return (isInterface() ? "interface " : "class ") + getName();
122       * Get whether this class is an interface or not.  Array    }
123       * types are not interfaces.  
124       * @return whether this class is an interface or not.    /**
125       * @since JDK1.0     * Use the classloader of the current class to load, link, and initialize
126       */     * a class. This is equivalent to your code calling
127      public native boolean isInterface();     * <code>Class.forName(name, true, getClass().getClassLoader())</code>.
128       *
129      /**     * @param name the name of the class to find
130       * Get the direct superclass of this class.  If this is     * @return the Class object representing the class
131       * an interface, it will get the direct superinterface.     * @throws ClassNotFoundException if the class was not found by the
132       * @return the direct superclass of this class.     *         classloader
133       * @since JDK1.0     * @throws LinkageError if linking the class fails
134       */     * @throws ExceptionInInitializerError if the class loads, but an exception
135      public native Class getSuperclass();     *         occurs during initialization
136       */
137      /**    //XXX This does not need to be native.
138       * Get the interfaces this class <EM>directly</EM>    public static native Class forName(String name)
139       * implements, in the order that they were declared.      throws ClassNotFoundException;
140       * This method may return an empty array, but will    /*
141       * never return null.    {
142       * @return the interfaces this class directly implements.      return forName(name, true,
143       * @since JDK1.0                     VMSecurityManager.getClassContext()[1].getClassLoader());
144       */    }
145      public native Class[] getInterfaces();    */
146    
147      /**    /**
148       * Get a new instance of this class by calling the     * Use the specified classloader to load and link a class. If the loader
149       * no-argument constructor.     * is null, this uses the bootstrap class loader (provide the security
150       * @return a new instance of this class.     * check succeeds). Unfortunately, this method cannot be used to obtain
151       * @exception InstantiationException if there is not a     * the Class objects for primitive types or for void, you have to use
152       *            no-arg constructor for this class, or if     * the fields in the appropriate java.lang wrapper classes.
153       *            an exception occurred during instantiation,     *
154       *            or if the target constructor throws an     * <p>Calls <code>classloader.loadclass(name, initialize)</code>.
155       *            exception.     *
156       * @exception IllegalAccessException if you are not     * @param name the name of the class to find
157       *            allowed to access the no-arg constructor of     * @param initialize whether or not to initialize the class at this time
158       *            this Class for whatever reason.     * @param classloader the classloader to use to find the class; null means
159       * @since JDK1.0     *        to use the bootstrap class loader
160       */     * @throws ClassNotFoundException if the class was not found by the
161      public Object newInstance() throws InstantiationException, IllegalAccessException {     *         classloader
162          try {     * @throws LinkageError if linking the class fails
163              return getConstructor(new Class[0]).newInstance(new Object[0]);     * @throws ExceptionInInitializerError if the class loads, but an exception
164          } catch(SecurityException e) {     *         occurs during initialization
165              throw new IllegalAccessException("Cannot access no-arg constructor");     * @throws SecurityException if the <code>classloader</code> argument
166          } catch(IllegalArgumentException e) {     *         is <code>null</code> and the caller does not have the
167              throw new UnknownError("IllegalArgumentException thrown from Constructor.newInstance().  Something is rotten in Denmark.");     *         <code>RuntimePermission("getClassLoader")</code> permission
168          } catch(InvocationTargetException e) {     * @see ClassLoader
169              throw new InstantiationException("Target threw an exception.");     * @since 1.2
170          } catch(NoSuchMethodException e) {     */
171              throw new InstantiationException("Method not found");    public static Class forName(String name, boolean initialize,
172          }                                ClassLoader classloader)
173      }      throws ClassNotFoundException
174          {
175      /**      if (classloader == null)
176       * Get the ClassLoader that loaded this class.  If it was        {
177       * loaded by the system classloader, this method will          // Check if we may get the system classloader
      * return null.  
      * @return the ClassLoader that loaded this class.  
      * @since JDK1.0  
      */  
     public native ClassLoader getClassLoader();  
   
     /**  
      * Use the system classloader to load and link a class.  
      * @param name the name of the class to find.  
      * @return the Class object representing the class.  
      * @exception ClassNotFoundException if the class was not  
      *            found by the system classloader.  
      * @since JDK1.0  
      */  
     public static native Class forName(String name) throws ClassNotFoundException;  
   
     /**  
      * Use the specified classloader to load and link a class.  
      * Calls <code>classloader.loadclass(name, initialize)</code>.  
      * @param name the name of the class to find.  
      * @param initialize wether or not to initialize the class.  
      * This is only a hint for optimization. Set this to false if the class  
      * will not (immediatly) be used to initialize objects.  
      * @param classloader the classloader to use to find the class.  
      * When classloader is <code>null</code> this methods acts the  
      * same as <code>forName(String)</code> (and uses the system class loader).  
      * @exception ClassNotFoundException if the class was not  
      *            found by the specified classloader.  
      * @exception SecurityException if the <code>classloader</code> argument  
      *            is <code>null</code> and the caller does not have the  
      *            <code>RuntimePermission("getClassLoader")</code>  
      *            (to get the system classloader) and was not loaded by the  
      *            system classloader (or bootstrap classloader).  
      * @since 1.2  
      */  
     public static Class forName(String name,  
                                 boolean initialize,  
                                 ClassLoader classloader)  
         throws ClassNotFoundException  
     {  
         if (classloader == null) {  
             // Check if we may get the system classloader  
             SecurityManager sm = System.getSecurityManager();  
             if (sm != null) {  
                 // Get the calling class and classloader  
                 Class c = VMSecurityManager.getClassContext()[1];  
                 ClassLoader cl = c.getClassLoader();  
                 if (cl != null && cl != ClassLoader.systemClassLoader)  
                     sm.checkPermission  
                         (new RuntimePermission("getClassLoader"));  
             }  
             classloader = ClassLoader.systemClassLoader;  
         }  
   
         return classloader.loadClass(name, initialize);  
     }  
   
     /**  
      * Discover whether an Object is an instance of this  
      * Class.  Think of it as almost like  
      * <CODE>o instanceof (this class)</CODE>.  
      * @param o the Object to check  
      * @return whether o is an instance of this class.  
      * @since JDK1.1  
      */  
     public native boolean isInstance(Object o);  
   
     /**  
      * Discover whether an instance of the Class parameter  
      * would be an instance of this Class as well.  Think of  
      * doing <CODE>isInstance(c.newInstance())</CODE> or even  
      * <CODE>c instanceof (this class)</CODE>.  
      * @param c the class to check  
      * @return whether an instance of c would be an instance  
      *         of this class as well.  
      * @since JDK1.1  
      */  
     public native boolean isAssignableFrom(Class c);  
   
     /**  
      * Return whether this class is an array type.  
      * @return whether this class is an array type.  
      * @since JDK1.1  
      */  
     public boolean isArray() {  
         return getName().charAt(0) == '[';  
     }  
   
     /**  
      * Return whether this class is a primitive type.  A  
      * primitive type class is a class representing a kind of  
      * "placeholder" for the various primitive types.  You  
      * can access the various primitive type classes through  
      * java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc.  
      * @return whether this class is a primitive type.  
      * @since JDK1.1  
      */  
     public native boolean isPrimitive();  
       
     /**  
      * If this is an array, get the Class representing the  
      * type of array.  Examples: [[java.lang.String would  
      * return [java.lang.String, and calling getComponentType  
      * on that would give java.lang.String.  If this is not  
      * an array, returns null.  
      * @return the array type of this class, or null.  
      * @since JDK1.1  
      */  
     public Class getComponentType() {  
         if(isArray()) {  
             try {  
                 return Class.forName(getName().substring(1));  
             } catch(ClassNotFoundException e) {  
                 return null;  
             }  
         } else {  
             return null;  
         }  
     }  
       
     /**  
      * Get the signers of this class.  
      * @return the signers of this class.  
      * @since JDK1.1  
      */  
     public Object[] getSigners() {  
         return signers;  
     }  
   
     /**  
      * Set the signers of this class.  
      * @param signers the signers of this class.  
      * @since JDK1.1  
      */  
     void setSigners(Object[] signers) {  
         this.signers = signers;  
     }  
   
     /**  
      * Get a resource URL using this class's package using  
      * the getClassLoader().getResource() method.  If this  
      * class was loaded using the system classloader,  
      * ClassLoader.getSystemResource() is used instead.<P>  
      *  
      * If the name you supply is absolute (it starts with a  
      * <CODE>/</CODE>), then it is passed on to getResource()  
      * as is.  If it is relative, the package name is  
      * prepended, with <CODE>.</CODE>'s replaced with  
      * <CODE>/</CODE> slashes.<P>  
      *  
      * The URL returned is system- and classloader-  
      * dependent, and could change across implementations.  
      * @param name the name of the resource, generally a  
      *        path.  
      * @return the URL to the resource.  
      */  
     public java.net.URL getResource(String name) {  
         if(name.length() > 0 && name.charAt(0) != '/') {  
             name = ClassHelper.getPackagePortion(getName()).replace('.','/') + "/" + name;  
         }  
         ClassLoader c = getClassLoader();  
         if(c == null) {  
             return ClassLoader.getSystemResource(name);  
         } else {  
             return c.getResource(name);  
         }  
     }  
       
     /**  
      * Get a resource using this class's package using the  
      * getClassLoader().getResource() method.  If this class  
      * was loaded using the system classloader,  
      * ClassLoader.getSystemResource() is used instead.<P>  
      *  
      * If the name you supply is absolute (it starts with a  
      * <CODE>/</CODE>), then it is passed on to getResource()  
      * as is.  If it is relative, the package name is  
      * prepended, with <CODE>.</CODE>'s replaced with  
      * <CODE>/</CODE> slashes.<P>  
      *  
      * The URL returned is system- and classloader-  
      * dependent, and could change across implementations.  
      * @param name the name of the resource, generally a  
      *        path.  
      * @return An InputStream with the contents of the  
      *         resource in it.  
      */  
     public java.io.InputStream getResourceAsStream(String name) {  
         if(name.length() > 0 && name.charAt(0) != '/') {  
             name = ClassHelper.getPackagePortion(getName()).replace('.','/') + "/" + name;  
         }  
         ClassLoader c = getClassLoader();  
         if(c == null) {  
             return ClassLoader.getSystemResourceAsStream(name);  
         } else {  
             return c.getResourceAsStream(name);  
         }  
     }  
       
     /**  
      * Get the modifiers of this class.  These can be checked  
      * against using java.lang.reflect.Modifier.  
      * @return the modifiers of this class.  
      * @see java.lang.reflect.Modifer  
      * @since JDK1.1  
      */  
     public native int getModifiers();  
   
     /**  
      * If this is an inner class, return the class that  
      * declared it.  If not, return null.  
      * @return the declaring class of this class.  
      * @since JDK1.1  
      */  
     public native Class getDeclaringClass();  
   
     /**  
      * Get all the public inner classes, declared in this  
      * class or inherited from superclasses, that are  
      * members of this class.  
      * @return all public inner classes in this class.  
      */  
     public native Class[] getClasses();  
   
     /**  
      * Get all the inner classes declared in this class.  
      * @return all inner classes declared in this class.  
      * @exception SecurityException if you do not have access  
      *            to non-public inner classes of this class.  
      */  
     public native Class[] getDeclaredClasses() throws SecurityException;  
   
     /**  
      * Get a public constructor from this class.  
      * @param args the argument types for the constructor.  
      * @return the constructor.  
      * @exception NoSuchMethodException if the constructor does  
      *            not exist.  
      * @exception SecurityException if you do not have access to public  
      *            members of this class.  
      */  
     public native Constructor getConstructor(Class[] args) throws NoSuchMethodException, SecurityException;  
       
     /**  
      * Get a constructor declared in this class.  
      * @param args the argument types for the constructor.  
      * @return the constructor.  
      * @exception NoSuchMethodException if the constructor does  
      *            not exist in this class.  
      * @exception SecurityException if you do not have access to  
      *            non-public members of this class.  
      */  
     public native Constructor getDeclaredConstructor(Class[] args) throws NoSuchMethodException, SecurityException;  
   
     /**  
      * Get all public constructors from this class.  
      * @return all public constructors in this class.  
      * @exception SecurityException if you do not have access to public  
      *            members of this class.  
      */  
     public native Constructor[] getConstructors() throws SecurityException;  
   
     /**  
      * Get all constructors declared in this class.  
      * @return all constructors declared in this class.  
      * @exception SecurityException if you do not have access to  
      *            non-public members of this class.  
      */  
     public native Constructor[] getDeclaredConstructors() throws SecurityException;  
   
   
     /**  
      * Get a public method from this class.  
      * @param name the name of the method.  
      * @param args the argument types for the method.  
      * @return the method.  
      * @exception NoSuchMethodException if the method does  
      *            not exist.  
      * @exception SecurityException if you do not have access to public  
      *            members of this class.  
      */  
     public native Method getMethod(String name, Class[] args) throws NoSuchMethodException, SecurityException;  
   
     /**  
      * Get a method declared in this class.  
      * @param name the name of the method.  
      * @param args the argument types for the method.  
      * @return the method.  
      * @exception NoSuchMethodException if the method does  
      *            not exist in this class.  
      * @exception SecurityException if you do not have access to  
      *            non-public members of this class.  
      */  
     public native Method getDeclaredMethod(String name, Class[] args) throws NoSuchMethodException, SecurityException;  
   
     /**  
      * Get all public methods from this class.  
      * @return all public methods in this class.  
      * @exception SecurityException if you do not have access to public  
      *            members of this class.  
      */  
     public native Method[] getMethods() throws SecurityException;  
   
     /**  
      * Get all methods declared in this class.  
      * @return all methods declared in this class.  
      * @exception SecurityException if you do not have access to  
      *            non-public members of this class.  
      */  
     public native Method[] getDeclaredMethods() throws SecurityException;  
   
   
     /**  
      * Get a public field from this class.  
      * @param name the name of the field.  
      * @return the field.  
      * @exception NoSuchFieldException if the field does  
      *            not exist.  
      * @exception SecurityException if you do not have access to public  
      *            members of this class.  
      */  
     public native Field getField(String name) throws NoSuchFieldException, SecurityException;  
       
     /**  
      * Get a field declared in this class.  
      * @param name the name of the field.  
      * @return the field.  
      * @exception NoSuchFieldException if the field does  
      *            not exist in this class.  
      * @exception SecurityException if you do not have access to  
      *            non-public members of this class.  
      */  
     public native Field getDeclaredField(String name) throws NoSuchFieldException, SecurityException;  
       
     /**  
      * Get all public fields from this class.  
      *  
      * @return all public fields in this class.  
      * @exception SecurityException if you do not have access to public  
      *            members of this class.  
      */  
     public native Field[] getFields() throws SecurityException;  
   
     /**  
      * Get all fields declared in this class.  
      *  
      * @return all fieilds declared in this class.  
      * @exception SecurityException if you do not have access to  
      *            non-public members of this class.  
      */  
     public native Field[] getDeclaredFields() throws SecurityException;  
   
     /**  
      * Returns the <code>Package</code> in which this class is defined  
      * Returns null when this information is not available from the  
      * classloader of this class or when the classloader of this class  
      * is null.  
      *  
      * @since 1.2  
      */  
     public Package getPackage() {  
         ClassLoader cl = getClassLoader();  
         if (cl != null)  
             return cl.getPackage(ClassHelper.getPackagePortion(getName()));  
         else  
             return null;  
     }  
   
     /**  
      * Returns the protection domain of this class. If the classloader  
      * did not record the protection domain when creating this class  
      * the unknown protection domain is returned which has a <code>null</code>  
      * code source and all permissions.  
      *  
      * @exception SecurityException if a security manager exists and the caller  
      * does not have <code>RuntimePermission("getProtectionDomain")</code>.  
      *  
      * @since 1.2  
      */  
     public ProtectionDomain getProtectionDomain() {  
178          SecurityManager sm = System.getSecurityManager();          SecurityManager sm = System.getSecurityManager();
179          if (sm != null)          if (sm != null)
180              sm.checkPermission(protectionDomainPermission);            {
181                // Get the calling class and classloader
182                Class c = VMSecurityManager.getClassContext()[1];
183                ClassLoader cl = c.getClassLoader();
184                if (cl != null && cl != ClassLoader.systemClassLoader)
185                  sm.checkPermission(new RuntimePermission("getClassLoader"));
186              }
187            classloader = ClassLoader.systemClassLoader;
188          }
189        return classloader.loadClass(name, initialize);
190      }
191    
192      /**
193       * Get a new instance of this class by calling the no-argument constructor.
194       * The class is initialized if it has not been already. A security check
195       * may be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
196       * as well as <code>checkPackageAccess</code> both having to succeed.
197       *
198       * @return a new instance of this class
199       * @throws InstantiationException if there is not a no-arg constructor
200       *         for this class, including interfaces, abstract classes, arrays,
201       *         primitive types, and void; or if an exception occurred during
202       *         the constructor
203       * @throws IllegalAccessException if you are not allowed to access the
204       *         no-arg constructor because of scoping reasons
205       * @throws SecurityException if the security check fails
206       * @throws ExceptionInInitializerError if class initialization caused by
207       *         this call fails with an exception
208       */
209      public Object newInstance()
210        throws InstantiationException, IllegalAccessException
211      {
212        try
213          {
214            //XXX What about getConstructor(null).newInstance(null)?
215            return getConstructor(new Class[0]).newInstance(new Object[0]);
216          }
217        catch(SecurityException e)
218          {
219            //XXX Why is this trapped?
220            throw new IllegalAccessException("Cannot access no-arg constructor");
221          }
222        catch(IllegalArgumentException e)
223          {
224            throw new UnknownError("IllegalArgumentException thrown from Constructor.newInstance().  Something is rotten in Denmark.");
225          }
226        catch(InvocationTargetException e)
227          {
228            //XXX Chain this.
229            throw new InstantiationException("Target threw an exception.");
230          }
231        catch(NoSuchMethodException e)
232          {
233            //XXX Chain this.
234            throw new InstantiationException("Method not found");
235          }
236      }
237    
238      /**
239       * Discover whether an Object is an instance of this Class.  Think of it
240       * as almost like <code>o instanceof (this class)</code>.
241       *
242       * @param o the Object to check
243       * @return whether o is an instance of this class
244       * @since 1.1
245       */
246      public native boolean isInstance(Object o);
247    
248      /**
249       * Discover whether an instance of the Class parameter would be an
250       * instance of this Class as well.  Think of doing
251       * <code>isInstance(c.newInstance())</code> or even
252       * <code>c.newInstance() instanceof (this class)</code>. While this
253       * checks widening conversions for objects, it must be exact for primitive
254       * types.
255       *
256       * @param c the class to check
257       * @return whether an instance of c would be an instance of this class
258       *         as well
259       * @throws NullPointerException if c is null
260       * @since 1.1
261       */
262      public native boolean isAssignableFrom(Class c);
263    
264      /**
265       * Check whether this class is an interface or not.  Array types are not
266       * interfaces.
267       *
268       * @return whether this class is an interface or not
269       */
270      public native boolean isInterface();
271    
272      /**
273       * Return whether this class is an array type.
274       *
275       * @return whether this class is an array type
276       * @since 1.1
277       */
278      public boolean isArray()
279      {
280        return getName().charAt(0) == '[';
281      }
282    
283      /**
284       * Return whether this class is a primitive type.  A primitive type class
285       * is a class representing a kind of "placeholder" for the various
286       * primitive types, or void.  You can access the various primitive type
287       * classes through java.lang.Boolean.TYPE, java.lang.Integer.TYPE, etc.,
288       * or through boolean.class, int.class, etc.
289       *
290       * @return whether this class is a primitive type
291       * @see Boolean#TYPE
292       * @see Byte#TYPE
293       * @see Character#TYPE
294       * @see Short#TYPE
295       * @see Integer#TYPE
296       * @see Long#TYPE
297       * @see Float#TYPE
298       * @see Double#TYPE
299       * @see Void#TYPE
300       * @since 1.1
301       */
302      public native boolean isPrimitive();
303    
304      /**
305       * Get the name of this class, separated by dots for package separators.
306       * Primitive types and arrays are encoded as:
307       * <pre>
308       * boolean             Z
309       * byte                B
310       * char                C
311       * short               S
312       * int                 I
313       * long                J
314       * float               F
315       * double              D
316       * void                V
317       * array type          [<em>element type</em>
318       * class or interface, alone: &lt;dotted name&gt;
319       * class or interface, as element type: L&lt;dotten name&gt;;
320       *
321       * @return the name of this class
322       */
323      public native String getName();
324    
325      /**
326       * Get the ClassLoader that loaded this class.  If it was loaded by the
327       * system classloader, this method will return null. If there is a security
328       * manager, and the caller's class loader does not match the requested
329       * one, a security check of <code>RuntimePermission("getClassLoader")</code>
330       * must first succeed. Primitive types and void return null.
331       *
332       * @return the ClassLoader that loaded this class
333       * @throws SecurityException if the security check fails
334       * @see ClassLoader
335       * @see RuntimePermission
336       */
337      public native ClassLoader getClassLoader();
338    
339      /**
340       * Get the direct superclass of this class.  If this is an interface,
341       * Object, a primitive type, or void, it will return null. If this is an
342       * array type, it will return Object.
343       *
344       * @return the direct superclass of this class
345       */
346      public native Class getSuperclass();
347    
348      /**
349       * Returns the <code>Package</code> in which this class is defined
350       * Returns null when this information is not available from the
351       * classloader of this class or when the classloader of this class
352       * is null.
353       *
354       * @return the package for this class, if it is available
355       * @since 1.2
356       */
357      public Package getPackage()
358      {
359        ClassLoader cl = getClassLoader();
360        if (cl != null)
361          return cl.getPackage(ClassHelper.getPackagePortion(getName()));
362        return null;
363      }
364    
365      /**
366       * Get the interfaces this class <EM>directly</EM> implements, in the
367       * order that they were declared. This returns an empty array, not null,
368       * for Object, primitives, void, and classes or interfaces with no direct
369       * superinterface. Array types return Cloneable and Serializable.
370       *
371       * @return the interfaces this class directly implements
372       */
373      public native Class[] getInterfaces();
374    
375      /**
376       * If this is an array, get the Class representing the type of array.
377       * Examples: "[[Ljava.lang.String;" would return "[Ljava.lang.String;", and
378       * calling getComponentType on that would give "java.lang.String".  If
379       * this is not an array, returns null.
380       *
381       * @return the array type of this class, or null
382       * @see Array
383       * @since 1.1
384       */
385      public Class getComponentType()
386      {
387        if(isArray())
388          try
389            {
390              //XXX This should not initialize the component type.
391              return Class.forName(getName().substring(1));
392            }
393          catch(ClassNotFoundException e)
394            {
395            }
396        return null;
397      }
398    
399          if (pd == null)    /**
400              return unknownProtectionDomain;     * Get the modifiers of this class.  These can be decoded using Modifier,
401       * and is limited to one of public, protected, or private, and any of
402       * final, static, abstract, or interface. An array class has the same
403       * public, protected, or private modifier as its component type, and is
404       * marked final but not an interface. Primitive types and void are marked
405       * public and final, but not an interface.
406       *
407       * @return the modifiers of this class
408       * @see Modifer
409       * @since 1.1
410       */
411      public native int getModifiers();
412    
413      /**
414       * Get the signers of this class. This returns null if there are no signers,
415       * such as for primitive types or void.
416       *
417       * @return the signers of this class
418       * @since 1.1
419       */
420      public Object[] getSigners()
421      {
422        return signers;
423      }
424    
425      /**
426       * Set the signers of this class.
427       *
428       * @param signers the signers of this class
429       */
430      void setSigners(Object[] signers)
431      {
432        this.signers = signers;
433      }
434    
435      /**
436       * If this is a nested or inner class, return the class that declared it.
437       * If not, return null.
438       *
439       * @return the declaring class of this class
440       * @since 1.1
441       */
442      public native Class getDeclaringClass();
443    
444      /**
445       * Get all the public member classes and interfaces declared in this
446       * class or inherited from superclasses. This returns an array of length
447       * 0 if there are no member classes, including for primitive types. A
448       * security check may be performed, with
449       * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
450       * <code>checkPackageAccess</code> both having to succeed.
451       *
452       * @return all public member classes in this class
453       * @throws SecurityException if the security check fails
454       * @since 1.1
455       */
456      public native Class[] getClasses();
457    
458      /**
459       * Get all the public fields declared in this class or inherited from
460       * superclasses. This returns an array of length 0 if there are no fields,
461       * including for primitive types. This does not return the implicit length
462       * field of arrays. A security check may be performed, with
463       * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
464       * <code>checkPackageAccess</code> both having to succeed.
465       *
466       * @return all public fields in this class
467       * @throws SecurityException if the security check fails
468       * @since 1.1
469       */
470      public native Field[] getFields();
471    
472      /**
473       * Get all the public methods declared in this class or inherited from
474       * superclasses. This returns an array of length 0 if there are no methods,
475       * including for primitive types. This does include the implicit methods of
476       * arrays and interfaces which mirror methods of Object, nor does it
477       * include constructors or the class initialization methods. The Virtual
478       * Machine allows multiple methods with the same signature but differing
479       * return types; all such methods are in the returned array. A security
480       * check may be performed, with
481       * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
482       * <code>checkPackageAccess</code> both having to succeed.
483       *
484       * @return all public methods in this class
485       * @throws SecurityException if the security check fails
486       * @since 1.1
487       */
488      public native Method[] getMethods();
489    
490      /**
491       * Get all the public constructors of this class. This returns an array of
492       * length 0 if there are no constructors, including for primitive types,
493       * arrays, and interfaces. It does, however, include the default
494       * constructor if one was supplied by the compiler. A security check may
495       * be performed, with <code>checkMemberAccess(this, Member.PUBLIC)</code>
496       * as well as <code>checkPackageAccess</code> both having to succeed.
497       *
498       * @return all public constructors in this class
499       * @throws SecurityException if the security check fails
500       * @since 1.1
501       */
502      public native Constructor[] getConstructors();
503    
504      /**
505       * Get a public field declared or inherited in this class, where name is
506       * its simple name. If the class contains multiple accessible fields by
507       * that name, an arbitrary one is returned. The implicit length field of
508       * arrays is not available. A security check may be performed, with
509       * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
510       * <code>checkPackageAccess</code> both having to succeed.
511       *
512       * @param name the name of the field
513       * @return the field
514       * @throws NoSuchFieldException if the field does not exist
515       * @throws SecurityException if the security check fails
516       * @see #getFields()
517       * @since 1.1
518       */
519      public native Field getField(String name) throws NoSuchFieldException;
520    
521      /**
522       * Get a public method declared or inherited in this class, where name is
523       * its simple name. The implicit methods of Object are not available from
524       * arrays or interfaces.  Constructors (named "<init>" in the class file)
525       * and class initializers (name "<clinit>") are not available.  The Virtual
526       * Machine allows multiple methods with the same signature but differing
527       * return types, and the class can inherit multiple methods of the same
528       * return type; in such a case the most specific return types are favored,
529       * then the final choice is arbitrary. If the method takes no argument, an
530       * array of zero elements and null are equivalent for the types argument.
531       * A security check may be performed, with
532       * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
533       * <code>checkPackageAccess</code> both having to succeed.
534       *
535       * @param name the name of the method
536       * @param types the type of each parameter
537       * @return the method
538       * @throws NoSuchMethodException if the method does not exist
539       * @throws SecurityException if the security check fails
540       * @see #getMethods()
541       * @since 1.1
542       */
543       public native Method getMethod(String name, Class[] args)
544         throws NoSuchMethodException;
545    
546      /**
547       * Get a public constructor declared in this class. If the constructor takes
548       * no argument, an array of zero elements and null are equivalent for the
549       * types argument. A security check may be performed, with
550       * <code>checkMemberAccess(this, Member.PUBLIC)</code> as well as
551       * <code>checkPackageAccess</code> both having to succeed.
552       *
553       * @param types the type of each parameter
554       * @return the constructor
555       * @throws NoSuchMethodException if the constructor does not exist
556       * @throws SecurityException if the security check fails
557       * @see #getConstructors()
558       * @since 1.1
559       */
560      public native Constructor getConstructor(Class[] args)
561        throws NoSuchMethodException;
562    
563      /**
564       * Get all the declared member classes and interfaces in this class, but
565       * not those inherited from superclasses. This returns an array of length
566       * 0 if there are no member classes, including for primitive types. A
567       * security check may be performed, with
568       * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
569       * <code>checkPackageAccess</code> both having to succeed.
570       *
571       * @return all declared member classes in this class
572       * @throws SecurityException if the security check fails
573       * @since 1.1
574       */
575      public native Class[] getDeclaredClasses();
576    
577      /**
578       * Get all the declared fields in this class, but not those inherited from
579       * superclasses. This returns an array of length 0 if there are no fields,
580       * including for primitive types. This does not return the implicit length
581       * field of arrays. A security check may be performed, with
582       * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
583       * <code>checkPackageAccess</code> both having to succeed.
584       *
585       * @return all declared fields in this class
586       * @throws SecurityException if the security check fails
587       * @since 1.1
588       */
589      public native Field[] getDeclaredFields();
590    
591      /**
592       * Get all the declared methods in this class, but not those inherited from
593       * superclasses. This returns an array of length 0 if there are no methods,
594       * including for primitive types. This does include the implicit methods of
595       * arrays and interfaces which mirror methods of Object, nor does it
596       * include constructors or the class initialization methods. The Virtual
597       * Machine allows multiple methods with the same signature but differing
598       * return types; all such methods are in the returned array. A security
599       * check may be performed, with
600       * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
601       * <code>checkPackageAccess</code> both having to succeed.
602       *
603       * @return all declared methods in this class
604       * @throws SecurityException if the security check fails
605       * @since 1.1
606       */
607      public native Method[] getDeclaredMethods();
608    
609      /**
610       * Get all the declared constructors of this class. This returns an array of
611       * length 0 if there are no constructors, including for primitive types,
612       * arrays, and interfaces. It does, however, include the default
613       * constructor if one was supplied by the compiler. A security check may
614       * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
615       * as well as <code>checkPackageAccess</code> both having to succeed.
616       *
617       * @return all constructors in this class
618       * @throws SecurityException if the security check fails
619       * @since 1.1
620       */
621      public native Constructor[] getDeclaredConstructors();
622    
623      /**
624       * Get a field declared in this class, where name is its simple name. The
625       * implicit length field of arrays is not available. A security check may
626       * be performed, with <code>checkMemberAccess(this, Member.DECLARED)</code>
627       * as well as <code>checkPackageAccess</code> both having to succeed.
628       *
629       * @param name the name of the field
630       * @return the field
631       * @throws NoSuchFieldException if the field does not exist
632       * @throws SecurityException if the security check fails
633       * @see #getDeclaredFields()
634       * @since 1.1
635       */
636      public native Field getDeclaredField(String name)
637        throws NoSuchFieldException;
638    
639      /**
640       * Get a method declared in this class, where name is its simple name. The
641       * implicit methods of Object are not available from arrays or interfaces.
642       * Constructors (named "<init>" in the class file) and class initializers
643       * (name "<clinit>") are not available.  The Virtual Machine allows
644       * multiple methods with the same signature but differing return types; in
645       * such a case the most specific return types are favored, then the final
646       * choice is arbitrary. If the method takes no argument, an array of zero
647       * elements and null are equivalent for the types argument. A security
648       * check may be performed, with
649       * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
650       * <code>checkPackageAccess</code> both having to succeed.
651       *
652       * @param name the name of the method
653       * @param types the type of each parameter
654       * @return the method
655       * @throws NoSuchMethodException if the method does not exist
656       * @throws SecurityException if the security check fails
657       * @see #getDeclaredMethods()
658       * @since 1.1
659       */
660       public native Method getDeclaredMethod(String name, Class[] args)
661         throws NoSuchMethodException;
662    
663      /**
664       * Get a constructor declared in this class. If the constructor takes no
665       * argument, an array of zero elements and null are equivalent for the
666       * types argument. A security check may be performed, with
667       * <code>checkMemberAccess(this, Member.DECLARED)</code> as well as
668       * <code>checkPackageAccess</code> both having to succeed.
669       *
670       * @param types the type of each parameter
671       * @return the constructor
672       * @throws NoSuchMethodException if the constructor does not exist
673       * @throws SecurityException if the security check fails
674       * @see #getDeclaredConstructors()
675       * @since 1.1
676       */
677      public native Constructor getDeclaredConstructor(Class[] args)
678        throws NoSuchMethodException;
679    
680      /**
681       * Get a resource using this class's package using the
682       * getClassLoader().getResourceAsStream() method.  If this class was loaded
683       * using the system classloader, ClassLoader.getSystemResource() is used
684       * instead.
685       *
686       * <p>If the name you supply is absolute (it starts with a <code>/</code>),
687       * then it is passed on to getResource() as is.  If it is relative, the
688       * package name is prepended, and <code>.</code>'s are replaced with
689       * <code>/</code>.
690       *
691       * <p>The URL returned is system- and classloader-dependent, and could
692       * change across implementations.
693       *
694       * @param name the name of the resource, generally a path
695       * @return an InputStream with the contents of the resource in it, or null
696       * @throws NullPointerException if name is null
697       * @since 1.1
698       */
699      public InputStream getResourceAsStream(String name)
700      {
701        if (name.length() > 0 && name.charAt(0) != '/')
702            name = ClassHelper.getPackagePortion(getName()).replace('.','/')
703              + "/" + name;
704        ClassLoader c = getClassLoader();
705        if (c == null)
706          return ClassLoader.getSystemResourceAsStream(name);
707        return c.getResourceAsStream(name);
708      }
709    
710      /**
711       * Get a resource URL using this class's package using the
712       * getClassLoader().getResource() method.  If this class was loaded using
713       * the system classloader, ClassLoader.getSystemResource() is used instead.
714       *
715       * <p>If the name you supply is absolute (it starts with a <code>/</code>),
716       * then it is passed on to getResource() as is.  If it is relative, the
717       * package name is prepended, and <code>.</code>'s are replaced with
718       * <code>/</code>.
719       *
720       * <p>The URL returned is system- and classloader-dependent, and could
721       * change across implementations.
722       *
723       * @param name the name of the resource, generally a path
724       * @return the URL to the resource
725       * @throws NullPointerException if name is null
726       * @since 1.1
727       */
728      public URL getResource(String name)
729      {
730        if(name.length() > 0 && name.charAt(0) != '/')
731          name = ClassHelper.getPackagePortion(getName()).replace('.','/')
732            + "/" + name;
733        ClassLoader c = getClassLoader();
734        if (c == null)
735          return ClassLoader.getSystemResource(name);
736        return c.getResource(name);
737      }
738    
739      /**
740       * Returns the protection domain of this class. If the classloader did not
741       * record the protection domain when creating this class the unknown
742       * protection domain is returned which has a <code>null</code> code source
743       * and all permissions. A security check may be performed, with
744       * <code>RuntimePermission("getProtectionDomain")</code>.
745       *
746       * @return the protection domain
747       * @throws SecurityException if the security check fails
748       * @see RuntimePermission
749       * @since 1.2
750       */
751      public ProtectionDomain getProtectionDomain()
752      {
753        SecurityManager sm = System.getSecurityManager();
754        if (sm != null)
755          sm.checkPermission(protectionDomainPermission);
756    
757        return pd == null ? unknownProtectionDomain : pd;
758      }
759    
760      /**
761       * Returns the desired assertion status of this class, if it were to be
762       * initialized at this moment. The class assertion status, if set, is
763       * returned; the backup is the default package status; then if there is
764       * a class loader, that default is returned; and finally the system default
765       * is returned. This method seldom needs calling in user code, but exists
766       * for compilers to implement the assert statement. Note that there is no
767       * guarantee that the result of this method matches the class's actual
768       * assertion status.
769       *
770       * @return the desired assertion status
771       * @see ClassLoader#setClassAssertionStatus(String, boolean)
772       * @see ClassLoader#setPackageAssertionStatus(String, boolean)
773       * @see ClassLoader#setDefaultAssertionStatus(boolean)
774       * @since 1.4
775       * @XXX For 1.4 compliance, implement this method.
776      public boolean desiredAssertionStatus()
777      {
778        ClassLoader c = getClassLoader();
779        Object status;
780        if (c == null)
781          return VMClassLoader.defaultAssertionStatus();
782        if (c.classAssertionStatus != null)
783          synchronized (c)
784            {
785              status = c.classAssertionStatus.get(getName());
786              if (status != null)
787                return status.equals(Boolean.TRUE);
788            }
789        else
790          {
791            status = ClassLoader.systemClassAssertionStatus.get(getName());
792            if (status != null)
793              return status.equals(Boolean.TRUE);
794          }
795        if (c.packageAssertionStatus != null)
796          synchronized (c)
797            {
798              String name = ClassHelper.getPackagePortion(getName());
799              if ("".equals(name))
800                status = c.packageAssertionStatus.get(null);
801              else
802                do
803                  {
804                    status = c.packageAssertionStatus.get(name);
805                    name = ClassHelper.getPackagePortion(name);
806                  }
807                while (! "".equals(name) && status == null);
808              if (status != null)
809                return status.equals(Boolean.TRUE);
810            }
811        else
812          {
813            String name = ClassHelper.getPackagePortion(getName());
814            if ("".equals(name))
815              status = ClassLoader.systemPackageAssertionStatus.get(null);
816          else          else
817              return pd;            do
818      }              {
819                  status = ClassLoader.systemPackageAssertionStatus.get(name);
820                  name = ClassHelper.getPackagePortion(name);
821                }
822              while (! "".equals(name) && status == null);
823            if (status != null)
824              return status.equals(Boolean.TRUE);
825          }
826        return c.defaultAssertionStatus;
827      }
828      */
829  }  }
830    

Legend:
Removed from v.1.17  
changed lines
  Added in v.1.18

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26