| 1 | // Method.java - Represent method of class or interface.
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 1998, 1999, 2000, 2001 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.reflect;
|
|---|
| 12 |
|
|---|
| 13 | import gnu.gcj.RawData;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | * @author Tom Tromey <[email protected]>
|
|---|
| 17 | * @date December 12, 1998
|
|---|
| 18 | */
|
|---|
| 19 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
|
|---|
| 20 | * "The Java Language Specification", ISBN 0-201-63451-1
|
|---|
| 21 | * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
|
|---|
| 22 | * Status: Complete, but not correct: access checks aren't done.
|
|---|
| 23 | */
|
|---|
| 24 |
|
|---|
| 25 | public final class Method extends AccessibleObject implements Member
|
|---|
| 26 | {
|
|---|
| 27 | public boolean equals (Object obj)
|
|---|
| 28 | {
|
|---|
| 29 | if (! (obj instanceof Method))
|
|---|
| 30 | return false;
|
|---|
| 31 | Method m = (Method) obj;
|
|---|
| 32 | return declaringClass == m.declaringClass && offset == m.offset;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | public Class getDeclaringClass ()
|
|---|
| 36 | {
|
|---|
| 37 | return declaringClass;
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | public Class[] getExceptionTypes ()
|
|---|
| 41 | {
|
|---|
| 42 | if (exception_types == null)
|
|---|
| 43 | getType();
|
|---|
| 44 | return (Class[]) exception_types.clone();
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | public native int getModifiers ();
|
|---|
| 48 |
|
|---|
| 49 | public native String getName ();
|
|---|
| 50 |
|
|---|
| 51 | private native void getType ();
|
|---|
| 52 |
|
|---|
| 53 | public Class[] getParameterTypes ()
|
|---|
| 54 | {
|
|---|
| 55 | if (parameter_types == null)
|
|---|
| 56 | getType();
|
|---|
| 57 | return (Class[]) parameter_types.clone();
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public Class getReturnType ()
|
|---|
| 61 | {
|
|---|
| 62 | if (return_type == null)
|
|---|
| 63 | getType();
|
|---|
| 64 | return return_type;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | public int hashCode ()
|
|---|
| 68 | {
|
|---|
| 69 | // FIXME.
|
|---|
| 70 | return getName().hashCode() + declaringClass.getName().hashCode();
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | public native Object invoke (Object obj, Object[] args)
|
|---|
| 74 | throws IllegalAccessException, IllegalArgumentException,
|
|---|
| 75 | InvocationTargetException;
|
|---|
| 76 |
|
|---|
| 77 | // Append a class name to a string buffer. We try to print the
|
|---|
| 78 | // fully-qualified name, the way that a Java programmer would expect
|
|---|
| 79 | // it to be written. Weirdly, Class has no appropriate method for
|
|---|
| 80 | // this.
|
|---|
| 81 | static void appendClassName (StringBuffer buf, Class k)
|
|---|
| 82 | {
|
|---|
| 83 | if (k.isArray ())
|
|---|
| 84 | {
|
|---|
| 85 | appendClassName (buf, k.getComponentType ());
|
|---|
| 86 | buf.append ("[]");
|
|---|
| 87 | }
|
|---|
| 88 | else
|
|---|
| 89 | {
|
|---|
| 90 | // This is correct for primitive and reference types. Really
|
|---|
| 91 | // we'd like `Main$Inner' to be printed as `Main.Inner', I
|
|---|
| 92 | // think, but that is a pain.
|
|---|
| 93 | buf.append (k.getName ());
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | public String toString ()
|
|---|
| 98 | {
|
|---|
| 99 | if (parameter_types == null)
|
|---|
| 100 | getType ();
|
|---|
| 101 |
|
|---|
| 102 | StringBuffer b = new StringBuffer ();
|
|---|
| 103 | Modifier.toString(getModifiers(), b);
|
|---|
| 104 | b.append(" ");
|
|---|
| 105 | appendClassName (b, return_type);
|
|---|
| 106 | b.append(" ");
|
|---|
| 107 | appendClassName (b, declaringClass);
|
|---|
| 108 | b.append(".");
|
|---|
| 109 | b.append(getName());
|
|---|
| 110 | b.append("(");
|
|---|
| 111 | for (int i = 0; i < parameter_types.length; ++i)
|
|---|
| 112 | {
|
|---|
| 113 | appendClassName (b, parameter_types[i]);
|
|---|
| 114 | if (i < parameter_types.length - 1)
|
|---|
| 115 | b.append(",");
|
|---|
| 116 | }
|
|---|
| 117 | b.append(")");
|
|---|
| 118 | if (exception_types.length > 0)
|
|---|
| 119 | {
|
|---|
| 120 | b.append(" throws ");
|
|---|
| 121 | for (int i = 0; i < exception_types.length; ++i)
|
|---|
| 122 | {
|
|---|
| 123 | appendClassName (b, exception_types[i]);
|
|---|
| 124 | if (i < exception_types.length - 1)
|
|---|
| 125 | b.append(",");
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | return b.toString();
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | private Method ()
|
|---|
| 132 | {
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | // Declaring class.
|
|---|
| 136 | private Class declaringClass;
|
|---|
| 137 |
|
|---|
| 138 | // Exception types.
|
|---|
| 139 | private Class[] exception_types;
|
|---|
| 140 | // Name cache. (Initially null.)
|
|---|
| 141 | private String name;
|
|---|
| 142 | // Parameter types.
|
|---|
| 143 | private Class[] parameter_types;
|
|---|
| 144 | // Return type.
|
|---|
| 145 | private Class return_type;
|
|---|
| 146 |
|
|---|
| 147 | // Offset in bytes from the start of declaringClass's methods array.
|
|---|
| 148 | private int offset;
|
|---|
| 149 | }
|
|---|