FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / lang / reflect / Method.java
blob7bd0a312511607f86cd4eb2d967af77ab577d0e2
1 // Method.java - Represent method of class or interface.
3 /* Copyright (C) 1998, 1999, 2000, 2001 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
9 details. */
11 package java.lang.reflect;
13 import gnu.gcj.RawData;
15 /**
16 * @author Tom Tromey <tromey@cygnus.com>
17 * @date December 12, 1998
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.
25 public final class Method extends AccessibleObject implements Member
27 public boolean equals (Object obj)
29 if (! (obj instanceof Method))
30 return false;
31 Method m = (Method) obj;
32 return declaringClass == m.declaringClass && offset == m.offset;
35 public Class getDeclaringClass ()
37 return declaringClass;
40 public Class[] getExceptionTypes ()
42 if (exception_types == null)
43 getType();
44 return (Class[]) exception_types.clone();
47 public native int getModifiers ();
49 public native String getName ();
51 private native void getType ();
53 public Class[] getParameterTypes ()
55 if (parameter_types == null)
56 getType();
57 return (Class[]) parameter_types.clone();
60 public Class getReturnType ()
62 if (return_type == null)
63 getType();
64 return return_type;
67 public int hashCode ()
69 // FIXME.
70 return getName().hashCode() + declaringClass.getName().hashCode();
73 public native Object invoke (Object obj, Object[] args)
74 throws IllegalAccessException, IllegalArgumentException,
75 InvocationTargetException;
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)
83 if (k.isArray ())
85 appendClassName (buf, k.getComponentType ());
86 buf.append ("[]");
88 else
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 ());
97 public String toString ()
99 if (parameter_types == null)
100 getType ();
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)
113 appendClassName (b, parameter_types[i]);
114 if (i < parameter_types.length - 1)
115 b.append(",");
117 b.append(")");
118 if (exception_types.length > 0)
120 b.append(" throws ");
121 for (int i = 0; i < exception_types.length; ++i)
123 appendClassName (b, exception_types[i]);
124 if (i < exception_types.length - 1)
125 b.append(",");
128 return b.toString();
131 private Method ()
135 // Declaring class.
136 private Class declaringClass;
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;
147 // Offset in bytes from the start of declaringClass's methods array.
148 private int offset;