• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "art_method.h"
20 #include "handle_scope-inl.h"
21 #include "jni_internal.h"
22 #include "mirror/field.h"
23 #include "mirror/method.h"
24 #include "mirror/method_handle_impl.h"
25 #include "runtime.h"
26 #include "scoped_thread_state_change-inl.h"
27 
28 namespace art {
29 
MethodHandleImpl_getMemberInternal(JNIEnv * env,jobject thiz)30 static jobject MethodHandleImpl_getMemberInternal(JNIEnv* env, jobject thiz) {
31   ScopedObjectAccess soa(env);
32   StackHandleScope<2> hs(soa.Self());
33   Handle<mirror::MethodHandleImpl> handle = hs.NewHandle(
34       soa.Decode<mirror::MethodHandleImpl>(thiz));
35 
36   // Check the handle kind, we need to materialize a Field for field accessors,
37   // a Method for method invokers and a Constructor for constructors.
38   const mirror::MethodHandle::Kind handle_kind = handle->GetHandleKind();
39 
40   // We check this here because we pass false to CreateFromArtField and
41   // CreateFromArtMethod.
42   DCHECK(!Runtime::Current()->IsActiveTransaction());
43 
44   MutableHandle<mirror::Object> h_object(hs.NewHandle<mirror::Object>(nullptr));
45   if (handle_kind >= mirror::MethodHandle::kFirstAccessorKind) {
46     ArtField* const field = handle->GetTargetField();
47     h_object.Assign(mirror::Field::CreateFromArtField<kRuntimePointerSize, false>(
48         soa.Self(), field, false /* force_resolve */));
49   } else {
50     ArtMethod* const method = handle->GetTargetMethod();
51     if (method->IsConstructor()) {
52       h_object.Assign(mirror::Constructor::CreateFromArtMethod<kRuntimePointerSize, false>(
53           soa.Self(), method));
54     } else {
55       h_object.Assign(mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(
56           soa.Self(), method));
57     }
58   }
59 
60   if (UNLIKELY(h_object == nullptr)) {
61     soa.Self()->AssertPendingOOMException();
62     return nullptr;
63   }
64 
65   return soa.AddLocalReference<jobject>(h_object.Get());
66 }
67 
68 static JNINativeMethod gMethods[] = {
69   NATIVE_METHOD(MethodHandleImpl, getMemberInternal, "()Ljava/lang/reflect/Member;"),
70 };
71 
register_java_lang_invoke_MethodHandleImpl(JNIEnv * env)72 void register_java_lang_invoke_MethodHandleImpl(JNIEnv* env) {
73   REGISTER_NATIVE_METHODS("java/lang/invoke/MethodHandleImpl");
74 }
75 
76 }  // namespace art
77