blob: 039bbf293295480497b7c31bde58eb8c79d08b32 [file] [log] [blame]
Orion Hodsonc069a302017-01-18 09:23:12 +00001/*
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
Andreas Gampec6ea7d02017-02-01 16:46:28 -080019#include "class-inl.h"
David Sehr8c0961f2018-01-23 16:11:38 -080020#include "dex/modifiers.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000021#include "gc_root-inl.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000022#include "handle_scope.h"
Orion Hodsonf8db2c32017-07-07 20:07:12 +010023#include "jni_internal.h"
24#include "mirror/method_handle_impl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "object-inl.h"
Orion Hodsonf8db2c32017-07-07 20:07:12 +010026#include "well_known_classes.h"
Orion Hodsonc069a302017-01-18 09:23:12 +000027
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
Orion Hodsonf8db2c32017-07-07 20:07:12 +010060MethodHandlesLookup* 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
Orion Hodsonc069a302017-01-18 09:23:12 +000082} // namespace mirror
83} // namespace art