Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 "class.h" |
| 18 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 19 | #include "android-base/stringprintf.h" |
| 20 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 21 | #include "art_field-inl.h" |
| 22 | #include "art_method-inl.h" |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 23 | #include "base/logging.h" // For VLOG. |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 24 | #include "base/utils.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include "class-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 26 | #include "class_ext.h" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 27 | #include "class_linker-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 28 | #include "class_loader.h" |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 29 | #include "class_root.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 30 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 31 | #include "dex/dex_file-inl.h" |
| 32 | #include "dex/dex_file_annotations.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 33 | #include "dex_cache.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 34 | #include "gc/accounting/card_table-inl.h" |
Andreas Gampe | e15b9b1 | 2018-10-29 12:54:27 -0700 | [diff] [blame] | 35 | #include "gc/heap-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 36 | #include "handle_scope-inl.h" |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 37 | #include "subtype_check.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 38 | #include "method.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 39 | #include "object-inl.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 40 | #include "object-refvisitor-inl.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 41 | #include "object_array-inl.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 42 | #include "object_lock.h" |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 43 | #include "string-inl.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 44 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 45 | #include "thread.h" |
| 46 | #include "throwable.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 47 | #include "well_known_classes.h" |
| 48 | |
| 49 | namespace art { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 50 | |
| 51 | // TODO: move to own CC file? |
| 52 | constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity]; |
| 53 | constexpr size_t BitString::kCapacity; |
| 54 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 55 | namespace mirror { |
| 56 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 57 | using android::base::StringPrintf; |
| 58 | |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 59 | ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) { |
| 60 | const char* expected_name = nullptr; |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 61 | ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid. |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 62 | if (name != nullptr && name->GetLength() >= 2) { |
| 63 | // Perfect hash for the expected values: from the second letters of the primitive types, |
| 64 | // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'. |
| 65 | char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1); |
| 66 | switch (hash) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 67 | case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break; |
| 68 | case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break; |
| 69 | case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break; |
| 70 | case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break; |
| 71 | case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break; |
| 72 | case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break; |
| 73 | case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break; |
| 74 | case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break; |
| 75 | case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break; |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 76 | default: break; |
| 77 | } |
| 78 | } |
| 79 | if (expected_name != nullptr && name->Equals(expected_name)) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 80 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 81 | DCHECK(klass != nullptr); |
| 82 | return klass; |
| 83 | } else { |
| 84 | Thread* self = Thread::Current(); |
| 85 | if (name == nullptr) { |
| 86 | // Note: ThrowNullPointerException() requires a message which we deliberately want to omit. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 87 | self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg= */ nullptr); |
Vladimir Marko | 7287c4d | 2018-02-15 10:41:07 +0000 | [diff] [blame] | 88 | } else { |
| 89 | self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str()); |
| 90 | } |
| 91 | return nullptr; |
| 92 | } |
| 93 | } |
| 94 | |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 95 | ClassExt* Class::EnsureExtDataPresent(Thread* self) { |
| 96 | ObjPtr<ClassExt> existing(GetExtData()); |
| 97 | if (!existing.IsNull()) { |
| 98 | return existing.Ptr(); |
| 99 | } |
| 100 | StackHandleScope<3> hs(self); |
| 101 | // Handlerize 'this' since we are allocating here. |
| 102 | Handle<Class> h_this(hs.NewHandle(this)); |
| 103 | // Clear exception so we can allocate. |
| 104 | Handle<Throwable> throwable(hs.NewHandle(self->GetException())); |
| 105 | self->ClearException(); |
| 106 | // Allocate the ClassExt |
| 107 | Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 108 | if (new_ext == nullptr) { |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 109 | // OOM allocating the classExt. |
| 110 | // TODO Should we restore the suppressed exception? |
| 111 | self->AssertPendingOOMException(); |
| 112 | return nullptr; |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 113 | } else { |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 114 | MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_)); |
| 115 | bool set; |
| 116 | // Set the ext_data_ field using CAS semantics. |
| 117 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | a9746b9 | 2018-06-22 10:25:40 -0700 | [diff] [blame] | 118 | set = h_this->CasFieldObject<true>(ext_offset, |
| 119 | nullptr, |
| 120 | new_ext.Get(), |
| 121 | CASMode::kStrong, |
| 122 | std::memory_order_seq_cst); |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 123 | } else { |
Mathieu Chartier | a9746b9 | 2018-06-22 10:25:40 -0700 | [diff] [blame] | 124 | set = h_this->CasFieldObject<false>(ext_offset, |
| 125 | nullptr, |
| 126 | new_ext.Get(), |
| 127 | CASMode::kStrong, |
| 128 | std::memory_order_seq_cst); |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 129 | } |
| 130 | ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData()); |
| 131 | DCHECK(!set || h_this->GetExtData() == new_ext.Get()); |
| 132 | CHECK(!ret.IsNull()); |
| 133 | // Restore the exception if there was one. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 134 | if (throwable != nullptr) { |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 135 | self->SetException(throwable.Get()); |
| 136 | } |
| 137 | return ret.Ptr(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 141 | void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) { |
| 142 | ClassStatus old_status = h_this->GetStatus(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 143 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 144 | bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 145 | if (LIKELY(class_linker_initialized)) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 146 | if (UNLIKELY(new_status <= old_status && |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 147 | new_status != ClassStatus::kErrorUnresolved && |
| 148 | new_status != ClassStatus::kErrorResolved && |
| 149 | new_status != ClassStatus::kRetired)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 150 | LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass() |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 151 | << " " << old_status << " -> " << new_status; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 152 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 153 | if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 154 | // When classes are being resolved the resolution code should hold the lock. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 155 | CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId()) |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 156 | << "Attempt to change status of class while not holding its lock: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 157 | << h_this->PrettyClass() << " " << old_status << " -> " << new_status; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 158 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 159 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 160 | if (UNLIKELY(IsErroneous(new_status))) { |
| 161 | CHECK(!h_this->IsErroneous()) |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 162 | << "Attempt to set as erroneous an already erroneous class " |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 163 | << h_this->PrettyClass() |
| 164 | << " old_status: " << old_status << " new_status: " << new_status; |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 165 | CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved); |
Andreas Gampe | 31decb1 | 2015-08-24 21:09:05 -0700 | [diff] [blame] | 166 | if (VLOG_IS_ON(class_linker)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 167 | LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous."; |
Andreas Gampe | 31decb1 | 2015-08-24 21:09:05 -0700 | [diff] [blame] | 168 | if (self->IsExceptionPending()) { |
| 169 | LOG(ERROR) << "Exception: " << self->GetException()->Dump(); |
| 170 | } |
| 171 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 172 | |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 173 | ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self)); |
| 174 | if (!ext.IsNull()) { |
| 175 | self->AssertPendingException(); |
| 176 | ext->SetVerifyError(self->GetException()); |
| 177 | } else { |
| 178 | self->AssertPendingOOMException(); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 179 | } |
| 180 | self->AssertPendingException(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 181 | } |
Alex Light | 0273ad1 | 2016-11-02 11:19:31 -0700 | [diff] [blame] | 182 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 183 | if (kBitstringSubtypeCheckEnabled) { |
| 184 | // FIXME: This looks broken with respect to aborted transactions. |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 185 | ObjPtr<mirror::Class> h_this_ptr = h_this.Get(); |
| 186 | SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status); |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 187 | } else { |
| 188 | // The ClassStatus is always in the 4 most-significant bits of status_. |
| 189 | static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32"); |
| 190 | uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize); |
| 191 | if (Runtime::Current()->IsActiveTransaction()) { |
| 192 | h_this->SetField32Volatile<true>(StatusOffset(), new_status_value); |
| 193 | } else { |
| 194 | h_this->SetField32Volatile<false>(StatusOffset(), new_status_value); |
| 195 | } |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // Setting the object size alloc fast path needs to be after the status write so that if the |
| 199 | // alloc path sees a valid object size, we would know that it's initialized as long as it has a |
| 200 | // load-acquire/fake dependency. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 201 | if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) { |
Mathieu Chartier | 161db1d | 2016-09-01 14:06:54 -0700 | [diff] [blame] | 202 | DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max()); |
| 203 | // Finalizable objects must always go slow path. |
| 204 | if (!h_this->IsFinalizable()) { |
| 205 | h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment)); |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 206 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 207 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 208 | |
| 209 | if (!class_linker_initialized) { |
| 210 | // When the class linker is being initialized its single threaded and by definition there can be |
| 211 | // no waiters. During initialization classes may appear temporary but won't be retired as their |
| 212 | // size was statically computed. |
| 213 | } else { |
| 214 | // Classes that are being resolved or initialized need to notify waiters that the class status |
| 215 | // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 216 | if (h_this->IsTemp()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 217 | // Class is a temporary one, ensure that waiters for resolution get notified of retirement |
| 218 | // so that they can grab the new version of the class from the class linker's table. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 219 | CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor(); |
| 220 | if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 221 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 222 | } |
| 223 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 224 | CHECK_NE(new_status, ClassStatus::kRetired); |
| 225 | if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 226 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 229 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 230 | } |
| 231 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 232 | void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) { |
Chang Xing | 6d3e768 | 2017-07-11 10:31:29 -0700 | [diff] [blame] | 233 | SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 236 | void Class::SetClassSize(uint32_t new_class_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 237 | if (kIsDebugBuild && new_class_size < GetClassSize()) { |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 238 | DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail); |
| 239 | LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 240 | LOG(FATAL) << "class=" << PrettyTypeOf(); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 241 | } |
Chang Xing | 6d3e768 | 2017-07-11 10:31:29 -0700 | [diff] [blame] | 242 | SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 246 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 247 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 248 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 249 | String* Class::ComputeName(Handle<Class> h_this) { |
| 250 | String* name = h_this->GetName(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 251 | if (name != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 252 | return name; |
| 253 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 254 | std::string temp; |
| 255 | const char* descriptor = h_this->GetDescriptor(&temp); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 256 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 257 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 258 | // The descriptor indicates that this is the class for |
| 259 | // a primitive type; special-case the return value. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 260 | const char* c_name = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 261 | switch (descriptor[0]) { |
| 262 | case 'Z': c_name = "boolean"; break; |
| 263 | case 'B': c_name = "byte"; break; |
| 264 | case 'C': c_name = "char"; break; |
| 265 | case 'S': c_name = "short"; break; |
| 266 | case 'I': c_name = "int"; break; |
| 267 | case 'J': c_name = "long"; break; |
| 268 | case 'F': c_name = "float"; break; |
| 269 | case 'D': c_name = "double"; break; |
| 270 | case 'V': c_name = "void"; break; |
| 271 | default: |
| 272 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 273 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 274 | name = String::AllocFromModifiedUtf8(self, c_name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 275 | } else { |
| 276 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 277 | // components. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 278 | name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 279 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 280 | h_this->SetName(name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 281 | return name; |
| 282 | } |
| 283 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 284 | void Class::DumpClass(std::ostream& os, int flags) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 285 | if ((flags & kDumpClassFullDetail) == 0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 286 | os << PrettyClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 287 | if ((flags & kDumpClassClassLoader) != 0) { |
| 288 | os << ' ' << GetClassLoader(); |
| 289 | } |
| 290 | if ((flags & kDumpClassInitialized) != 0) { |
| 291 | os << ' ' << GetStatus(); |
| 292 | } |
| 293 | os << "\n"; |
| 294 | return; |
| 295 | } |
| 296 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 297 | Thread* const self = Thread::Current(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 298 | StackHandleScope<2> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 299 | Handle<Class> h_this(hs.NewHandle(this)); |
| 300 | Handle<Class> h_super(hs.NewHandle(GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 301 | auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 302 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 303 | std::string temp; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 304 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 305 | << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 306 | os << " objectSize=" << SizeOf() << " " |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 307 | << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 308 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 309 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 310 | if (h_super != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 311 | os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader() |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 312 | << ")\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 313 | } |
| 314 | if (IsArrayClass()) { |
| 315 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 316 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 317 | const size_t num_direct_interfaces = NumDirectInterfaces(); |
| 318 | if (num_direct_interfaces > 0) { |
| 319 | os << " interfaces (" << num_direct_interfaces << "):\n"; |
| 320 | for (size_t i = 0; i < num_direct_interfaces; ++i) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 321 | ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 322 | if (interface == nullptr) { |
| 323 | os << StringPrintf(" %2zd: nullptr!\n", i); |
| 324 | } else { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 325 | ObjPtr<ClassLoader> cl = interface->GetClassLoader(); |
| 326 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr()); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 327 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 328 | } |
| 329 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 330 | if (!IsLoaded()) { |
| 331 | os << " class not yet loaded"; |
| 332 | } else { |
| 333 | // After this point, this may have moved due to GetDirectInterface. |
| 334 | os << " vtable (" << h_this->NumVirtualMethods() << " entries, " |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 335 | << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n"; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 336 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 337 | os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 338 | h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 339 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 340 | os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; |
| 341 | for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 342 | os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 343 | h_this->GetDirectMethod(i, image_pointer_size)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 344 | } |
| 345 | if (h_this->NumStaticFields() > 0) { |
| 346 | os << " static fields (" << h_this->NumStaticFields() << " entries):\n"; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 347 | if (h_this->IsResolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 348 | for (size_t i = 0; i < h_this->NumStaticFields(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 349 | os << StringPrintf(" %2zd: %s\n", i, |
| 350 | ArtField::PrettyField(h_this->GetStaticField(i)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 351 | } |
| 352 | } else { |
| 353 | os << " <not yet available>"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 354 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 355 | } |
| 356 | if (h_this->NumInstanceFields() > 0) { |
| 357 | os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n"; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 358 | if (h_this->IsResolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 359 | for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 360 | os << StringPrintf(" %2zd: %s\n", i, |
| 361 | ArtField::PrettyField(h_this->GetInstanceField(i)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 362 | } |
| 363 | } else { |
| 364 | os << " <not yet available>"; |
| 365 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 371 | if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 372 | // Sanity check that the number of bits set in the reference offset bitmap |
| 373 | // agrees with the number of references |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 374 | uint32_t count = 0; |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 375 | for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 376 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
| 377 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 378 | // +1 for the Class in Object. |
| 379 | CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 380 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 381 | // Not called within a transaction. |
| 382 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 383 | new_reference_offsets); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 386 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
| 387 | size_t i = 0; |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 388 | size_t min_length = std::min(descriptor1.size(), descriptor2.size()); |
| 389 | while (i < min_length && descriptor1[i] == descriptor2[i]) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 390 | ++i; |
| 391 | } |
| 392 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 393 | descriptor2.find('/', i) != StringPiece::npos) { |
| 394 | return false; |
| 395 | } else { |
| 396 | return true; |
| 397 | } |
| 398 | } |
| 399 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 400 | bool Class::IsInSamePackage(ObjPtr<Class> that) { |
| 401 | ObjPtr<Class> klass1 = this; |
| 402 | ObjPtr<Class> klass2 = that; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 403 | if (klass1 == klass2) { |
| 404 | return true; |
| 405 | } |
| 406 | // Class loaders must match. |
| 407 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 408 | return false; |
| 409 | } |
| 410 | // Arrays are in the same package when their element classes are. |
| 411 | while (klass1->IsArrayClass()) { |
| 412 | klass1 = klass1->GetComponentType(); |
| 413 | } |
| 414 | while (klass2->IsArrayClass()) { |
| 415 | klass2 = klass2->GetComponentType(); |
| 416 | } |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 417 | // trivial check again for array types |
| 418 | if (klass1 == klass2) { |
| 419 | return true; |
| 420 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 421 | // Compare the package part of the descriptor string. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 422 | std::string temp1, temp2; |
| 423 | return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 426 | bool Class::IsThrowableClass() { |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 427 | return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 430 | void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 431 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 432 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 433 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 434 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 435 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 438 | template <typename SignatureType> |
| 439 | static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass, |
| 440 | const StringPiece& name, |
| 441 | const SignatureType& signature, |
| 442 | PointerSize pointer_size) |
| 443 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 444 | // If the current class is not an interface, skip the search of its declared methods; |
| 445 | // such lookup is used only to distinguish between IncompatibleClassChangeError and |
| 446 | // NoSuchMethodError and the caller has already tried to search methods in the class. |
| 447 | if (LIKELY(klass->IsInterface())) { |
| 448 | // Search declared methods, both direct and virtual. |
| 449 | // (This lookup is used also for invoke-static on interface classes.) |
| 450 | for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) { |
| 451 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 452 | return &method; |
| 453 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 456 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 457 | // TODO: If there is a unique maximally-specific non-abstract superinterface method, |
| 458 | // we should return it, otherwise an arbitrary one can be returned. |
| 459 | ObjPtr<IfTable> iftable = klass->GetIfTable(); |
| 460 | for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) { |
| 461 | ObjPtr<Class> iface = iftable->GetInterface(i); |
| 462 | for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) { |
| 463 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 464 | return &method; |
| 465 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 468 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 469 | // Then search for public non-static methods in the java.lang.Object. |
| 470 | if (LIKELY(klass->IsInterface())) { |
| 471 | ObjPtr<Class> object_class = klass->GetSuperClass(); |
| 472 | DCHECK(object_class->IsObjectClass()); |
| 473 | for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) { |
| 474 | if (method.IsPublic() && !method.IsStatic() && |
| 475 | method.GetName() == name && method.GetSignature() == signature) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 476 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 480 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 483 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, |
| 484 | const StringPiece& signature, |
| 485 | PointerSize pointer_size) { |
| 486 | return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 489 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, |
| 490 | const Signature& signature, |
| 491 | PointerSize pointer_size) { |
| 492 | return FindInterfaceMethodWithSignature(this, name, signature, pointer_size); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 495 | ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache, |
| 496 | uint32_t dex_method_idx, |
| 497 | PointerSize pointer_size) { |
| 498 | // We always search by name and signature, ignoring the type index in the MethodId. |
| 499 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 500 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
| 501 | StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 502 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 503 | return FindInterfaceMethod(name, signature, pointer_size); |
| 504 | } |
| 505 | |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 506 | static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass, |
| 507 | ObjPtr<mirror::Class> declaring_class) |
| 508 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 509 | if (klass->IsArrayClass()) { |
| 510 | return declaring_class->IsObjectClass(); |
| 511 | } else if (klass->IsInterface()) { |
| 512 | return declaring_class->IsObjectClass() || declaring_class == klass; |
| 513 | } else { |
| 514 | return klass->IsSubClass(declaring_class); |
| 515 | } |
| 516 | } |
| 517 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 518 | static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass, |
| 519 | ObjPtr<mirror::Class> declaring_class, |
| 520 | ArtMethod& method) |
| 521 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 522 | DCHECK_EQ(declaring_class, method.GetDeclaringClass()); |
| 523 | DCHECK_NE(klass, declaring_class); |
Alex Light | afb6647 | 2017-08-01 09:54:49 -0700 | [diff] [blame] | 524 | DCHECK(IsValidInheritanceCheck(klass, declaring_class)); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 525 | uint32_t access_flags = method.GetAccessFlags(); |
| 526 | if ((access_flags & (kAccPublic | kAccProtected)) != 0) { |
| 527 | return true; |
| 528 | } |
| 529 | if ((access_flags & kAccPrivate) != 0) { |
| 530 | return false; |
| 531 | } |
| 532 | for (; klass != declaring_class; klass = klass->GetSuperClass()) { |
| 533 | if (!klass->IsInSamePackage(declaring_class)) { |
| 534 | return false; |
| 535 | } |
| 536 | } |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | template <typename SignatureType> |
| 541 | static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass, |
| 542 | const StringPiece& name, |
| 543 | const SignatureType& signature, |
| 544 | PointerSize pointer_size) |
| 545 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 546 | // Search declared methods first. |
| 547 | for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) { |
| 548 | ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 549 | if (np_method->GetName() == name && np_method->GetSignature() == signature) { |
| 550 | return &method; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | // Then search the superclass chain. If we find an inherited method, return it. |
| 555 | // If we find a method that's not inherited because of access restrictions, |
| 556 | // try to find a method inherited from an interface in copied methods. |
| 557 | ObjPtr<Class> klass = this_klass->GetSuperClass(); |
| 558 | ArtMethod* uninherited_method = nullptr; |
| 559 | for (; klass != nullptr; klass = klass->GetSuperClass()) { |
| 560 | DCHECK(!klass->IsProxyClass()); |
| 561 | for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) { |
| 562 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 563 | if (IsInheritedMethod(this_klass, klass, method)) { |
| 564 | return &method; |
| 565 | } |
| 566 | uninherited_method = &method; |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | if (uninherited_method != nullptr) { |
| 571 | break; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | // Then search copied methods. |
| 576 | // If we found a method that's not inherited, stop the search in its declaring class. |
| 577 | ObjPtr<Class> end_klass = klass; |
| 578 | DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr); |
| 579 | klass = this_klass; |
| 580 | if (UNLIKELY(klass->IsProxyClass())) { |
| 581 | DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty()); |
| 582 | klass = klass->GetSuperClass(); |
| 583 | } |
| 584 | for (; klass != end_klass; klass = klass->GetSuperClass()) { |
| 585 | DCHECK(!klass->IsProxyClass()); |
| 586 | for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) { |
| 587 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 588 | return &method; // No further check needed, copied methods are inherited by definition. |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | return uninherited_method; // Return the `uninherited_method` if any. |
| 593 | } |
| 594 | |
| 595 | |
| 596 | ArtMethod* Class::FindClassMethod(const StringPiece& name, |
| 597 | const StringPiece& signature, |
| 598 | PointerSize pointer_size) { |
| 599 | return FindClassMethodWithSignature(this, name, signature, pointer_size); |
| 600 | } |
| 601 | |
| 602 | ArtMethod* Class::FindClassMethod(const StringPiece& name, |
| 603 | const Signature& signature, |
| 604 | PointerSize pointer_size) { |
| 605 | return FindClassMethodWithSignature(this, name, signature, pointer_size); |
| 606 | } |
| 607 | |
| 608 | ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache, |
| 609 | uint32_t dex_method_idx, |
| 610 | PointerSize pointer_size) { |
| 611 | // FIXME: Hijacking a proxy class by a custom class loader can break this assumption. |
| 612 | DCHECK(!IsProxyClass()); |
| 613 | |
| 614 | // First try to find a declared method by dex_method_idx if we have a dex_cache match. |
| 615 | ObjPtr<DexCache> this_dex_cache = GetDexCache(); |
| 616 | if (this_dex_cache == dex_cache) { |
| 617 | // Lookup is always performed in the class referenced by the MethodId. |
| 618 | DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_); |
| 619 | for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) { |
| 620 | if (method.GetDexMethodIndex() == dex_method_idx) { |
| 621 | return &method; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | // If not found, we need to search by name and signature. |
| 626 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 627 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
| 628 | const Signature signature = dex_file.GetMethodSignature(method_id); |
| 629 | StringPiece name; // Delay strlen() until actually needed. |
| 630 | // If we do not have a dex_cache match, try to find the declared method in this class now. |
| 631 | if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) { |
| 632 | DCHECK(name.empty()); |
| 633 | name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 634 | for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) { |
| 635 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 636 | return &method; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | // Then search the superclass chain. If we find an inherited method, return it. |
| 642 | // If we find a method that's not inherited because of access restrictions, |
| 643 | // try to find a method inherited from an interface in copied methods. |
| 644 | ArtMethod* uninherited_method = nullptr; |
| 645 | ObjPtr<Class> klass = GetSuperClass(); |
| 646 | for (; klass != nullptr; klass = klass->GetSuperClass()) { |
| 647 | ArtMethod* candidate_method = nullptr; |
| 648 | ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size); |
| 649 | if (klass->GetDexCache() == dex_cache) { |
| 650 | // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because |
| 651 | // the type index differs, so compare the name index and proto index. |
| 652 | for (ArtMethod& method : declared_methods) { |
| 653 | const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex()); |
| 654 | if (cmp_method_id.name_idx_ == method_id.name_idx_ && |
| 655 | cmp_method_id.proto_idx_ == method_id.proto_idx_) { |
| 656 | candidate_method = &method; |
| 657 | break; |
| 658 | } |
| 659 | } |
| 660 | } else { |
| 661 | if (!declared_methods.empty() && name.empty()) { |
| 662 | name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 663 | } |
| 664 | for (ArtMethod& method : declared_methods) { |
| 665 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 666 | candidate_method = &method; |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | if (candidate_method != nullptr) { |
| 672 | if (IsInheritedMethod(this, klass, *candidate_method)) { |
| 673 | return candidate_method; |
| 674 | } else { |
| 675 | uninherited_method = candidate_method; |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | // Then search copied methods. |
| 682 | // If we found a method that's not inherited, stop the search in its declaring class. |
| 683 | ObjPtr<Class> end_klass = klass; |
| 684 | DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr); |
| 685 | // After we have searched the declared methods of the super-class chain, |
| 686 | // search copied methods which can contain methods from interfaces. |
| 687 | for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) { |
| 688 | ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size); |
| 689 | if (!copied_methods.empty() && name.empty()) { |
| 690 | name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 691 | } |
| 692 | for (ArtMethod& method : copied_methods) { |
| 693 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 694 | return &method; // No further check needed, copied methods are inherited by definition. |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | return uninherited_method; // Return the `uninherited_method` if any. |
| 699 | } |
| 700 | |
| 701 | ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) { |
| 702 | // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy(). |
| 703 | DCHECK(!IsProxyClass()); |
| 704 | StringPiece name("<init>"); |
| 705 | for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) { |
| 706 | if (method.GetName() == name && method.GetSignature() == signature) { |
| 707 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 708 | } |
| 709 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 710 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 711 | } |
| 712 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 713 | ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name, |
| 714 | PointerSize pointer_size) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 715 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 716 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 717 | if (name == np_method->GetName()) { |
| 718 | return &method; |
| 719 | } |
| 720 | } |
| 721 | return nullptr; |
| 722 | } |
| 723 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 724 | ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name, |
| 725 | PointerSize pointer_size) { |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 726 | for (auto& method : GetVirtualMethods(pointer_size)) { |
| 727 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 728 | if (name == np_method->GetName()) { |
| 729 | return &method; |
| 730 | } |
| 731 | } |
| 732 | return nullptr; |
| 733 | } |
| 734 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 735 | ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 736 | DCHECK(method->GetDeclaringClass()->IsInterface()); |
| 737 | DCHECK(IsInterface()) << "Should only be called on a interface class"; |
| 738 | // Check if we have one defined on this interface first. This includes searching copied ones to |
| 739 | // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We |
| 740 | // don't do any indirect method checks here. |
| 741 | for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) { |
| 742 | if (method->HasSameNameAndSignature(&iface_method)) { |
| 743 | return &iface_method; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | std::vector<ArtMethod*> abstract_methods; |
| 748 | // Search through the IFTable for a working version. We don't need to check for conflicts |
| 749 | // because if there was one it would appear in this classes virtual_methods_ above. |
| 750 | |
| 751 | Thread* self = Thread::Current(); |
| 752 | StackHandleScope<2> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 753 | MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable())); |
| 754 | MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 755 | size_t iftable_count = GetIfTableCount(); |
| 756 | // Find the method. We don't need to check for conflicts because they would have been in the |
| 757 | // copied virtuals of this interface. Order matters, traverse in reverse topological order; most |
| 758 | // subtypiest interfaces get visited first. |
| 759 | for (size_t k = iftable_count; k != 0;) { |
| 760 | k--; |
| 761 | DCHECK_LT(k, iftable->Count()); |
| 762 | iface.Assign(iftable->GetInterface(k)); |
| 763 | // Iterate through every declared method on this interface. Each direct method's name/signature |
| 764 | // is unique so the order of the inner loop doesn't matter. |
| 765 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) { |
| 766 | ArtMethod* current_method = &method_iter; |
| 767 | if (current_method->HasSameNameAndSignature(method)) { |
| 768 | if (current_method->IsDefault()) { |
| 769 | // Handle JLS soft errors, a default method from another superinterface tree can |
| 770 | // "override" an abstract method(s) from another superinterface tree(s). To do this, |
| 771 | // ignore any [default] method which are dominated by the abstract methods we've seen so |
| 772 | // far. Check if overridden by any in abstract_methods. We do not need to check for |
| 773 | // default_conflicts because we would hit those before we get to this loop. |
| 774 | bool overridden = false; |
| 775 | for (ArtMethod* possible_override : abstract_methods) { |
| 776 | DCHECK(possible_override->HasSameNameAndSignature(current_method)); |
| 777 | if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) { |
| 778 | overridden = true; |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | if (!overridden) { |
| 783 | return current_method; |
| 784 | } |
| 785 | } else { |
| 786 | // Is not default. |
| 787 | // This might override another default method. Just stash it for now. |
| 788 | abstract_methods.push_back(current_method); |
| 789 | } |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | // If we reach here we either never found any declaration of the method (in which case |
| 794 | // 'abstract_methods' is empty or we found no non-overriden default methods in which case |
| 795 | // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one |
| 796 | // of these arbitrarily. |
| 797 | return abstract_methods.empty() ? nullptr : abstract_methods[0]; |
| 798 | } |
| 799 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 800 | ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 801 | for (ArtMethod& method : GetDirectMethods(pointer_size)) { |
| 802 | if (method.IsClassInitializer()) { |
| 803 | DCHECK_STREQ(method.GetName(), "<clinit>"); |
| 804 | DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V"); |
| 805 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 806 | } |
| 807 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 808 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 811 | // Custom binary search to avoid double comparisons from std::binary_search. |
| 812 | static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields, |
| 813 | const StringPiece& name, |
| 814 | const StringPiece& type) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 815 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 816 | if (fields == nullptr) { |
| 817 | return nullptr; |
| 818 | } |
| 819 | size_t low = 0; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 820 | size_t high = fields->size(); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 821 | ArtField* ret = nullptr; |
| 822 | while (low < high) { |
| 823 | size_t mid = (low + high) / 2; |
| 824 | ArtField& field = fields->At(mid); |
| 825 | // Fields are sorted by class, then name, then type descriptor. This is verified in dex file |
| 826 | // verifier. There can be multiple fields with the same in the same class name due to proguard. |
| 827 | int result = StringPiece(field.GetName()).Compare(name); |
| 828 | if (result == 0) { |
| 829 | result = StringPiece(field.GetTypeDescriptor()).Compare(type); |
| 830 | } |
| 831 | if (result < 0) { |
| 832 | low = mid + 1; |
| 833 | } else if (result > 0) { |
| 834 | high = mid; |
| 835 | } else { |
| 836 | ret = &field; |
| 837 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 838 | } |
| 839 | } |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 840 | if (kIsDebugBuild) { |
| 841 | ArtField* found = nullptr; |
| 842 | for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) { |
| 843 | if (name == field.GetName() && type == field.GetTypeDescriptor()) { |
| 844 | found = &field; |
| 845 | break; |
| 846 | } |
| 847 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 848 | CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField(); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 849 | } |
| 850 | return ret; |
| 851 | } |
| 852 | |
| 853 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
| 854 | // Binary search by name. Interfaces are not relevant because they can't contain instance fields. |
| 855 | return FindFieldByNameAndType(GetIFieldsPtr(), name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 858 | ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 859 | if (GetDexCache() == dex_cache) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 860 | for (ArtField& field : GetIFields()) { |
| 861 | if (field.GetDexFieldIndex() == dex_field_idx) { |
| 862 | return &field; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 866 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 869 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 870 | // Is the field in this class, or any of its superclasses? |
| 871 | // Interfaces are not relevant because they can't contain instance fields. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 872 | for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 873 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 874 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 875 | return f; |
| 876 | } |
| 877 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 878 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 881 | ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 882 | // Is the field in this class, or any of its superclasses? |
| 883 | // Interfaces are not relevant because they can't contain instance fields. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 884 | for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 885 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 886 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 887 | return f; |
| 888 | } |
| 889 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 890 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 891 | } |
| 892 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 893 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 894 | DCHECK(type != nullptr); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 895 | return FindFieldByNameAndType(GetSFieldsPtr(), name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 896 | } |
| 897 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 898 | ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 899 | if (dex_cache == GetDexCache()) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 900 | for (ArtField& field : GetSFields()) { |
| 901 | if (field.GetDexFieldIndex() == dex_field_idx) { |
| 902 | return &field; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 906 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 907 | } |
| 908 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 909 | ArtField* Class::FindStaticField(Thread* self, |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 910 | ObjPtr<Class> klass, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 911 | const StringPiece& name, |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 912 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 913 | // Is the field in this class (or its interfaces), or any of its |
| 914 | // superclasses (or their interfaces)? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 915 | for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 916 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 917 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 918 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 919 | return f; |
| 920 | } |
| 921 | // Is this field in any of this class' interfaces? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 922 | for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { |
| 923 | ObjPtr<Class> interface = GetDirectInterface(self, k, i); |
| 924 | DCHECK(interface != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 925 | f = FindStaticField(self, interface, name, type); |
| 926 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 927 | return f; |
| 928 | } |
| 929 | } |
| 930 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 931 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 932 | } |
| 933 | |
Vladimir Marko | bb268b1 | 2016-06-30 15:52:56 +0100 | [diff] [blame] | 934 | ArtField* Class::FindStaticField(Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 935 | ObjPtr<Class> klass, |
| 936 | ObjPtr<DexCache> dex_cache, |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 937 | uint32_t dex_field_idx) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 938 | for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 939 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 940 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 941 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 942 | return f; |
| 943 | } |
Vladimir Marko | bb268b1 | 2016-06-30 15:52:56 +0100 | [diff] [blame] | 944 | // Though GetDirectInterface() should not cause thread suspension when called |
| 945 | // from here, it takes a Handle as an argument, so we need to wrap `k`. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 946 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 947 | // Is this field in any of this class' interfaces? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 948 | for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { |
| 949 | ObjPtr<Class> interface = GetDirectInterface(self, k, i); |
| 950 | DCHECK(interface != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 951 | f = FindStaticField(self, interface, dex_cache, dex_field_idx); |
| 952 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 953 | return f; |
| 954 | } |
| 955 | } |
| 956 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 957 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 960 | ArtField* Class::FindField(Thread* self, |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 961 | ObjPtr<Class> klass, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 962 | const StringPiece& name, |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 963 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 964 | // Find a field using the JLS field resolution order |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 965 | for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 966 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 967 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 968 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 969 | return f; |
| 970 | } |
| 971 | f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 972 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 973 | return f; |
| 974 | } |
| 975 | // Is this field in any of this class' interfaces? |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 976 | for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) { |
| 977 | ObjPtr<Class> interface = GetDirectInterface(self, k, i); |
| 978 | DCHECK(interface != nullptr); |
| 979 | f = FindStaticField(self, interface, name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 980 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 981 | return f; |
| 982 | } |
| 983 | } |
| 984 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 985 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 986 | } |
| 987 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 988 | void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 989 | DCHECK(IsVerified()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 990 | for (auto& m : GetMethods(pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 991 | if (!m.IsNative() && m.IsInvokable()) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 992 | m.SetSkipAccessChecks(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 995 | } |
| 996 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 997 | const char* Class::GetDescriptor(std::string* storage) { |
| 998 | if (IsPrimitive()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 999 | return Primitive::Descriptor(GetPrimitiveType()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1000 | } else if (IsArrayClass()) { |
| 1001 | return GetArrayDescriptor(storage); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1002 | } else if (IsProxyClass()) { |
| 1003 | *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1004 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1005 | } else { |
| 1006 | const DexFile& dex_file = GetDexFile(); |
| 1007 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 1008 | return dex_file.GetTypeDescriptor(type_id); |
| 1009 | } |
| 1010 | } |
| 1011 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 1012 | const char* Class::GetArrayDescriptor(std::string* storage) { |
| 1013 | std::string temp; |
| 1014 | const char* elem_desc = GetComponentType()->GetDescriptor(&temp); |
| 1015 | *storage = "["; |
| 1016 | *storage += elem_desc; |
| 1017 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | const DexFile::ClassDef* Class::GetClassDef() { |
| 1021 | uint16_t class_def_idx = GetDexClassDefIndex(); |
| 1022 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 1023 | return nullptr; |
| 1024 | } |
| 1025 | return &GetDexFile().GetClassDef(class_def_idx); |
| 1026 | } |
| 1027 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1028 | dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1029 | DCHECK(!IsPrimitive()); |
| 1030 | DCHECK(!IsArrayClass()); |
| 1031 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 1032 | } |
| 1033 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1034 | ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) { |
| 1035 | DCHECK(klass != nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1036 | DCHECK(!klass->IsPrimitive()); |
| 1037 | if (klass->IsArrayClass()) { |
| 1038 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1039 | // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass(). |
| 1040 | ObjPtr<Class> interface; |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1041 | if (idx == 0) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1042 | interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1043 | } else { |
| 1044 | DCHECK_EQ(1U, idx); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1045 | interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1046 | } |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1047 | DCHECK(interface != nullptr); |
| 1048 | return interface; |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1049 | } else if (klass->IsProxyClass()) { |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 1050 | ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1051 | DCHECK(interfaces != nullptr); |
| 1052 | return interfaces->Get(idx); |
| 1053 | } else { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1054 | dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1055 | ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType( |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1056 | type_idx, klass->GetDexCache(), klass->GetClassLoader()); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1057 | return interface; |
| 1058 | } |
| 1059 | } |
| 1060 | |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1061 | ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) { |
| 1062 | ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx); |
| 1063 | if (interface == nullptr) { |
| 1064 | DCHECK(!klass->IsArrayClass()); |
| 1065 | DCHECK(!klass->IsProxyClass()); |
| 1066 | dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 1067 | interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get()); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 1068 | CHECK(interface != nullptr || self->IsExceptionPending()); |
| 1069 | } |
| 1070 | return interface; |
| 1071 | } |
| 1072 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1073 | ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1074 | DCHECK(klass != nullptr); |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1075 | DCHECK(!klass->IsInterface()); |
| 1076 | DCHECK(!IsInterface()); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1077 | ObjPtr<Class> common_super_class = this; |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1078 | while (!common_super_class->IsAssignableFrom(klass.Get())) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1079 | ObjPtr<Class> old_common = common_super_class; |
Aart Bik | 22deed0 | 2016-04-04 14:19:01 -0700 | [diff] [blame] | 1080 | common_super_class = old_common->GetSuperClass(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1081 | DCHECK(common_super_class != nullptr) << old_common->PrettyClass(); |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1082 | } |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 1083 | return common_super_class; |
| 1084 | } |
| 1085 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1086 | const char* Class::GetSourceFile() { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1087 | const DexFile& dex_file = GetDexFile(); |
| 1088 | const DexFile::ClassDef* dex_class_def = GetClassDef(); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 1089 | if (dex_class_def == nullptr) { |
| 1090 | // Generated classes have no class def. |
| 1091 | return nullptr; |
| 1092 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1093 | return dex_file.GetSourceFile(*dex_class_def); |
| 1094 | } |
| 1095 | |
| 1096 | std::string Class::GetLocation() { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1097 | ObjPtr<DexCache> dex_cache = GetDexCache(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1098 | if (dex_cache != nullptr && !IsProxyClass()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 1099 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 1100 | } |
| 1101 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 1102 | return "generated class"; |
| 1103 | } |
| 1104 | |
| 1105 | const DexFile::TypeList* Class::GetInterfaceTypeList() { |
| 1106 | const DexFile::ClassDef* class_def = GetClassDef(); |
| 1107 | if (class_def == nullptr) { |
| 1108 | return nullptr; |
| 1109 | } |
| 1110 | return GetDexFile().GetInterfacesList(*class_def); |
| 1111 | } |
| 1112 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1113 | void Class::PopulateEmbeddedVTable(PointerSize pointer_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1114 | PointerArray* table = GetVTableDuringLinking(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1115 | CHECK(table != nullptr) << PrettyClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1116 | const size_t table_length = table->GetLength(); |
| 1117 | SetEmbeddedVTableLength(table_length); |
| 1118 | for (size_t i = 0; i < table_length; i++) { |
| 1119 | SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1120 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 1121 | // Keep java.lang.Object class's vtable around for since it's easier |
| 1122 | // to be reused by array classes during their linking. |
| 1123 | if (!IsObjectClass()) { |
| 1124 | SetVTable(nullptr); |
| 1125 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1128 | class ReadBarrierOnNativeRootsVisitor { |
| 1129 | public: |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1130 | void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED, |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1131 | MemberOffset offset ATTRIBUTE_UNUSED, |
| 1132 | bool is_static ATTRIBUTE_UNUSED) const {} |
| 1133 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1134 | void VisitRootIfNonNull(CompressedReference<Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1135 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1136 | if (!root->IsNull()) { |
| 1137 | VisitRoot(root); |
| 1138 | } |
| 1139 | } |
| 1140 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1141 | void VisitRoot(CompressedReference<Object>* root) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1142 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1143 | ObjPtr<Object> old_ref = root->AsMirrorPtr(); |
| 1144 | ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1145 | if (old_ref != new_ref) { |
| 1146 | // Update the field atomically. This may fail if mutator updates before us, but it's ok. |
| 1147 | auto* atomic_root = |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1148 | reinterpret_cast<Atomic<CompressedReference<Object>>*>(root); |
Orion Hodson | 4557b38 | 2018-01-03 11:47:54 +0000 | [diff] [blame] | 1149 | atomic_root->CompareAndSetStrongSequentiallyConsistent( |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1150 | CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()), |
| 1151 | CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr())); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1152 | } |
| 1153 | } |
| 1154 | }; |
| 1155 | |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1156 | // The pre-fence visitor for Class::CopyOf(). |
| 1157 | class CopyClassVisitor { |
| 1158 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1159 | CopyClassVisitor(Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1160 | Handle<Class>* orig, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1161 | size_t new_length, |
| 1162 | size_t copy_bytes, |
| 1163 | ImTable* imt, |
| 1164 | PointerSize pointer_size) |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1165 | : self_(self), orig_(orig), new_length_(new_length), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1166 | copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) { |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1169 | void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1170 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1171 | StackHandleScope<1> hs(self_); |
| 1172 | Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass())); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1173 | Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 1174 | Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1175 | h_new_class_obj->PopulateEmbeddedVTable(pointer_size_); |
| 1176 | h_new_class_obj->SetImt(imt_, pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 1177 | h_new_class_obj->SetClassSize(new_length_); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 1178 | // Visit all of the references to make sure there is no from space references in the native |
| 1179 | // roots. |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1180 | ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences( |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 1181 | ReadBarrierOnNativeRootsVisitor(), VoidFunctor()); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | private: |
| 1185 | Thread* const self_; |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1186 | Handle<Class>* const orig_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1187 | const size_t new_length_; |
| 1188 | const size_t copy_bytes_; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 1189 | ImTable* imt_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1190 | const PointerSize pointer_size_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1191 | DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); |
| 1192 | }; |
| 1193 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1194 | Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1195 | DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); |
| 1196 | // We may get copied by a compacting GC. |
| 1197 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1198 | Handle<Class> h_this(hs.NewHandle(this)); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 1199 | Runtime* runtime = Runtime::Current(); |
| 1200 | gc::Heap* heap = runtime->GetHeap(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1201 | // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() |
| 1202 | // to skip copying the tail part that we will overwrite here. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1203 | CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 1204 | ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker()); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1205 | ObjPtr<Object> new_class = kMovingClasses ? |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 1206 | heap->AllocObject<true>(self, java_lang_Class, new_length, visitor) : |
| 1207 | heap->AllocNonMovableObject<true>(self, java_lang_Class, new_length, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1208 | if (UNLIKELY(new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1209 | self->AssertPendingOOMException(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1210 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1211 | } |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1212 | return new_class->AsClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1213 | } |
| 1214 | |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1215 | bool Class::ProxyDescriptorEquals(const char* match) { |
| 1216 | DCHECK(IsProxyClass()); |
| 1217 | return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match; |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1220 | // TODO: Move this to java_lang_Class.cc? |
| 1221 | ArtMethod* Class::GetDeclaredConstructor( |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1222 | Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) { |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1223 | for (auto& m : GetDirectMethods(pointer_size)) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1224 | // Skip <clinit> which is a static constructor, as well as non constructors. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1225 | if (m.IsStatic() || !m.IsConstructor()) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1226 | continue; |
| 1227 | } |
| 1228 | // May cause thread suspension and exceptions. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1229 | if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1230 | return &m; |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1231 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1232 | if (UNLIKELY(self->IsExceptionPending())) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1233 | return nullptr; |
| 1234 | } |
| 1235 | } |
| 1236 | return nullptr; |
| 1237 | } |
| 1238 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1239 | uint32_t Class::Depth() { |
| 1240 | uint32_t depth = 0; |
Roland Levillain | d32ead2 | 2018-05-30 17:38:21 +0100 | [diff] [blame] | 1241 | for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1242 | depth++; |
| 1243 | } |
| 1244 | return depth; |
| 1245 | } |
| 1246 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 1247 | dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) { |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 1248 | std::string temp; |
| 1249 | const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp)); |
Andreas Gampe | 2722f38 | 2017-06-08 18:03:25 -0700 | [diff] [blame] | 1250 | return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1253 | template <PointerSize kPointerSize, bool kTransactionActive> |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1254 | ObjPtr<Method> Class::GetDeclaredMethodInternal( |
| 1255 | Thread* self, |
| 1256 | ObjPtr<Class> klass, |
| 1257 | ObjPtr<String> name, |
| 1258 | ObjPtr<ObjectArray<Class>> args) { |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1259 | // Covariant return types permit the class to define multiple |
| 1260 | // methods with the same name and parameter types. Prefer to |
| 1261 | // return a non-synthetic method in such situations. We may |
| 1262 | // still return a synthetic method to handle situations like |
| 1263 | // escalated visibility. We never return miranda methods that |
| 1264 | // were synthesized by the runtime. |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1265 | StackHandleScope<3> hs(self); |
| 1266 | auto h_method_name = hs.NewHandle(name); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1267 | if (UNLIKELY(h_method_name == nullptr)) { |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1268 | ThrowNullPointerException("name == null"); |
| 1269 | return nullptr; |
| 1270 | } |
| 1271 | auto h_args = hs.NewHandle(args); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1272 | Handle<Class> h_klass = hs.NewHandle(klass); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1273 | ArtMethod* result = nullptr; |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1274 | for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) { |
| 1275 | auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1276 | // May cause thread suspension. |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 1277 | ObjPtr<String> np_name = np_method->ResolveNameString(); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1278 | if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) { |
| 1279 | if (UNLIKELY(self->IsExceptionPending())) { |
| 1280 | return nullptr; |
| 1281 | } |
| 1282 | continue; |
| 1283 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1284 | if (!m.IsMiranda()) { |
| 1285 | if (!m.IsSynthetic()) { |
| 1286 | return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m); |
| 1287 | } |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1288 | result = &m; // Remember as potential result if it's not a miranda method. |
| 1289 | } |
| 1290 | } |
| 1291 | if (result == nullptr) { |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1292 | for (auto& m : h_klass->GetDirectMethods(kPointerSize)) { |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1293 | auto modifiers = m.GetAccessFlags(); |
| 1294 | if ((modifiers & kAccConstructor) != 0) { |
| 1295 | continue; |
| 1296 | } |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1297 | auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1298 | // May cause thread suspension. |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 1299 | ObjPtr<String> np_name = np_method->ResolveNameString(); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1300 | if (np_name == nullptr) { |
| 1301 | self->AssertPendingException(); |
| 1302 | return nullptr; |
| 1303 | } |
| 1304 | if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) { |
| 1305 | if (UNLIKELY(self->IsExceptionPending())) { |
| 1306 | return nullptr; |
| 1307 | } |
| 1308 | continue; |
| 1309 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1310 | DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods. |
| 1311 | if ((modifiers & kAccSynthetic) == 0) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1312 | return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1313 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 1314 | result = &m; // Remember as potential result. |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | return result != nullptr |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1318 | ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result) |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1319 | : nullptr; |
| 1320 | } |
| 1321 | |
| 1322 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1323 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1324 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1325 | ObjPtr<Class> klass, |
| 1326 | ObjPtr<String> name, |
| 1327 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1328 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1329 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1330 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1331 | ObjPtr<Class> klass, |
| 1332 | ObjPtr<String> name, |
| 1333 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1334 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1335 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1336 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1337 | ObjPtr<Class> klass, |
| 1338 | ObjPtr<String> name, |
| 1339 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1340 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1341 | ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1342 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1343 | ObjPtr<Class> klass, |
| 1344 | ObjPtr<String> name, |
| 1345 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | bc4d218 | 2016-02-22 10:03:12 -0800 | [diff] [blame] | 1346 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1347 | template <PointerSize kPointerSize, bool kTransactionActive> |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1348 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal( |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1349 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1350 | ObjPtr<Class> klass, |
| 1351 | ObjPtr<ObjectArray<Class>> args) { |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1352 | StackHandleScope<1> hs(self); |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1353 | ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize); |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1354 | return result != nullptr |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1355 | ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result) |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1356 | : nullptr; |
| 1357 | } |
| 1358 | |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1359 | // Constructor::CreateFromArtMethod<kTransactionActive>(self, result) |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1360 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1361 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1362 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>( |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1363 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1364 | ObjPtr<Class> klass, |
| 1365 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1366 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1367 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1368 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1369 | ObjPtr<Class> klass, |
| 1370 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1371 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1372 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>( |
Andreas Gampe | e01e364 | 2016-07-25 13:06:04 -0700 | [diff] [blame] | 1373 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1374 | ObjPtr<Class> klass, |
| 1375 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1376 | template |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1377 | ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>( |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1378 | Thread* self, |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 1379 | ObjPtr<Class> klass, |
| 1380 | ObjPtr<ObjectArray<Class>> args); |
Andreas Gampe | 6039e56 | 2016-04-05 18:18:43 -0700 | [diff] [blame] | 1381 | |
Andreas Gampe | 715fdc2 | 2016-04-18 17:07:30 -0700 | [diff] [blame] | 1382 | int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) { |
| 1383 | if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) { |
| 1384 | return default_value; |
| 1385 | } |
| 1386 | uint32_t flags; |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 1387 | if (!annotations::GetInnerClassFlags(h_this, &flags)) { |
Andreas Gampe | 715fdc2 | 2016-04-18 17:07:30 -0700 | [diff] [blame] | 1388 | return default_value; |
| 1389 | } |
| 1390 | return flags; |
| 1391 | } |
| 1392 | |
Mathieu Chartier | 93bbee0 | 2016-08-31 09:38:40 -0700 | [diff] [blame] | 1393 | void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) { |
| 1394 | if (Runtime::Current()->IsActiveTransaction()) { |
| 1395 | SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size); |
| 1396 | } else { |
| 1397 | SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size); |
| 1398 | } |
| 1399 | } |
| 1400 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1401 | std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) { |
| 1402 | if (klass == nullptr) { |
| 1403 | return "null"; |
| 1404 | } |
| 1405 | return klass->PrettyDescriptor(); |
| 1406 | } |
| 1407 | |
| 1408 | std::string Class::PrettyDescriptor() { |
| 1409 | std::string temp; |
| 1410 | return art::PrettyDescriptor(GetDescriptor(&temp)); |
| 1411 | } |
| 1412 | |
| 1413 | std::string Class::PrettyClass(ObjPtr<mirror::Class> c) { |
| 1414 | if (c == nullptr) { |
| 1415 | return "null"; |
| 1416 | } |
| 1417 | return c->PrettyClass(); |
| 1418 | } |
| 1419 | |
| 1420 | std::string Class::PrettyClass() { |
| 1421 | std::string result; |
| 1422 | result += "java.lang.Class<"; |
| 1423 | result += PrettyDescriptor(); |
| 1424 | result += ">"; |
| 1425 | return result; |
| 1426 | } |
| 1427 | |
| 1428 | std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) { |
| 1429 | if (c == nullptr) { |
| 1430 | return "null"; |
| 1431 | } |
| 1432 | return c->PrettyClassAndClassLoader(); |
| 1433 | } |
| 1434 | |
| 1435 | std::string Class::PrettyClassAndClassLoader() { |
| 1436 | std::string result; |
| 1437 | result += "java.lang.Class<"; |
| 1438 | result += PrettyDescriptor(); |
| 1439 | result += ","; |
| 1440 | result += mirror::Object::PrettyTypeOf(GetClassLoader()); |
| 1441 | // TODO: add an identifying hash value for the loader |
| 1442 | result += ">"; |
| 1443 | return result; |
| 1444 | } |
| 1445 | |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 1446 | template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() { |
| 1447 | // Check class is loaded/retired or this is java.lang.String that has a |
| 1448 | // circularity issue during loading the names of its members |
| 1449 | DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() || |
| 1450 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() || |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1451 | this == GetClassRoot<String>()) |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 1452 | << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>() |
| 1453 | << " IsRetired=" << IsRetired<kVerifyFlags>() |
| 1454 | << " IsErroneous=" << |
| 1455 | IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 1456 | << " IsString=" << (this == GetClassRoot<String>()) |
Andreas Gampe | 90b936d | 2017-01-31 08:58:55 -0800 | [diff] [blame] | 1457 | << " status= " << GetStatus<kVerifyFlags>() |
| 1458 | << " descriptor=" << PrettyDescriptor(); |
| 1459 | } |
| 1460 | // Instantiate the common cases. |
| 1461 | template void Class::GetAccessFlagsDCheck<kVerifyNone>(); |
| 1462 | template void Class::GetAccessFlagsDCheck<kVerifyThis>(); |
| 1463 | template void Class::GetAccessFlagsDCheck<kVerifyReads>(); |
| 1464 | template void Class::GetAccessFlagsDCheck<kVerifyWrites>(); |
| 1465 | template void Class::GetAccessFlagsDCheck<kVerifyAll>(); |
| 1466 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1467 | } // namespace mirror |
| 1468 | } // namespace art |