OSDN Git Service

2000-11-25 Anthony Green <green@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / Class.java
1 // Class.java - Representation of a Java class.
2
3 /* Copyright (C) 1998, 1999, 2000  Free Software Foundation
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 package java.lang;
12 import java.io.Serializable;
13 import java.io.InputStream;
14 import java.lang.reflect.*;
15
16 /**
17  * @author Tom Tromey <tromey@cygnus.com>
18  * @date October 1, 1998 
19  */
20
21 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
22  * "The Java Language Specification", ISBN 0-201-63451-1
23  * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
24  * plus gcj compiler sources (to determine object layout)
25  * Status:  Sufficient for our purposes, but some methods missing
26  * and some not implemented.
27  */
28
29 public final class Class implements Serializable
30 {
31   public static native Class forName (String className)
32     throws ClassNotFoundException;
33   public static native Class forName (String className, ClassLoader loader)
34     throws ClassNotFoundException;
35   public native Class[] getClasses ();
36   public native ClassLoader getClassLoader ();
37   public native Class getComponentType ();
38
39   public native Constructor getConstructor (Class[] parameterTypes)
40     throws NoSuchMethodException, SecurityException;
41
42   // This is used to implement getConstructors and
43   // getDeclaredConstructors.
44   private native Constructor[] _getConstructors (boolean declared)
45     throws SecurityException;
46
47   public Constructor[] getConstructors () throws SecurityException
48   {
49     return _getConstructors (false);
50   }
51
52   public native Constructor getDeclaredConstructor (Class[] parameterTypes)
53     throws NoSuchMethodException, SecurityException;
54
55   public native Class[] getDeclaredClasses () throws SecurityException;
56
57   public Constructor[] getDeclaredConstructors () throws SecurityException
58   {
59     return _getConstructors (true);
60   }
61
62   public native Field getDeclaredField (String fieldName)
63     throws NoSuchFieldException, SecurityException;
64   public native Field[] getDeclaredFields () throws SecurityException;
65   public native Method getDeclaredMethod (String methodName,
66                                           Class[] parameterTypes)
67     throws NoSuchMethodException, SecurityException;
68   public native Method[] getDeclaredMethods () throws SecurityException;
69
70   // This is marked as unimplemented in the JCL book.
71   public native Class getDeclaringClass ();
72
73   private native Field getField (String fieldName, int hash)
74     throws NoSuchFieldException, SecurityException;
75
76   public Field getField (String fieldName)
77     throws NoSuchFieldException, SecurityException
78   {
79     SecurityManager s = System.getSecurityManager();
80     if (s != null)
81       s.checkMemberAccess (this, java.lang.reflect.Member.DECLARED);
82     Field fld = getField(fieldName, fieldName.hashCode());
83     if (fld == null)
84       throw new NoSuchFieldException(fieldName);
85     return fld;
86   }
87
88   private native Field[] _getFields (Field[] result, int offset);
89   public native Field[] getFields () throws SecurityException;
90
91   public native Class[] getInterfaces ();
92
93   private final native void getSignature (StringBuffer buffer);
94   private static final native String getSignature (Class[] parameterTypes,
95                                                    boolean is_construtor);
96
97   public native Method getMethod (String methodName, Class[] parameterTypes)
98     throws NoSuchMethodException, SecurityException;
99   private native int _getMethods (Method[] result, int offset);
100   public native Method[] getMethods () throws SecurityException;
101
102   public native int getModifiers ();
103   public native String getName ();
104
105   public java.net.URL getResource (String resourceName)
106   {
107     String name = resourcePath (resourceName);
108     ClassLoader loader = getClassLoader ();
109     if (loader == null)
110       return ClassLoader.getSystemResource (name);
111     else
112       return loader.getResource (name);
113   }
114
115   public java.io.InputStream getResourceAsStream (String resourceName)
116   {
117     String name = resourcePath (resourceName);
118     ClassLoader loader = getClassLoader ();
119     if (loader == null)
120       return ClassLoader.getSystemResourceAsStream (name);
121     else
122       return loader.getResourceAsStream (name);
123   }
124
125   private String resourcePath (String resourceName)
126   {
127     if (resourceName.startsWith ("/"))
128       return resourceName.substring (1);
129
130     Class c = this;
131     while (c.isArray ())
132       c = c.getComponentType ();
133
134     String packageName = c.getName ().replace ('.', '/');
135     int end = packageName.lastIndexOf ('/');
136     if (end == -1)
137       return resourceName;
138     else
139       return packageName.substring (0, end+1) + resourceName;
140   }
141
142   // FIXME: implement.  Requires java.security.
143   public Object[] getSigners ()
144   {
145     return null;
146   }
147
148   public native Class getSuperclass ();
149   public native boolean isArray ();
150   public native boolean isAssignableFrom (Class cls);
151   public native boolean isInstance (Object obj);
152   public native boolean isInterface ();
153   public native boolean isPrimitive ();
154   public native Object newInstance ()
155     throws InstantiationException, IllegalAccessException;
156
157   public String toString ()
158   {
159     if (isPrimitive ())
160       return getName ();
161     return (isInterface () ? "interface " : "class ") + getName ();
162   }
163
164   // Don't allow new classes to be made.
165   private Class ()
166   {
167   }
168
169   // Do a security check.
170   private void checkMemberAccess (int flags)
171   {
172     SecurityManager sm = System.getSecurityManager();
173     if (sm != null)
174       sm.checkMemberAccess(this, flags);
175   }
176
177   // Initialize the class.
178   private native void initializeClass ();
179
180   // finalization
181   protected native void finalize ();
182 }