blob: 0b26bd7c4afeb7df42c434f2d84c89e1b3c00e37 [file] [log] [blame]
Narayan Kamathbd2fed52017-01-25 10:46:54 +00001/*
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 Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Narayan Kamathbd2fed52017-01-25 10:46:54 +000021#include "art_method.h"
22#include "handle_scope-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010023#include "jni/jni_internal.h"
Narayan Kamathbd2fed52017-01-25 10:46:54 +000024#include "mirror/field.h"
25#include "mirror/method.h"
26#include "mirror/method_handle_impl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070027#include "native_util.h"
Narayan Kamathbd2fed52017-01-25 10:46:54 +000028#include "runtime.h"
29#include "scoped_thread_state_change-inl.h"
30
31namespace art {
32
33static 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
43 // We check this here because we pass false to CreateFromArtField and
44 // CreateFromArtMethod.
45 DCHECK(!Runtime::Current()->IsActiveTransaction());
46
47 MutableHandle<mirror::Object> h_object(hs.NewHandle<mirror::Object>(nullptr));
48 if (handle_kind >= mirror::MethodHandle::kFirstAccessorKind) {
49 ArtField* const field = handle->GetTargetField();
50 h_object.Assign(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -070051 soa.Self(), field, /* force_resolve= */ false));
Narayan Kamathbd2fed52017-01-25 10:46:54 +000052 } else {
53 ArtMethod* const method = handle->GetTargetMethod();
54 if (method->IsConstructor()) {
55 h_object.Assign(mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize, false>(
56 soa.Self(), method));
57 } else {
58 h_object.Assign(mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(
59 soa.Self(), method));
60 }
61 }
62
Andreas Gampefa4333d2017-02-14 11:10:34 -080063 if (UNLIKELY(h_object == nullptr)) {
Narayan Kamathbd2fed52017-01-25 10:46:54 +000064 soa.Self()->AssertPendingOOMException();
65 return nullptr;
66 }
67
68 return soa.AddLocalReference<jobject>(h_object.Get());
69}
70
71static JNINativeMethod gMethods[] = {
72 NATIVE_METHOD(MethodHandleImpl, getMemberInternal, "()Ljava/lang/reflect/Member;"),
73};
74
75void register_java_lang_invoke_MethodHandleImpl(JNIEnv* env) {
76 REGISTER_NATIVE_METHODS("java/lang/invoke/MethodHandleImpl");
77}
78
79} // namespace art