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-inl.h"
20#include "dex/modifiers.h"
21#include "gc_root-inl.h"
22#include "handle_scope.h"
23#include "jni_internal.h"
24#include "mirror/method_handle_impl.h"
25#include "object-inl.h"
26#include "well_known_classes.h"
27
28namespace art {
29namespace mirror {
30
31GcRoot<mirror::Class> MethodHandlesLookup::static_class_;
32
33void MethodHandlesLookup::SetClass(Class* klass) {
34  CHECK(static_class_.IsNull()) << static_class_.Read() << " " << klass;
35  CHECK(klass != nullptr);
36  static_class_ = GcRoot<Class>(klass);
37}
38
39void MethodHandlesLookup::ResetClass() {
40  CHECK(!static_class_.IsNull());
41  static_class_ = GcRoot<Class>(nullptr);
42}
43
44void MethodHandlesLookup::VisitRoots(RootVisitor* visitor) {
45  static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
46}
47
48MethodHandlesLookup* MethodHandlesLookup::Create(Thread* const self, Handle<Class> lookup_class)
49  REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!Roles::uninterruptible_) {
50  static constexpr uint32_t kAllModes = kAccPublic | kAccPrivate | kAccProtected | kAccStatic;
51
52  StackHandleScope<1> hs(self);
53  Handle<MethodHandlesLookup> mhl(
54      hs.NewHandle(ObjPtr<MethodHandlesLookup>::DownCast(StaticClass()->AllocObject(self))));
55  mhl->SetFieldObject<false>(LookupClassOffset(), lookup_class.Get());
56  mhl->SetField32<false>(AllowedModesOffset(), kAllModes);
57  return mhl.Get();
58}
59
60MethodHandlesLookup* MethodHandlesLookup::GetDefault(Thread* const self) {
61  ArtMethod* lookup = jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandles_lookup);
62  JValue result;
63  lookup->Invoke(self, nullptr, 0, &result, "L");
64  return down_cast<MethodHandlesLookup*>(result.GetL());
65}
66
67MethodHandle* MethodHandlesLookup::FindConstructor(Thread* const self,
68                                                           Handle<Class> klass,
69                                                           Handle<MethodType> method_type) {
70  ArtMethod* findConstructor =
71      jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandles_Lookup_findConstructor);
72  uint32_t args[] = {
73    static_cast<uint32_t>(reinterpret_cast<uintptr_t>(this)),
74    static_cast<uint32_t>(reinterpret_cast<uintptr_t>(klass.Get())),
75    static_cast<uint32_t>(reinterpret_cast<uintptr_t>(method_type.Get()))
76  };
77  JValue result;
78  findConstructor->Invoke(self, args, sizeof(args), &result, "LLL");
79  return down_cast<MethodHandle*>(result.GetL());
80}
81
82}  // namespace mirror
83}  // namespace art
84