1
|
1 |
/*
|
|
2 |
* Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*
|
|
23 |
*/
|
|
24 |
|
|
25 |
#include "incls/_precompiled.incl"
|
|
26 |
#include "incls/_reflection.cpp.incl"
|
|
27 |
|
|
28 |
#define JAVA_1_5_VERSION 49
|
|
29 |
|
|
30 |
static void trace_class_resolution(klassOop to_class) {
|
|
31 |
ResourceMark rm;
|
|
32 |
int line_number = -1;
|
|
33 |
const char * source_file = NULL;
|
|
34 |
klassOop caller = NULL;
|
|
35 |
JavaThread* jthread = JavaThread::current();
|
|
36 |
if (jthread->has_last_Java_frame()) {
|
|
37 |
vframeStream vfst(jthread);
|
|
38 |
// skip over any frames belonging to java.lang.Class
|
|
39 |
while (!vfst.at_end() &&
|
|
40 |
instanceKlass::cast(vfst.method()->method_holder())->name() == vmSymbols::java_lang_Class()) {
|
|
41 |
vfst.next();
|
|
42 |
}
|
|
43 |
if (!vfst.at_end()) {
|
|
44 |
// this frame is a likely suspect
|
|
45 |
caller = vfst.method()->method_holder();
|
|
46 |
line_number = vfst.method()->line_number_from_bci(vfst.bci());
|
|
47 |
symbolOop s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
|
|
48 |
if (s != NULL) {
|
|
49 |
source_file = s->as_C_string();
|
|
50 |
}
|
|
51 |
}
|
|
52 |
}
|
|
53 |
if (caller != NULL) {
|
|
54 |
const char * from = Klass::cast(caller)->external_name();
|
|
55 |
const char * to = Klass::cast(to_class)->external_name();
|
|
56 |
// print in a single call to reduce interleaving between threads
|
|
57 |
if (source_file != NULL) {
|
|
58 |
tty->print("RESOLVE %s %s %s:%d (reflection)\n", from, to, source_file, line_number);
|
|
59 |
} else {
|
|
60 |
tty->print("RESOLVE %s %s (reflection)\n", from, to);
|
|
61 |
}
|
|
62 |
}
|
|
63 |
}
|
|
64 |
|
|
65 |
|
|
66 |
oop Reflection::box(jvalue* value, BasicType type, TRAPS) {
|
|
67 |
if (type == T_VOID) {
|
|
68 |
return NULL;
|
|
69 |
}
|
|
70 |
if (type == T_OBJECT || type == T_ARRAY) {
|
|
71 |
// regular objects are not boxed
|
|
72 |
return (oop) value->l;
|
|
73 |
}
|
|
74 |
oop result = java_lang_boxing_object::create(type, value, CHECK_NULL);
|
|
75 |
if (result == NULL) {
|
|
76 |
THROW_(vmSymbols::java_lang_IllegalArgumentException(), result);
|
|
77 |
}
|
|
78 |
return result;
|
|
79 |
}
|
|
80 |
|
|
81 |
|
|
82 |
BasicType Reflection::unbox_for_primitive(oop box, jvalue* value, TRAPS) {
|
|
83 |
if (box == NULL) {
|
|
84 |
THROW_(vmSymbols::java_lang_IllegalArgumentException(), T_ILLEGAL);
|
|
85 |
}
|
|
86 |
return java_lang_boxing_object::get_value(box, value);
|
|
87 |
}
|
|
88 |
|
|
89 |
BasicType Reflection::unbox_for_regular_object(oop box, jvalue* value) {
|
|
90 |
// Note: box is really the unboxed oop. It might even be a Short, etc.!
|
|
91 |
value->l = (jobject) box;
|
|
92 |
return T_OBJECT;
|
|
93 |
}
|
|
94 |
|
|
95 |
|
|
96 |
void Reflection::widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS) {
|
|
97 |
assert(wide_type != current_type, "widen should not be called with identical types");
|
|
98 |
switch (wide_type) {
|
|
99 |
case T_BOOLEAN:
|
|
100 |
case T_BYTE:
|
|
101 |
case T_CHAR:
|
|
102 |
break; // fail
|
|
103 |
case T_SHORT:
|
|
104 |
switch (current_type) {
|
|
105 |
case T_BYTE:
|
|
106 |
value->s = (jshort) value->b;
|
|
107 |
return;
|
|
108 |
}
|
|
109 |
break; // fail
|
|
110 |
case T_INT:
|
|
111 |
switch (current_type) {
|
|
112 |
case T_BYTE:
|
|
113 |
value->i = (jint) value->b;
|
|
114 |
return;
|
|
115 |
case T_CHAR:
|
|
116 |
value->i = (jint) value->c;
|
|
117 |
return;
|
|
118 |
case T_SHORT:
|
|
119 |
value->i = (jint) value->s;
|
|
120 |
return;
|
|
121 |
}
|
|
122 |
break; // fail
|
|
123 |
case T_LONG:
|
|
124 |
switch (current_type) {
|
|
125 |
case T_BYTE:
|
|
126 |
value->j = (jlong) value->b;
|
|
127 |
return;
|
|
128 |
case T_CHAR:
|
|
129 |
value->j = (jlong) value->c;
|
|
130 |
return;
|
|
131 |
case T_SHORT:
|
|
132 |
value->j = (jlong) value->s;
|
|
133 |
return;
|
|
134 |
case T_INT:
|
|
135 |
value->j = (jlong) value->i;
|
|
136 |
return;
|
|
137 |
}
|
|
138 |
break; // fail
|
|
139 |
case T_FLOAT:
|
|
140 |
switch (current_type) {
|
|
141 |
case T_BYTE:
|
|
142 |
value->f = (jfloat) value->b;
|
|
143 |
return;
|
|
144 |
case T_CHAR:
|
|
145 |
value->f = (jfloat) value->c;
|
|
146 |
return;
|
|
147 |
case T_SHORT:
|
|
148 |
value->f = (jfloat) value->s;
|
|
149 |
return;
|
|
150 |
case T_INT:
|
|
151 |
value->f = (jfloat) value->i;
|
|
152 |
return;
|
|
153 |
case T_LONG:
|
|
154 |
value->f = (jfloat) value->j;
|
|
155 |
return;
|
|
156 |
}
|
|
157 |
break; // fail
|
|
158 |
case T_DOUBLE:
|
|
159 |
switch (current_type) {
|
|
160 |
case T_BYTE:
|
|
161 |
value->d = (jdouble) value->b;
|
|
162 |
return;
|
|
163 |
case T_CHAR:
|
|
164 |
value->d = (jdouble) value->c;
|
|
165 |
return;
|
|
166 |
case T_SHORT:
|
|
167 |
value->d = (jdouble) value->s;
|
|
168 |
return;
|
|
169 |
case T_INT:
|
|
170 |
value->d = (jdouble) value->i;
|
|
171 |
return;
|
|
172 |
case T_FLOAT:
|
|
173 |
value->d = (jdouble) value->f;
|
|
174 |
return;
|
|
175 |
case T_LONG:
|
|
176 |
value->d = (jdouble) value->j;
|
|
177 |
return;
|
|
178 |
}
|
|
179 |
break; // fail
|
|
180 |
default:
|
|
181 |
break; // fail
|
|
182 |
}
|
|
183 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
|
|
184 |
}
|
|
185 |
|
|
186 |
|
|
187 |
BasicType Reflection::array_get(jvalue* value, arrayOop a, int index, TRAPS) {
|
|
188 |
if (!a->is_within_bounds(index)) {
|
|
189 |
THROW_(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), T_ILLEGAL);
|
|
190 |
}
|
|
191 |
if (a->is_objArray()) {
|
|
192 |
value->l = (jobject) objArrayOop(a)->obj_at(index);
|
|
193 |
return T_OBJECT;
|
|
194 |
} else {
|
|
195 |
assert(a->is_typeArray(), "just checking");
|
|
196 |
BasicType type = typeArrayKlass::cast(a->klass())->element_type();
|
|
197 |
switch (type) {
|
|
198 |
case T_BOOLEAN:
|
|
199 |
value->z = typeArrayOop(a)->bool_at(index);
|
|
200 |
break;
|
|
201 |
case T_CHAR:
|
|
202 |
value->c = typeArrayOop(a)->char_at(index);
|
|
203 |
break;
|
|
204 |
case T_FLOAT:
|
|
205 |
value->f = typeArrayOop(a)->float_at(index);
|
|
206 |
break;
|
|
207 |
case T_DOUBLE:
|
|
208 |
value->d = typeArrayOop(a)->double_at(index);
|
|
209 |
break;
|
|
210 |
case T_BYTE:
|
|
211 |
value->b = typeArrayOop(a)->byte_at(index);
|
|
212 |
break;
|
|
213 |
case T_SHORT:
|
|
214 |
value->s = typeArrayOop(a)->short_at(index);
|
|
215 |
break;
|
|
216 |
case T_INT:
|
|
217 |
value->i = typeArrayOop(a)->int_at(index);
|
|
218 |
break;
|
|
219 |
case T_LONG:
|
|
220 |
value->j = typeArrayOop(a)->long_at(index);
|
|
221 |
break;
|
|
222 |
default:
|
|
223 |
return T_ILLEGAL;
|
|
224 |
}
|
|
225 |
return type;
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
|
|
230 |
void Reflection::array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS) {
|
|
231 |
if (!a->is_within_bounds(index)) {
|
|
232 |
THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
|
|
233 |
}
|
|
234 |
if (a->is_objArray()) {
|
|
235 |
if (value_type == T_OBJECT) {
|
|
236 |
oop obj = (oop) value->l;
|
|
237 |
if (obj != NULL) {
|
|
238 |
klassOop element_klass = objArrayKlass::cast(a->klass())->element_klass();
|
|
239 |
if (!obj->is_a(element_klass)) {
|
|
240 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "array element type mismatch");
|
|
241 |
}
|
|
242 |
}
|
|
243 |
objArrayOop(a)->obj_at_put(index, obj);
|
|
244 |
}
|
|
245 |
} else {
|
|
246 |
assert(a->is_typeArray(), "just checking");
|
|
247 |
BasicType array_type = typeArrayKlass::cast(a->klass())->element_type();
|
|
248 |
if (array_type != value_type) {
|
|
249 |
// The widen operation can potentially throw an exception, but cannot block,
|
|
250 |
// so typeArrayOop a is safe if the call succeeds.
|
|
251 |
widen(value, value_type, array_type, CHECK);
|
|
252 |
}
|
|
253 |
switch (array_type) {
|
|
254 |
case T_BOOLEAN:
|
|
255 |
typeArrayOop(a)->bool_at_put(index, value->z);
|
|
256 |
break;
|
|
257 |
case T_CHAR:
|
|
258 |
typeArrayOop(a)->char_at_put(index, value->c);
|
|
259 |
break;
|
|
260 |
case T_FLOAT:
|
|
261 |
typeArrayOop(a)->float_at_put(index, value->f);
|
|
262 |
break;
|
|
263 |
case T_DOUBLE:
|
|
264 |
typeArrayOop(a)->double_at_put(index, value->d);
|
|
265 |
break;
|
|
266 |
case T_BYTE:
|
|
267 |
typeArrayOop(a)->byte_at_put(index, value->b);
|
|
268 |
break;
|
|
269 |
case T_SHORT:
|
|
270 |
typeArrayOop(a)->short_at_put(index, value->s);
|
|
271 |
break;
|
|
272 |
case T_INT:
|
|
273 |
typeArrayOop(a)->int_at_put(index, value->i);
|
|
274 |
break;
|
|
275 |
case T_LONG:
|
|
276 |
typeArrayOop(a)->long_at_put(index, value->j);
|
|
277 |
break;
|
|
278 |
default:
|
|
279 |
THROW(vmSymbols::java_lang_IllegalArgumentException());
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
|
|
285 |
klassOop Reflection::basic_type_mirror_to_arrayklass(oop basic_type_mirror, TRAPS) {
|
|
286 |
assert(java_lang_Class::is_primitive(basic_type_mirror), "just checking");
|
|
287 |
BasicType type = java_lang_Class::primitive_type(basic_type_mirror);
|
|
288 |
if (type == T_VOID) {
|
|
289 |
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
|
|
290 |
} else {
|
|
291 |
return Universe::typeArrayKlassObj(type);
|
|
292 |
}
|
|
293 |
}
|
|
294 |
|
|
295 |
|
|
296 |
oop Reflection:: basic_type_arrayklass_to_mirror(klassOop basic_type_arrayklass, TRAPS) {
|
|
297 |
BasicType type = typeArrayKlass::cast(basic_type_arrayklass)->element_type();
|
|
298 |
return Universe::java_mirror(type);
|
|
299 |
}
|
|
300 |
|
|
301 |
|
|
302 |
arrayOop Reflection::reflect_new_array(oop element_mirror, jint length, TRAPS) {
|
|
303 |
if (element_mirror == NULL) {
|
|
304 |
THROW_0(vmSymbols::java_lang_NullPointerException());
|
|
305 |
}
|
|
306 |
if (length < 0) {
|
|
307 |
THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
|
|
308 |
}
|
|
309 |
if (java_lang_Class::is_primitive(element_mirror)) {
|
|
310 |
klassOop tak = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
|
|
311 |
return typeArrayKlass::cast(tak)->allocate(length, THREAD);
|
|
312 |
} else {
|
|
313 |
klassOop k = java_lang_Class::as_klassOop(element_mirror);
|
|
314 |
if (Klass::cast(k)->oop_is_array() && arrayKlass::cast(k)->dimension() >= MAX_DIM) {
|
|
315 |
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
|
|
316 |
}
|
|
317 |
return oopFactory::new_objArray(k, length, THREAD);
|
|
318 |
}
|
|
319 |
}
|
|
320 |
|
|
321 |
|
|
322 |
arrayOop Reflection::reflect_new_multi_array(oop element_mirror, typeArrayOop dim_array, TRAPS) {
|
|
323 |
assert(dim_array->is_typeArray(), "just checking");
|
|
324 |
assert(typeArrayKlass::cast(dim_array->klass())->element_type() == T_INT, "just checking");
|
|
325 |
|
|
326 |
if (element_mirror == NULL) {
|
|
327 |
THROW_0(vmSymbols::java_lang_NullPointerException());
|
|
328 |
}
|
|
329 |
|
|
330 |
int len = dim_array->length();
|
|
331 |
if (len <= 0 || len > MAX_DIM) {
|
|
332 |
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
|
|
333 |
}
|
|
334 |
|
|
335 |
jint dimensions[MAX_DIM]; // C array copy of intArrayOop
|
|
336 |
for (int i = 0; i < len; i++) {
|
|
337 |
int d = dim_array->int_at(i);
|
|
338 |
if (d < 0) {
|
|
339 |
THROW_0(vmSymbols::java_lang_NegativeArraySizeException());
|
|
340 |
}
|
|
341 |
dimensions[i] = d;
|
|
342 |
}
|
|
343 |
|
|
344 |
klassOop klass;
|
|
345 |
int dim = len;
|
|
346 |
if (java_lang_Class::is_primitive(element_mirror)) {
|
|
347 |
klass = basic_type_mirror_to_arrayklass(element_mirror, CHECK_NULL);
|
|
348 |
} else {
|
|
349 |
klass = java_lang_Class::as_klassOop(element_mirror);
|
|
350 |
if (Klass::cast(klass)->oop_is_array()) {
|
|
351 |
int k_dim = arrayKlass::cast(klass)->dimension();
|
|
352 |
if (k_dim + len > MAX_DIM) {
|
|
353 |
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
|
|
354 |
}
|
|
355 |
dim += k_dim;
|
|
356 |
}
|
|
357 |
}
|
|
358 |
klass = Klass::cast(klass)->array_klass(dim, CHECK_NULL);
|
|
359 |
oop obj = arrayKlass::cast(klass)->multi_allocate(len, dimensions, THREAD);
|
|
360 |
assert(obj->is_array(), "just checking");
|
|
361 |
return arrayOop(obj);
|
|
362 |
}
|
|
363 |
|
|
364 |
|
|
365 |
oop Reflection::array_component_type(oop mirror, TRAPS) {
|
|
366 |
if (java_lang_Class::is_primitive(mirror)) {
|
|
367 |
return NULL;
|
|
368 |
}
|
|
369 |
|
|
370 |
klassOop klass = java_lang_Class::as_klassOop(mirror);
|
|
371 |
if (!Klass::cast(klass)->oop_is_array()) {
|
|
372 |
return NULL;
|
|
373 |
}
|
|
374 |
|
|
375 |
oop result = arrayKlass::cast(klass)->component_mirror();
|
|
376 |
#ifdef ASSERT
|
|
377 |
oop result2 = NULL;
|
|
378 |
if (arrayKlass::cast(klass)->dimension() == 1) {
|
|
379 |
if (Klass::cast(klass)->oop_is_typeArray()) {
|
|
380 |
result2 = basic_type_arrayklass_to_mirror(klass, CHECK_NULL);
|
|
381 |
} else {
|
|
382 |
result2 = Klass::cast(objArrayKlass::cast(klass)->element_klass())->java_mirror();
|
|
383 |
}
|
|
384 |
} else {
|
|
385 |
klassOop lower_dim = arrayKlass::cast(klass)->lower_dimension();
|
|
386 |
assert(Klass::cast(lower_dim)->oop_is_array(), "just checking");
|
|
387 |
result2 = Klass::cast(lower_dim)->java_mirror();
|
|
388 |
}
|
|
389 |
assert(result == result2, "results must be consistent");
|
|
390 |
#endif //ASSERT
|
|
391 |
return result;
|
|
392 |
}
|
|
393 |
|
|
394 |
|
|
395 |
bool Reflection::reflect_check_access(klassOop field_class, AccessFlags acc, klassOop target_class, bool is_method_invoke, TRAPS) {
|
|
396 |
// field_class : declaring class
|
|
397 |
// acc : declared field access
|
|
398 |
// target_class : for protected
|
|
399 |
|
|
400 |
// Check if field or method is accessible to client. Throw an
|
|
401 |
// IllegalAccessException and return false if not.
|
|
402 |
|
|
403 |
// The "client" is the class associated with the nearest real frame
|
|
404 |
// getCallerClass already skips Method.invoke frames, so pass 0 in
|
|
405 |
// that case (same as classic).
|
|
406 |
ResourceMark rm(THREAD);
|
|
407 |
assert(THREAD->is_Java_thread(), "sanity check");
|
|
408 |
klassOop client_class = ((JavaThread *)THREAD)->security_get_caller_class(is_method_invoke ? 0 : 1);
|
|
409 |
|
|
410 |
if (client_class != field_class) {
|
|
411 |
if (!verify_class_access(client_class, field_class, false)
|
|
412 |
|| !verify_field_access(client_class,
|
|
413 |
field_class,
|
|
414 |
field_class,
|
|
415 |
acc,
|
|
416 |
false)) {
|
|
417 |
THROW_(vmSymbols::java_lang_IllegalAccessException(), false);
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
// Additional test for protected members: JLS 6.6.2
|
|
422 |
|
|
423 |
if (acc.is_protected()) {
|
|
424 |
if (target_class != client_class) {
|
|
425 |
if (!is_same_class_package(client_class, field_class)) {
|
|
426 |
if (!Klass::cast(target_class)->is_subclass_of(client_class)) {
|
|
427 |
THROW_(vmSymbols::java_lang_IllegalAccessException(), false);
|
|
428 |
}
|
|
429 |
}
|
|
430 |
}
|
|
431 |
}
|
|
432 |
|
|
433 |
// Passed all tests
|
|
434 |
return true;
|
|
435 |
}
|
|
436 |
|
|
437 |
|
|
438 |
bool Reflection::verify_class_access(klassOop current_class, klassOop new_class, bool classloader_only) {
|
|
439 |
// Verify that current_class can access new_class. If the classloader_only
|
|
440 |
// flag is set, we automatically allow any accesses in which current_class
|
|
441 |
// doesn't have a classloader.
|
|
442 |
if ((current_class == NULL) ||
|
|
443 |
(current_class == new_class) ||
|
|
444 |
(instanceKlass::cast(new_class)->is_public()) ||
|
|
445 |
is_same_class_package(current_class, new_class)) {
|
|
446 |
return true;
|
|
447 |
}
|
|
448 |
// New (1.4) reflection implementation. Allow all accesses from
|
|
449 |
// sun/reflect/MagicAccessorImpl subclasses to succeed trivially.
|
|
450 |
if ( JDK_Version::is_gte_jdk14x_version()
|
|
451 |
&& UseNewReflection
|
|
452 |
&& Klass::cast(current_class)->is_subclass_of(SystemDictionary::reflect_magic_klass())) {
|
|
453 |
return true;
|
|
454 |
}
|
|
455 |
|
|
456 |
return can_relax_access_check_for(current_class, new_class, classloader_only);
|
|
457 |
}
|
|
458 |
|
|
459 |
bool Reflection::can_relax_access_check_for(
|
|
460 |
klassOop accessor, klassOop accessee, bool classloader_only) {
|
|
461 |
instanceKlass* accessor_ik = instanceKlass::cast(accessor);
|
|
462 |
instanceKlass* accessee_ik = instanceKlass::cast(accessee);
|
|
463 |
if (RelaxAccessControlCheck ||
|
|
464 |
(accessor_ik->major_version() < JAVA_1_5_VERSION &&
|
|
465 |
accessee_ik->major_version() < JAVA_1_5_VERSION)) {
|
|
466 |
return classloader_only &&
|
|
467 |
Verifier::relax_verify_for(accessor_ik->class_loader()) &&
|
|
468 |
accessor_ik->protection_domain() == accessee_ik->protection_domain() &&
|
|
469 |
accessor_ik->class_loader() == accessee_ik->class_loader();
|
|
470 |
} else {
|
|
471 |
return false;
|
|
472 |
}
|
|
473 |
}
|
|
474 |
|
|
475 |
bool Reflection::verify_field_access(klassOop current_class,
|
|
476 |
klassOop resolved_class,
|
|
477 |
klassOop field_class,
|
|
478 |
AccessFlags access,
|
|
479 |
bool classloader_only,
|
|
480 |
bool protected_restriction) {
|
|
481 |
// Verify that current_class can access a field of field_class, where that
|
|
482 |
// field's access bits are "access". We assume that we've already verified
|
|
483 |
// that current_class can access field_class.
|
|
484 |
//
|
|
485 |
// If the classloader_only flag is set, we automatically allow any accesses
|
|
486 |
// in which current_class doesn't have a classloader.
|
|
487 |
//
|
|
488 |
// "resolved_class" is the runtime type of "field_class". Sometimes we don't
|
|
489 |
// need this distinction (e.g. if all we have is the runtime type, or during
|
|
490 |
// class file parsing when we only care about the static type); in that case
|
|
491 |
// callers should ensure that resolved_class == field_class.
|
|
492 |
//
|
|
493 |
if ((current_class == NULL) ||
|
|
494 |
(current_class == field_class) ||
|
|
495 |
access.is_public()) {
|
|
496 |
return true;
|
|
497 |
}
|
|
498 |
|
|
499 |
if (access.is_protected()) {
|
|
500 |
if (!protected_restriction) {
|
|
501 |
// See if current_class is a subclass of field_class
|
|
502 |
if (Klass::cast(current_class)->is_subclass_of(field_class)) {
|
|
503 |
if (current_class == resolved_class ||
|
|
504 |
field_class == resolved_class ||
|
|
505 |
Klass::cast(current_class)->is_subclass_of(resolved_class) ||
|
|
506 |
Klass::cast(resolved_class)->is_subclass_of(current_class)) {
|
|
507 |
return true;
|
|
508 |
}
|
|
509 |
}
|
|
510 |
}
|
|
511 |
}
|
|
512 |
|
|
513 |
if (!access.is_private() && is_same_class_package(current_class, field_class)) {
|
|
514 |
return true;
|
|
515 |
}
|
|
516 |
|
|
517 |
// New (1.4) reflection implementation. Allow all accesses from
|
|
518 |
// sun/reflect/MagicAccessorImpl subclasses to succeed trivially.
|
|
519 |
if ( JDK_Version::is_gte_jdk14x_version()
|
|
520 |
&& UseNewReflection
|
|
521 |
&& Klass::cast(current_class)->is_subclass_of(SystemDictionary::reflect_magic_klass())) {
|
|
522 |
return true;
|
|
523 |
}
|
|
524 |
|
|
525 |
return can_relax_access_check_for(
|
|
526 |
current_class, field_class, classloader_only);
|
|
527 |
}
|
|
528 |
|
|
529 |
|
|
530 |
bool Reflection::is_same_class_package(klassOop class1, klassOop class2) {
|
|
531 |
return instanceKlass::cast(class1)->is_same_class_package(class2);
|
|
532 |
}
|
|
533 |
|
|
534 |
|
|
535 |
// Checks that the 'outer' klass has declared 'inner' as being an inner klass. If not,
|
|
536 |
// throw an incompatible class change exception
|
|
537 |
void Reflection::check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner, TRAPS) {
|
|
538 |
const int inner_class_info_index = 0;
|
|
539 |
const int outer_class_info_index = 1;
|
|
540 |
|
|
541 |
typeArrayHandle icls (THREAD, outer->inner_classes());
|
|
542 |
constantPoolHandle cp (THREAD, outer->constants());
|
|
543 |
for(int i = 0; i < icls->length(); i += 4) {
|
|
544 |
int ioff = icls->ushort_at(i + inner_class_info_index);
|
|
545 |
int ooff = icls->ushort_at(i + outer_class_info_index);
|
|
546 |
|
|
547 |
if (ioff != 0 && ooff != 0) {
|
|
548 |
klassOop o = cp->klass_at(ooff, CHECK);
|
|
549 |
if (o == outer()) {
|
|
550 |
klassOop i = cp->klass_at(ioff, CHECK);
|
|
551 |
if (i == inner()) {
|
|
552 |
return;
|
|
553 |
}
|
|
554 |
}
|
|
555 |
}
|
|
556 |
}
|
|
557 |
|
|
558 |
// 'inner' not declared as an inner klass in outer
|
|
559 |
ResourceMark rm(THREAD);
|
|
560 |
Exceptions::fthrow(
|
|
561 |
THREAD_AND_LOCATION,
|
|
562 |
vmSymbolHandles::java_lang_IncompatibleClassChangeError(),
|
|
563 |
"%s and %s disagree on InnerClasses attribute",
|
|
564 |
outer->external_name(),
|
|
565 |
inner->external_name()
|
|
566 |
);
|
|
567 |
}
|
|
568 |
|
|
569 |
// Utility method converting a single SignatureStream element into java.lang.Class instance
|
|
570 |
|
|
571 |
oop get_mirror_from_signature(methodHandle method, SignatureStream* ss, TRAPS) {
|
|
572 |
switch (ss->type()) {
|
|
573 |
default:
|
|
574 |
assert(ss->type() != T_VOID || ss->at_return_type(), "T_VOID should only appear as return type");
|
|
575 |
return java_lang_Class::primitive_mirror(ss->type());
|
|
576 |
case T_OBJECT:
|
|
577 |
case T_ARRAY:
|
|
578 |
symbolOop name = ss->as_symbol(CHECK_NULL);
|
|
579 |
oop loader = instanceKlass::cast(method->method_holder())->class_loader();
|
|
580 |
oop protection_domain = instanceKlass::cast(method->method_holder())->protection_domain();
|
|
581 |
klassOop k = SystemDictionary::resolve_or_fail(
|
|
582 |
symbolHandle(THREAD, name),
|
|
583 |
Handle(THREAD, loader),
|
|
584 |
Handle(THREAD, protection_domain),
|
|
585 |
true, CHECK_NULL);
|
|
586 |
if (TraceClassResolution) {
|
|
587 |
trace_class_resolution(k);
|
|
588 |
}
|
|
589 |
return k->klass_part()->java_mirror();
|
|
590 |
};
|
|
591 |
}
|
|
592 |
|
|
593 |
|
|
594 |
objArrayHandle Reflection::get_parameter_types(methodHandle method, int parameter_count, oop* return_type, TRAPS) {
|
|
595 |
// Allocate array holding parameter types (java.lang.Class instances)
|
|
596 |
objArrayOop m = oopFactory::new_objArray(SystemDictionary::class_klass(), parameter_count, CHECK_(objArrayHandle()));
|
|
597 |
objArrayHandle mirrors (THREAD, m);
|
|
598 |
int index = 0;
|
|
599 |
// Collect parameter types
|
|
600 |
symbolHandle signature (THREAD, method->signature());
|
|
601 |
SignatureStream ss(signature);
|
|
602 |
while (!ss.at_return_type()) {
|
|
603 |
oop mirror = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
|
|
604 |
mirrors->obj_at_put(index++, mirror);
|
|
605 |
ss.next();
|
|
606 |
}
|
|
607 |
assert(index == parameter_count, "invalid parameter count");
|
|
608 |
if (return_type != NULL) {
|
|
609 |
// Collect return type as well
|
|
610 |
assert(ss.at_return_type(), "return type should be present");
|
|
611 |
*return_type = get_mirror_from_signature(method, &ss, CHECK_(objArrayHandle()));
|
|
612 |
}
|
|
613 |
return mirrors;
|
|
614 |
}
|
|
615 |
|
|
616 |
objArrayHandle Reflection::get_exception_types(methodHandle method, TRAPS) {
|
|
617 |
return method->resolved_checked_exceptions(CHECK_(objArrayHandle()));
|
|
618 |
}
|
|
619 |
|
|
620 |
|
|
621 |
Handle Reflection::new_type(symbolHandle signature, KlassHandle k, TRAPS) {
|
|
622 |
// Basic types
|
|
623 |
BasicType type = vmSymbols::signature_type(signature());
|
|
624 |
if (type != T_OBJECT) {
|
|
625 |
return Handle(THREAD, Universe::java_mirror(type));
|
|
626 |
}
|
|
627 |
|
|
628 |
oop loader = instanceKlass::cast(k())->class_loader();
|
|
629 |
oop protection_domain = Klass::cast(k())->protection_domain();
|
|
630 |
klassOop result = SystemDictionary::resolve_or_fail(signature,
|
|
631 |
Handle(THREAD, loader),
|
|
632 |
Handle(THREAD, protection_domain),
|
|
633 |
true, CHECK_(Handle()));
|
|
634 |
|
|
635 |
if (TraceClassResolution) {
|
|
636 |
trace_class_resolution(result);
|
|
637 |
}
|
|
638 |
|
|
639 |
oop nt = Klass::cast(result)->java_mirror();
|
|
640 |
return Handle(THREAD, nt);
|
|
641 |
}
|
|
642 |
|
|
643 |
|
|
644 |
oop Reflection::new_method(methodHandle method, bool intern_name, bool for_constant_pool_access, TRAPS) {
|
|
645 |
// In jdk1.2.x, getMethods on an interface erroneously includes <clinit>, thus the complicated assert.
|
|
646 |
// Also allow sun.reflect.ConstantPool to refer to <clinit> methods as java.lang.reflect.Methods.
|
|
647 |
assert(!method()->is_initializer() ||
|
|
648 |
(for_constant_pool_access && method()->is_static()) ||
|
|
649 |
(method()->name() == vmSymbols::class_initializer_name()
|
|
650 |
&& Klass::cast(method()->method_holder())->is_interface() && JDK_Version::is_jdk12x_version()), "should call new_constructor instead");
|
|
651 |
instanceKlassHandle holder (THREAD, method->method_holder());
|
|
652 |
int slot = method->method_idnum();
|
|
653 |
|
|
654 |
symbolHandle signature (THREAD, method->signature());
|
|
655 |
int parameter_count = ArgumentCount(signature).size();
|
|
656 |
oop return_type_oop = NULL;
|
|
657 |
objArrayHandle parameter_types = get_parameter_types(method, parameter_count, &return_type_oop, CHECK_NULL);
|
|
658 |
if (parameter_types.is_null() || return_type_oop == NULL) return NULL;
|
|
659 |
|
|
660 |
Handle return_type(THREAD, return_type_oop);
|
|
661 |
|
|
662 |
objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
|
|
663 |
|
|
664 |
if (exception_types.is_null()) return NULL;
|
|
665 |
|
|
666 |
symbolHandle method_name(THREAD, method->name());
|
|
667 |
Handle name;
|
|
668 |
if (intern_name) {
|
|
669 |
// intern_name is only true with UseNewReflection
|
|
670 |
oop name_oop = StringTable::intern(method_name(), CHECK_NULL);
|
|
671 |
name = Handle(THREAD, name_oop);
|
|
672 |
} else {
|
|
673 |
name = java_lang_String::create_from_symbol(method_name, CHECK_NULL);
|
|
674 |
}
|
|
675 |
if (name.is_null()) return NULL;
|
|
676 |
|
|
677 |
int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
|
|
678 |
|
|
679 |
Handle mh = java_lang_reflect_Method::create(CHECK_NULL);
|
|
680 |
|
|
681 |
java_lang_reflect_Method::set_clazz(mh(), holder->java_mirror());
|
|
682 |
java_lang_reflect_Method::set_slot(mh(), slot);
|
|
683 |
java_lang_reflect_Method::set_name(mh(), name());
|
|
684 |
java_lang_reflect_Method::set_return_type(mh(), return_type());
|
|
685 |
java_lang_reflect_Method::set_parameter_types(mh(), parameter_types());
|
|
686 |
java_lang_reflect_Method::set_exception_types(mh(), exception_types());
|
|
687 |
java_lang_reflect_Method::set_modifiers(mh(), modifiers);
|
|
688 |
java_lang_reflect_Method::set_override(mh(), false);
|
|
689 |
if (java_lang_reflect_Method::has_signature_field() &&
|
|
690 |
method->generic_signature() != NULL) {
|
|
691 |
symbolHandle gs(THREAD, method->generic_signature());
|
|
692 |
Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
|
|
693 |
java_lang_reflect_Method::set_signature(mh(), sig());
|
|
694 |
}
|
|
695 |
if (java_lang_reflect_Method::has_annotations_field()) {
|
|
696 |
java_lang_reflect_Method::set_annotations(mh(), method->annotations());
|
|
697 |
}
|
|
698 |
if (java_lang_reflect_Method::has_parameter_annotations_field()) {
|
|
699 |
java_lang_reflect_Method::set_parameter_annotations(mh(), method->parameter_annotations());
|
|
700 |
}
|
|
701 |
if (java_lang_reflect_Method::has_annotation_default_field()) {
|
|
702 |
java_lang_reflect_Method::set_annotation_default(mh(), method->annotation_default());
|
|
703 |
}
|
|
704 |
return mh();
|
|
705 |
}
|
|
706 |
|
|
707 |
|
|
708 |
oop Reflection::new_constructor(methodHandle method, TRAPS) {
|
|
709 |
assert(method()->is_initializer(), "should call new_method instead");
|
|
710 |
|
|
711 |
instanceKlassHandle holder (THREAD, method->method_holder());
|
|
712 |
int slot = method->method_idnum();
|
|
713 |
|
|
714 |
symbolHandle signature (THREAD, method->signature());
|
|
715 |
int parameter_count = ArgumentCount(signature).size();
|
|
716 |
objArrayHandle parameter_types = get_parameter_types(method, parameter_count, NULL, CHECK_NULL);
|
|
717 |
if (parameter_types.is_null()) return NULL;
|
|
718 |
|
|
719 |
objArrayHandle exception_types = get_exception_types(method, CHECK_NULL);
|
|
720 |
if (exception_types.is_null()) return NULL;
|
|
721 |
|
|
722 |
int modifiers = method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
|
|
723 |
|
|
724 |
Handle ch = java_lang_reflect_Constructor::create(CHECK_NULL);
|
|
725 |
|
|
726 |
java_lang_reflect_Constructor::set_clazz(ch(), holder->java_mirror());
|
|
727 |
java_lang_reflect_Constructor::set_slot(ch(), slot);
|
|
728 |
java_lang_reflect_Constructor::set_parameter_types(ch(), parameter_types());
|
|
729 |
java_lang_reflect_Constructor::set_exception_types(ch(), exception_types());
|
|
730 |
java_lang_reflect_Constructor::set_modifiers(ch(), modifiers);
|
|
731 |
java_lang_reflect_Constructor::set_override(ch(), false);
|
|
732 |
if (java_lang_reflect_Constructor::has_signature_field() &&
|
|
733 |
method->generic_signature() != NULL) {
|
|
734 |
symbolHandle gs(THREAD, method->generic_signature());
|
|
735 |
Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
|
|
736 |
java_lang_reflect_Constructor::set_signature(ch(), sig());
|
|
737 |
}
|
|
738 |
if (java_lang_reflect_Constructor::has_annotations_field()) {
|
|
739 |
java_lang_reflect_Constructor::set_annotations(ch(), method->annotations());
|
|
740 |
}
|
|
741 |
if (java_lang_reflect_Constructor::has_parameter_annotations_field()) {
|
|
742 |
java_lang_reflect_Constructor::set_parameter_annotations(ch(), method->parameter_annotations());
|
|
743 |
}
|
|
744 |
return ch();
|
|
745 |
}
|
|
746 |
|
|
747 |
|
|
748 |
oop Reflection::new_field(fieldDescriptor* fd, bool intern_name, TRAPS) {
|
|
749 |
symbolHandle field_name(THREAD, fd->name());
|
|
750 |
Handle name;
|
|
751 |
if (intern_name) {
|
|
752 |
// intern_name is only true with UseNewReflection
|
|
753 |
oop name_oop = StringTable::intern(field_name(), CHECK_NULL);
|
|
754 |
name = Handle(THREAD, name_oop);
|
|
755 |
} else {
|
|
756 |
name = java_lang_String::create_from_symbol(field_name, CHECK_NULL);
|
|
757 |
}
|
|
758 |
symbolHandle signature (THREAD, fd->signature());
|
|
759 |
KlassHandle holder (THREAD, fd->field_holder());
|
|
760 |
Handle type = new_type(signature, holder, CHECK_NULL);
|
|
761 |
Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
|
|
762 |
|
|
763 |
java_lang_reflect_Field::set_clazz(rh(), Klass::cast(fd->field_holder())->java_mirror());
|
|
764 |
java_lang_reflect_Field::set_slot(rh(), fd->index());
|
|
765 |
java_lang_reflect_Field::set_name(rh(), name());
|
|
766 |
java_lang_reflect_Field::set_type(rh(), type());
|
|
767 |
// Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
|
|
768 |
java_lang_reflect_Field::set_modifiers(rh(), fd->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
|
|
769 |
java_lang_reflect_Field::set_override(rh(), false);
|
|
770 |
if (java_lang_reflect_Field::has_signature_field() &&
|
|
771 |
fd->generic_signature() != NULL) {
|
|
772 |
symbolHandle gs(THREAD, fd->generic_signature());
|
|
773 |
Handle sig = java_lang_String::create_from_symbol(gs, CHECK_NULL);
|
|
774 |
java_lang_reflect_Field::set_signature(rh(), sig());
|
|
775 |
}
|
|
776 |
if (java_lang_reflect_Field::has_annotations_field()) {
|
|
777 |
java_lang_reflect_Field::set_annotations(rh(), fd->annotations());
|
|
778 |
}
|
|
779 |
return rh();
|
|
780 |
}
|
|
781 |
|
|
782 |
|
|
783 |
//---------------------------------------------------------------------------
|
|
784 |
//
|
|
785 |
// Supporting routines for old native code-based reflection (pre-JDK 1.4).
|
|
786 |
//
|
|
787 |
// See reflection.hpp for details.
|
|
788 |
//
|
|
789 |
//---------------------------------------------------------------------------
|
|
790 |
|
|
791 |
#ifdef SUPPORT_OLD_REFLECTION
|
|
792 |
|
|
793 |
methodHandle Reflection::resolve_interface_call(instanceKlassHandle klass, methodHandle method,
|
|
794 |
KlassHandle recv_klass, Handle receiver, TRAPS) {
|
|
795 |
assert(!method.is_null() , "method should not be null");
|
|
796 |
|
|
797 |
CallInfo info;
|
|
798 |
symbolHandle signature (THREAD, method->signature());
|
|
799 |
symbolHandle name (THREAD, method->name());
|
|
800 |
LinkResolver::resolve_interface_call(info, receiver, recv_klass, klass,
|
|
801 |
name, signature,
|
|
802 |
KlassHandle(), false, true,
|
|
803 |
CHECK_(methodHandle()));
|
|
804 |
return info.selected_method();
|
|
805 |
}
|
|
806 |
|
|
807 |
|
|
808 |
oop Reflection::invoke(instanceKlassHandle klass, methodHandle reflected_method,
|
|
809 |
Handle receiver, bool override, objArrayHandle ptypes,
|
|
810 |
BasicType rtype, objArrayHandle args, bool is_method_invoke, TRAPS) {
|
|
811 |
ResourceMark rm(THREAD);
|
|
812 |
|
|
813 |
methodHandle method; // actual method to invoke
|
|
814 |
KlassHandle target_klass; // target klass, receiver's klass for non-static
|
|
815 |
|
|
816 |
// Ensure klass is initialized
|
|
817 |
klass->initialize(CHECK_NULL);
|
|
818 |
|
|
819 |
bool is_static = reflected_method->is_static();
|
|
820 |
if (is_static) {
|
|
821 |
// ignore receiver argument
|
|
822 |
method = reflected_method;
|
|
823 |
target_klass = klass;
|
|
824 |
} else {
|
|
825 |
// check for null receiver
|
|
826 |
if (receiver.is_null()) {
|
|
827 |
THROW_0(vmSymbols::java_lang_NullPointerException());
|
|
828 |
}
|
|
829 |
// Check class of receiver against class declaring method
|
|
830 |
if (!receiver->is_a(klass())) {
|
|
831 |
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class");
|
|
832 |
}
|
|
833 |
// target klass is receiver's klass
|
|
834 |
target_klass = KlassHandle(THREAD, receiver->klass());
|
|
835 |
// no need to resolve if method is private or <init>
|
|
836 |
if (reflected_method->is_private() || reflected_method->name() == vmSymbols::object_initializer_name()) {
|
|
837 |
method = reflected_method;
|
|
838 |
} else {
|
|
839 |
// resolve based on the receiver
|
|
840 |
if (instanceKlass::cast(reflected_method->method_holder())->is_interface()) {
|
|
841 |
// resolve interface call
|
|
842 |
if (ReflectionWrapResolutionErrors) {
|
|
843 |
// new default: 6531596
|
|
844 |
// Match resolution errors with those thrown due to reflection inlining
|
|
845 |
// Linktime resolution & IllegalAccessCheck already done by Class.getMethod()
|
|
846 |
method = resolve_interface_call(klass, reflected_method, target_klass, receiver, THREAD);
|
|
847 |
if (HAS_PENDING_EXCEPTION) {
|
|
848 |
// Method resolution threw an exception; wrap it in an InvocationTargetException
|
|
849 |
oop resolution_exception = PENDING_EXCEPTION;
|
|
850 |
CLEAR_PENDING_EXCEPTION;
|
|
851 |
JavaCallArguments args(Handle(THREAD, resolution_exception));
|
|
852 |
THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(),
|
|
853 |
vmSymbolHandles::throwable_void_signature(),
|
|
854 |
&args);
|
|
855 |
}
|
|
856 |
} else {
|
|
857 |
method = resolve_interface_call(klass, reflected_method, target_klass, receiver, CHECK_(NULL));
|
|
858 |
}
|
|
859 |
} else {
|
|
860 |
// if the method can be overridden, we resolve using the vtable index.
|
|
861 |
int index = reflected_method->vtable_index();
|
|
862 |
method = reflected_method;
|
|
863 |
if (index != methodOopDesc::nonvirtual_vtable_index) {
|
|
864 |
// target_klass might be an arrayKlassOop but all vtables start at
|
|
865 |
// the same place. The cast is to avoid virtual call and assertion.
|
|
866 |
instanceKlass* inst = (instanceKlass*)target_klass()->klass_part();
|
|
867 |
method = methodHandle(THREAD, inst->method_at_vtable(index));
|
|
868 |
}
|
|
869 |
if (!method.is_null()) {
|
|
870 |
// Check for abstract methods as well
|
|
871 |
if (method->is_abstract()) {
|
|
872 |
// new default: 6531596
|
|
873 |
if (ReflectionWrapResolutionErrors) {
|
|
874 |
ResourceMark rm(THREAD);
|
|
875 |
Handle h_origexception = Exceptions::new_exception(THREAD,
|
|
876 |
vmSymbols::java_lang_AbstractMethodError(),
|
|
877 |
methodOopDesc::name_and_sig_as_C_string(Klass::cast(target_klass()),
|
|
878 |
method->name(),
|
|
879 |
method->signature()));
|
|
880 |
JavaCallArguments args(h_origexception);
|
|
881 |
THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(),
|
|
882 |
vmSymbolHandles::throwable_void_signature(),
|
|
883 |
&args);
|
|
884 |
} else {
|
|
885 |
ResourceMark rm(THREAD);
|
|
886 |
THROW_MSG_0(vmSymbols::java_lang_AbstractMethodError(),
|
|
887 |
methodOopDesc::name_and_sig_as_C_string(Klass::cast(target_klass()),
|
|
888 |
method->name(),
|
|
889 |
method->signature()));
|
|
890 |
}
|
|
891 |
}
|
|
892 |
}
|
|
893 |
}
|
|
894 |
}
|
|
895 |
}
|
|
896 |
|
|
897 |
// I believe this is a ShouldNotGetHere case which requires
|
|
898 |
// an internal vtable bug. If you ever get this please let Karen know.
|
|
899 |
if (method.is_null()) {
|
|
900 |
ResourceMark rm(THREAD);
|
|
901 |
THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
|
|
902 |
methodOopDesc::name_and_sig_as_C_string(Klass::cast(klass()),
|
|
903 |
reflected_method->name(),
|
|
904 |
reflected_method->signature()));
|
|
905 |
}
|
|
906 |
|
|
907 |
// In the JDK 1.4 reflection implementation, the security check is
|
|
908 |
// done at the Java level
|
|
909 |
if (!(JDK_Version::is_gte_jdk14x_version() && UseNewReflection)) {
|
|
910 |
|
|
911 |
// Access checking (unless overridden by Method)
|
|
912 |
if (!override) {
|
|
913 |
if (!(klass->is_public() && reflected_method->is_public())) {
|
|
914 |
bool access = Reflection::reflect_check_access(klass(), reflected_method->access_flags(), target_klass(), is_method_invoke, CHECK_NULL);
|
|
915 |
if (!access) {
|
|
916 |
return NULL; // exception
|
|
917 |
}
|
|
918 |
}
|
|
919 |
}
|
|
920 |
|
|
921 |
} // !(Universe::is_gte_jdk14x_version() && UseNewReflection)
|
|
922 |
|
|
923 |
assert(ptypes->is_objArray(), "just checking");
|
|
924 |
int args_len = args.is_null() ? 0 : args->length();
|
|
925 |
// Check number of arguments
|
|
926 |
if (ptypes->length() != args_len) {
|
|
927 |
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "wrong number of arguments");
|
|
928 |
}
|
|
929 |
|
|
930 |
// Create object to contain parameters for the JavaCall
|
|
931 |
JavaCallArguments java_args(method->size_of_parameters());
|
|
932 |
|
|
933 |
if (!is_static) {
|
|
934 |
java_args.push_oop(receiver);
|
|
935 |
}
|
|
936 |
|
|
937 |
for (int i = 0; i < args_len; i++) {
|
|
938 |
oop type_mirror = ptypes->obj_at(i);
|
|
939 |
oop arg = args->obj_at(i);
|
|
940 |
if (java_lang_Class::is_primitive(type_mirror)) {
|
|
941 |
jvalue value;
|
|
942 |
BasicType ptype = basic_type_mirror_to_basic_type(type_mirror, CHECK_NULL);
|
|
943 |
BasicType atype = unbox_for_primitive(arg, &value, CHECK_NULL);
|
|
944 |
if (ptype != atype) {
|
|
945 |
widen(&value, atype, ptype, CHECK_NULL);
|
|
946 |
}
|
|
947 |
switch (ptype) {
|
|
948 |
case T_BOOLEAN: java_args.push_int(value.z); break;
|
|
949 |
case T_CHAR: java_args.push_int(value.c); break;
|
|
950 |
case T_BYTE: java_args.push_int(value.b); break;
|
|
951 |
case T_SHORT: java_args.push_int(value.s); break;
|
|
952 |
case T_INT: java_args.push_int(value.i); break;
|
|
953 |
case T_LONG: java_args.push_long(value.j); break;
|
|
954 |
case T_FLOAT: java_args.push_float(value.f); break;
|
|
955 |
case T_DOUBLE: java_args.push_double(value.d); break;
|
|
956 |
default:
|
|
957 |
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
|
|
958 |
}
|
|
959 |
} else {
|
|
960 |
if (arg != NULL) {
|
|
961 |
klassOop k = java_lang_Class::as_klassOop(type_mirror);
|
|
962 |
if (!arg->is_a(k)) {
|
|
963 |
THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
|
|
964 |
}
|
|
965 |
}
|
|
966 |
Handle arg_handle(THREAD, arg); // Create handle for argument
|
|
967 |
java_args.push_oop(arg_handle); // Push handle
|
|
968 |
}
|
|
969 |
}
|
|
970 |
|
|
971 |
assert(java_args.size_of_parameters() == method->size_of_parameters(), "just checking");
|
|
972 |
|
|
973 |
// All oops (including receiver) is passed in as Handles. An potential oop is returned as an
|
|
974 |
// oop (i.e., NOT as an handle)
|
|
975 |
JavaValue result(rtype);
|
|
976 |
JavaCalls::call(&result, method, &java_args, THREAD);
|
|
977 |
|
|
978 |
if (HAS_PENDING_EXCEPTION) {
|
|
979 |
// Method threw an exception; wrap it in an InvocationTargetException
|
|
980 |
oop target_exception = PENDING_EXCEPTION;
|
|
981 |
CLEAR_PENDING_EXCEPTION;
|
|
982 |
JavaCallArguments args(Handle(THREAD, target_exception));
|
|
983 |
THROW_ARG_0(vmSymbolHandles::java_lang_reflect_InvocationTargetException(),
|
|
984 |
vmSymbolHandles::throwable_void_signature(),
|
|
985 |
&args);
|
|
986 |
} else {
|
|
987 |
if (rtype == T_BOOLEAN || rtype == T_BYTE || rtype == T_CHAR || rtype == T_SHORT)
|
|
988 |
narrow((jvalue*) result.get_value_addr(), rtype, CHECK_NULL);
|
|
989 |
return box((jvalue*) result.get_value_addr(), rtype, CHECK_NULL);
|
|
990 |
}
|
|
991 |
}
|
|
992 |
|
|
993 |
|
|
994 |
void Reflection::narrow(jvalue* value, BasicType narrow_type, TRAPS) {
|
|
995 |
switch (narrow_type) {
|
|
996 |
case T_BOOLEAN:
|
|
997 |
value->z = (jboolean) value->i;
|
|
998 |
return;
|
|
999 |
case T_BYTE:
|
|
1000 |
value->b = (jbyte) value->i;
|
|
1001 |
return;
|
|
1002 |
case T_CHAR:
|
|
1003 |
value->c = (jchar) value->i;
|
|
1004 |
return;
|
|
1005 |
case T_SHORT:
|
|
1006 |
value->s = (jshort) value->i;
|
|
1007 |
return;
|
|
1008 |
default:
|
|
1009 |
break; // fail
|
|
1010 |
}
|
|
1011 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "argument type mismatch");
|
|
1012 |
}
|
|
1013 |
|
|
1014 |
|
|
1015 |
BasicType Reflection::basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS) {
|
|
1016 |
assert(java_lang_Class::is_primitive(basic_type_mirror), "just checking");
|
|
1017 |
return java_lang_Class::primitive_type(basic_type_mirror);
|
|
1018 |
}
|
|
1019 |
|
|
1020 |
|
|
1021 |
bool Reflection::match_parameter_types(methodHandle method, objArrayHandle types, int parameter_count, TRAPS) {
|
|
1022 |
int types_len = types.is_null() ? 0 : types->length();
|
|
1023 |
if (types_len != parameter_count) return false;
|
|
1024 |
if (parameter_count > 0) {
|
|
1025 |
objArrayHandle method_types = get_parameter_types(method, parameter_count, NULL, CHECK_false);
|
|
1026 |
for (int index = 0; index < parameter_count; index++) {
|
|
1027 |
if (types->obj_at(index) != method_types->obj_at(index)) {
|
|
1028 |
return false;
|
|
1029 |
}
|
|
1030 |
}
|
|
1031 |
}
|
|
1032 |
return true;
|
|
1033 |
}
|
|
1034 |
|
|
1035 |
|
|
1036 |
oop Reflection::new_field(FieldStream* st, TRAPS) {
|
|
1037 |
symbolHandle field_name(THREAD, st->name());
|
|
1038 |
Handle name = java_lang_String::create_from_symbol(field_name, CHECK_NULL);
|
|
1039 |
symbolHandle signature(THREAD, st->signature());
|
|
1040 |
Handle type = new_type(signature, st->klass(), CHECK_NULL);
|
|
1041 |
Handle rh = java_lang_reflect_Field::create(CHECK_NULL);
|
|
1042 |
oop result = rh();
|
|
1043 |
|
|
1044 |
java_lang_reflect_Field::set_clazz(result, st->klass()->java_mirror());
|
|
1045 |
java_lang_reflect_Field::set_slot(result, st->index());
|
|
1046 |
java_lang_reflect_Field::set_name(result, name());
|
|
1047 |
java_lang_reflect_Field::set_type(result, type());
|
|
1048 |
// Note the ACC_ANNOTATION bit, which is a per-class access flag, is never set here.
|
|
1049 |
java_lang_reflect_Field::set_modifiers(result, st->access_flags().as_int() & JVM_RECOGNIZED_FIELD_MODIFIERS);
|
|
1050 |
java_lang_reflect_Field::set_override(result, false);
|
|
1051 |
return result;
|
|
1052 |
}
|
|
1053 |
|
|
1054 |
|
|
1055 |
bool Reflection::resolve_field(Handle field_mirror, Handle& receiver, fieldDescriptor* fd, bool check_final, TRAPS) {
|
|
1056 |
if (field_mirror.is_null()) {
|
|
1057 |
THROW_(vmSymbols::java_lang_NullPointerException(), false);
|
|
1058 |
}
|
|
1059 |
|
|
1060 |
instanceKlassHandle klass (THREAD, java_lang_Class::as_klassOop(java_lang_reflect_Field::clazz(field_mirror())));
|
|
1061 |
int slot = java_lang_reflect_Field::slot(field_mirror());
|
|
1062 |
|
|
1063 |
// Ensure klass is initialized
|
|
1064 |
klass->initialize(CHECK_false);
|
|
1065 |
fd->initialize(klass(), slot);
|
|
1066 |
|
|
1067 |
bool is_static = fd->is_static();
|
|
1068 |
KlassHandle receiver_klass;
|
|
1069 |
|
|
1070 |
if (is_static) {
|
|
1071 |
receiver = KlassHandle(THREAD, klass());
|
|
1072 |
receiver_klass = klass;
|
|
1073 |
} else {
|
|
1074 |
// Check object is a non-null instance of declaring class
|
|
1075 |
if (receiver.is_null()) {
|
|
1076 |
THROW_(vmSymbols::java_lang_NullPointerException(), false);
|
|
1077 |
}
|
|
1078 |
if (!receiver->is_a(klass())) {
|
|
1079 |
THROW_MSG_(vmSymbols::java_lang_IllegalArgumentException(), "object is not an instance of declaring class", false);
|
|
1080 |
}
|
|
1081 |
receiver_klass = KlassHandle(THREAD, receiver->klass());
|
|
1082 |
}
|
|
1083 |
|
|
1084 |
// Access checking (unless overridden by Field)
|
|
1085 |
if (!java_lang_reflect_Field::override(field_mirror())) {
|
|
1086 |
if (!(klass->is_public() && fd->is_public())) {
|
|
1087 |
bool access_check = reflect_check_access(klass(), fd->access_flags(), receiver_klass(), false, CHECK_false);
|
|
1088 |
if (!access_check) {
|
|
1089 |
return false; // exception
|
|
1090 |
}
|
|
1091 |
}
|
|
1092 |
}
|
|
1093 |
|
|
1094 |
if (check_final && fd->is_final()) {
|
|
1095 |
// In 1.3 we always throw an error when attempting to set a final field.
|
|
1096 |
// In 1.2.x, this was allowed in the override bit was set by calling Field.setAccessible(true).
|
|
1097 |
// We currently maintain backwards compatibility. See bug 4250960.
|
|
1098 |
bool strict_final_check = !JDK_Version::is_jdk12x_version();
|
|
1099 |
if (strict_final_check || !java_lang_reflect_Field::override(field_mirror())) {
|
|
1100 |
THROW_MSG_(vmSymbols::java_lang_IllegalAccessException(), "field is final", false);
|
|
1101 |
}
|
|
1102 |
}
|
|
1103 |
return true;
|
|
1104 |
}
|
|
1105 |
|
|
1106 |
|
|
1107 |
BasicType Reflection::field_get(jvalue* value, fieldDescriptor* fd, Handle receiver) {
|
|
1108 |
BasicType field_type = fd->field_type();
|
|
1109 |
int offset = fd->offset();
|
|
1110 |
switch (field_type) {
|
|
1111 |
case T_BOOLEAN:
|
|
1112 |
value->z = receiver->bool_field(offset);
|
|
1113 |
break;
|
|
1114 |
case T_CHAR:
|
|
1115 |
value->c = receiver->char_field(offset);
|
|
1116 |
break;
|
|
1117 |
case T_FLOAT:
|
|
1118 |
value->f = receiver->float_field(offset);
|
|
1119 |
break;
|
|
1120 |
case T_DOUBLE:
|
|
1121 |
value->d = receiver->double_field(offset);
|
|
1122 |
break;
|
|
1123 |
case T_BYTE:
|
|
1124 |
value->b = receiver->byte_field(offset);
|
|
1125 |
break;
|
|
1126 |
case T_SHORT:
|
|
1127 |
value->s = receiver->short_field(offset);
|
|
1128 |
break;
|
|
1129 |
case T_INT:
|
|
1130 |
value->i = receiver->int_field(offset);
|
|
1131 |
break;
|
|
1132 |
case T_LONG:
|
|
1133 |
value->j = receiver->long_field(offset);
|
|
1134 |
break;
|
|
1135 |
case T_OBJECT:
|
|
1136 |
case T_ARRAY:
|
|
1137 |
value->l = (jobject) receiver->obj_field(offset);
|
|
1138 |
break;
|
|
1139 |
default:
|
|
1140 |
return T_ILLEGAL;
|
|
1141 |
}
|
|
1142 |
return field_type;
|
|
1143 |
}
|
|
1144 |
|
|
1145 |
|
|
1146 |
void Reflection::field_set(jvalue* value, fieldDescriptor* fd, Handle receiver, BasicType value_type, TRAPS) {
|
|
1147 |
BasicType field_type = fd->field_type();
|
|
1148 |
if (field_type != value_type) {
|
|
1149 |
widen(value, value_type, field_type, CHECK);
|
|
1150 |
}
|
|
1151 |
|
|
1152 |
int offset = fd->offset();
|
|
1153 |
switch (field_type) {
|
|
1154 |
case T_BOOLEAN:
|
|
1155 |
receiver->bool_field_put(offset, value->z);
|
|
1156 |
break;
|
|
1157 |
case T_CHAR:
|
|
1158 |
receiver->char_field_put(offset, value->c);
|
|
1159 |
break;
|
|
1160 |
case T_FLOAT:
|
|
1161 |
receiver->float_field_put(offset, value->f);
|
|
1162 |
break;
|
|
1163 |
case T_DOUBLE:
|
|
1164 |
receiver->double_field_put(offset, value->d);
|
|
1165 |
break;
|
|
1166 |
case T_BYTE:
|
|
1167 |
receiver->byte_field_put(offset, value->b);
|
|
1168 |
break;
|
|
1169 |
case T_SHORT:
|
|
1170 |
receiver->short_field_put(offset, value->s);
|
|
1171 |
break;
|
|
1172 |
case T_INT:
|
|
1173 |
receiver->int_field_put(offset, value->i);
|
|
1174 |
break;
|
|
1175 |
case T_LONG:
|
|
1176 |
receiver->long_field_put(offset, value->j);
|
|
1177 |
break;
|
|
1178 |
case T_OBJECT:
|
|
1179 |
case T_ARRAY: {
|
|
1180 |
Handle obj(THREAD, (oop) value->l);
|
|
1181 |
if (obj.not_null()) {
|
|
1182 |
symbolHandle signature(THREAD, fd->signature());
|
|
1183 |
Handle loader (THREAD, fd->loader());
|
|
1184 |
Handle protect (THREAD, Klass::cast(fd->field_holder())->protection_domain());
|
|
1185 |
klassOop k = SystemDictionary::resolve_or_fail(signature, loader, protect, true, CHECK); // may block
|
|
1186 |
if (!obj->is_a(k)) {
|
|
1187 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "field type mismatch");
|
|
1188 |
}
|
|
1189 |
}
|
|
1190 |
receiver->obj_field_put(offset, obj());
|
|
1191 |
break;
|
|
1192 |
}
|
|
1193 |
default:
|
|
1194 |
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "field type mismatch");
|
|
1195 |
}
|
|
1196 |
}
|
|
1197 |
|
|
1198 |
|
|
1199 |
oop Reflection::reflect_field(oop mirror, symbolOop field_name, jint which, TRAPS) {
|
|
1200 |
// Exclude primitive types and array types
|
|
1201 |
if (java_lang_Class::is_primitive(mirror)) return NULL;
|
|
1202 |
if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) return NULL;
|
|
1203 |
|
|
1204 |
instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(mirror));
|
|
1205 |
bool local_fields_only = (which == DECLARED);
|
|
1206 |
|
|
1207 |
// Ensure class is linked
|
|
1208 |
k->link_class(CHECK_NULL);
|
|
1209 |
|
|
1210 |
// Search class and interface fields
|
|
1211 |
for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) {
|
|
1212 |
if (st.name() == field_name) {
|
|
1213 |
if (local_fields_only || st.access_flags().is_public()) {
|
|
1214 |
return new_field(&st, THREAD);
|
|
1215 |
}
|
|
1216 |
}
|
|
1217 |
}
|
|
1218 |
|
|
1219 |
return NULL;
|
|
1220 |
}
|
|
1221 |
|
|
1222 |
|
|
1223 |
objArrayOop Reflection::reflect_fields(oop mirror, jint which, TRAPS) {
|
|
1224 |
// Exclude primitive types and array types
|
|
1225 |
if (java_lang_Class::is_primitive(mirror)
|
|
1226 |
|| Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) {
|
|
1227 |
symbolHandle name = vmSymbolHandles::java_lang_reflect_Field();
|
|
1228 |
klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL);
|
|
1229 |
return oopFactory::new_objArray(klass, 0, CHECK_NULL); // Return empty array
|
|
1230 |
}
|
|
1231 |
|
|
1232 |
instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(mirror));
|
|
1233 |
|
|
1234 |
// Ensure class is linked
|
|
1235 |
k->link_class(CHECK_NULL);
|
|
1236 |
|
|
1237 |
bool local_fields_only = (which == DECLARED);
|
|
1238 |
int count = 0;
|
|
1239 |
{ // Compute fields count for class and interface fields
|
|
1240 |
for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) {
|
|
1241 |
if (local_fields_only || st.access_flags().is_public()) {
|
|
1242 |
count++;
|
|
1243 |
}
|
|
1244 |
}
|
|
1245 |
}
|
|
1246 |
|
|
1247 |
// Allocate result
|
|
1248 |
symbolHandle name = vmSymbolHandles::java_lang_reflect_Field();
|
|
1249 |
klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL);
|
|
1250 |
objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL);
|
|
1251 |
objArrayHandle result (THREAD, r);
|
|
1252 |
|
|
1253 |
// Fill in results backwards
|
|
1254 |
{
|
|
1255 |
for (FieldStream st(k, local_fields_only, false); !st.eos(); st.next()) {
|
|
1256 |
if (local_fields_only || st.access_flags().is_public()) {
|
|
1257 |
oop field = new_field(&st, CHECK_NULL);
|
|
1258 |
result->obj_at_put(--count, field);
|
|
1259 |
}
|
|
1260 |
}
|
|
1261 |
assert(count == 0, "just checking");
|
|
1262 |
}
|
|
1263 |
return result();
|
|
1264 |
}
|
|
1265 |
|
|
1266 |
|
|
1267 |
oop Reflection::reflect_method(oop mirror, symbolHandle method_name, objArrayHandle types, jint which, TRAPS) {
|
|
1268 |
if (java_lang_Class::is_primitive(mirror)) return NULL;
|
|
1269 |
klassOop klass = java_lang_Class::as_klassOop(mirror);
|
|
1270 |
if (Klass::cast(klass)->oop_is_array() && which == MEMBER_DECLARED) return NULL;
|
|
1271 |
|
|
1272 |
if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) {
|
|
1273 |
klass = SystemDictionary::object_klass();
|
|
1274 |
}
|
|
1275 |
instanceKlassHandle h_k(THREAD, klass);
|
|
1276 |
|
|
1277 |
// Ensure klass is linked (need not be initialized)
|
|
1278 |
h_k->link_class(CHECK_NULL);
|
|
1279 |
|
|
1280 |
// For interfaces include static initializers under jdk1.2.x (since classic does that)
|
|
1281 |
bool include_clinit = JDK_Version::is_jdk12x_version() && h_k->is_interface();
|
|
1282 |
|
|
1283 |
switch (which) {
|
|
1284 |
case MEMBER_PUBLIC:
|
|
1285 |
// First the public non-static methods (works if method holder is an interface)
|
|
1286 |
// Note that we can ignore checks for overridden methods, since we go up the hierarchy.
|
|
1287 |
{
|
|
1288 |
for (MethodStream st(h_k, false, false); !st.eos(); st.next()) {
|
|
1289 |
methodHandle m(THREAD, st.method());
|
|
1290 |
// For interfaces include static initializers since classic does that!
|
|
1291 |
if (method_name() == m->name() && (include_clinit || (m->is_public() && !m->is_static() && !m->is_initializer()))) {
|
|
1292 |
symbolHandle signature(THREAD, m->signature());
|
|
1293 |
bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL);
|
|
1294 |
if (parameter_match) {
|
|
1295 |
return new_method(m, false, false, THREAD);
|
|
1296 |
}
|
|
1297 |
}
|
|
1298 |
}
|
|
1299 |
}
|
|
1300 |
// Then the public static methods (works if method holder is an interface)
|
|
1301 |
{
|
|
1302 |
for (MethodStream st(h_k, false, false); !st.eos(); st.next()) {
|
|
1303 |
methodHandle m(THREAD, st.method());
|
|
1304 |
if (method_name() == m->name() && m->is_public() && m->is_static() && !m->is_initializer()) {
|
|
1305 |
symbolHandle signature(THREAD, m->signature());
|
|
1306 |
bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL);
|
|
1307 |
if (parameter_match) {
|
|
1308 |
return new_method(m, false, false, THREAD);
|
|
1309 |
}
|
|
1310 |
}
|
|
1311 |
}
|
|
1312 |
}
|
|
1313 |
break;
|
|
1314 |
case MEMBER_DECLARED:
|
|
1315 |
// All local methods
|
|
1316 |
{
|
|
1317 |
for (MethodStream st(h_k, true, true); !st.eos(); st.next()) {
|
|
1318 |
methodHandle m(THREAD, st.method());
|
|
1319 |
if (method_name() == m->name() && !m->is_initializer()) {
|
|
1320 |
symbolHandle signature(THREAD, m->signature());
|
|
1321 |
bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL);
|
|
1322 |
if (parameter_match) {
|
|
1323 |
return new_method(m, false, false, THREAD);
|
|
1324 |
}
|
|
1325 |
}
|
|
1326 |
}
|
|
1327 |
}
|
|
1328 |
break;
|
|
1329 |
default:
|
|
1330 |
break;
|
|
1331 |
}
|
|
1332 |
return NULL;
|
|
1333 |
}
|
|
1334 |
|
|
1335 |
|
|
1336 |
objArrayOop Reflection::reflect_methods(oop mirror, jint which, TRAPS) {
|
|
1337 |
// Exclude primitive types
|
|
1338 |
if (java_lang_Class::is_primitive(mirror) ||
|
|
1339 |
(Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array() && (which == MEMBER_DECLARED))) {
|
|
1340 |
klassOop klass = SystemDictionary::reflect_method_klass();
|
|
1341 |
return oopFactory::new_objArray(klass, 0, CHECK_NULL); // Return empty array
|
|
1342 |
}
|
|
1343 |
|
|
1344 |
klassOop klass = java_lang_Class::as_klassOop(mirror);
|
|
1345 |
if (Klass::cast(java_lang_Class::as_klassOop(mirror))->oop_is_array()) {
|
|
1346 |
klass = SystemDictionary::object_klass();
|
|
1347 |
}
|
|
1348 |
instanceKlassHandle h_k(THREAD, klass);
|
|
1349 |
|
|
1350 |
// Ensure klass is linked (need not be initialized)
|
|
1351 |
h_k->link_class(CHECK_NULL);
|
|
1352 |
|
|
1353 |
// We search the (super)interfaces only if h_k is an interface itself
|
|
1354 |
bool is_interface = h_k->is_interface();
|
|
1355 |
|
|
1356 |
// For interfaces include static initializers under jdk1.2.x (since classic does that)
|
|
1357 |
bool include_clinit = JDK_Version::is_jdk12x_version() && is_interface;
|
|
1358 |
|
|
1359 |
switch (which) {
|
|
1360 |
case MEMBER_PUBLIC:
|
|
1361 |
{
|
|
1362 |
|
|
1363 |
// Count public methods (non-static and static)
|
|
1364 |
int count = 0;
|
|
1365 |
{
|
|
1366 |
for (MethodStream st(h_k, false, false); !st.eos(); st.next()) {
|
|
1367 |
methodOop m = st.method();
|
|
1368 |
// For interfaces include static initializers since classic does that!
|
|
1369 |
if (include_clinit || (!m->is_initializer() && m->is_public() && !m->is_overridden_in(h_k()))) {
|
|
1370 |
count++;
|
|
1371 |
}
|
|
1372 |
}
|
|
1373 |
}
|
|
1374 |
|
|
1375 |
// Allocate result
|
|
1376 |
klassOop klass = SystemDictionary::reflect_method_klass();
|
|
1377 |
objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL);
|
|
1378 |
objArrayHandle h_result (THREAD, r);
|
|
1379 |
|
|
1380 |
// Fill in results backwards
|
|
1381 |
{
|
|
1382 |
// First the non-static public methods
|
|
1383 |
for (MethodStream st(h_k, false, false); !st.eos(); st.next()) {
|
|
1384 |
methodHandle m (THREAD, st.method());
|
|
1385 |
if (!m->is_static() && !m->is_initializer() && m->is_public() && !m->is_overridden_in(h_k())) {
|
|
1386 |
oop method = new_method(m, false, false, CHECK_NULL);
|
|
1387 |
if (method == NULL) {
|
|
1388 |
return NULL;
|
|
1389 |
} else {
|
|
1390 |
h_result->obj_at_put(--count, method);
|
|
1391 |
}
|
|
1392 |
}
|
|
1393 |
}
|
|
1394 |
}
|
|
1395 |
{
|
|
1396 |
// Then the static public methods
|
|
1397 |
for (MethodStream st(h_k, false, !is_interface); !st.eos(); st.next()) {
|
|
1398 |
methodHandle m (THREAD, st.method());
|
|
1399 |
if (m->is_static() && (include_clinit || (!m->is_initializer()) && m->is_public() && !m->is_overridden_in(h_k()))) {
|
|
1400 |
oop method = new_method(m, false, false, CHECK_NULL);
|
|
1401 |
if (method == NULL) {
|
|
1402 |
return NULL;
|
|
1403 |
} else {
|
|
1404 |
h_result->obj_at_put(--count, method);
|
|
1405 |
}
|
|
1406 |
}
|
|
1407 |
}
|
|
1408 |
}
|
|
1409 |
|
|
1410 |
assert(count == 0, "just checking");
|
|
1411 |
return h_result();
|
|
1412 |
}
|
|
1413 |
|
|
1414 |
case MEMBER_DECLARED:
|
|
1415 |
{
|
|
1416 |
// Count all methods
|
|
1417 |
int count = 0;
|
|
1418 |
{
|
|
1419 |
for (MethodStream st(h_k, true, !is_interface); !st.eos(); st.next()) {
|
|
1420 |
methodOop m = st.method();
|
|
1421 |
if (!m->is_initializer()) {
|
|
1422 |
count++;
|
|
1423 |
}
|
|
1424 |
}
|
|
1425 |
}
|
|
1426 |
// Allocate result
|
|
1427 |
klassOop klass = SystemDictionary::reflect_method_klass();
|
|
1428 |
objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL);
|
|
1429 |
objArrayHandle h_result (THREAD, r);
|
|
1430 |
|
|
1431 |
// Fill in results backwards
|
|
1432 |
{
|
|
1433 |
for (MethodStream st(h_k, true, true); !st.eos(); st.next()) {
|
|
1434 |
methodHandle m (THREAD, st.method());
|
|
1435 |
if (!m->is_initializer()) {
|
|
1436 |
oop method = new_method(m, false, false, CHECK_NULL);
|
|
1437 |
if (method == NULL) {
|
|
1438 |
return NULL;
|
|
1439 |
} else {
|
|
1440 |
h_result->obj_at_put(--count, method);
|
|
1441 |
}
|
|
1442 |
}
|
|
1443 |
}
|
|
1444 |
}
|
|
1445 |
assert(count == 0, "just checking");
|
|
1446 |
return h_result();
|
|
1447 |
}
|
|
1448 |
}
|
|
1449 |
ShouldNotReachHere();
|
|
1450 |
return NULL;
|
|
1451 |
}
|
|
1452 |
|
|
1453 |
|
|
1454 |
oop Reflection::reflect_constructor(oop mirror, objArrayHandle types, jint which, TRAPS) {
|
|
1455 |
|
|
1456 |
// Exclude primitive, interface and array types
|
|
1457 |
bool prim = java_lang_Class::is_primitive(mirror);
|
|
1458 |
Klass* klass = prim ? NULL : Klass::cast(java_lang_Class::as_klassOop(mirror));
|
|
1459 |
if (prim || klass->is_interface() || klass->oop_is_array()) return NULL;
|
|
1460 |
|
|
1461 |
// Must be instance klass
|
|
1462 |
instanceKlassHandle h_k(THREAD, java_lang_Class::as_klassOop(mirror));
|
|
1463 |
|
|
1464 |
// Ensure klass is linked (need not be initialized)
|
|
1465 |
h_k->link_class(CHECK_NULL);
|
|
1466 |
|
|
1467 |
bool local_only = (which == MEMBER_DECLARED);
|
|
1468 |
for (MethodStream st(h_k, true, true); !st.eos(); st.next()) {
|
|
1469 |
methodHandle m(THREAD, st.method());
|
|
1470 |
if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) {
|
|
1471 |
symbolHandle signature(THREAD, m->signature());
|
|
1472 |
bool parameter_match = match_parameter_types(m, types, ArgumentCount(signature).size(), CHECK_NULL);
|
|
1473 |
if (parameter_match) {
|
|
1474 |
return new_constructor(m, THREAD);
|
|
1475 |
}
|
|
1476 |
}
|
|
1477 |
}
|
|
1478 |
|
|
1479 |
return NULL;
|
|
1480 |
}
|
|
1481 |
|
|
1482 |
|
|
1483 |
objArrayOop Reflection::reflect_constructors(oop mirror, jint which, TRAPS) {
|
|
1484 |
// Exclude primitive, interface and array types
|
|
1485 |
bool prim = java_lang_Class::is_primitive(mirror);
|
|
1486 |
Klass* k = prim ? NULL : Klass::cast(java_lang_Class::as_klassOop(mirror));
|
|
1487 |
if (prim || k->is_interface() || k->oop_is_array()) {
|
|
1488 |
return oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), 0, CHECK_NULL); // Return empty array
|
|
1489 |
}
|
|
1490 |
|
|
1491 |
// Must be instanceKlass at this point
|
|
1492 |
instanceKlassHandle h_k(THREAD, java_lang_Class::as_klassOop(mirror));
|
|
1493 |
|
|
1494 |
// Ensure klass is linked (need not be initialized)
|
|
1495 |
h_k->link_class(CHECK_NULL);
|
|
1496 |
|
|
1497 |
bool local_only = (which == MEMBER_DECLARED);
|
|
1498 |
int count = 0;
|
|
1499 |
{
|
|
1500 |
for (MethodStream st(h_k, true, true); !st.eos(); st.next()) {
|
|
1501 |
methodOop m = st.method();
|
|
1502 |
if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) {
|
|
1503 |
count++;
|
|
1504 |
}
|
|
1505 |
}
|
|
1506 |
}
|
|
1507 |
|
|
1508 |
// Allocate result
|
|
1509 |
symbolHandle name = vmSymbolHandles::java_lang_reflect_Constructor();
|
|
1510 |
klassOop klass = SystemDictionary::resolve_or_fail(name, true, CHECK_NULL);
|
|
1511 |
objArrayOop r = oopFactory::new_objArray(klass, count, CHECK_NULL);
|
|
1512 |
objArrayHandle h_result (THREAD, r);
|
|
1513 |
|
|
1514 |
// Fill in results backwards
|
|
1515 |
{
|
|
1516 |
for (MethodStream st(h_k, true, true); !st.eos(); st.next()) {
|
|
1517 |
methodHandle m (THREAD, st.method());
|
|
1518 |
if (m->name() == vmSymbols::object_initializer_name() && (local_only || m->is_public())) {
|
|
1519 |
oop constr = new_constructor(m, CHECK_NULL);
|
|
1520 |
if (constr == NULL) {
|
|
1521 |
return NULL;
|
|
1522 |
} else {
|
|
1523 |
h_result->obj_at_put(--count, constr);
|
|
1524 |
}
|
|
1525 |
}
|
|
1526 |
}
|
|
1527 |
assert(count == 0, "just checking");
|
|
1528 |
}
|
|
1529 |
return h_result();
|
|
1530 |
}
|
|
1531 |
|
|
1532 |
|
|
1533 |
// This would be nicer if, say, java.lang.reflect.Method was a subclass
|
|
1534 |
// of java.lang.reflect.Constructor
|
|
1535 |
|
|
1536 |
oop Reflection::invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS) {
|
|
1537 |
oop mirror = java_lang_reflect_Method::clazz(method_mirror);
|
|
1538 |
int slot = java_lang_reflect_Method::slot(method_mirror);
|
|
1539 |
bool override = java_lang_reflect_Method::override(method_mirror) != 0;
|
|
1540 |
objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Method::parameter_types(method_mirror)));
|
|
1541 |
|
|
1542 |
oop return_type_mirror = java_lang_reflect_Method::return_type(method_mirror);
|
|
1543 |
BasicType rtype;
|
|
1544 |
if (java_lang_Class::is_primitive(return_type_mirror)) {
|
|
1545 |
rtype = basic_type_mirror_to_basic_type(return_type_mirror, CHECK_NULL);
|
|
1546 |
} else {
|
|
1547 |
rtype = T_OBJECT;
|
|
1548 |
}
|
|
1549 |
|
|
1550 |
instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(mirror));
|
|
1551 |
if (!klass->methods()->is_within_bounds(slot)) {
|
|
1552 |
THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
|
|
1553 |
}
|
|
1554 |
methodHandle method(THREAD, methodOop(klass->methods()->obj_at(slot)));
|
|
1555 |
|
|
1556 |
return invoke(klass, method, receiver, override, ptypes, rtype, args, true, THREAD);
|
|
1557 |
}
|
|
1558 |
|
|
1559 |
|
|
1560 |
oop Reflection::invoke_constructor(oop constructor_mirror, objArrayHandle args, TRAPS) {
|
|
1561 |
oop mirror = java_lang_reflect_Constructor::clazz(constructor_mirror);
|
|
1562 |
int slot = java_lang_reflect_Constructor::slot(constructor_mirror);
|
|
1563 |
bool override = java_lang_reflect_Constructor::override(constructor_mirror) != 0;
|
|
1564 |
objArrayHandle ptypes(THREAD, objArrayOop(java_lang_reflect_Constructor::parameter_types(constructor_mirror)));
|
|
1565 |
|
|
1566 |
instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(mirror));
|
|
1567 |
if (!klass->methods()->is_within_bounds(slot)) {
|
|
1568 |
THROW_MSG_0(vmSymbols::java_lang_InternalError(), "invoke");
|
|
1569 |
}
|
|
1570 |
methodHandle method(THREAD, methodOop(klass->methods()->obj_at(slot)));
|
|
1571 |
assert(method->name() == vmSymbols::object_initializer_name(), "invalid constructor");
|
|
1572 |
|
|
1573 |
// Make sure klass gets initialize
|
|
1574 |
klass->initialize(CHECK_NULL);
|
|
1575 |
|
|
1576 |
// Create new instance (the receiver)
|
|
1577 |
klass->check_valid_for_instantiation(false, CHECK_NULL);
|
|
1578 |
Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
|
|
1579 |
|
|
1580 |
// Ignore result from call and return receiver
|
|
1581 |
invoke(klass, method, receiver, override, ptypes, T_VOID, args, false, CHECK_NULL);
|
|
1582 |
return receiver();
|
|
1583 |
}
|
|
1584 |
|
|
1585 |
|
|
1586 |
#endif /* SUPPORT_OLD_REFLECTION */
|