src/share/vm/runtime/reflection.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_RUNTIME_REFLECTION_HPP
aoqi@0 26 #define SHARE_VM_RUNTIME_REFLECTION_HPP
aoqi@0 27
aoqi@0 28 #include "oops/oop.hpp"
aoqi@0 29 #include "runtime/fieldDescriptor.hpp"
aoqi@0 30 #include "utilities/accessFlags.hpp"
aoqi@0 31 #include "utilities/growableArray.hpp"
aoqi@0 32
aoqi@0 33 // Class Reflection contains utility methods needed for implementing the
aoqi@0 34 // reflection api.
aoqi@0 35 //
aoqi@0 36 // Used by functions in the JVM interface.
aoqi@0 37 //
aoqi@0 38 // NOTE that in JDK 1.4 most of reflection is now implemented in Java
aoqi@0 39 // using dynamic bytecode generation. The Array class has not yet been
aoqi@0 40 // rewritten using bytecodes; if it were, most of the rest of this
aoqi@0 41 // class could go away, as well as a few more entry points in jvm.cpp.
aoqi@0 42
aoqi@0 43 class FieldStream;
aoqi@0 44
aoqi@0 45 class Reflection: public AllStatic {
aoqi@0 46 private:
aoqi@0 47 // Access checking
aoqi@0 48 static bool reflect_check_access(Klass* field_class, AccessFlags acc, Klass* target_class, bool is_method_invoke, TRAPS);
aoqi@0 49
aoqi@0 50 // Conversion
aoqi@0 51 static Klass* basic_type_mirror_to_arrayklass(oop basic_type_mirror, TRAPS);
aoqi@0 52 static oop basic_type_arrayklass_to_mirror(Klass* basic_type_arrayklass, TRAPS);
aoqi@0 53
aoqi@0 54 static objArrayHandle get_parameter_types(methodHandle method, int parameter_count, oop* return_type, TRAPS);
aoqi@0 55 static objArrayHandle get_exception_types(methodHandle method, TRAPS);
aoqi@0 56 // Creating new java.lang.reflect.xxx wrappers
aoqi@0 57 static Handle new_type(Symbol* signature, KlassHandle k, TRAPS);
aoqi@0 58
aoqi@0 59 public:
aoqi@0 60 // Constants defined by java reflection api classes
aoqi@0 61 enum SomeConstants {
aoqi@0 62 PUBLIC = 0,
aoqi@0 63 DECLARED = 1,
aoqi@0 64 MEMBER_PUBLIC = 0,
aoqi@0 65 MEMBER_DECLARED = 1,
aoqi@0 66 MAX_DIM = 255
aoqi@0 67 };
aoqi@0 68
aoqi@0 69 // Boxing. Returns boxed value of appropriate type. Throws IllegalArgumentException.
aoqi@0 70 static oop box(jvalue* v, BasicType type, TRAPS);
aoqi@0 71 // Unboxing. Returns type code and sets value.
aoqi@0 72 static BasicType unbox_for_primitive(oop boxed_value, jvalue* value, TRAPS);
aoqi@0 73 static BasicType unbox_for_regular_object(oop boxed_value, jvalue* value);
aoqi@0 74
aoqi@0 75 // Widening of basic types. Throws IllegalArgumentException.
aoqi@0 76 static void widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS);
aoqi@0 77
aoqi@0 78 // Reflective array access. Returns type code. Throws ArrayIndexOutOfBoundsException.
aoqi@0 79 static BasicType array_get(jvalue* value, arrayOop a, int index, TRAPS);
aoqi@0 80 static void array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS);
aoqi@0 81 // Returns mirror on array element type (NULL for basic type arrays and non-arrays).
aoqi@0 82 static oop array_component_type(oop mirror, TRAPS);
aoqi@0 83
aoqi@0 84 // Object creation
aoqi@0 85 static arrayOop reflect_new_array(oop element_mirror, jint length, TRAPS);
aoqi@0 86 static arrayOop reflect_new_multi_array(oop element_mirror, typeArrayOop dimensions, TRAPS);
aoqi@0 87
aoqi@0 88 // Verification
aoqi@0 89 static bool verify_class_access(Klass* current_class, Klass* new_class, bool classloader_only);
aoqi@0 90
aoqi@0 91 static bool verify_field_access(Klass* current_class,
aoqi@0 92 Klass* resolved_class,
aoqi@0 93 Klass* field_class,
aoqi@0 94 AccessFlags access,
aoqi@0 95 bool classloader_only,
aoqi@0 96 bool protected_restriction = false);
aoqi@0 97 static bool is_same_class_package(Klass* class1, Klass* class2);
aoqi@0 98 static bool is_same_package_member(Klass* class1, Klass* class2, TRAPS);
aoqi@0 99
aoqi@0 100 static bool can_relax_access_check_for(
aoqi@0 101 Klass* accessor, Klass* accesee, bool classloader_only);
aoqi@0 102
aoqi@0 103 // inner class reflection
aoqi@0 104 // raise an ICCE unless the required relationship can be proven to hold
aoqi@0 105 // If inner_is_member, require the inner to be a member of the outer.
aoqi@0 106 // If !inner_is_member, require the inner to be anonymous (a non-member).
aoqi@0 107 // Caller is responsible for figuring out in advance which case must be true.
aoqi@0 108 static void check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner,
aoqi@0 109 bool inner_is_member, TRAPS);
aoqi@0 110
aoqi@0 111 //
aoqi@0 112 // Support for reflection based on dynamic bytecode generation (JDK 1.4)
aoqi@0 113 //
aoqi@0 114
aoqi@0 115 // Create a java.lang.reflect.Method object based on a method
aoqi@0 116 static oop new_method(methodHandle method, bool intern_name, bool for_constant_pool_access, TRAPS);
aoqi@0 117 // Create a java.lang.reflect.Constructor object based on a method
aoqi@0 118 static oop new_constructor(methodHandle method, TRAPS);
aoqi@0 119 // Create a java.lang.reflect.Field object based on a field descriptor
aoqi@0 120 static oop new_field(fieldDescriptor* fd, bool intern_name, TRAPS);
aoqi@0 121 // Create a java.lang.reflect.Parameter object based on a
aoqi@0 122 // MethodParameterElement
aoqi@0 123 static oop new_parameter(Handle method, int index, Symbol* sym,
aoqi@0 124 int flags, TRAPS);
aoqi@0 125
aoqi@0 126 private:
aoqi@0 127 // method resolution for invoke
aoqi@0 128 static methodHandle resolve_interface_call(instanceKlassHandle klass, methodHandle method, KlassHandle recv_klass, Handle receiver, TRAPS);
aoqi@0 129 // Method call (shared by invoke_method and invoke_constructor)
aoqi@0 130 static oop invoke(instanceKlassHandle klass, methodHandle method, Handle receiver, bool override, objArrayHandle ptypes, BasicType rtype, objArrayHandle args, bool is_method_invoke, TRAPS);
aoqi@0 131
aoqi@0 132 // Narrowing of basic types. Used to create correct jvalues for
aoqi@0 133 // boolean, byte, char and short return return values from interpreter
aoqi@0 134 // which are returned as ints. Throws IllegalArgumentException.
aoqi@0 135 static void narrow(jvalue* value, BasicType narrow_type, TRAPS);
aoqi@0 136
aoqi@0 137 // Conversion
aoqi@0 138 static BasicType basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS);
aoqi@0 139
aoqi@0 140 public:
aoqi@0 141 // Method invokation through java.lang.reflect.Method
aoqi@0 142 static oop invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS);
aoqi@0 143 // Method invokation through java.lang.reflect.Constructor
aoqi@0 144 static oop invoke_constructor(oop method_mirror, objArrayHandle args, TRAPS);
aoqi@0 145
aoqi@0 146 };
aoqi@0 147
aoqi@0 148 #endif // SHARE_VM_RUNTIME_REFLECTION_HPP

mercurial