| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 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 | |
| Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 19 | #include "class-inl.h" |
| David Sehr | 8c0961f | 2018-01-23 16:11:38 -0800 | [diff] [blame^] | 20 | #include "dex/modifiers.h" |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 21 | #include "gc_root-inl.h" |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 22 | #include "handle_scope.h" |
| Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 23 | #include "jni_internal.h" |
| 24 | #include "mirror/method_handle_impl.h" |
| Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include "object-inl.h" |
| Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 26 | #include "well_known_classes.h" |
| Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace mirror { |
| 30 | |
| 31 | GcRoot<mirror::Class> MethodHandlesLookup::static_class_; |
| 32 | |
| 33 | void 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 | |
| 39 | void MethodHandlesLookup::ResetClass() { |
| 40 | CHECK(!static_class_.IsNull()); |
| 41 | static_class_ = GcRoot<Class>(nullptr); |
| 42 | } |
| 43 | |
| 44 | void MethodHandlesLookup::VisitRoots(RootVisitor* visitor) { |
| 45 | static_class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
| 46 | } |
| 47 | |
| 48 | MethodHandlesLookup* 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 Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 60 | MethodHandlesLookup* 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 | |
| 67 | MethodHandle* 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 Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 82 | } // namespace mirror |
| 83 | } // namespace art |