| 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 | import java.security.*;
|
|---|
| 16 |
|
|---|
| 17 | /**
|
|---|
| 18 | * @author Tom Tromey <[email protected]>
|
|---|
| 19 | * @date October 1, 1998
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 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.
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | public final class Class implements Serializable
|
|---|
| 31 | {
|
|---|
| 32 | public static native Class forName (String className)
|
|---|
| 33 | throws ClassNotFoundException;
|
|---|
| 34 | /** @since 1.2 */
|
|---|
| 35 | public static native Class forName (String className, boolean initialize,
|
|---|
| 36 | ClassLoader loader)
|
|---|
| 37 | throws ClassNotFoundException;
|
|---|
| 38 | public native Class[] getClasses ();
|
|---|
| 39 | public native ClassLoader getClassLoader ();
|
|---|
| 40 | public native Class getComponentType ();
|
|---|
| 41 |
|
|---|
| 42 | public native Constructor getConstructor (Class[] parameterTypes)
|
|---|
| 43 | throws NoSuchMethodException, SecurityException;
|
|---|
| 44 |
|
|---|
| 45 | // This is used to implement getConstructors and
|
|---|
| 46 | // getDeclaredConstructors.
|
|---|
| 47 | private native Constructor[] _getConstructors (boolean declared)
|
|---|
| 48 | throws SecurityException;
|
|---|
| 49 |
|
|---|
| 50 | public Constructor[] getConstructors () throws SecurityException
|
|---|
| 51 | {
|
|---|
| 52 | return _getConstructors (false);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | public native Constructor getDeclaredConstructor (Class[] parameterTypes)
|
|---|
| 56 | throws NoSuchMethodException, SecurityException;
|
|---|
| 57 |
|
|---|
| 58 | public native Class[] getDeclaredClasses () throws SecurityException;
|
|---|
| 59 |
|
|---|
| 60 | public Constructor[] getDeclaredConstructors () throws SecurityException
|
|---|
| 61 | {
|
|---|
| 62 | return _getConstructors (true);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | public native Field getDeclaredField (String fieldName)
|
|---|
| 66 | throws NoSuchFieldException, SecurityException;
|
|---|
| 67 | public native Field[] getDeclaredFields () throws SecurityException;
|
|---|
| 68 | public native Method getDeclaredMethod (String methodName,
|
|---|
| 69 | Class[] parameterTypes)
|
|---|
| 70 | throws NoSuchMethodException, SecurityException;
|
|---|
| 71 | public native Method[] getDeclaredMethods () throws SecurityException;
|
|---|
| 72 |
|
|---|
| 73 | // This is marked as unimplemented in the JCL book.
|
|---|
| 74 | public native Class getDeclaringClass ();
|
|---|
| 75 |
|
|---|
| 76 | private native Field getField (String fieldName, int hash)
|
|---|
| 77 | throws NoSuchFieldException, SecurityException;
|
|---|
| 78 |
|
|---|
| 79 | public Field getField (String fieldName)
|
|---|
| 80 | throws NoSuchFieldException, SecurityException
|
|---|
| 81 | {
|
|---|
| 82 | SecurityManager s = System.getSecurityManager();
|
|---|
| 83 | if (s != null)
|
|---|
| 84 | s.checkMemberAccess (this, java.lang.reflect.Member.DECLARED);
|
|---|
| 85 | Field fld = getField(fieldName, fieldName.hashCode());
|
|---|
| 86 | if (fld == null)
|
|---|
| 87 | throw new NoSuchFieldException(fieldName);
|
|---|
| 88 | return fld;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | private native Field[] _getFields (Field[] result, int offset);
|
|---|
| 92 | public native Field[] getFields () throws SecurityException;
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Returns the <code>Package</code> in which this class is defined
|
|---|
| 96 | * Returns null when this information is not available from the
|
|---|
| 97 | * classloader of this class or when the classloader of this class
|
|---|
| 98 | * is null.
|
|---|
| 99 | *
|
|---|
| 100 | * @since 1.2
|
|---|
| 101 | */
|
|---|
| 102 | public Package getPackage()
|
|---|
| 103 | {
|
|---|
| 104 | ClassLoader cl = getClassLoader();
|
|---|
| 105 | if (cl != null)
|
|---|
| 106 | {
|
|---|
| 107 | String name = getName();
|
|---|
| 108 | String pkg = "";
|
|---|
| 109 | int idx = name.lastIndexOf('.');
|
|---|
| 110 | if (idx >= 0)
|
|---|
| 111 | pkg = name.substring(0, idx);
|
|---|
| 112 | return cl.getPackage(pkg);
|
|---|
| 113 | }
|
|---|
| 114 | else
|
|---|
| 115 | return null;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | public native Class[] getInterfaces ();
|
|---|
| 119 |
|
|---|
| 120 | private final native void getSignature (StringBuffer buffer);
|
|---|
| 121 | private static final native String getSignature (Class[] parameterTypes,
|
|---|
| 122 | boolean is_construtor);
|
|---|
| 123 |
|
|---|
| 124 | public native Method getMethod (String methodName, Class[] parameterTypes)
|
|---|
| 125 | throws NoSuchMethodException, SecurityException;
|
|---|
| 126 | private native int _getMethods (Method[] result, int offset);
|
|---|
| 127 | public native Method[] getMethods () throws SecurityException;
|
|---|
| 128 |
|
|---|
| 129 | public native int getModifiers ();
|
|---|
| 130 | public native String getName ();
|
|---|
| 131 |
|
|---|
| 132 | public java.net.URL getResource (String resourceName)
|
|---|
| 133 | {
|
|---|
| 134 | String name = resourcePath (resourceName);
|
|---|
| 135 | ClassLoader loader = getClassLoader ();
|
|---|
| 136 | if (loader == null)
|
|---|
| 137 | return ClassLoader.getSystemResource (name);
|
|---|
| 138 | else
|
|---|
| 139 | return loader.getResource (name);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | public java.io.InputStream getResourceAsStream (String resourceName)
|
|---|
| 143 | {
|
|---|
| 144 | String name = resourcePath (resourceName);
|
|---|
| 145 | ClassLoader loader = getClassLoader ();
|
|---|
| 146 | if (loader == null)
|
|---|
| 147 | return ClassLoader.getSystemResourceAsStream (name);
|
|---|
| 148 | else
|
|---|
| 149 | return loader.getResourceAsStream (name);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | private String resourcePath (String resourceName)
|
|---|
| 153 | {
|
|---|
| 154 | if (resourceName.startsWith ("/"))
|
|---|
| 155 | return resourceName.substring (1);
|
|---|
| 156 |
|
|---|
| 157 | Class c = this;
|
|---|
| 158 | while (c.isArray ())
|
|---|
| 159 | c = c.getComponentType ();
|
|---|
| 160 |
|
|---|
| 161 | String packageName = c.getName ().replace ('.', '/');
|
|---|
| 162 | int end = packageName.lastIndexOf ('/');
|
|---|
| 163 | if (end == -1)
|
|---|
| 164 | return resourceName;
|
|---|
| 165 | else
|
|---|
| 166 | return packageName.substring (0, end+1) + resourceName;
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | // FIXME: implement. Requires java.security.
|
|---|
| 170 | public Object[] getSigners ()
|
|---|
| 171 | {
|
|---|
| 172 | return null;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | public native Class getSuperclass ();
|
|---|
| 176 | public native boolean isArray ();
|
|---|
| 177 | public native boolean isAssignableFrom (Class cls);
|
|---|
| 178 | public native boolean isInstance (Object obj);
|
|---|
| 179 | public native boolean isInterface ();
|
|---|
| 180 | public native boolean isPrimitive ();
|
|---|
| 181 | public native Object newInstance ()
|
|---|
| 182 | throws InstantiationException, IllegalAccessException;
|
|---|
| 183 |
|
|---|
| 184 | // We need a native method to retrieve the protection domain, because we
|
|---|
| 185 | // can't add fields to java.lang.Class that are accessible from Java.
|
|---|
| 186 | private native ProtectionDomain getProtectionDomain0();
|
|---|
| 187 |
|
|---|
| 188 | /**
|
|---|
| 189 | * Returns the protection domain of this class. If the classloader
|
|---|
| 190 | * did not record the protection domain when creating this class
|
|---|
| 191 | * the unknown protection domain is returned which has a <code>null</code>
|
|---|
| 192 | * code source and all permissions.
|
|---|
| 193 | *
|
|---|
| 194 | * @exception SecurityException if a security manager exists and the caller
|
|---|
| 195 | * does not have <code>RuntimePermission("getProtectionDomain")</code>.
|
|---|
| 196 | *
|
|---|
| 197 | * @since 1.2
|
|---|
| 198 | */
|
|---|
| 199 | public ProtectionDomain getProtectionDomain()
|
|---|
| 200 | {
|
|---|
| 201 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 202 | if (sm != null)
|
|---|
| 203 | sm.checkPermission(ClassLoader.protectionDomainPermission);
|
|---|
| 204 |
|
|---|
| 205 | ProtectionDomain protectionDomain = getProtectionDomain0();
|
|---|
| 206 |
|
|---|
| 207 | if (protectionDomain == null)
|
|---|
| 208 | return ClassLoader.unknownProtectionDomain;
|
|---|
| 209 | else
|
|---|
| 210 | return protectionDomain;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | public String toString ()
|
|---|
| 214 | {
|
|---|
| 215 | if (isPrimitive ())
|
|---|
| 216 | return getName ();
|
|---|
| 217 | return (isInterface () ? "interface " : "class ") + getName ();
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | // Don't allow new classes to be made.
|
|---|
| 221 | private Class ()
|
|---|
| 222 | {
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | // Do a security check.
|
|---|
| 226 | private void checkMemberAccess (int flags)
|
|---|
| 227 | {
|
|---|
| 228 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 229 | if (sm != null)
|
|---|
| 230 | sm.checkMemberAccess(this, flags);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | // Initialize the class.
|
|---|
| 234 | private native void initializeClass ();
|
|---|
| 235 |
|
|---|
| 236 | // finalization
|
|---|
| 237 | protected native void finalize ();
|
|---|
| 238 | }
|
|---|