blob: 6ba6e25f8afa8361e2c95b1ff730aa6a3b0e7bdb [file] [log] [blame]
Narayan Kamathafa48272016-08-03 12:46:58 +01001/*
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
Orion Hodsonc069a302017-01-18 09:23:12 +000020#include "art_field.h"
21#include "art_method.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010022#include "class.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010023#include "method_type.h"
Vladimir Marko5aead702019-03-27 11:00:36 +000024#include "obj_ptr.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "object.h"
Narayan Kamathafa48272016-08-03 12:46:58 +010026
27namespace art {
28
Narayan Kamathbd2fed52017-01-25 10:46:54 +000029struct MethodHandleOffsets;
Narayan Kamathafa48272016-08-03 12:46:58 +010030struct MethodHandleImplOffsets;
Alex Lightc18eba32019-09-24 14:36:27 -070031class ReflectiveValueVisitor;
Narayan Kamathafa48272016-08-03 12:46:58 +010032
33namespace mirror {
34
35// C++ mirror of java.lang.invoke.MethodHandle
36class MANAGED MethodHandle : public Object {
37 public:
Alex Light8f187c32021-04-20 14:29:00 -070038 MIRROR_CLASS("Ljava/lang/invoke/MethodHandle;");
39
Orion Hodson811bd5f2016-12-07 11:35:37 +000040 // Defines the behaviour of a given method handle. The behaviour
41 // of a handle of a given kind is identical to the dex bytecode behaviour
42 // of the equivalent instruction.
43 //
44 // NOTE: These must be kept in sync with the constants defined in
45 // java.lang.invoke.MethodHandle.
46 enum Kind {
47 kInvokeVirtual = 0,
48 kInvokeSuper,
49 kInvokeDirect,
50 kInvokeStatic,
51 kInvokeInterface,
52 kInvokeTransform,
53 kInvokeCallSiteTransform,
Orion Hodsonb8b93872018-01-30 07:51:10 +000054 kInvokeVarHandle,
55 kInvokeVarHandleExact,
Orion Hodson811bd5f2016-12-07 11:35:37 +000056 kInstanceGet,
57 kInstancePut,
58 kStaticGet,
59 kStaticPut,
60 kLastValidKind = kStaticPut,
61 kFirstAccessorKind = kInstanceGet,
62 kLastAccessorKind = kStaticPut,
Orion Hodsonb8b93872018-01-30 07:51:10 +000063 kLastInvokeKind = kInvokeVarHandleExact
Orion Hodson811bd5f2016-12-07 11:35:37 +000064 };
65
66 Kind GetHandleKind() REQUIRES_SHARED(Locks::mutator_lock_) {
67 const int32_t handle_kind = GetField32(OFFSET_OF_OBJECT_MEMBER(MethodHandle, handle_kind_));
68 DCHECK(handle_kind >= 0 &&
69 handle_kind <= static_cast<int32_t>(Kind::kLastValidKind));
70 return static_cast<Kind>(handle_kind);
71 }
72
Vladimir Marko5aead702019-03-27 11:00:36 +000073 ALWAYS_INLINE ObjPtr<mirror::MethodType> GetMethodType() REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamathafa48272016-08-03 12:46:58 +010074
Vladimir Marko5aead702019-03-27 11:00:36 +000075 ALWAYS_INLINE ObjPtr<mirror::MethodType> GetNominalType() REQUIRES_SHARED(Locks::mutator_lock_);
Narayan Kamath0a8485e2016-11-02 18:47:11 +000076
Orion Hodson3d617ac2016-10-19 14:00:46 +010077 ArtField* GetTargetField() REQUIRES_SHARED(Locks::mutator_lock_) {
78 return reinterpret_cast<ArtField*>(
79 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
80 }
81
Narayan Kamathafa48272016-08-03 12:46:58 +010082 ArtMethod* GetTargetMethod() REQUIRES_SHARED(Locks::mutator_lock_) {
83 return reinterpret_cast<ArtMethod*>(
84 GetField64(OFFSET_OF_OBJECT_MEMBER(MethodHandle, art_field_or_method_)));
85 }
86
Orion Hodsonfe92d122018-01-02 10:45:17 +000087 // Gets the return type for a named invoke method, or nullptr if the invoke method is not
88 // supported.
89 static const char* GetReturnTypeDescriptor(const char* invoke_method_name);
90
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000091 // Used when classes become structurally obsolete to change the MethodHandle to refer to the new
92 // method or field.
Alex Lightc18eba32019-09-24 14:36:27 -070093 void VisitTarget(ReflectiveValueVisitor* v) REQUIRES(Locks::mutator_lock_);
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000094
Orion Hodsonc069a302017-01-18 09:23:12 +000095 protected:
96 void Initialize(uintptr_t art_field_or_method, Kind kind, Handle<MethodType> method_type)
97 REQUIRES_SHARED(Locks::mutator_lock_);
98
Narayan Kamathafa48272016-08-03 12:46:58 +010099 private:
Narayan Kamathc5889ce2017-01-19 20:42:23 +0000100 HeapReference<mirror::MethodHandle> cached_spread_invoker_;
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000101 HeapReference<mirror::MethodType> nominal_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +0100102 HeapReference<mirror::MethodType> method_type_;
Narayan Kamathafa48272016-08-03 12:46:58 +0100103 uint32_t handle_kind_;
Narayan Kamathc5889ce2017-01-19 20:42:23 +0000104 uint64_t art_field_or_method_;
Narayan Kamathafa48272016-08-03 12:46:58 +0100105
106 private:
Orion Hodsonc069a302017-01-18 09:23:12 +0000107 static MemberOffset CachedSpreadInvokerOffset() {
108 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, cached_spread_invoker_));
109 }
Narayan Kamath0a8485e2016-11-02 18:47:11 +0000110 static MemberOffset NominalTypeOffset() {
111 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, nominal_type_));
Narayan Kamathafa48272016-08-03 12:46:58 +0100112 }
113 static MemberOffset MethodTypeOffset() {
114 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, method_type_));
115 }
116 static MemberOffset ArtFieldOrMethodOffset() {
117 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, art_field_or_method_));
118 }
119 static MemberOffset HandleKindOffset() {
120 return MemberOffset(OFFSETOF_MEMBER(MethodHandle, handle_kind_));
121 }
122
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000123 friend struct art::MethodHandleOffsets; // for verifying offset information
Narayan Kamathafa48272016-08-03 12:46:58 +0100124 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandle);
125};
126
127// C++ mirror of java.lang.invoke.MethodHandleImpl
128class MANAGED MethodHandleImpl : public MethodHandle {
129 public:
Alex Light8f187c32021-04-20 14:29:00 -0700130 MIRROR_CLASS("Ljava/lang/invoke/MethodHandleImpl;");
131
Vladimir Marko5aead702019-03-27 11:00:36 +0000132 static ObjPtr<mirror::MethodHandleImpl> Create(Thread* const self,
133 uintptr_t art_field_or_method,
134 MethodHandle::Kind kind,
135 Handle<MethodType> method_type)
Orion Hodsonc069a302017-01-18 09:23:12 +0000136 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_);
137
Narayan Kamathafa48272016-08-03 12:46:58 +0100138 private:
Narayan Kamathbd2fed52017-01-25 10:46:54 +0000139 static MemberOffset InfoOffset() {
140 return MemberOffset(OFFSETOF_MEMBER(MethodHandleImpl, info_));
141 }
142
143 HeapReference<mirror::Object> info_; // Unused by the runtime.
Narayan Kamathafa48272016-08-03 12:46:58 +0100144
145 friend struct art::MethodHandleImplOffsets; // for verifying offset information
146 DISALLOW_IMPLICIT_CONSTRUCTORS(MethodHandleImpl);
147};
148
149} // namespace mirror
150} // namespace art
151
152#endif // ART_RUNTIME_MIRROR_METHOD_HANDLE_IMPL_H_