aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #ifndef SHARE_VM_RUNTIME_REFLECTION_HPP aoqi@0: #define SHARE_VM_RUNTIME_REFLECTION_HPP aoqi@0: aoqi@0: #include "oops/oop.hpp" aoqi@0: #include "runtime/fieldDescriptor.hpp" aoqi@0: #include "utilities/accessFlags.hpp" aoqi@0: #include "utilities/growableArray.hpp" aoqi@0: aoqi@0: // Class Reflection contains utility methods needed for implementing the aoqi@0: // reflection api. aoqi@0: // aoqi@0: // Used by functions in the JVM interface. aoqi@0: // aoqi@0: // NOTE that in JDK 1.4 most of reflection is now implemented in Java aoqi@0: // using dynamic bytecode generation. The Array class has not yet been aoqi@0: // rewritten using bytecodes; if it were, most of the rest of this aoqi@0: // class could go away, as well as a few more entry points in jvm.cpp. aoqi@0: aoqi@0: class FieldStream; aoqi@0: aoqi@0: class Reflection: public AllStatic { aoqi@0: private: aoqi@0: // Access checking aoqi@0: static bool reflect_check_access(Klass* field_class, AccessFlags acc, Klass* target_class, bool is_method_invoke, TRAPS); aoqi@0: aoqi@0: // Conversion aoqi@0: static Klass* basic_type_mirror_to_arrayklass(oop basic_type_mirror, TRAPS); aoqi@0: static oop basic_type_arrayklass_to_mirror(Klass* basic_type_arrayklass, TRAPS); aoqi@0: aoqi@0: static objArrayHandle get_parameter_types(methodHandle method, int parameter_count, oop* return_type, TRAPS); aoqi@0: static objArrayHandle get_exception_types(methodHandle method, TRAPS); aoqi@0: // Creating new java.lang.reflect.xxx wrappers aoqi@0: static Handle new_type(Symbol* signature, KlassHandle k, TRAPS); aoqi@0: aoqi@0: public: aoqi@0: // Constants defined by java reflection api classes aoqi@0: enum SomeConstants { aoqi@0: PUBLIC = 0, aoqi@0: DECLARED = 1, aoqi@0: MEMBER_PUBLIC = 0, aoqi@0: MEMBER_DECLARED = 1, aoqi@0: MAX_DIM = 255 aoqi@0: }; aoqi@0: aoqi@0: // Boxing. Returns boxed value of appropriate type. Throws IllegalArgumentException. aoqi@0: static oop box(jvalue* v, BasicType type, TRAPS); aoqi@0: // Unboxing. Returns type code and sets value. aoqi@0: static BasicType unbox_for_primitive(oop boxed_value, jvalue* value, TRAPS); aoqi@0: static BasicType unbox_for_regular_object(oop boxed_value, jvalue* value); aoqi@0: aoqi@0: // Widening of basic types. Throws IllegalArgumentException. aoqi@0: static void widen(jvalue* value, BasicType current_type, BasicType wide_type, TRAPS); aoqi@0: aoqi@0: // Reflective array access. Returns type code. Throws ArrayIndexOutOfBoundsException. aoqi@0: static BasicType array_get(jvalue* value, arrayOop a, int index, TRAPS); aoqi@0: static void array_set(jvalue* value, arrayOop a, int index, BasicType value_type, TRAPS); aoqi@0: // Returns mirror on array element type (NULL for basic type arrays and non-arrays). aoqi@0: static oop array_component_type(oop mirror, TRAPS); aoqi@0: aoqi@0: // Object creation aoqi@0: static arrayOop reflect_new_array(oop element_mirror, jint length, TRAPS); aoqi@0: static arrayOop reflect_new_multi_array(oop element_mirror, typeArrayOop dimensions, TRAPS); aoqi@0: aoqi@0: // Verification aoqi@0: static bool verify_class_access(Klass* current_class, Klass* new_class, bool classloader_only); aoqi@0: aoqi@0: static bool verify_field_access(Klass* current_class, aoqi@0: Klass* resolved_class, aoqi@0: Klass* field_class, aoqi@0: AccessFlags access, aoqi@0: bool classloader_only, aoqi@0: bool protected_restriction = false); aoqi@0: static bool is_same_class_package(Klass* class1, Klass* class2); aoqi@0: static bool is_same_package_member(Klass* class1, Klass* class2, TRAPS); aoqi@0: aoqi@0: static bool can_relax_access_check_for( aoqi@0: Klass* accessor, Klass* accesee, bool classloader_only); aoqi@0: aoqi@0: // inner class reflection aoqi@0: // raise an ICCE unless the required relationship can be proven to hold aoqi@0: // If inner_is_member, require the inner to be a member of the outer. aoqi@0: // If !inner_is_member, require the inner to be anonymous (a non-member). aoqi@0: // Caller is responsible for figuring out in advance which case must be true. aoqi@0: static void check_for_inner_class(instanceKlassHandle outer, instanceKlassHandle inner, aoqi@0: bool inner_is_member, TRAPS); aoqi@0: aoqi@0: // aoqi@0: // Support for reflection based on dynamic bytecode generation (JDK 1.4) aoqi@0: // aoqi@0: aoqi@0: // Create a java.lang.reflect.Method object based on a method aoqi@0: static oop new_method(methodHandle method, bool intern_name, bool for_constant_pool_access, TRAPS); aoqi@0: // Create a java.lang.reflect.Constructor object based on a method aoqi@0: static oop new_constructor(methodHandle method, TRAPS); aoqi@0: // Create a java.lang.reflect.Field object based on a field descriptor aoqi@0: static oop new_field(fieldDescriptor* fd, bool intern_name, TRAPS); aoqi@0: // Create a java.lang.reflect.Parameter object based on a aoqi@0: // MethodParameterElement aoqi@0: static oop new_parameter(Handle method, int index, Symbol* sym, aoqi@0: int flags, TRAPS); aoqi@0: aoqi@0: private: aoqi@0: // method resolution for invoke aoqi@0: static methodHandle resolve_interface_call(instanceKlassHandle klass, methodHandle method, KlassHandle recv_klass, Handle receiver, TRAPS); aoqi@0: // Method call (shared by invoke_method and invoke_constructor) aoqi@0: static oop invoke(instanceKlassHandle klass, methodHandle method, Handle receiver, bool override, objArrayHandle ptypes, BasicType rtype, objArrayHandle args, bool is_method_invoke, TRAPS); aoqi@0: aoqi@0: // Narrowing of basic types. Used to create correct jvalues for aoqi@0: // boolean, byte, char and short return return values from interpreter aoqi@0: // which are returned as ints. Throws IllegalArgumentException. aoqi@0: static void narrow(jvalue* value, BasicType narrow_type, TRAPS); aoqi@0: aoqi@0: // Conversion aoqi@0: static BasicType basic_type_mirror_to_basic_type(oop basic_type_mirror, TRAPS); aoqi@0: aoqi@0: public: aoqi@0: // Method invokation through java.lang.reflect.Method aoqi@0: static oop invoke_method(oop method_mirror, Handle receiver, objArrayHandle args, TRAPS); aoqi@0: // Method invokation through java.lang.reflect.Constructor aoqi@0: static oop invoke_constructor(oop method_mirror, objArrayHandle args, TRAPS); aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: #endif // SHARE_VM_RUNTIME_REFLECTION_HPP