1
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 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 |
// Class Reflection contains utility methods needed for implementing the
|
|
26 |
// reflection api.
|
|
27 |
//
|
|
28 |
// Used by functions in the JVM interface.
|
|
29 |
//
|
|
30 |
// NOTE that in JDK 1.4 most of reflection is now implemented in Java
|
|
31 |
// using dynamic bytecode generation. The Array class has not yet been
|
|
32 |
// rewritten using bytecodes; if it were, most of the rest of this
|
|
33 |
// class could go away, as well as a few more entry points in jvm.cpp.
|
|
34 |
|
|
35 |
class FieldStream;
|
|
36 |
|
|
37 |
class Reflection: public AllStatic {
|
|
38 |
private:
|
|
39 |
// Access checking
|
|
40 |
static bool reflect_check_access(klassOop field_class, AccessFlags acc, klassOop target_class, bool is_method_invoke, TRAPS);
|
|
41 |
|
|
42 |
// Conversion
|
|
43 |
static klassOop basic_type_mirror_to_arrayklass(oop basic_type_mirror, TRAPS);
|
|
44 |
static oop basic_type_arrayklass_to_mirror(klassOop basic_type_arrayklass, TRAPS);
|
|
45 |
|
|
46 |
static objArrayHandle get_parameter_types(methodHandle method, int parameter_count, oop* return_type, TRAPS);
|
|
47 |
static objArrayHandle get_exception_types(methodHandle method, TRAPS);
|
|
48 |
// Creating new java.lang.reflect.xxx wrappers
|
|
49 |
static Handle new_type(symbolHandle signature, KlassHandle k, TRAPS);
|
|
50 |
|
|
51 |
public:
|
|
52 |
// Constants defined by java reflection api classes
|
|
53 |
enum SomeConstants {
|
|
54 |
PUBLIC = 0,
|
|
55 |
DECLARED = 1,
|
|
56 |
MEMBER_PUBLIC = 0,
|
|
57 |
MEMBER_DECLARED = 1,
|
|
58 |
MAX_DIM = 255
|
|
59 |
};
|
|
60 |
|
|
61 |
// Boxing. Returns boxed value of appropriate type. Throws IllegalArgumentException.
|
|
62 |
static oop box(jvalue* v, BasicType type, TRAPS);
|
|
63 |
// Unboxing. Returns type code and sets value.
|
|
64 |
static BasicType unbox_for_primitive(oop boxed_value, jvalue* value, TRAPS);
|
|
65 |
static BasicType unbox_for_regular_object(oop boxed_value, jvalue* value);
|
|
66 |
|
|
67 |
// Widening of basic types. Throws IllegalArgumentException.
|
|
68 |
static void widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS);
|
|
69 |
|
|
70 |
// Reflective array access. Returns type code. Throws ArrayIndexOutOfBoundsException.
|
|
71 |
static BasicType array_get(jvalue* value, arrayOop a, int index, TRAPS);
|
|
72 |
static void array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS);
|
|
73 |
// Returns mirror on array element type (NULL for basic type arrays and non-arrays).
|
|
74 |
static oop array_component_type(oop mirror, TRAPS);
|
|
75 |
|
|
76 |
// Object creation
|
|
77 |
static arrayOop reflect_new_array(oop element_mirror, jint length, TRAPS);
|
|
78 |
static arrayOop reflect_new_multi_array(oop element_mirror, typeArrayOop dimensions, TRAPS);
|
|
79 |
|
|
80 |
// Verification
|
|
81 |
static bool verify_class_access(klassOop current_class, klassOop new_class, bool classloader_only);
|
|
82 |
|
|
83 |
static bool verify_field_access(klassOop current_class,
|
|
84 |
klassOop resolved_class,
|
|
85 |
klassOop field_class,
|
|
86 |
AccessFlags access,
|
|
87 |
bool classloader_only,
|
|
88 |
bool protected_restriction = false);
|
|
89 |
static bool is_same_class_package(klassOop class1, klassOop class2);
|
|
90 |
|
|
91 |
static bool can_relax_access_check_for(
|
|
92 |
klassOop accessor, klassOop accesee, bool classloader_only);
|
|
93 |
|
|
94 |
// inner class reflection
|
|
95 |
static void check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner, TRAPS);
|
|
96 |
|
|
97 |
//
|
|
98 |
// Support for reflection based on dynamic bytecode generation (JDK 1.4)
|
|
99 |
//
|
|
100 |
|
|
101 |
// Create a java.lang.reflect.Method object based on a method
|
|
102 |
static oop new_method(methodHandle method, bool intern_name, bool for_constant_pool_access, TRAPS);
|
|
103 |
// Create a java.lang.reflect.Constructor object based on a method
|
|
104 |
static oop new_constructor(methodHandle method, TRAPS);
|
|
105 |
// Create a java.lang.reflect.Field object based on a field descriptor
|
|
106 |
static oop new_field(fieldDescriptor* fd, bool intern_name, TRAPS);
|
|
107 |
|
|
108 |
//---------------------------------------------------------------------------
|
|
109 |
//
|
|
110 |
// Support for old native code-based reflection (pre-JDK 1.4)
|
|
111 |
//
|
|
112 |
// NOTE: the method and constructor invocation code is still used
|
|
113 |
// for startup time reasons; see reflectionCompat.hpp.
|
|
114 |
//
|
|
115 |
//---------------------------------------------------------------------------
|
|
116 |
|
|
117 |
#ifdef SUPPORT_OLD_REFLECTION
|
|
118 |
private:
|
|
119 |
// method resolution for invoke
|
|
120 |
static methodHandle resolve_interface_call(instanceKlassHandle klass, methodHandle method, KlassHandle recv_klass, Handle receiver, TRAPS);
|
|
121 |
// Method call (shared by invoke_method and invoke_constructor)
|
|
122 |
static oop invoke(instanceKlassHandle klass, methodHandle method, Handle receiver, bool override, objArrayHandle ptypes, BasicType rtype, objArrayHandle args, bool is_method_invoke, TRAPS);
|
|
123 |
|
|
124 |
// Narrowing of basic types. Used to create correct jvalues for
|
|
125 |
// boolean, byte, char and short return return values from interpreter
|
|
126 |
// which are returned as ints. Throws IllegalArgumentException.
|
|
127 |
static void narrow(jvalue* value, BasicType narrow_type, TRAPS);
|
|
128 |
|
|
129 |
// Conversion
|
|
130 |
static BasicType basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS);
|
|
131 |
|
|
132 |
static bool match_parameter_types(methodHandle method, objArrayHandle types, int parameter_count, TRAPS);
|
|
133 |
// Creating new java.lang.reflect.xxx wrappers
|
|
134 |
static oop new_field(FieldStream* st, TRAPS);
|
|
135 |
|
|
136 |
public:
|
|
137 |
// Field lookup and verification.
|
|
138 |
static bool resolve_field(Handle field_mirror, Handle& receiver, fieldDescriptor* fd, bool check_final, TRAPS);
|
|
139 |
|
|
140 |
// Reflective field access. Returns type code. Throws IllegalArgumentException.
|
|
141 |
static BasicType field_get(jvalue* value, fieldDescriptor* fd, Handle receiver);
|
|
142 |
static void field_set(jvalue* value, fieldDescriptor* fd, Handle receiver, BasicType value_type, TRAPS);
|
|
143 |
|
|
144 |
// Reflective lookup of fields. Returns java.lang.reflect.Field instances.
|
|
145 |
static oop reflect_field(oop mirror, symbolOop field_name, jint which, TRAPS);
|
|
146 |
static objArrayOop reflect_fields(oop mirror, jint which, TRAPS);
|
|
147 |
|
|
148 |
// Reflective lookup of methods. Returns java.lang.reflect.Method instances.
|
|
149 |
static oop reflect_method(oop mirror, symbolHandle method_name, objArrayHandle types, jint which, TRAPS);
|
|
150 |
static objArrayOop reflect_methods(oop mirror, jint which, TRAPS);
|
|
151 |
|
|
152 |
// Reflective lookup of constructors. Returns java.lang.reflect.Constructor instances.
|
|
153 |
static oop reflect_constructor(oop mirror, objArrayHandle types, jint which, TRAPS);
|
|
154 |
static objArrayOop reflect_constructors(oop mirror, jint which, TRAPS);
|
|
155 |
|
|
156 |
// Method invokation through java.lang.reflect.Method
|
|
157 |
static oop invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS);
|
|
158 |
// Method invokation through java.lang.reflect.Constructor
|
|
159 |
static oop invoke_constructor(oop method_mirror, objArrayHandle args, TRAPS);
|
|
160 |
#endif /* SUPPORT_OLD_REFLECTION */
|
|
161 |
|
|
162 |
};
|