1 /* 2 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_RUNTIME_REFLECTION_HPP 26 #define SHARE_RUNTIME_REFLECTION_HPP 27 28 #include "oops/oop.hpp" 29 #include "runtime/fieldDescriptor.hpp" 30 #include "utilities/accessFlags.hpp" 31 #include "utilities/growableArray.hpp" 32 33 // Class Reflection contains utility methods needed for implementing the 34 // reflection api. 35 // 36 // Used by functions in the JVM interface. 37 // 38 // NOTE that in JDK 1.4 most of reflection is now implemented in Java 39 // using dynamic bytecode generation. The Array class has not yet been 40 // rewritten using bytecodes; if it were, most of the rest of this 41 // class could go away, as well as a few more entry points in jvm.cpp. 42 43 class Reflection: public AllStatic { 44 public: 45 // Constants defined by java reflection api classes 46 enum SomeConstants { 47 PUBLIC = 0, 48 DECLARED = 1, 49 MEMBER_PUBLIC = 0, 50 MEMBER_DECLARED = 1, 51 MAX_DIM = 255 52 }; 53 54 // Results returned by verify_class_access() 55 enum VerifyClassAccessResults { 56 ACCESS_OK = 0, 57 MODULE_NOT_READABLE = 1, 58 TYPE_NOT_EXPORTED = 2, 59 OTHER_PROBLEM = 3 60 }; 61 62 // Boxing. Returns boxed value of appropriate type. Throws IllegalArgumentException. 63 static oop box(jvalue* v, BasicType type, TRAPS); 64 // Unboxing. Returns type code and sets value. 65 static BasicType unbox_for_primitive(oop boxed_value, jvalue* value, TRAPS); 66 static BasicType unbox_for_regular_object(oop boxed_value, jvalue* value); 67 68 // Widening of basic types. Throws IllegalArgumentException. 69 static void widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS); 70 71 // Reflective array access. Returns type code. Throws ArrayIndexOutOfBoundsException. 72 static BasicType array_get(jvalue* value, arrayOop a, int index, TRAPS); 73 static void array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS); 74 75 // Object creation 76 static arrayOop reflect_new_array(oop element_mirror, jint length, TRAPS); 77 static arrayOop reflect_new_multi_array(oop element_mirror, typeArrayOop dimensions, TRAPS); 78 79 // Verification 80 static VerifyClassAccessResults verify_class_access(const Klass* current_class, 81 const InstanceKlass* new_class, 82 bool classloader_only); 83 // Return an error message specific to the specified Klass*'s and result. 84 // This function must be called from within a block containing a ResourceMark. 85 static char* verify_class_access_msg(const Klass* current_class, 86 const InstanceKlass* new_class, 87 const VerifyClassAccessResults result); 88 89 static bool verify_member_access(const Klass* current_class, 90 const Klass* resolved_class, 91 const Klass* member_class, 92 AccessFlags access, 93 bool classloader_only, 94 bool protected_restriction, 95 TRAPS); 96 static bool is_same_class_package(const Klass* class1, const Klass* class2); 97 98 // inner class reflection 99 // raise an ICCE unless the required relationship can be proven to hold 100 // If inner_is_member, require the inner to be a member of the outer. 101 // If !inner_is_member, require the inner to be anonymous (a non-member). 102 // Caller is responsible for figuring out in advance which case must be true. 103 static void check_for_inner_class(const InstanceKlass* outer, 104 const InstanceKlass* inner, 105 bool inner_is_member, 106 TRAPS); 107 108 // 109 // Support for reflection based on dynamic bytecode generation (JDK 1.4) 110 // 111 112 // Create a java.lang.reflect.Method object based on a method 113 static oop new_method(const methodHandle& method, bool for_constant_pool_access, TRAPS); 114 // Create a java.lang.reflect.Constructor object based on a method 115 static oop new_constructor(const methodHandle& method, TRAPS); 116 // Create a java.lang.reflect.Field object based on a field descriptor 117 static oop new_field(fieldDescriptor* fd, TRAPS); 118 // Create a java.lang.reflect.Parameter object based on a 119 // MethodParameterElement 120 static oop new_parameter(Handle method, int index, Symbol* sym, 121 int flags, TRAPS); 122 // Method invocation through java.lang.reflect.Method 123 static oop invoke_method(oop method_mirror, 124 Handle receiver, 125 objArrayHandle args, 126 TRAPS); 127 // Method invocation through java.lang.reflect.Constructor 128 static oop invoke_constructor(oop method_mirror, objArrayHandle args, TRAPS); 129 130 }; 131 132 #endif // SHARE_RUNTIME_REFLECTION_HPP