| 1 | // Class.java - Representation of a Java class.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2002 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 |
|
|---|
| 69 | private native Method _getDeclaredMethod (String methodName,
|
|---|
| 70 | Class[] parameterTypes);
|
|---|
| 71 |
|
|---|
| 72 | public Method getDeclaredMethod (String methodName, Class[] parameterTypes)
|
|---|
| 73 | throws NoSuchMethodException, SecurityException
|
|---|
| 74 | {
|
|---|
| 75 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 76 | if (sm != null)
|
|---|
| 77 | {
|
|---|
| 78 | sm.checkMemberAccess(this, Member.DECLARED);
|
|---|
| 79 | Package p = getPackage();
|
|---|
| 80 | if (p != null)
|
|---|
| 81 | sm.checkPackageAccess(p.getName());
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if ("<init>".equals(methodName) || "<clinit>".equals(methodName))
|
|---|
| 85 | throw new NoSuchMethodException(methodName);
|
|---|
| 86 |
|
|---|
| 87 | Method m = _getDeclaredMethod(methodName, parameterTypes);
|
|---|
| 88 | if (m == null)
|
|---|
| 89 | throw new NoSuchMethodException (methodName);
|
|---|
| 90 | return m;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public native Method[] getDeclaredMethods () throws SecurityException;
|
|---|
| 94 |
|
|---|
| 95 | // This is marked as unimplemented in the JCL book.
|
|---|
| 96 | public native Class getDeclaringClass ();
|
|---|
| 97 |
|
|---|
| 98 | private native Field getField (String fieldName, int hash)
|
|---|
| 99 | throws NoSuchFieldException, SecurityException;
|
|---|
| 100 |
|
|---|
| 101 | public Field getField (String fieldName)
|
|---|
| 102 | throws NoSuchFieldException, SecurityException
|
|---|
| 103 | {
|
|---|
| 104 | SecurityManager s = System.getSecurityManager();
|
|---|
| 105 | if (s != null)
|
|---|
| 106 | s.checkMemberAccess (this, java.lang.reflect.Member.DECLARED);
|
|---|
| 107 | Field fld = getField(fieldName, fieldName.hashCode());
|
|---|
| 108 | if (fld == null)
|
|---|
| 109 | throw new NoSuchFieldException(fieldName);
|
|---|
| 110 | return fld;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | private native Field[] _getFields (Field[] result, int offset);
|
|---|
| 114 | public native Field[] getFields () throws SecurityException;
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Returns the <code>Package</code> in which this class is defined
|
|---|
| 118 | * Returns null when this information is not available from the
|
|---|
| 119 | * classloader of this class or when the classloader of this class
|
|---|
| 120 | * is null.
|
|---|
| 121 | *
|
|---|
| 122 | * @since 1.2
|
|---|
| 123 | */
|
|---|
| 124 | public Package getPackage()
|
|---|
| 125 | {
|
|---|
| 126 | ClassLoader cl = getClassLoader();
|
|---|
| 127 | if (cl != null)
|
|---|
| 128 | {
|
|---|
| 129 | String name = getName();
|
|---|
| 130 | String pkg = "";
|
|---|
| 131 | int idx = name.lastIndexOf('.');
|
|---|
| 132 | if (idx >= 0)
|
|---|
| 133 | pkg = name.substring(0, idx);
|
|---|
| 134 | return cl.getPackage(pkg);
|
|---|
| 135 | }
|
|---|
| 136 | else
|
|---|
| 137 | return null;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | public native Class[] getInterfaces ();
|
|---|
| 141 |
|
|---|
| 142 | private final native void getSignature (StringBuffer buffer);
|
|---|
| 143 | private static final native String getSignature (Class[] parameterTypes,
|
|---|
| 144 | boolean is_construtor);
|
|---|
| 145 |
|
|---|
| 146 | public native Method _getMethod (String methodName, Class[] parameterTypes);
|
|---|
| 147 |
|
|---|
| 148 | public Method getMethod (String methodName, Class[] parameterTypes)
|
|---|
| 149 | throws NoSuchMethodException, SecurityException
|
|---|
| 150 | {
|
|---|
| 151 | SecurityManager sm = System.getSecurityManager();
|
|---|
| 152 | if (sm != null)
|
|---|
| 153 | {
|
|---|
| 154 | sm.checkMemberAccess(this, Member.PUBLIC);
|
|---|
| 155 | Package p = getPackage();
|
|---|
| 156 | if (p != null)
|
|---|
| 157 | sm.checkPackageAccess(p.getName());
|
|---|
| 158 | }
|
|---|
|
|---|