1 /*
2  * Copyright (C) 2017 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 "method_handles_lookup.h"
18 
19 #include "class-alloc-inl.h"
20 #include "class_root-inl.h"
21 #include "dex/modifiers.h"
22 #include "handle_scope.h"
23 #include "jni/jni_internal.h"
24 #include "mirror/method_handle_impl.h"
25 #include "obj_ptr-inl.h"
26 #include "object-inl.h"
27 #include "well_known_classes.h"
28 
29 namespace art {
30 namespace mirror {
31 
Create(Thread * const self,Handle<Class> lookup_class)32 ObjPtr<MethodHandlesLookup> MethodHandlesLookup::Create(Thread* const self,
33                                                         Handle<Class> lookup_class)
34   REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_) {
35   static constexpr uint32_t kAllModes = kAccPublic | kAccPrivate | kAccProtected | kAccStatic;
36 
37   ObjPtr<MethodHandlesLookup> mhl = ObjPtr<MethodHandlesLookup>::DownCast(
38       GetClassRoot<MethodHandlesLookup>()->AllocObject(self));
39   mhl->SetFieldObject<false>(LookupClassOffset(), lookup_class.Get());
40   mhl->SetField32<false>(AllowedModesOffset(), kAllModes);
41   return mhl;
42 }
43 
GetDefault(Thread * const self)44 ObjPtr<MethodHandlesLookup> MethodHandlesLookup::GetDefault(Thread* const self) {
45   ArtMethod* lookup = jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandles_lookup);
46   JValue result;
47   lookup->Invoke(self, nullptr, 0, &result, "L");
48   return ObjPtr<MethodHandlesLookup>::DownCast(result.GetL());
49 }
50 
FindConstructor(Thread * const self,Handle<Class> klass,Handle<MethodType> method_type)51 ObjPtr<MethodHandle> MethodHandlesLookup::FindConstructor(Thread* const self,
52                                                           Handle<Class> klass,
53                                                           Handle<MethodType> method_type) {
54   ArtMethod* findConstructor =
55       jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandles_Lookup_findConstructor);
56   uint32_t args[] = {
57     static_cast<uint32_t>(reinterpret_cast<uintptr_t>(this)),
58     static_cast<uint32_t>(reinterpret_cast<uintptr_t>(klass.Get())),
59     static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_type.Get()))
60   };
61   JValue result;
62   findConstructor->Invoke(self, args, sizeof(args), &result, "LLL");
63   return ObjPtr<MethodHandle>::DownCast(result.GetL());
64 }
65 
66 }  // namespace mirror
67 }  // namespace art
68