OSDN Git Service

127cc52457da6272e133d050948899c7caba312e
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / Class.java
1 // Class.java - Representation of a Java class.
2
3 /* Copyright (C) 1998, 1999, 2000  Cygnus Solutions
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 native Class[] getClasses ();
34   public native ClassLoader getClassLoader ();
35   public native Class getComponentType ();
36
37   public native Constructor getConstructor (Class[] parameterTypes)
38     throws NoSuchMethodException, SecurityException;
39
40   // This is used to implement getConstructors and
41   // getDeclaredConstructors.
42   private native Constructor[] _getConstructors (boolean declared)
43     throws SecurityException;
44
45   public Constructor[] getConstructors () throws SecurityException
46   {
47     return _getConstructors (false);
48   }
49
50   public native Constructor getDeclaredConstructor (Class[] parameterTypes)
51     throws NoSuchMethodException, SecurityException;
52
53   public native Class[] getDeclaredClasses () throws SecurityException;
54
55   public Constructor[] getDeclaredConstructors () throws SecurityException
56   {
57     return _getConstructors (true);
58   }
59
60   public native Field getDeclaredField (String fieldName)
61     throws NoSuchFieldException, SecurityException;
62   public native Field[] getDeclaredFields () throws SecurityException;
63   public native Method getDeclaredMethod (String methodName,
64                                           Class[] parameterTypes)
65     throws NoSuchMethodException, SecurityException;
66   public native Method[] getDeclaredMethods () throws SecurityException;
67
68   // This is marked as unimplemented in the JCL book.
69   public native Class getDeclaringClass ();
70
71   private native Field getField (String fieldName, int hash)
72     throws NoSuchFieldException, SecurityException;
73
74   public Field getField (String fieldName)
75     throws NoSuchFieldException, SecurityException
76   {
77     SecurityManager s = System.getSecurityManager();
78     if (s != null)
79       s.checkMemberAccess (this, java.lang.reflect.Member.DECLARED);
80     Field fld = getField(fieldName, fieldName.hashCode());
81     if (fld == null)
82       throw new NoSuchFieldException(fieldName);
83     return fld;
84   }
85
86   private native Field[] _getFields (Field[] result, int offset);
87   public native Field[] getFields () throws SecurityException;
88
89   public native Class[] getInterfaces ();
90
91   private final native void getSignature (StringBuffer buffer);
92   private static final native String getSignature (Class[] parameterTypes,
93                                                    boolean is_construtor);
94
95   public native Method getMethod (String methodName, Class[] parameterTypes)
96     throws NoSuchMethodException, SecurityException;
97   private native int _getMethods (Method[] result, int offset);
98   public native Method[] getMethods () throws SecurityException;
99
100   public native int getModifiers ();
101   public native String getName ();
102
103   public java.net.URL getResource (String resourceName)
104   {
105     String name = resourcePath (resourceName);
106     ClassLoader loader = getClassLoader ();
107     if (loader == null)
108       return ClassLoader.getSystemResource (name);
109     else
110       return loader.getResource (name);
111   }
112
113   public java.io.InputStream getResourceAsStream (String resourceName)
114   {
115     String name = resourcePath (resourceName);
116     ClassLoader loader = getClassLoader ();
117     if (loader == null)
118       return ClassLoader.getSystemResourceAsStream (name);
119     else
120       return loader.getResourceAsStream (name);
121   }
122
123   private String resourcePath (String resourceName)
124   {
125     if (resourceName.startsWith ("/"))
126       return resourceName.substring (1);
127
128     Class c = this;
129     while (c.isArray ())
130       c = c.getComponentType ();
131
132     String packageName = c.getName ().replace ('.', '/');
133     int end = packageName.lastIndexOf ('/');
134     if (end == -1)
135       return resourceName;
136     else
137       return packageName.substring (0, end+1) + resourceName;
138   }
139
140   // FIXME: implement.  Requires java.security.
141   public Object[] getSigners ()
142   {
143     return null;
144   }
145
146   public native Class getSuperclass ();
147   public native boolean isArray ();
148   public native boolean isAssignableFrom (Class cls);
149   public native boolean isInstance (Object obj);
150   public native boolean isInterface ();
151   public native boolean isPrimitive ();
152   public native Object newInstance ()
153     throws InstantiationException, IllegalAccessException;
154
155   public String toString ()
156   {
157     if (isPrimitive ())
158       return getName ();
159     return (isInterface () ? "interface " : "class ") + getName ();
160   }
161
162   // Don't allow new classes to be made.
163   private Class ()
164   {
165   }
166
167   // Do a security check.
168   private void checkMemberAccess (int flags)
169   {
170     SecurityManager sm = System.getSecurityManager();
171     if (sm != null)
172       sm.checkMemberAccess(this, flags);
173   }
174
175   // FIXME: this method exists only because we cannot catch Java
176   // exceptions from C++ code.  This is a helper for initializeClass.
177   private Throwable hackTrampoline (int what, Throwable old_exception)
178     {
179       Throwable new_val = null;
180       try
181         {
182           if (what == 0)
183             initializeClass ();
184           else if (what == 1)
185             hackRunInitializers ();
186           else if (what == 2)
187             new_val = new ExceptionInInitializerError (old_exception);
188         }
189       catch (Throwable t)
190         {
191           new_val = t;
192         }
193       return new_val;
194     }
195
196   // FIXME: this is a hack to let us run the class initializers.  We
197   // could do it inline in initializeClass() if we could catch Java
198   // exceptions from C++.
199   private native void hackRunInitializers ();
200
201   // Initialize the class.
202   private native void initializeClass ();
203
204   // finalization
205   protected native void finalize ();
206 }