1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_ 18 #define ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_ 19 20 #include "art_field.h" 21 #include "art_method.h" 22 #include "base/macros.h" 23 #include "class.h" 24 #include "method_type.h" 25 #include "obj_ptr.h" 26 #include "object.h" 27 28 namespace art HIDDEN { 29 30 struct MethodHandleOffsets; 31 struct MethodHandleImplOffsets; 32 class ReflectiveValueVisitor; 33 34 namespace mirror { 35 36 // C++ mirror of java.lang.invoke.MethodHandle 37 class MANAGED MethodHandle : public Object { 38 public: 39 MIRROR_CLASS("Ljava/lang/invoke/MethodHandle;"); 40 41 // Defines the behaviour of a given method handle. The behaviour 42 // of a handle of a given kind is identical to the dex bytecode behaviour 43 // of the equivalent instruction. 44 // 45 // NOTE: These must be kept in sync with the constants defined in 46 // java.lang.invoke.MethodHandle. 47 enum Kind { 48 kInvokeVirtual = 0, 49 kInvokeSuper, 50 kInvokeDirect, 51 kInvokeStatic, 52 kInvokeInterface, 53 kInvokeTransform, 54 kInvokeVarHandle, 55 kInvokeVarHandleExact, 56 kInstanceGet, 57 kInstancePut, 58 kStaticGet, 59 kStaticPut, 60 kLastValidKind = kStaticPut, 61 kFirstAccessorKind = kInstanceGet, 62 kLastAccessorKind = kStaticPut, 63 kLastInvokeKind = kInvokeVarHandleExact 64 }; 65 66 Kind GetHandleKind() REQUIRES_SHARED(Locks::mutator_lock_); 67 68 ObjPtr<mirror::MethodType> GetMethodType() REQUIRES_SHARED(Locks::mutator_lock_); 69 70 ObjPtr<mirror::MethodHandle> GetAsTypeCache() REQUIRES_SHARED(Locks::mutator_lock_); 71 72 ArtField* GetTargetField() REQUIRES_SHARED(Locks::mutator_lock_); 73 74 ArtMethod* GetTargetMethod() REQUIRES_SHARED(Locks::mutator_lock_); 75 76 // Gets the return type for a named invoke method, or nullptr if the invoke method is not 77 // supported. 78 static const char* GetReturnTypeDescriptor(const char* invoke_method_name); 79 80 // Used when classes become structurally obsolete to change the MethodHandle to refer to the new 81 // method or field. 82 void VisitTarget(ReflectiveValueVisitor* v) REQUIRES(Locks::mutator_lock_); 83 ArtFieldOrMethodOffset()84 static MemberOffset ArtFieldOrMethodOffset() { 85 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, art_field_or_method_)); 86 } 87 HandleKindOffset()88 static MemberOffset HandleKindOffset() { 89 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, handle_kind_)); 90 } 91 MethodTypeOffset()92 static MemberOffset MethodTypeOffset() { 93 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, method_type_)); 94 } 95 96 protected: 97 void Initialize(uintptr_t art_field_or_method, Kind kind, Handle<MethodType> method_type) 98 REQUIRES_SHARED(Locks::mutator_lock_); 99 100 private: 101 HeapReference<mirror::MethodHandle> as_type_cache_; 102 HeapReference<mirror::MethodHandle> cached_spread_invoker_; 103 HeapReference<mirror::MethodType> method_type_; 104 uint32_t handle_kind_; 105 uint64_t art_field_or_method_; 106 107 private: CachedSpreadInvokerOffset()108 static MemberOffset CachedSpreadInvokerOffset() { 109 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, cached_spread_invoker_)); 110 } AsTypeCacheOffset()111 static MemberOffset AsTypeCacheOffset() { 112 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, as_type_cache_)); 113 } 114 115 friend struct art::MethodHandleOffsets; // for verifying offset information 116 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandle); 117 }; 118 119 // C++ mirror of java.lang.invoke.MethodHandleImpl 120 class MANAGED MethodHandleImpl : public MethodHandle { 121 public: 122 MIRROR_CLASS("Ljava/lang/invoke/MethodHandleImpl;"); 123 124 EXPORT static ObjPtr<mirror::MethodHandleImpl> Create(Thread* const self, 125 uintptr_t art_field_or_method, 126 MethodHandle::Kind kind, 127 Handle<MethodType> method_type) 128 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_); 129 130 private: 131 HeapReference<mirror::Object> target_class_or_info_; // Unused by the runtime. 132 133 friend struct art::MethodHandleImplOffsets; // for verifying offset information 134 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandleImpl); 135 }; 136 137 } // namespace mirror 138 } // namespace art 139 140 #endif // ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_ 141