1 // Class.java - Representation of a Java class.
3 /* Copyright (C) 1998, 1999, 2000, 2002, 2003 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.*;
15 import java.security.*;
18 * @author Tom Tromey <tromey@cygnus.com>
19 * @date October 1, 1998
22 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
23 * "The Java Language Specification", ISBN 0-201-63451-1
24 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
25 * plus gcj compiler sources (to determine object layout)
26 * Status: Sufficient for our purposes, but some methods missing
27 * and some not implemented.
30 public final class Class implements Serializable
32 public static native Class forName (String className)
33 throws ClassNotFoundException;
35 public static native Class forName (String className, boolean initialize,
37 throws ClassNotFoundException;
38 public native Class[] getClasses ();
39 public native ClassLoader getClassLoader ();
40 public native Class getComponentType ();
42 public native Constructor getConstructor (Class[] parameterTypes)
43 throws NoSuchMethodException, SecurityException;
45 // This is used to implement getConstructors and
46 // getDeclaredConstructors.
47 private native Constructor[] _getConstructors (boolean declared)
48 throws SecurityException;
50 public Constructor[] getConstructors () throws SecurityException
52 return _getConstructors (false);
55 public native Constructor getDeclaredConstructor (Class[] parameterTypes)
56 throws NoSuchMethodException, SecurityException;
58 public native Class[] getDeclaredClasses () throws SecurityException;
60 public Constructor[] getDeclaredConstructors () throws SecurityException
62 return _getConstructors (true);
65 public native Field getDeclaredField (String fieldName)
66 throws NoSuchFieldException, SecurityException;
67 public native Field[] getDeclaredFields () throws SecurityException;
69 private native Method _getDeclaredMethod (String methodName,
70 Class[] parameterTypes);
72 public Method getDeclaredMethod (String methodName, Class[] parameterTypes)
73 throws NoSuchMethodException, SecurityException
75 memberAccessCheck(Member.DECLARED);
77 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
78 throw new NoSuchMethodException(methodName);
80 Method m = _getDeclaredMethod(methodName, parameterTypes);
82 throw new NoSuchMethodException (methodName);
86 public native Method[] getDeclaredMethods () throws SecurityException;
88 // This is marked as unimplemented in the JCL book.
89 public native Class getDeclaringClass ();
91 private native Field getField (String fieldName, int hash)
92 throws NoSuchFieldException, SecurityException;
94 public Field getField (String fieldName)
95 throws NoSuchFieldException, SecurityException
97 memberAccessCheck (Member.PUBLIC);
98 Field fld = getField(fieldName, fieldName.hashCode());
100 throw new NoSuchFieldException(fieldName);
104 private native Field[] _getFields (Field[] result, int offset);
105 public native Field[] getFields () throws SecurityException;
108 * Returns the <code>Package</code> in which this class is defined
109 * Returns null when this information is not available from the
110 * classloader of this class or when the classloader of this class
115 public Package getPackage()
117 ClassLoader cl = getClassLoader();
120 String name = getName();
122 int idx = name.lastIndexOf('.');
124 pkg = name.substring(0, idx);
125 return cl.getPackage(pkg);
131 public native Class[] getInterfaces ();
133 private final native void getSignature (StringBuffer buffer);
134 private static final native String getSignature (Class[] parameterTypes,
135 boolean is_construtor);
137 public native Method _getMethod (String methodName, Class[] parameterTypes);
139 public Method getMethod (String methodName, Class[] parameterTypes)
140 throws NoSuchMethodException, SecurityException
142 memberAccessCheck(Member.PUBLIC);
144 if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
145 throw new NoSuchMethodException(methodName);
147 Method m = _getMethod(methodName, parameterTypes);
149 throw new NoSuchMethodException (methodName);
153 private native int _getMethods (Method[] result, int offset);
154 public native Method[] getMethods () throws SecurityException;
156 public native int getModifiers ();
157 public native String getName ();
159 public java.net.URL getResource (String resourceName)
161 String name = resourcePath (resourceName);
162 ClassLoader loader = getClassLoader ();
164 return ClassLoader.getSystemResource (name);
166 return loader.getResource (name);
169 public java.io.InputStream getResourceAsStream (String resourceName)
171 String name = resourcePath (resourceName);
172 ClassLoader loader = getClassLoader ();
174 return ClassLoader.getSystemResourceAsStream (name);
176 return loader.getResourceAsStream (name);
179 private String resourcePath (String resourceName)
181 if (resourceName.startsWith ("/"))
182 return resourceName.substring (1);
186 c = c.getComponentType ();
188 String packageName = c.getName ().replace ('.', '/');
189 int end = packageName.lastIndexOf ('/');
193 return packageName.substring (0, end+1) + resourceName;
196 public native Object[] getSigners ();
197 native void setSigners(Object[] signers);
199 public native Class getSuperclass ();
200 public native boolean isArray ();
201 public native boolean isAssignableFrom (Class cls);
202 public native boolean isInstance (Object obj);
203 public native boolean isInterface ();
204 public native boolean isPrimitive ();
205 public native Object newInstance ()
206 throws InstantiationException, IllegalAccessException;
208 // We need a native method to retrieve the protection domain, because we
209 // can't add fields to java.lang.Class that are accessible from Java.
210 private native ProtectionDomain getProtectionDomain0();
213 * Returns the protection domain of this class. If the classloader
214 * did not record the protection domain when creating this class
215 * the unknown protection domain is returned which has a <code>null</code>
216 * code source and all permissions.
218 * @exception SecurityException if a security manager exists and the caller
219 * does not have <code>RuntimePermission("getProtectionDomain")</code>.
223 public ProtectionDomain getProtectionDomain()
225 SecurityManager sm = System.getSecurityManager();
227 sm.checkPermission(VMClassLoader.protectionDomainPermission);
229 ProtectionDomain protectionDomain = getProtectionDomain0();
231 if (protectionDomain == null)
232 return VMClassLoader.unknownProtectionDomain;
234 return protectionDomain;
237 public String toString ()
241 return (isInterface () ? "interface " : "class ") + getName ();
245 * Returns the desired assertion status of this class, if it were to be
246 * initialized at this moment. The class assertion status, if set, is
247 * returned; the backup is the default package status; then if there is
248 * a class loader, that default is returned; and finally the system default
249 * is returned. This method seldom needs calling in user code, but exists
250 * for compilers to implement the assert statement. Note that there is no
251 * guarantee that the result of this method matches the class's actual
254 * @return the desired assertion status
255 * @see ClassLoader#setClassAssertionStatus(String, boolean)
256 * @see ClassLoader#setPackageAssertionStatus(String, boolean)
257 * @see ClassLoader#setDefaultAssertionStatus(boolean)
260 public boolean desiredAssertionStatus()
262 ClassLoader c = getClassLoader();
265 return VMClassLoader.defaultAssertionStatus();
266 if (c.classAssertionStatus != null)
269 status = c.classAssertionStatus.get(getName());
271 return status.equals(Boolean.TRUE);
275 status = ClassLoader.systemClassAssertionStatus.get(getName());
277 return status.equals(Boolean.TRUE);
279 if (c.packageAssertionStatus != null)
282 String name = getPackagePortion(getName());
284 status = c.packageAssertionStatus.get(null);
288 status = c.packageAssertionStatus.get(name);
289 name = getPackagePortion(name);
291 while (! "".equals(name) && status == null);
293 return status.equals(Boolean.TRUE);
297 String name = getPackagePortion(getName());
299 status = ClassLoader.systemPackageAssertionStatus.get(null);
303 status = ClassLoader.systemPackageAssertionStatus.get(name);
304 name = getPackagePortion(name);
306 while (! "".equals(name) && status == null);
308 return status.equals(Boolean.TRUE);
310 return c.defaultAssertionStatus;
313 // Don't allow new classes to be made.
318 // Initialize the class.
319 private native void initializeClass ();
322 protected native void finalize () throws Throwable;
325 * Strip the last portion of the name (after the last dot).
327 * @param name the name to get package of
328 * @return the package name, or "" if no package
330 private static String getPackagePortion(String name)
332 int lastInd = name.lastIndexOf('.');
335 return name.substring(0, lastInd);
339 * Perform security checks common to all of the methods that
340 * get members of this Class.
342 private void memberAccessCheck(int which)
344 SecurityManager sm = System.getSecurityManager();
347 sm.checkMemberAccess(this, which);
348 Package pkg = getPackage();
350 sm.checkPackageAccess(pkg.getName());