jtulach@258
|
1 |
/*
|
jtulach@258
|
2 |
* Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
|
jtulach@258
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jtulach@258
|
4 |
*
|
jtulach@258
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jtulach@258
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jtulach@258
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jtulach@258
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jtulach@258
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jtulach@258
|
10 |
*
|
jtulach@258
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jtulach@258
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jtulach@258
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jtulach@258
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jtulach@258
|
15 |
* accompanied this code).
|
jtulach@258
|
16 |
*
|
jtulach@258
|
17 |
* You should have received a copy of the GNU General Public License version
|
jtulach@258
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jtulach@258
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jtulach@258
|
20 |
*
|
jtulach@258
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jtulach@258
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jtulach@258
|
23 |
* questions.
|
jtulach@258
|
24 |
*/
|
jtulach@258
|
25 |
|
jtulach@258
|
26 |
package java.lang.reflect;
|
jtulach@258
|
27 |
|
jtulach@258
|
28 |
import java.lang.annotation.Annotation;
|
jaroslav@262
|
29 |
import org.apidesign.bck2brwsr.core.JavaScriptBody;
|
jaroslav@266
|
30 |
import org.apidesign.bck2brwsr.emul.AnnotationImpl;
|
jaroslav@391
|
31 |
import org.apidesign.bck2brwsr.emul.MethodImpl;
|
jtulach@258
|
32 |
|
jtulach@258
|
33 |
/**
|
jtulach@258
|
34 |
* A {@code Method} provides information about, and access to, a single method
|
jtulach@258
|
35 |
* on a class or interface. The reflected method may be a class method
|
jtulach@258
|
36 |
* or an instance method (including an abstract method).
|
jtulach@258
|
37 |
*
|
jtulach@258
|
38 |
* <p>A {@code Method} permits widening conversions to occur when matching the
|
jtulach@258
|
39 |
* actual parameters to invoke with the underlying method's formal
|
jtulach@258
|
40 |
* parameters, but it throws an {@code IllegalArgumentException} if a
|
jtulach@258
|
41 |
* narrowing conversion would occur.
|
jtulach@258
|
42 |
*
|
jtulach@258
|
43 |
* @see Member
|
jtulach@258
|
44 |
* @see java.lang.Class
|
jtulach@258
|
45 |
* @see java.lang.Class#getMethods()
|
jtulach@258
|
46 |
* @see java.lang.Class#getMethod(String, Class[])
|
jtulach@258
|
47 |
* @see java.lang.Class#getDeclaredMethods()
|
jtulach@258
|
48 |
* @see java.lang.Class#getDeclaredMethod(String, Class[])
|
jtulach@258
|
49 |
*
|
jtulach@258
|
50 |
* @author Kenneth Russell
|
jtulach@258
|
51 |
* @author Nakul Saraiya
|
jtulach@258
|
52 |
*/
|
jtulach@258
|
53 |
public final
|
jtulach@258
|
54 |
class Method extends AccessibleObject implements GenericDeclaration,
|
jtulach@258
|
55 |
Member {
|
jaroslav@262
|
56 |
private final Class<?> clazz;
|
jaroslav@262
|
57 |
private final String name;
|
jaroslav@262
|
58 |
private final Object data;
|
jaroslav@264
|
59 |
private final String sig;
|
jtulach@258
|
60 |
|
jtulach@258
|
61 |
// Generics infrastructure
|
jtulach@258
|
62 |
|
jaroslav@262
|
63 |
private String getGenericSignature() {return null;}
|
jtulach@258
|
64 |
|
jtulach@258
|
65 |
/**
|
jtulach@258
|
66 |
* Package-private constructor used by ReflectAccess to enable
|
jtulach@258
|
67 |
* instantiation of these objects in Java code from the java.lang
|
jtulach@258
|
68 |
* package via sun.reflect.LangReflectAccess.
|
jtulach@258
|
69 |
*/
|
jaroslav@264
|
70 |
Method(Class<?> declaringClass, String name, Object data, String sig)
|
jtulach@258
|
71 |
{
|
jtulach@258
|
72 |
this.clazz = declaringClass;
|
jtulach@258
|
73 |
this.name = name;
|
jaroslav@262
|
74 |
this.data = data;
|
jaroslav@264
|
75 |
this.sig = sig;
|
jtulach@258
|
76 |
}
|
jtulach@258
|
77 |
|
jtulach@258
|
78 |
/**
|
jtulach@258
|
79 |
* Package-private routine (exposed to java.lang.Class via
|
jtulach@258
|
80 |
* ReflectAccess) which returns a copy of this Method. The copy's
|
jtulach@258
|
81 |
* "root" field points to this Method.
|
jtulach@258
|
82 |
*/
|
jtulach@258
|
83 |
Method copy() {
|
jaroslav@262
|
84 |
return this;
|
jtulach@258
|
85 |
}
|
jtulach@258
|
86 |
|
jtulach@258
|
87 |
/**
|
jtulach@258
|
88 |
* Returns the {@code Class} object representing the class or interface
|
jtulach@258
|
89 |
* that declares the method represented by this {@code Method} object.
|
jtulach@258
|
90 |
*/
|
jtulach@258
|
91 |
public Class<?> getDeclaringClass() {
|
jtulach@258
|
92 |
return clazz;
|
jtulach@258
|
93 |
}
|
jtulach@258
|
94 |
|
jtulach@258
|
95 |
/**
|
jtulach@258
|
96 |
* Returns the name of the method represented by this {@code Method}
|
jtulach@258
|
97 |
* object, as a {@code String}.
|
jtulach@258
|
98 |
*/
|
jtulach@258
|
99 |
public String getName() {
|
jtulach@258
|
100 |
return name;
|
jtulach@258
|
101 |
}
|
jtulach@258
|
102 |
|
jtulach@258
|
103 |
/**
|
jtulach@258
|
104 |
* Returns the Java language modifiers for the method represented
|
jtulach@258
|
105 |
* by this {@code Method} object, as an integer. The {@code Modifier} class should
|
jtulach@258
|
106 |
* be used to decode the modifiers.
|
jtulach@258
|
107 |
*
|
jtulach@258
|
108 |
* @see Modifier
|
jtulach@258
|
109 |
*/
|
jtulach@258
|
110 |
public int getModifiers() {
|
jaroslav@392
|
111 |
return getAccess(data);
|
jtulach@258
|
112 |
}
|
jaroslav@392
|
113 |
|
jaroslav@392
|
114 |
@JavaScriptBody(args = "self", body = "return self.access;")
|
jaroslav@392
|
115 |
private static native int getAccess(Object self);
|
jaroslav@392
|
116 |
|
jtulach@258
|
117 |
/**
|
jtulach@258
|
118 |
* Returns an array of {@code TypeVariable} objects that represent the
|
jtulach@258
|
119 |
* type variables declared by the generic declaration represented by this
|
jtulach@258
|
120 |
* {@code GenericDeclaration} object, in declaration order. Returns an
|
jtulach@258
|
121 |
* array of length 0 if the underlying generic declaration declares no type
|
jtulach@258
|
122 |
* variables.
|
jtulach@258
|
123 |
*
|
jtulach@258
|
124 |
* @return an array of {@code TypeVariable} objects that represent
|
jtulach@258
|
125 |
* the type variables declared by this generic declaration
|
jtulach@258
|
126 |
* @throws GenericSignatureFormatError if the generic
|
jtulach@258
|
127 |
* signature of this generic declaration does not conform to
|
jtulach@258
|
128 |
* the format specified in
|
jtulach@258
|
129 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jtulach@258
|
130 |
* @since 1.5
|
jtulach@258
|
131 |
*/
|
jtulach@258
|
132 |
public TypeVariable<Method>[] getTypeParameters() {
|
jaroslav@260
|
133 |
throw new UnsupportedOperationException();
|
jtulach@258
|
134 |
}
|
jtulach@258
|
135 |
|
jtulach@258
|
136 |
/**
|
jtulach@258
|
137 |
* Returns a {@code Class} object that represents the formal return type
|
jtulach@258
|
138 |
* of the method represented by this {@code Method} object.
|
jtulach@258
|
139 |
*
|
jtulach@258
|
140 |
* @return the return type for the method this object represents
|
jtulach@258
|
141 |
*/
|
jtulach@258
|
142 |
public Class<?> getReturnType() {
|
jaroslav@354
|
143 |
switch (sig.charAt(0)) {
|
jaroslav@354
|
144 |
case 'I': return Integer.TYPE;
|
jaroslav@354
|
145 |
case 'J': return Long.TYPE;
|
jaroslav@354
|
146 |
case 'D': return Double.TYPE;
|
jaroslav@354
|
147 |
case 'F': return Float.TYPE;
|
jaroslav@354
|
148 |
case 'B': return Byte.TYPE;
|
jaroslav@354
|
149 |
case 'Z': return Boolean.TYPE;
|
jaroslav@354
|
150 |
case 'S': return Short.TYPE;
|
jaroslav@405
|
151 |
case 's': return String.class;
|
jaroslav@406
|
152 |
case 'o': return Object.class;
|
jaroslav@354
|
153 |
// case 'V': return Void.TYPE;
|
jaroslav@354
|
154 |
case 'L': try {
|
jaroslav@354
|
155 |
int up = sig.indexOf("_2");
|
jaroslav@354
|
156 |
String type = sig.substring(1, up);
|
jaroslav@354
|
157 |
return Class.forName(type);
|
jaroslav@354
|
158 |
} catch (ClassNotFoundException ex) {
|
jaroslav@354
|
159 |
// should not happen
|
jaroslav@354
|
160 |
}
|
jaroslav@354
|
161 |
}
|
jaroslav@354
|
162 |
throw new UnsupportedOperationException(sig);
|
jtulach@258
|
163 |
}
|
jtulach@258
|
164 |
|
jtulach@258
|
165 |
/**
|
jtulach@258
|
166 |
* Returns a {@code Type} object that represents the formal return
|
jtulach@258
|
167 |
* type of the method represented by this {@code Method} object.
|
jtulach@258
|
168 |
*
|
jtulach@258
|
169 |
* <p>If the return type is a parameterized type,
|
jtulach@258
|
170 |
* the {@code Type} object returned must accurately reflect
|
jtulach@258
|
171 |
* the actual type parameters used in the source code.
|
jtulach@258
|
172 |
*
|
jtulach@258
|
173 |
* <p>If the return type is a type variable or a parameterized type, it
|
jtulach@258
|
174 |
* is created. Otherwise, it is resolved.
|
jtulach@258
|
175 |
*
|
jtulach@258
|
176 |
* @return a {@code Type} object that represents the formal return
|
jtulach@258
|
177 |
* type of the underlying method
|
jtulach@258
|
178 |
* @throws GenericSignatureFormatError
|
jtulach@258
|
179 |
* if the generic method signature does not conform to the format
|
jtulach@258
|
180 |
* specified in
|
jtulach@258
|
181 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jtulach@258
|
182 |
* @throws TypeNotPresentException if the underlying method's
|
jtulach@258
|
183 |
* return type refers to a non-existent type declaration
|
jtulach@258
|
184 |
* @throws MalformedParameterizedTypeException if the
|
jtulach@258
|
185 |
* underlying method's return typed refers to a parameterized
|
jtulach@258
|
186 |
* type that cannot be instantiated for any reason
|
jtulach@258
|
187 |
* @since 1.5
|
jtulach@258
|
188 |
*/
|
jtulach@258
|
189 |
public Type getGenericReturnType() {
|
jaroslav@260
|
190 |
throw new UnsupportedOperationException();
|
jtulach@258
|
191 |
}
|
jtulach@258
|
192 |
|
jtulach@258
|
193 |
|
jtulach@258
|
194 |
/**
|
jtulach@258
|
195 |
* Returns an array of {@code Class} objects that represent the formal
|
jtulach@258
|
196 |
* parameter types, in declaration order, of the method
|
jtulach@258
|
197 |
* represented by this {@code Method} object. Returns an array of length
|
jtulach@258
|
198 |
* 0 if the underlying method takes no parameters.
|
jtulach@258
|
199 |
*
|
jtulach@258
|
200 |
* @return the parameter types for the method this object
|
jtulach@258
|
201 |
* represents
|
jtulach@258
|
202 |
*/
|
jtulach@258
|
203 |
public Class<?>[] getParameterTypes() {
|
jaroslav@262
|
204 |
throw new UnsupportedOperationException();
|
jaroslav@262
|
205 |
//return (Class<?>[]) parameterTypes.clone();
|
jtulach@258
|
206 |
}
|
jtulach@258
|
207 |
|
jtulach@258
|
208 |
/**
|
jtulach@258
|
209 |
* Returns an array of {@code Type} objects that represent the formal
|
jtulach@258
|
210 |
* parameter types, in declaration order, of the method represented by
|
jtulach@258
|
211 |
* this {@code Method} object. Returns an array of length 0 if the
|
jtulach@258
|
212 |
* underlying method takes no parameters.
|
jtulach@258
|
213 |
*
|
jtulach@258
|
214 |
* <p>If a formal parameter type is a parameterized type,
|
jtulach@258
|
215 |
* the {@code Type} object returned for it must accurately reflect
|
jtulach@258
|
216 |
* the actual type parameters used in the source code.
|
jtulach@258
|
217 |
*
|
jtulach@258
|
218 |
* <p>If a formal parameter type is a type variable or a parameterized
|
jtulach@258
|
219 |
* type, it is created. Otherwise, it is resolved.
|
jtulach@258
|
220 |
*
|
jtulach@258
|
221 |
* @return an array of Types that represent the formal
|
jtulach@258
|
222 |
* parameter types of the underlying method, in declaration order
|
jtulach@258
|
223 |
* @throws GenericSignatureFormatError
|
jtulach@258
|
224 |
* if the generic method signature does not conform to the format
|
jtulach@258
|
225 |
* specified in
|
jtulach@258
|
226 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jtulach@258
|
227 |
* @throws TypeNotPresentException if any of the parameter
|
jtulach@258
|
228 |
* types of the underlying method refers to a non-existent type
|
jtulach@258
|
229 |
* declaration
|
jtulach@258
|
230 |
* @throws MalformedParameterizedTypeException if any of
|
jtulach@258
|
231 |
* the underlying method's parameter types refer to a parameterized
|
jtulach@258
|
232 |
* type that cannot be instantiated for any reason
|
jtulach@258
|
233 |
* @since 1.5
|
jtulach@258
|
234 |
*/
|
jtulach@258
|
235 |
public Type[] getGenericParameterTypes() {
|
jaroslav@260
|
236 |
throw new UnsupportedOperationException();
|
jtulach@258
|
237 |
}
|
jtulach@258
|
238 |
|
jtulach@258
|
239 |
|
jtulach@258
|
240 |
/**
|
jtulach@258
|
241 |
* Returns an array of {@code Class} objects that represent
|
jtulach@258
|
242 |
* the types of the exceptions declared to be thrown
|
jtulach@258
|
243 |
* by the underlying method
|
jtulach@258
|
244 |
* represented by this {@code Method} object. Returns an array of length
|
jtulach@258
|
245 |
* 0 if the method declares no exceptions in its {@code throws} clause.
|
jtulach@258
|
246 |
*
|
jtulach@258
|
247 |
* @return the exception types declared as being thrown by the
|
jtulach@258
|
248 |
* method this object represents
|
jtulach@258
|
249 |
*/
|
jtulach@258
|
250 |
public Class<?>[] getExceptionTypes() {
|
jaroslav@262
|
251 |
throw new UnsupportedOperationException();
|
jaroslav@262
|
252 |
//return (Class<?>[]) exceptionTypes.clone();
|
jtulach@258
|
253 |
}
|
jtulach@258
|
254 |
|
jtulach@258
|
255 |
/**
|
jtulach@258
|
256 |
* Returns an array of {@code Type} objects that represent the
|
jtulach@258
|
257 |
* exceptions declared to be thrown by this {@code Method} object.
|
jtulach@258
|
258 |
* Returns an array of length 0 if the underlying method declares
|
jtulach@258
|
259 |
* no exceptions in its {@code throws} clause.
|
jtulach@258
|
260 |
*
|
jtulach@258
|
261 |
* <p>If an exception type is a type variable or a parameterized
|
jtulach@258
|
262 |
* type, it is created. Otherwise, it is resolved.
|
jtulach@258
|
263 |
*
|
jtulach@258
|
264 |
* @return an array of Types that represent the exception types
|
jtulach@258
|
265 |
* thrown by the underlying method
|
jtulach@258
|
266 |
* @throws GenericSignatureFormatError
|
jtulach@258
|
267 |
* if the generic method signature does not conform to the format
|
jtulach@258
|
268 |
* specified in
|
jtulach@258
|
269 |
* <cite>The Java™ Virtual Machine Specification</cite>
|
jtulach@258
|
270 |
* @throws TypeNotPresentException if the underlying method's
|
jtulach@258
|
271 |
* {@code throws} clause refers to a non-existent type declaration
|
jtulach@258
|
272 |
* @throws MalformedParameterizedTypeException if
|
jtulach@258
|
273 |
* the underlying method's {@code throws} clause refers to a
|
jtulach@258
|
274 |
* parameterized type that cannot be instantiated for any reason
|
jtulach@258
|
275 |
* @since 1.5
|
jtulach@258
|
276 |
*/
|
jtulach@258
|
277 |
public Type[] getGenericExceptionTypes() {
|
jaroslav@260
|
278 |
throw new UnsupportedOperationException();
|
jtulach@258
|
279 |
}
|
jtulach@258
|
280 |
|
jtulach@258
|
281 |
/**
|
jtulach@258
|
282 |
* Compares this {@code Method} against the specified object. Returns
|
jtulach@258
|
283 |
* true if the objects are the same. Two {@code Methods} are the same if
|
jtulach@258
|
284 |
* they were declared by the same class and have the same name
|
jtulach@258
|
285 |
* and formal parameter types and return type.
|
jtulach@258
|
286 |
*/
|
jtulach@258
|
287 |
public boolean equals(Object obj) {
|
jtulach@258
|
288 |
if (obj != null && obj instanceof Method) {
|
jtulach@258
|
289 |
Method other = (Method)obj;
|
jaroslav@262
|
290 |
return data == other.data;
|
jtulach@258
|
291 |
}
|
jtulach@258
|
292 |
return false;
|
jtulach@258
|
293 |
}
|
jtulach@258
|
294 |
|
jtulach@258
|
295 |
/**
|
jtulach@258
|
296 |
* Returns a hashcode for this {@code Method}. The hashcode is computed
|
jtulach@258
|
297 |
* as the exclusive-or of the hashcodes for the underlying
|
jtulach@258
|
298 |
* method's declaring class name and the method's name.
|
jtulach@258
|
299 |
*/
|
jtulach@258
|
300 |
public int hashCode() {
|
jtulach@258
|
301 |
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
|
jtulach@258
|
302 |
}
|
jtulach@258
|
303 |
|
jtulach@258
|
304 |
/**
|
jtulach@258
|
305 |
* Returns a string describing this {@code Method}. The string is
|
jtulach@258
|
306 |
* formatted as the method access modifiers, if any, followed by
|
jtulach@258
|
307 |
* the method return type, followed by a space, followed by the
|
jtulach@258
|
308 |
* class declaring the method, followed by a period, followed by
|
jtulach@258
|
309 |
* the method name, followed by a parenthesized, comma-separated
|
jtulach@258
|
310 |
* list of the method's formal parameter types. If the method
|
jtulach@258
|
311 |
* throws checked exceptions, the parameter list is followed by a
|
jtulach@258
|
312 |
* space, followed by the word throws followed by a
|
jtulach@258
|
313 |
* comma-separated list of the thrown exception types.
|
jtulach@258
|
314 |
* For example:
|
jtulach@258
|
315 |
* <pre>
|
jtulach@258
|
316 |
* public boolean java.lang.Object.equals(java.lang.Object)
|
jtulach@258
|
317 |
* </pre>
|
jtulach@258
|
318 |
*
|
jtulach@258
|
319 |
* <p>The access modifiers are placed in canonical order as
|
jtulach@258
|
320 |
* specified by "The Java Language Specification". This is
|
jtulach@258
|
321 |
* {@code public}, {@code protected} or {@code private} first,
|
jtulach@258
|
322 |
* and then other modifiers in the following order:
|
jtulach@258
|
323 |
* {@code abstract}, {@code static}, {@code final},
|
jtulach@258
|
324 |
* {@code synchronized}, {@code native}, {@code strictfp}.
|
jtulach@258
|
325 |
*/
|
jtulach@258
|
326 |
public String toString() {
|
jtulach@258
|
327 |
try {
|
jtulach@258
|
328 |
StringBuilder sb = new StringBuilder();
|
jtulach@258
|
329 |
int mod = getModifiers() & Modifier.methodModifiers();
|
jtulach@258
|
330 |
if (mod != 0) {
|
jtulach@258
|
331 |
sb.append(Modifier.toString(mod)).append(' ');
|
jtulach@258
|
332 |
}
|
jtulach@258
|
333 |
sb.append(Field.getTypeName(getReturnType())).append(' ');
|
jtulach@258
|
334 |
sb.append(Field.getTypeName(getDeclaringClass())).append('.');
|
jtulach@258
|
335 |
sb.append(getName()).append('(');
|
jaroslav@262
|
336 |
/*
|
jtulach@258
|
337 |
Class<?>[] params = parameterTypes; // avoid clone
|
jtulach@258
|
338 |
for (int j = 0; j < params.length; j++) {
|
jtulach@258
|
339 |
sb.append(Field.getTypeName(params[j]));
|
jtulach@258
|
340 |
if (j < (params.length - 1))
|
jtulach@258
|
341 |
sb.append(',');
|
jtulach@258
|
342 |
}
|
jtulach@258
|
343 |
sb.append(')');
|
jtulach@258
|
344 |
Class<?>[] exceptions = exceptionTypes; // avoid clone
|
jtulach@258
|
345 |
if (exceptions.length > 0) {
|
jtulach@258
|
346 |
sb.append(" throws ");
|
jtulach@258
|
347 |
for (int k = 0; k < exceptions.length; k++) {
|
jtulach@258
|
348 |
sb.append(exceptions[k].getName());
|
jtulach@258
|
349 |
if (k < (exceptions.length - 1))
|
jtulach@258
|
350 |
sb.append(',');
|
jtulach@258
|
351 |
}
|
jtulach@258
|
352 |
}
|
jaroslav@262
|
353 |
*/
|
jtulach@258
|
354 |
return sb.toString();
|
jtulach@258
|
355 |
} catch (Exception e) {
|
jtulach@258
|
356 |
return "<" + e + ">";
|
jtulach@258
|
357 |
}
|
jtulach@258
|
358 |
}
|
jtulach@258
|
359 |
|
jtulach@258
|
360 |
/**
|
jtulach@258
|
361 |
* Returns a string describing this {@code Method}, including
|
jtulach@258
|
362 |
* type parameters. The string is formatted as the method access
|
jtulach@258
|
363 |
* modifiers, if any, followed by an angle-bracketed
|
jtulach@258
|
364 |
* comma-separated list of the method's type parameters, if any,
|
jtulach@258
|
365 |
* followed by the method's generic return type, followed by a
|
jtulach@258
|
366 |
* space, followed by the class declaring the method, followed by
|
jtulach@258
|
367 |
* a period, followed by the method name, followed by a
|
jtulach@258
|
368 |
* parenthesized, comma-separated list of the method's generic
|
jtulach@258
|
369 |
* formal parameter types.
|
jtulach@258
|
370 |
*
|
jtulach@258
|
371 |
* If this method was declared to take a variable number of
|
jtulach@258
|
372 |
* arguments, instead of denoting the last parameter as
|
jtulach@258
|
373 |
* "<tt><i>Type</i>[]</tt>", it is denoted as
|
jtulach@258
|
374 |
* "<tt><i>Type</i>...</tt>".
|
jtulach@258
|
375 |
*
|
jtulach@258
|
376 |
* A space is used to separate access modifiers from one another
|
jtulach@258
|
377 |
* and from the type parameters or return type. If there are no
|
jtulach@258
|
378 |
* type parameters, the type parameter list is elided; if the type
|
jtulach@258
|
379 |
* parameter list is present, a space separates the list from the
|
jtulach@258
|
380 |
* class name. If the method is declared to throw exceptions, the
|
jtulach@258
|
381 |
* parameter list is followed by a space, followed by the word
|
jtulach@258
|
382 |
* throws followed by a comma-separated list of the generic thrown
|
jtulach@258
|
383 |
* exception types. If there are no type parameters, the type
|
jtulach@258
|
384 |
* parameter list is elided.
|
jtulach@258
|
385 |
*
|
jtulach@258
|
386 |
* <p>The access modifiers are placed in canonical order as
|
jtulach@258
|
387 |
* specified by "The Java Language Specification". This is
|
jtulach@258
|
388 |
* {@code public}, {@code protected} or {@code private} first,
|
jtulach@258
|
389 |
* and then other modifiers in the following order:
|
jtulach@258
|
390 |
* {@code abstract}, {@code static}, {@code final},
|
jtulach@258
|
391 |
* {@code synchronized}, {@code native}, {@code strictfp}.
|
jtulach@258
|
392 |
*
|
jtulach@258
|
393 |
* @return a string describing this {@code Method},
|
jtulach@258
|
394 |
* include type parameters
|
jtulach@258
|
395 |
*
|
jtulach@258
|
396 |
* @since 1.5
|
jtulach@258
|
397 |
*/
|
jtulach@258
|
398 |
public String toGenericString() {
|
jtulach@258
|
399 |
try {
|
jtulach@258
|
400 |
StringBuilder sb = new StringBuilder();
|
jtulach@258
|
401 |
int mod = getModifiers() & Modifier.methodModifiers();
|
jtulach@258
|
402 |
if (mod != 0) {
|
jtulach@258
|
403 |
sb.append(Modifier.toString(mod)).append(' ');
|
jtulach@258
|
404 |
}
|
jtulach@258
|
405 |
TypeVariable<?>[] typeparms = getTypeParameters();
|
jtulach@258
|
406 |
if (typeparms.length > 0) {
|
jtulach@258
|
407 |
boolean first = true;
|
jtulach@258
|
408 |
sb.append('<');
|
jtulach@258
|
409 |
for(TypeVariable<?> typeparm: typeparms) {
|
jtulach@258
|
410 |
if (!first)
|
jtulach@258
|
411 |
sb.append(',');
|
jtulach@258
|
412 |
// Class objects can't occur here; no need to test
|
jtulach@258
|
413 |
// and call Class.getName().
|
jtulach@258
|
414 |
sb.append(typeparm.toString());
|
jtulach@258
|
415 |
first = false;
|
jtulach@258
|
416 |
}
|
jtulach@258
|
417 |
sb.append("> ");
|
jtulach@258
|
418 |
}
|
jtulach@258
|
419 |
|
jtulach@258
|
420 |
Type genRetType = getGenericReturnType();
|
jtulach@258
|
421 |
sb.append( ((genRetType instanceof Class<?>)?
|
jtulach@258
|
422 |
Field.getTypeName((Class<?>)genRetType):genRetType.toString()))
|
jtulach@258
|
423 |
.append(' ');
|
jtulach@258
|
424 |
|
jtulach@258
|
425 |
sb.append(Field.getTypeName(getDeclaringClass())).append('.');
|
jtulach@258
|
426 |
sb.append(getName()).append('(');
|
jtulach@258
|
427 |
Type[] params = getGenericParameterTypes();
|
jtulach@258
|
428 |
for (int j = 0; j < params.length; j++) {
|
jtulach@258
|
429 |
String param = (params[j] instanceof Class)?
|
jtulach@258
|
430 |
Field.getTypeName((Class)params[j]):
|
jtulach@258
|
431 |
(params[j].toString());
|
jtulach@258
|
432 |
if (isVarArgs() && (j == params.length - 1)) // replace T[] with T...
|
jtulach@258
|
433 |
param = param.replaceFirst("\\[\\]$", "...");
|
jtulach@258
|
434 |
sb.append(param);
|
jtulach@258
|
435 |
if (j < (params.length - 1))
|
jtulach@258
|
436 |
sb.append(',');
|
jtulach@258
|
437 |
}
|
jtulach@258
|
438 |
sb.append(')');
|
jtulach@258
|
439 |
Type[] exceptions = getGenericExceptionTypes();
|
jtulach@258
|
440 |
if (exceptions.length > 0) {
|
jtulach@258
|
441 |
sb.append(" throws ");
|
jtulach@258
|
442 |
for (int k = 0; k < exceptions.length; k++) {
|
jtulach@258
|
443 |
sb.append((exceptions[k] instanceof Class)?
|
jtulach@258
|
444 |
((Class)exceptions[k]).getName():
|
jtulach@258
|
445 |
exceptions[k].toString());
|
jtulach@258
|
446 |
if (k < (exceptions.length - 1))
|
jtulach@258
|
447 |
sb.append(',');
|
jtulach@258
|
448 |
}
|
jtulach@258
|
449 |
}
|
jtulach@258
|
450 |
return sb.toString();
|
jtulach@258
|
451 |
} catch (Exception e) {
|
jtulach@258
|
452 |
return "<" + e + ">";
|
jtulach@258
|
453 |
}
|
jtulach@258
|
454 |
}
|
jtulach@258
|
455 |
|
jtulach@258
|
456 |
/**
|
jtulach@258
|
457 |
* Invokes the underlying method represented by this {@code Method}
|
jtulach@258
|
458 |
* object, on the specified object with the specified parameters.
|
jtulach@258
|
459 |
* Individual parameters are automatically unwrapped to match
|
jtulach@258
|
460 |
* primitive formal parameters, and both primitive and reference
|
jtulach@258
|
461 |
* parameters are subject to method invocation conversions as
|
jtulach@258
|
462 |
* necessary.
|
jtulach@258
|
463 |
*
|
jtulach@258
|
464 |
* <p>If the underlying method is static, then the specified {@code obj}
|
jtulach@258
|
465 |
* argument is ignored. It may be null.
|
jtulach@258
|
466 |
*
|
jtulach@258
|
467 |
* <p>If the number of formal parameters required by the underlying method is
|
jtulach@258
|
468 |
* 0, the supplied {@code args} array may be of length 0 or null.
|
jtulach@258
|
469 |
*
|
jtulach@258
|
470 |
* <p>If the underlying method is an instance method, it is invoked
|
jtulach@258
|
471 |
* using dynamic method lookup as documented in The Java Language
|
jtulach@258
|
472 |
* Specification, Second Edition, section 15.12.4.4; in particular,
|
jtulach@258
|
473 |
* overriding based on the runtime type of the target object will occur.
|
jtulach@258
|
474 |
*
|
jtulach@258
|
475 |
* <p>If the underlying method is static, the class that declared
|
jtulach@258
|
476 |
* the method is initialized if it has not already been initialized.
|
jtulach@258
|
477 |
*
|
jtulach@258
|
478 |
* <p>If the method completes normally, the value it returns is
|
jtulach@258
|
479 |
* returned to the caller of invoke; if the value has a primitive
|
jtulach@258
|
480 |
* type, it is first appropriately wrapped in an object. However,
|
jtulach@258
|
481 |
* if the value has the type of an array of a primitive type, the
|
jtulach@258
|
482 |
* elements of the array are <i>not</i> wrapped in objects; in
|
jtulach@258
|
483 |
* other words, an array of primitive type is returned. If the
|
jtulach@258
|
484 |
* underlying method return type is void, the invocation returns
|
jtulach@258
|
485 |
* null.
|
jtulach@258
|
486 |
*
|
jtulach@258
|
487 |
* @param obj the object the underlying method is invoked from
|
jtulach@258
|
488 |
* @param args the arguments used for the method call
|
jtulach@258
|
489 |
* @return the result of dispatching the method represented by
|
jtulach@258
|
490 |
* this object on {@code obj} with parameters
|
jtulach@258
|
491 |
* {@code args}
|
jtulach@258
|
492 |
*
|
jtulach@258
|
493 |
* @exception IllegalAccessException if this {@code Method} object
|
jtulach@258
|
494 |
* is enforcing Java language access control and the underlying
|
jtulach@258
|
495 |
* method is inaccessible.
|
jtulach@258
|
496 |
* @exception IllegalArgumentException if the method is an
|
jtulach@258
|
497 |
* instance method and the specified object argument
|
jtulach@258
|
498 |
* is not an instance of the class or interface
|
jtulach@258
|
499 |
* declaring the underlying method (or of a subclass
|
jtulach@258
|
500 |
* or implementor thereof); if the number of actual
|
jtulach@258
|
501 |
* and formal parameters differ; if an unwrapping
|
jtulach@258
|
502 |
* conversion for primitive arguments fails; or if,
|
jtulach@258
|
503 |
* after possible unwrapping, a parameter value
|
jtulach@258
|
504 |
* cannot be converted to the corresponding formal
|
jtulach@258
|
505 |
* parameter type by a method invocation conversion.
|
jtulach@258
|
506 |
* @exception InvocationTargetException if the underlying method
|
jtulach@258
|
507 |
* throws an exception.
|
jtulach@258
|
508 |
* @exception NullPointerException if the specified object is null
|
jtulach@258
|
509 |
* and the method is an instance method.
|
jtulach@258
|
510 |
* @exception ExceptionInInitializerError if the initialization
|
jtulach@258
|
511 |
* provoked by this method fails.
|
jtulach@258
|
512 |
*/
|
jaroslav@354
|
513 |
public Object invoke(Object obj, Object... args)
|
jaroslav@354
|
514 |
throws IllegalAccessException, IllegalArgumentException,
|
jaroslav@354
|
515 |
InvocationTargetException
|
jaroslav@354
|
516 |
{
|
jaroslav@354
|
517 |
Object res = invoke0(this, obj, args);
|
jaroslav@354
|
518 |
if (getReturnType().isPrimitive()) {
|
jaroslav@354
|
519 |
res = fromPrimitive(getReturnType(), res);
|
jaroslav@354
|
520 |
}
|
jaroslav@354
|
521 |
return res;
|
jaroslav@354
|
522 |
}
|
jaroslav@354
|
523 |
|
jaroslav@262
|
524 |
@JavaScriptBody(args = { "method", "self", "args" }, body =
|
jaroslav@262
|
525 |
"if (args.length > 0) throw 'unsupported now';"
|
jaroslav@262
|
526 |
+ "return method.fld_data(self);"
|
jaroslav@262
|
527 |
)
|
jaroslav@354
|
528 |
private static native Object invoke0(Method m, Object self, Object[] args);
|
jaroslav@354
|
529 |
|
jaroslav@354
|
530 |
private static Object fromPrimitive(Class<?> type, Object o) {
|
jaroslav@354
|
531 |
if (type == Integer.TYPE) {
|
jaroslav@355
|
532 |
return fromRaw(Integer.class, "valueOf__Ljava_lang_Integer_2I", o);
|
jaroslav@354
|
533 |
}
|
jaroslav@355
|
534 |
if (type == Long.TYPE) {
|
jaroslav@355
|
535 |
return fromRaw(Long.class, "valueOf__Ljava_lang_Long_2J", o);
|
jaroslav@355
|
536 |
}
|
jaroslav@355
|
537 |
if (type == Double.TYPE) {
|
jaroslav@355
|
538 |
return fromRaw(Double.class, "valueOf__Ljava_lang_Double_2D", o);
|
jaroslav@355
|
539 |
}
|
jaroslav@355
|
540 |
if (type == Float.TYPE) {
|
jaroslav@355
|
541 |
return fromRaw(Float.class, "valueOf__Ljava_lang_Float_2F", o);
|
jaroslav@355
|
542 |
}
|
jaroslav@355
|
543 |
if (type == Byte.TYPE) {
|
jaroslav@355
|
544 |
return fromRaw(Byte.class, "valueOf__Ljava_lang_Byte_2B", o);
|
jaroslav@355
|
545 |
}
|
jaroslav@355
|
546 |
if (type == Boolean.TYPE) {
|
jaroslav@355
|
547 |
return fromRaw(Boolean.class, "valueOf__Ljava_lang_Boolean_2Z", o);
|
jaroslav@355
|
548 |
}
|
jaroslav@355
|
549 |
if (type == Short.TYPE) {
|
jaroslav@355
|
550 |
return fromRaw(Short.class, "valueOf__Ljava_lang_Short_2S", o);
|
jaroslav@355
|
551 |
}
|
jaroslav@355
|
552 |
// case 'V': return Void.TYPE;
|
jaroslav@355
|
553 |
throw new IllegalStateException("Can't convert " + o);
|
jtulach@258
|
554 |
}
|
jaroslav@354
|
555 |
|
jaroslav@355
|
556 |
@JavaScriptBody(args = { "cls", "m", "o" },
|
jaroslav@355
|
557 |
body = "return cls.cnstr(false)[m](o);"
|
jaroslav@355
|
558 |
)
|
jaroslav@355
|
559 |
private static native Integer fromRaw(Class<?> cls, String m, Object o);
|
jaroslav@354
|
560 |
|
jtulach@258
|
561 |
/**
|
jtulach@258
|
562 |
* Returns {@code true} if this method is a bridge
|
jtulach@258
|
563 |
* method; returns {@code false} otherwise.
|
jtulach@258
|
564 |
*
|
jtulach@258
|
565 |
* @return true if and only if this method is a bridge
|
jtulach@258
|
566 |
* method as defined by the Java Language Specification.
|
jtulach@258
|
567 |
* @since 1.5
|
jtulach@258
|
568 |
*/
|
jtulach@258
|
569 |
public boolean isBridge() {
|
jtulach@258
|
570 |
return (getModifiers() & Modifier.BRIDGE) != 0;
|
jtulach@258
|
571 |
}
|
jtulach@258
|
572 |
|
jtulach@258
|
573 |
/**
|
jtulach@258
|
574 |
* Returns {@code true} if this method was declared to take
|
jtulach@258
|
575 |
* a variable number of arguments; returns {@code false}
|
jtulach@258
|
576 |
* otherwise.
|
jtulach@258
|
577 |
*
|
jtulach@258
|
578 |
* @return {@code true} if an only if this method was declared to
|
jtulach@258
|
579 |
* take a variable number of arguments.
|
jtulach@258
|
580 |
* @since 1.5
|
jtulach@258
|
581 |
*/
|
jtulach@258
|
582 |
public boolean isVarArgs() {
|
jtulach@258
|
583 |
return (getModifiers() & Modifier.VARARGS) != 0;
|
jtulach@258
|
584 |
}
|
jtulach@258
|
585 |
|
jtulach@258
|
586 |
/**
|
jtulach@258
|
587 |
* Returns {@code true} if this method is a synthetic
|
jtulach@258
|
588 |
* method; returns {@code false} otherwise.
|
jtulach@258
|
589 |
*
|
jtulach@258
|
590 |
* @return true if and only if this method is a synthetic
|
jtulach@258
|
591 |
* method as defined by the Java Language Specification.
|
jtulach@258
|
592 |
* @since 1.5
|
jtulach@258
|
593 |
*/
|
jtulach@258
|
594 |
public boolean isSynthetic() {
|
jtulach@258
|
595 |
return Modifier.isSynthetic(getModifiers());
|
jtulach@258
|
596 |
}
|
jtulach@258
|
597 |
|
jaroslav@266
|
598 |
@JavaScriptBody(args = { "self", "ac" },
|
jaroslav@266
|
599 |
body =
|
jaroslav@266
|
600 |
"if (self.fld_data.anno) {"
|
jaroslav@266
|
601 |
+ " return self.fld_data.anno['L' + ac.jvmName + ';'];"
|
jaroslav@266
|
602 |
+ "} else return null;"
|
jaroslav@266
|
603 |
)
|
jaroslav@266
|
604 |
private Object getAnnotationData(Class<?> annotationClass) {
|
jaroslav@266
|
605 |
throw new UnsupportedOperationException();
|
jaroslav@266
|
606 |
}
|
jaroslav@266
|
607 |
|
jtulach@258
|
608 |
/**
|
jtulach@258
|
609 |
* @throws NullPointerException {@inheritDoc}
|
jtulach@258
|
610 |
* @since 1.5
|
jtulach@258
|
611 |
*/
|
jtulach@258
|
612 |
public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
|
jaroslav@266
|
613 |
Object data = getAnnotationData(annotationClass);
|
jaroslav@266
|
614 |
return data == null ? null : AnnotationImpl.create(annotationClass, data);
|
jtulach@258
|
615 |
}
|
jtulach@258
|
616 |
|
jtulach@258
|
617 |
/**
|
jtulach@258
|
618 |
* @since 1.5
|
jtulach@258
|
619 |
*/
|
jtulach@258
|
620 |
public Annotation[] getDeclaredAnnotations() {
|
jaroslav@260
|
621 |
throw new UnsupportedOperationException();
|
jtulach@258
|
622 |
}
|
jtulach@258
|
623 |
|
jtulach@258
|
624 |
/**
|
jtulach@258
|
625 |
* Returns the default value for the annotation member represented by
|
jtulach@258
|
626 |
* this {@code Method} instance. If the member is of a primitive type,
|
jtulach@258
|
627 |
* an instance of the corresponding wrapper type is returned. Returns
|
jtulach@258
|
628 |
* null if no default is associated with the member, or if the method
|
jtulach@258
|
629 |
* instance does not represent a declared member of an annotation type.
|
jtulach@258
|
630 |
*
|
jtulach@258
|
631 |
* @return the default value for the annotation member represented
|
jtulach@258
|
632 |
* by this {@code Method} instance.
|
jtulach@258
|
633 |
* @throws TypeNotPresentException if the annotation is of type
|
jtulach@258
|
634 |
* {@link Class} and no definition can be found for the
|
jtulach@258
|
635 |
* default class value.
|
jtulach@258
|
636 |
* @since 1.5
|
jtulach@258
|
637 |
*/
|
jtulach@258
|
638 |
public Object getDefaultValue() {
|
jaroslav@260
|
639 |
throw new UnsupportedOperationException();
|
jtulach@258
|
640 |
}
|
jtulach@258
|
641 |
|
jtulach@258
|
642 |
/**
|
jtulach@258
|
643 |
* Returns an array of arrays that represent the annotations on the formal
|
jtulach@258
|
644 |
* parameters, in declaration order, of the method represented by
|
jtulach@258
|
645 |
* this {@code Method} object. (Returns an array of length zero if the
|
jtulach@258
|
646 |
* underlying method is parameterless. If the method has one or more
|
jtulach@258
|
647 |
* parameters, a nested array of length zero is returned for each parameter
|
jtulach@258
|
648 |
* with no annotations.) The annotation objects contained in the returned
|
jtulach@258
|
649 |
* arrays are serializable. The caller of this method is free to modify
|
jtulach@258
|
650 |
* the returned arrays; it will have no effect on the arrays returned to
|
jtulach@258
|
651 |
* other callers.
|
jtulach@258
|
652 |
*
|
jtulach@258
|
653 |
* @return an array of arrays that represent the annotations on the formal
|
jtulach@258
|
654 |
* parameters, in declaration order, of the method represented by this
|
jtulach@258
|
655 |
* Method object
|
jtulach@258
|
656 |
* @since 1.5
|
jtulach@258
|
657 |
*/
|
jtulach@258
|
658 |
public Annotation[][] getParameterAnnotations() {
|
jaroslav@260
|
659 |
throw new UnsupportedOperationException();
|
jtulach@258
|
660 |
}
|
jaroslav@262
|
661 |
|
jaroslav@391
|
662 |
static {
|
jaroslav@391
|
663 |
MethodImpl.INSTANCE = new MethodImpl() {
|
jaroslav@391
|
664 |
protected Method create(Class<?> declaringClass, String name, Object data, String sig) {
|
jaroslav@391
|
665 |
return new Method(declaringClass, name, data, sig);
|
jaroslav@264
|
666 |
}
|
jaroslav@391
|
667 |
};
|
jaroslav@262
|
668 |
}
|
jtulach@258
|
669 |
}
|