1 // Class.java - Representation of a Java class.
3 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
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 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 ();
39 public native Constructor getConstructor (Class[] parameterTypes)
40 throws NoSuchMethodException, SecurityException;
42 // This is used to implement getConstructors and
43 // getDeclaredConstructors.
44 private native Constructor[] _getConstructors (boolean declared)
45 throws SecurityException;
47 public Constructor[] getConstructors () throws SecurityException
49 return _getConstructors (false);
52 public native Constructor getDeclaredConstructor (Class[] parameterTypes)
53 throws NoSuchMethodException, SecurityException;
55 public native Class[] getDeclaredClasses () throws SecurityException;
57 public Constructor[] getDeclaredConstructors () throws SecurityException
59 return _getConstructors (true);
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;
70 // This is marked as unimplemented in the JCL book.
71 public native Class getDeclaringClass ();
73 private native Field getField (String fieldName, int hash)
74 throws NoSuchFieldException, SecurityException;
76 public Field getField (String fieldName)
77 throws NoSuchFieldException, SecurityException
79 SecurityManager s = System.getSecurityManager();
81 s.checkMemberAccess (this, java.lang.reflect.Member.DECLARED);
82 Field fld = getField(fieldName, fieldName.hashCode());
84 throw new NoSuchFieldException(fieldName);
88 private native Field[] _getFields (Field[] result, int offset);
89 public native Field[] getFields () throws SecurityException;
91 public native Class[] getInterfaces ();
93 private final native void getSignature (StringBuffer buffer);
94 private static final native String getSignature (Class[] parameterTypes,
95 boolean is_construtor);
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;
102 public native int getModifiers ();
103 public native String getName ();
105 public java.net.URL getResource (String resourceName)
107 String name = resourcePath (resourceName);
108 ClassLoader loader = getClassLoader ();
110 return ClassLoader.getSystemResource (name);
112 return loader.getResource (name);
115 public java.io.InputStream getResourceAsStream (String resourceName)
117 String name = resourcePath (resourceName);
118 ClassLoader loader = getClassLoader ();
120 return ClassLoader.getSystemResourceAsStream (name);
122 return loader.getResourceAsStream (name);
125 private String resourcePath (String resourceName)
127 if (resourceName.startsWith ("/"))
128 return resourceName.substring (1);
132 c = c.getComponentType ();
134 String packageName = c.getName ().replace ('.', '/');
135 int end = packageName.lastIndexOf ('/');
139 return packageName.substring (0, end+1) + resourceName;
142 // FIXME: implement. Requires java.security.
143 public Object[] getSigners ()
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;
157 public String toString ()
161 return (isInterface () ? "interface " : "class ") + getName ();
164 // Don't allow new classes to be made.
169 // Do a security check.
170 private void checkMemberAccess (int flags)
172 SecurityManager sm = System.getSecurityManager();
174 sm.checkMemberAccess(this, flags);
177 // Initialize the class.
178 private native void initializeClass ();
181 protected native void finalize ();