1 // Class.java - Representation of a Java class.
3 /* Copyright (C) 1998, 1999, 2000 Cygnus Solutions
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 import java.io.Serializable;
13 import java.io.InputStream;
14 import java.lang.reflect.*;
17 * @author Tom Tromey <tromey@cygnus.com>
18 * @date October 1, 1998
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.
29 public final class Class implements Serializable
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 ();
37 public native Constructor getConstructor (Class[] parameterTypes)
38 throws NoSuchMethodException, SecurityException;
40 // This is used to implement getConstructors and
41 // getDeclaredConstructors.
42 private native Constructor[] _getConstructors (boolean declared)
43 throws SecurityException;
45 public Constructor[] getConstructors () throws SecurityException
47 return _getConstructors (false);
50 public native Constructor getDeclaredConstructor (Class[] parameterTypes)
51 throws NoSuchMethodException, SecurityException;
53 public native Class[] getDeclaredClasses () throws SecurityException;
55 public Constructor[] getDeclaredConstructors () throws SecurityException
57 return _getConstructors (true);
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;
68 // This is marked as unimplemented in the JCL book.
69 public native Class getDeclaringClass ();
71 private native Field getField (String fieldName, int hash)
72 throws NoSuchFieldException, SecurityException;
74 public Field getField (String fieldName)
75 throws NoSuchFieldException, SecurityException
77 SecurityManager s = System.getSecurityManager();
79 s.checkMemberAccess (this, java.lang.reflect.Member.DECLARED);
80 Field fld = getField(fieldName, fieldName.hashCode());
82 throw new NoSuchFieldException(fieldName);
86 private native Field[] _getFields (Field[] result, int offset);
87 public native Field[] getFields () throws SecurityException;
89 public native Class[] getInterfaces ();
91 private final native void getSignature (StringBuffer buffer);
92 private static final native String getSignature (Class[] parameterTypes,
93 boolean is_construtor);
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;
100 public native int getModifiers ();
101 public native String getName ();
103 public java.net.URL getResource (String resourceName)
105 String name = resourcePath (resourceName);
106 ClassLoader loader = getClassLoader ();
108 return ClassLoader.getSystemResource (name);
110 return loader.getResource (name);
113 public java.io.InputStream getResourceAsStream (String resourceName)
115 String name = resourcePath (resourceName);
116 ClassLoader loader = getClassLoader ();
118 return ClassLoader.getSystemResourceAsStream (name);
120 return loader.getResourceAsStream (name);
123 private String resourcePath (String resourceName)
125 if (resourceName.startsWith ("/"))
126 return resourceName.substring (1);
130 c = c.getComponentType ();
132 String packageName = c.getName ().replace ('.', '/');
133 int end = packageName.lastIndexOf ('/');
137 return packageName.substring (0, end+1) + resourceName;
140 // FIXME: implement. Requires java.security.
141 public Object[] getSigners ()
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;
155 public String toString ()
159 return (isInterface () ? "interface " : "class ") + getName ();
162 // Don't allow new classes to be made.
167 // Do a security check.
168 private void checkMemberAccess (int flags)
170 SecurityManager sm = System.getSecurityManager();
172 sm.checkMemberAccess(this, flags);
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)
179 Throwable new_val = null;
185 hackRunInitializers ();
187 new_val = new ExceptionInInitializerError (old_exception);
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 ();
201 // Initialize the class.
202 private native void initializeClass ();
205 protected native void finalize ();