Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include "java_lang_invoke_MethodHandleImpl.h" |
| 18 | |
Andreas Gampe | a14100c | 2017-04-24 15:09:56 -0700 | [diff] [blame] | 19 | #include "nativehelper/jni_macros.h" |
| 20 | |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 21 | #include "art_method.h" |
| 22 | #include "handle_scope-inl.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 23 | #include "jni/jni_internal.h" |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 24 | #include "mirror/field.h" |
| 25 | #include "mirror/method.h" |
| 26 | #include "mirror/method_handle_impl.h" |
Andreas Gampe | 87583b3 | 2017-05-25 11:22:18 -0700 | [diff] [blame] | 27 | #include "native_util.h" |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 28 | #include "runtime.h" |
| 29 | #include "scoped_thread_state_change-inl.h" |
| 30 | |
| 31 | namespace art { |
| 32 | |
| 33 | static jobject MethodHandleImpl_getMemberInternal(JNIEnv* env, jobject thiz) { |
| 34 | ScopedObjectAccess soa(env); |
| 35 | StackHandleScope<2> hs(soa.Self()); |
| 36 | Handle<mirror::MethodHandleImpl> handle = hs.NewHandle( |
| 37 | soa.Decode<mirror::MethodHandleImpl>(thiz)); |
| 38 | |
| 39 | // Check the handle kind, we need to materialize a Field for field accessors, |
| 40 | // a Method for method invokers and a Constructor for constructors. |
| 41 | const mirror::MethodHandle::Kind handle_kind = handle->GetHandleKind(); |
| 42 | |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 43 | MutableHandle<mirror::Object> h_object(hs.NewHandle<mirror::Object>(nullptr)); |
| 44 | if (handle_kind >= mirror::MethodHandle::kFirstAccessorKind) { |
| 45 | ArtField* const field = handle->GetTargetField(); |
Vladimir Marko | 0a6063a | 2020-05-14 16:39:14 +0100 | [diff] [blame] | 46 | h_object.Assign( |
| 47 | mirror::Field::CreateFromArtField(soa.Self(), field, /* force_resolve= */ false)); |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 48 | } else { |
| 49 | ArtMethod* const method = handle->GetTargetMethod(); |
| 50 | if (method->IsConstructor()) { |
Vladimir Marko | b6f4c79 | 2020-05-04 15:37:29 +0100 | [diff] [blame] | 51 | h_object.Assign( |
| 52 | mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize>(soa.Self(), method)); |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 53 | } else { |
Vladimir Marko | b6f4c79 | 2020-05-04 15:37:29 +0100 | [diff] [blame] | 54 | h_object.Assign(mirror::Method::CreateFromArtMethod<kRuntimePointerSize>(soa.Self(), method)); |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 58 | if (UNLIKELY(h_object == nullptr)) { |
Narayan Kamath | bd2fed5 | 2017-01-25 10:46:54 +0000 | [diff] [blame] | 59 | soa.Self()->AssertPendingOOMException(); |
| 60 | return nullptr; |
| 61 | } |
| 62 | |
| 63 | return soa.AddLocalReference<jobject>(h_object.Get()); |
| 64 | } |
| 65 | |
| 66 | static JNINativeMethod gMethods[] = { |
| 67 | NATIVE_METHOD(MethodHandleImpl, getMemberInternal, "()Ljava/lang/reflect/Member;"), |
| 68 | }; |
| 69 | |
| 70 | void register_java_lang_invoke_MethodHandleImpl(JNIEnv* env) { |
| 71 | REGISTER_NATIVE_METHODS("java/lang/invoke/MethodHandleImpl"); |
| 72 | } |
| 73 | |
| 74 | } // namespace art |