blob: a36fe1253c165c6f6030488499f007496f005e04 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
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 Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_field-inl.h"
22#include "art_method-inl.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080023#include "base/logging.h" // For VLOG.
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "class-inl.h"
Nicolas Geoffrayb476a292019-06-27 07:54:48 +000026#include "class_ext.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010027#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_loader.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010029#include "class_root.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080030#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file-inl.h"
32#include "dex/dex_file_annotations.h"
Andreas Gampead1aa632019-01-02 10:30:54 -080033#include "dex/signature-inl.h"
Vladimir Marko58412b12019-04-01 13:26:34 +010034#include "dex_cache-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/card_table-inl.h"
Andreas Gampee15b9b12018-10-29 12:54:27 -070036#include "gc/heap-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070037#include "handle_scope-inl.h"
David Brazdil4bcd6572019-02-02 20:08:44 +000038#include "hidden_api.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070039#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070040#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070041#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080042#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070044#include "object_lock.h"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010045#include "string-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070046#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "thread.h"
48#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "well_known_classes.h"
50
51namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070052
53// TODO: move to own CC file?
54constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
55constexpr size_t BitString::kCapacity;
56
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057namespace mirror {
58
Andreas Gampe46ee31b2016-12-14 10:11:49 -080059using android::base::StringPrintf;
60
Vladimir Marko7287c4d2018-02-15 10:41:07 +000061ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
62 const char* expected_name = nullptr;
Vladimir Markob4eb1b12018-05-24 11:09:38 +010063 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid.
Vladimir Marko7287c4d2018-02-15 10:41:07 +000064 if (name != nullptr && name->GetLength() >= 2) {
65 // Perfect hash for the expected values: from the second letters of the primitive types,
66 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
67 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
68 switch (hash) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010069 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break;
70 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break;
71 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break;
72 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break;
73 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break;
74 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break;
75 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break;
76 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break;
77 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break;
Vladimir Marko7287c4d2018-02-15 10:41:07 +000078 default: break;
79 }
80 }
81 if (expected_name != nullptr && name->Equals(expected_name)) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010082 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000083 DCHECK(klass != nullptr);
84 return klass;
85 } else {
86 Thread* self = Thread::Current();
87 if (name == nullptr) {
88 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
Andreas Gampe98ea9d92018-10-19 14:06:15 -070089 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg= */ nullptr);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000090 } else {
91 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
92 }
93 return nullptr;
94 }
95}
96
Vladimir Marko3068d582019-05-28 16:39:29 +010097ObjPtr<ClassExt> Class::EnsureExtDataPresent(Handle<Class> h_this, Thread* self) {
98 ObjPtr<ClassExt> existing(h_this->GetExtData());
Alex Light0273ad12016-11-02 11:19:31 -070099 if (!existing.IsNull()) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000100 return existing;
Alex Light0273ad12016-11-02 11:19:31 -0700101 }
Vladimir Marko3068d582019-05-28 16:39:29 +0100102 StackHandleScope<2> hs(self);
Alex Light0273ad12016-11-02 11:19:31 -0700103 // 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 Gampefa4333d2017-02-14 11:10:34 -0800108 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700109 // OOM allocating the classExt.
110 // TODO Should we restore the suppressed exception?
111 self->AssertPendingOOMException();
112 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800113 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700114 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 Chartiera9746b92018-06-22 10:25:40 -0700118 set = h_this->CasFieldObject<true>(ext_offset,
119 nullptr,
120 new_ext.Get(),
121 CASMode::kStrong,
122 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700123 } else {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700124 set = h_this->CasFieldObject<false>(ext_offset,
125 nullptr,
126 new_ext.Get(),
127 CASMode::kStrong,
128 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700129 }
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 Gampefa4333d2017-02-14 11:10:34 -0800134 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700135 self->SetException(throwable.Get());
136 }
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000137 return ret;
Andreas Gampe99babb62015-11-02 16:20:00 -0800138 }
139}
140
Vladimir Marko2c64a832018-01-04 11:31:56 +0000141void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
142 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
144 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700145 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000146 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000147 new_status != ClassStatus::kErrorUnresolved &&
148 new_status != ClassStatus::kErrorResolved &&
149 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700150 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700151 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700152 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000153 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700154 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700155 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700156 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700157 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700158 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000160 if (UNLIKELY(IsErroneous(new_status))) {
161 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700162 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000163 << h_this->PrettyClass()
164 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000165 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700166 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700167 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700168 if (self->IsExceptionPending()) {
169 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
170 }
171 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172
Vladimir Marko3068d582019-05-28 16:39:29 +0100173 ObjPtr<ClassExt> ext(EnsureExtDataPresent(h_this, self));
Alex Light0273ad12016-11-02 11:19:31 -0700174 if (!ext.IsNull()) {
175 self->AssertPendingException();
176 ext->SetVerifyError(self->GetException());
177 } else {
178 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700179 }
180 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 }
Alex Light0273ad12016-11-02 11:19:31 -0700182
Vladimir Marko305c38b2018-02-14 11:50:07 +0000183 if (kBitstringSubtypeCheckEnabled) {
184 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700185 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
186 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000187 } 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 Chartier93bbee02016-08-31 09:38:40 -0700196 }
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 Marko2c64a832018-01-04 11:31:56 +0000201 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700202 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 Chartier93bbee02016-08-31 09:38:40 -0700206 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100207 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700208
Andreas Gampe5b20b352018-10-11 19:03:20 -0700209 if (kIsDebugBuild && new_status >= ClassStatus::kInitialized) {
210 CHECK(h_this->WasVerificationAttempted()) << h_this->PrettyClassAndClassLoader();
211 }
212
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700213 if (!class_linker_initialized) {
214 // When the class linker is being initialized its single threaded and by definition there can be
215 // no waiters. During initialization classes may appear temporary but won't be retired as their
216 // size was statically computed.
217 } else {
218 // Classes that are being resolved or initialized need to notify waiters that the class status
219 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700220 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
222 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000223 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
224 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700225 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226 }
227 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000228 CHECK_NE(new_status, ClassStatus::kRetired);
229 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700230 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231 }
232 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700233 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234}
235
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700236void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700237 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238}
239
Ian Rogersef7d42f2014-01-06 12:55:46 -0800240void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700242 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
243 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700244 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700245 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700246 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247}
248
249// Return the class' name. The exact format is bizarre, but it's the specified behavior for
250// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
251// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
252// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Vladimir Marko179b7c62019-03-22 13:38:57 +0000253ObjPtr<String> Class::ComputeName(Handle<Class> h_this) {
254 ObjPtr<String> name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800255 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 return name;
257 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700258 std::string temp;
259 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800260 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
262 // The descriptor indicates that this is the class for
263 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700264 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 switch (descriptor[0]) {
266 case 'Z': c_name = "boolean"; break;
267 case 'B': c_name = "byte"; break;
268 case 'C': c_name = "char"; break;
269 case 'S': c_name = "short"; break;
270 case 'I': c_name = "int"; break;
271 case 'J': c_name = "long"; break;
272 case 'F': c_name = "float"; break;
273 case 'D': c_name = "double"; break;
274 case 'V': c_name = "void"; break;
275 default:
276 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
277 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800278 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 } else {
280 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
281 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700282 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700284 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 return name;
286}
287
Ian Rogersef7d42f2014-01-06 12:55:46 -0800288void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700290 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 if ((flags & kDumpClassClassLoader) != 0) {
292 os << ' ' << GetClassLoader();
293 }
294 if ((flags & kDumpClassInitialized) != 0) {
295 os << ' ' << GetStatus();
296 }
297 os << "\n";
298 return;
299 }
300
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700302 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700303 Handle<Class> h_this(hs.NewHandle(this));
304 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700306
Ian Rogers1ff3c982014-08-12 02:30:58 -0700307 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700309 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800311 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 os << StringPrintf(" access=0x%04x.%04x\n",
313 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800314 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700315 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700316 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 }
318 if (IsArrayClass()) {
319 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
320 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700321 const size_t num_direct_interfaces = NumDirectInterfaces();
322 if (num_direct_interfaces > 0) {
323 os << " interfaces (" << num_direct_interfaces << "):\n";
324 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000325 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700326 if (interface == nullptr) {
327 os << StringPrintf(" %2zd: nullptr!\n", i);
328 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700329 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
330 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700331 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 }
333 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700334 if (!IsLoaded()) {
335 os << " class not yet loaded";
336 } else {
337 // After this point, this may have moved due to GetDirectInterface.
338 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800339 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700340 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700341 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700342 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700344 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
345 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700346 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700348 }
349 if (h_this->NumStaticFields() > 0) {
350 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000351 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700352 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700353 os << StringPrintf(" %2zd: %s\n", i,
354 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700355 }
356 } else {
357 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700359 }
360 if (h_this->NumInstanceFields() > 0) {
361 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000362 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700363 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700364 os << StringPrintf(" %2zd: %s\n", i,
365 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700366 }
367 } else {
368 os << " <not yet available>";
369 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 }
371 }
372}
373
374void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700375 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 // Sanity check that the number of bits set in the reference offset bitmap
377 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700378 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700379 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 count += c->NumReferenceInstanceFieldsDuringLinking();
381 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700382 // +1 for the Class in Object.
383 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100385 // Not called within a transaction.
386 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700387 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388}
389
Vladimir Markoe027d722019-02-05 10:13:49 +0000390bool Class::IsInSamePackage(std::string_view descriptor1, std::string_view descriptor2) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700392 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
393 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 ++i;
395 }
Vladimir Markoe027d722019-02-05 10:13:49 +0000396 if (descriptor1.find('/', i) != std::string_view::npos ||
397 descriptor2.find('/', i) != std::string_view::npos) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 return false;
399 } else {
400 return true;
401 }
402}
403
Mathieu Chartier3398c782016-09-30 10:27:43 -0700404bool Class::IsInSamePackage(ObjPtr<Class> that) {
405 ObjPtr<Class> klass1 = this;
406 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 if (klass1 == klass2) {
408 return true;
409 }
410 // Class loaders must match.
411 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
412 return false;
413 }
414 // Arrays are in the same package when their element classes are.
415 while (klass1->IsArrayClass()) {
416 klass1 = klass1->GetComponentType();
417 }
418 while (klass2->IsArrayClass()) {
419 klass2 = klass2->GetComponentType();
420 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700421 // trivial check again for array types
422 if (klass1 == klass2) {
423 return true;
424 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700426 std::string temp1, temp2;
427 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428}
429
Ian Rogersef7d42f2014-01-06 12:55:46 -0800430bool Class::IsThrowableClass() {
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100431 return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432}
433
Vladimir Markoba118822017-06-12 15:41:56 +0100434template <typename SignatureType>
435static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000436 std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100437 const SignatureType& signature,
438 PointerSize pointer_size)
439 REQUIRES_SHARED(Locks::mutator_lock_) {
440 // If the current class is not an interface, skip the search of its declared methods;
441 // such lookup is used only to distinguish between IncompatibleClassChangeError and
442 // NoSuchMethodError and the caller has already tried to search methods in the class.
443 if (LIKELY(klass->IsInterface())) {
444 // Search declared methods, both direct and virtual.
445 // (This lookup is used also for invoke-static on interface classes.)
446 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
Eric Holkabdb4592019-05-16 08:33:12 -0700447 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100448 return &method;
449 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 }
451 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452
Vladimir Markoba118822017-06-12 15:41:56 +0100453 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
454 // we should return it, otherwise an arbitrary one can be returned.
455 ObjPtr<IfTable> iftable = klass->GetIfTable();
456 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
457 ObjPtr<Class> iface = iftable->GetInterface(i);
458 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
Eric Holkabdb4592019-05-16 08:33:12 -0700459 if (method.GetNameView() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100460 return &method;
461 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700462 }
463 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800464
Vladimir Markoba118822017-06-12 15:41:56 +0100465 // Then search for public non-static methods in the java.lang.Object.
466 if (LIKELY(klass->IsInterface())) {
467 ObjPtr<Class> object_class = klass->GetSuperClass();
468 DCHECK(object_class->IsObjectClass());
469 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
470 if (method.IsPublic() && !method.IsStatic() &&
Eric Holkabdb4592019-05-16 08:33:12 -0700471 method.GetNameView() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700472 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800473 }
474 }
475 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700476 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477}
478
Vladimir Markoe027d722019-02-05 10:13:49 +0000479ArtMethod* Class::FindInterfaceMethod(std::string_view name,
480 std::string_view signature,
Vladimir Markoba118822017-06-12 15:41:56 +0100481 PointerSize pointer_size) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000482 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483}
484
Vladimir Markoe027d722019-02-05 10:13:49 +0000485ArtMethod* Class::FindInterfaceMethod(std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100486 const Signature& signature,
487 PointerSize pointer_size) {
488 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700489}
490
Vladimir Markoba118822017-06-12 15:41:56 +0100491ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
492 uint32_t dex_method_idx,
493 PointerSize pointer_size) {
494 // We always search by name and signature, ignoring the type index in the MethodId.
495 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800496 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Eric Holkabdb4592019-05-16 08:33:12 -0700497 std::string_view name = dex_file.StringViewByIdx(method_id.name_idx_);
Vladimir Markoba118822017-06-12 15:41:56 +0100498 const Signature signature = dex_file.GetMethodSignature(method_id);
499 return FindInterfaceMethod(name, signature, pointer_size);
500}
501
Alex Lightafb66472017-08-01 09:54:49 -0700502static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
503 ObjPtr<mirror::Class> declaring_class)
504 REQUIRES_SHARED(Locks::mutator_lock_) {
505 if (klass->IsArrayClass()) {
506 return declaring_class->IsObjectClass();
507 } else if (klass->IsInterface()) {
508 return declaring_class->IsObjectClass() || declaring_class == klass;
509 } else {
510 return klass->IsSubClass(declaring_class);
511 }
512}
513
Vladimir Markoba118822017-06-12 15:41:56 +0100514static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
515 ObjPtr<mirror::Class> declaring_class,
516 ArtMethod& method)
517 REQUIRES_SHARED(Locks::mutator_lock_) {
518 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
519 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700520 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100521 uint32_t access_flags = method.GetAccessFlags();
522 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
523 return true;
524 }
525 if ((access_flags & kAccPrivate) != 0) {
526 return false;
527 }
528 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
529 if (!klass->IsInSamePackage(declaring_class)) {
530 return false;
531 }
532 }
533 return true;
534}
535
536template <typename SignatureType>
537static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000538 std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100539 const SignatureType& signature,
540 PointerSize pointer_size)
541 REQUIRES_SHARED(Locks::mutator_lock_) {
542 // Search declared methods first.
543 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
544 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
545 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
546 return &method;
547 }
548 }
549
550 // Then search the superclass chain. If we find an inherited method, return it.
551 // If we find a method that's not inherited because of access restrictions,
552 // try to find a method inherited from an interface in copied methods.
553 ObjPtr<Class> klass = this_klass->GetSuperClass();
554 ArtMethod* uninherited_method = nullptr;
555 for (; klass != nullptr; klass = klass->GetSuperClass()) {
556 DCHECK(!klass->IsProxyClass());
557 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
558 if (method.GetName() == name && method.GetSignature() == signature) {
559 if (IsInheritedMethod(this_klass, klass, method)) {
560 return &method;
561 }
562 uninherited_method = &method;
563 break;
564 }
565 }
566 if (uninherited_method != nullptr) {
567 break;
568 }
569 }
570
571 // Then search copied methods.
572 // If we found a method that's not inherited, stop the search in its declaring class.
573 ObjPtr<Class> end_klass = klass;
574 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
575 klass = this_klass;
576 if (UNLIKELY(klass->IsProxyClass())) {
577 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
578 klass = klass->GetSuperClass();
579 }
580 for (; klass != end_klass; klass = klass->GetSuperClass()) {
581 DCHECK(!klass->IsProxyClass());
582 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
583 if (method.GetName() == name && method.GetSignature() == signature) {
584 return &method; // No further check needed, copied methods are inherited by definition.
585 }
586 }
587 }
588 return uninherited_method; // Return the `uninherited_method` if any.
589}
590
591
Vladimir Markoe027d722019-02-05 10:13:49 +0000592ArtMethod* Class::FindClassMethod(std::string_view name,
593 std::string_view signature,
Vladimir Markoba118822017-06-12 15:41:56 +0100594 PointerSize pointer_size) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000595 return FindClassMethodWithSignature(this, name, signature, pointer_size);
Vladimir Markoba118822017-06-12 15:41:56 +0100596}
597
Vladimir Markoe027d722019-02-05 10:13:49 +0000598ArtMethod* Class::FindClassMethod(std::string_view name,
Vladimir Markoba118822017-06-12 15:41:56 +0100599 const Signature& signature,
600 PointerSize pointer_size) {
601 return FindClassMethodWithSignature(this, name, signature, pointer_size);
602}
603
604ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
605 uint32_t dex_method_idx,
606 PointerSize pointer_size) {
607 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
608 DCHECK(!IsProxyClass());
609
610 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
611 ObjPtr<DexCache> this_dex_cache = GetDexCache();
612 if (this_dex_cache == dex_cache) {
613 // Lookup is always performed in the class referenced by the MethodId.
614 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
615 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
616 if (method.GetDexMethodIndex() == dex_method_idx) {
617 return &method;
618 }
619 }
620 }
621 // If not found, we need to search by name and signature.
622 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800623 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +0100624 const Signature signature = dex_file.GetMethodSignature(method_id);
Vladimir Markoe027d722019-02-05 10:13:49 +0000625 std::string_view name; // Delay strlen() until actually needed.
Vladimir Markoba118822017-06-12 15:41:56 +0100626 // If we do not have a dex_cache match, try to find the declared method in this class now.
627 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
628 DCHECK(name.empty());
David Srbecky39d8c872018-11-01 16:44:20 +0000629 // Avoid string comparisons by comparing the respective unicode lengths first.
630 uint32_t length, other_length; // UTF16 length.
631 name = dex_file.GetMethodName(method_id, &length);
Vladimir Markoba118822017-06-12 15:41:56 +0100632 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
David Srbecky39d8c872018-11-01 16:44:20 +0000633 DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex);
634 const char* other_name = method.GetDexFile()->GetMethodName(
635 method.GetDexMethodIndex(), &other_length);
636 if (length == other_length && name == other_name && signature == method.GetSignature()) {
Vladimir Markoba118822017-06-12 15:41:56 +0100637 return &method;
638 }
639 }
640 }
641
642 // Then search the superclass chain. If we find an inherited method, return it.
643 // If we find a method that's not inherited because of access restrictions,
644 // try to find a method inherited from an interface in copied methods.
645 ArtMethod* uninherited_method = nullptr;
646 ObjPtr<Class> klass = GetSuperClass();
647 for (; klass != nullptr; klass = klass->GetSuperClass()) {
648 ArtMethod* candidate_method = nullptr;
649 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
650 if (klass->GetDexCache() == dex_cache) {
651 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
652 // the type index differs, so compare the name index and proto index.
653 for (ArtMethod& method : declared_methods) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800654 const dex::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
Vladimir Markoba118822017-06-12 15:41:56 +0100655 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
656 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
657 candidate_method = &method;
658 break;
659 }
660 }
661 } else {
662 if (!declared_methods.empty() && name.empty()) {
663 name = dex_file.StringDataByIdx(method_id.name_idx_);
664 }
665 for (ArtMethod& method : declared_methods) {
666 if (method.GetName() == name && method.GetSignature() == signature) {
667 candidate_method = &method;
668 break;
669 }
670 }
671 }
672 if (candidate_method != nullptr) {
673 if (IsInheritedMethod(this, klass, *candidate_method)) {
674 return candidate_method;
675 } else {
676 uninherited_method = candidate_method;
677 break;
678 }
679 }
680 }
681
682 // Then search copied methods.
683 // If we found a method that's not inherited, stop the search in its declaring class.
684 ObjPtr<Class> end_klass = klass;
685 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
686 // After we have searched the declared methods of the super-class chain,
687 // search copied methods which can contain methods from interfaces.
688 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
689 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
690 if (!copied_methods.empty() && name.empty()) {
691 name = dex_file.StringDataByIdx(method_id.name_idx_);
692 }
693 for (ArtMethod& method : copied_methods) {
694 if (method.GetName() == name && method.GetSignature() == signature) {
695 return &method; // No further check needed, copied methods are inherited by definition.
696 }
697 }
698 }
699 return uninherited_method; // Return the `uninherited_method` if any.
700}
701
Vladimir Markoe027d722019-02-05 10:13:49 +0000702ArtMethod* Class::FindConstructor(std::string_view signature, PointerSize pointer_size) {
Vladimir Markoba118822017-06-12 15:41:56 +0100703 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
704 DCHECK(!IsProxyClass());
Vladimir Markoe027d722019-02-05 10:13:49 +0000705 std::string_view name("<init>");
Vladimir Markoba118822017-06-12 15:41:56 +0100706 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
Vladimir Markoeb37ba52019-02-05 14:10:38 +0000707 if (method.GetName() == name && method.GetSignature() == signature) {
Vladimir Markoba118822017-06-12 15:41:56 +0100708 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800709 }
710 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700711 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800712}
713
Vladimir Markoe027d722019-02-05 10:13:49 +0000714ArtMethod* Class::FindDeclaredDirectMethodByName(std::string_view name, PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000715 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
Vladimir Markoe027d722019-02-05 10:13:49 +0000724ArtMethod* Class::FindDeclaredVirtualMethodByName(std::string_view name, PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000725 for (auto& method : GetVirtualMethods(pointer_size)) {
726 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
727 if (name == np_method->GetName()) {
728 return &method;
729 }
730 }
731 return nullptr;
732}
733
Andreas Gampe542451c2016-07-26 09:02:02 -0700734ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700735 DCHECK(method->GetDeclaringClass()->IsInterface());
736 DCHECK(IsInterface()) << "Should only be called on a interface class";
737 // Check if we have one defined on this interface first. This includes searching copied ones to
738 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
739 // don't do any indirect method checks here.
740 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
741 if (method->HasSameNameAndSignature(&iface_method)) {
742 return &iface_method;
743 }
744 }
745
746 std::vector<ArtMethod*> abstract_methods;
747 // Search through the IFTable for a working version. We don't need to check for conflicts
748 // because if there was one it would appear in this classes virtual_methods_ above.
749
750 Thread* self = Thread::Current();
751 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700752 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
753 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700754 size_t iftable_count = GetIfTableCount();
755 // Find the method. We don't need to check for conflicts because they would have been in the
756 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
757 // subtypiest interfaces get visited first.
758 for (size_t k = iftable_count; k != 0;) {
759 k--;
760 DCHECK_LT(k, iftable->Count());
761 iface.Assign(iftable->GetInterface(k));
762 // Iterate through every declared method on this interface. Each direct method's name/signature
763 // is unique so the order of the inner loop doesn't matter.
764 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
765 ArtMethod* current_method = &method_iter;
766 if (current_method->HasSameNameAndSignature(method)) {
767 if (current_method->IsDefault()) {
768 // Handle JLS soft errors, a default method from another superinterface tree can
769 // "override" an abstract method(s) from another superinterface tree(s). To do this,
770 // ignore any [default] method which are dominated by the abstract methods we've seen so
771 // far. Check if overridden by any in abstract_methods. We do not need to check for
772 // default_conflicts because we would hit those before we get to this loop.
773 bool overridden = false;
774 for (ArtMethod* possible_override : abstract_methods) {
775 DCHECK(possible_override->HasSameNameAndSignature(current_method));
776 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
777 overridden = true;
778 break;
779 }
780 }
781 if (!overridden) {
782 return current_method;
783 }
784 } else {
785 // Is not default.
786 // This might override another default method. Just stash it for now.
787 abstract_methods.push_back(current_method);
788 }
789 }
790 }
791 }
792 // If we reach here we either never found any declaration of the method (in which case
793 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
794 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
795 // of these arbitrarily.
796 return abstract_methods.empty() ? nullptr : abstract_methods[0];
797}
798
Andreas Gampe542451c2016-07-26 09:02:02 -0700799ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
801 if (method.IsClassInitializer()) {
802 DCHECK_STREQ(method.GetName(), "<clinit>");
803 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
804 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700805 }
806 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700807 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700808}
809
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700810// Custom binary search to avoid double comparisons from std::binary_search.
811static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
Vladimir Markoe027d722019-02-05 10:13:49 +0000812 std::string_view name,
813 std::string_view type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700814 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700815 if (fields == nullptr) {
816 return nullptr;
817 }
818 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100819 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700820 ArtField* ret = nullptr;
821 while (low < high) {
822 size_t mid = (low + high) / 2;
823 ArtField& field = fields->At(mid);
824 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
825 // verifier. There can be multiple fields with the same in the same class name due to proguard.
Vladimir Markoe027d722019-02-05 10:13:49 +0000826 // Note: std::string_view::compare() uses lexicographical comparison and treats the `char` as
827 // unsigned; for modified-UTF-8 without embedded nulls this is consistent with the
828 // CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues() ordering.
829 int result = std::string_view(field.GetName()).compare(name);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700830 if (result == 0) {
Vladimir Markoe027d722019-02-05 10:13:49 +0000831 result = std::string_view(field.GetTypeDescriptor()).compare(type);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700832 }
833 if (result < 0) {
834 low = mid + 1;
835 } else if (result > 0) {
836 high = mid;
837 } else {
838 ret = &field;
839 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800840 }
841 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700842 if (kIsDebugBuild) {
843 ArtField* found = nullptr;
844 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
845 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
846 found = &field;
847 break;
848 }
849 }
David Sehr709b0702016-10-13 09:12:37 -0700850 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700851 }
852 return ret;
853}
854
Vladimir Markoe027d722019-02-05 10:13:49 +0000855ArtField* Class::FindDeclaredInstanceField(std::string_view name, std::string_view type) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700856 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
857 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800858}
859
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700860ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800861 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700862 for (ArtField& field : GetIFields()) {
863 if (field.GetDexFieldIndex() == dex_field_idx) {
864 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800865 }
866 }
867 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700868 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800869}
870
Vladimir Markoe027d722019-02-05 10:13:49 +0000871ArtField* Class::FindInstanceField(std::string_view name, std::string_view type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800872 // Is the field in this class, or any of its superclasses?
873 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700874 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700875 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700876 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800877 return f;
878 }
879 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700880 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800881}
882
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700883ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800884 // Is the field in this class, or any of its superclasses?
885 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700886 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700887 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700888 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800889 return f;
890 }
891 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700892 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800893}
894
Vladimir Markoe027d722019-02-05 10:13:49 +0000895ArtField* Class::FindDeclaredStaticField(std::string_view name, std::string_view type) {
896 DCHECK(!type.empty());
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700897 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800898}
899
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700900ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700902 for (ArtField& field : GetSFields()) {
903 if (field.GetDexFieldIndex() == dex_field_idx) {
904 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800905 }
906 }
907 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700908 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800909}
910
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700911ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000912 ObjPtr<Class> klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000913 std::string_view name,
914 std::string_view type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800915 // Is the field in this class (or its interfaces), or any of its
916 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000917 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800918 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700919 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700920 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800921 return f;
922 }
923 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000924 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
925 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
926 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700927 f = FindStaticField(self, interface, name, type);
928 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800929 return f;
930 }
931 }
932 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700933 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800934}
935
Vladimir Markobb268b12016-06-30 15:52:56 +0100936ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700937 ObjPtr<Class> klass,
938 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700939 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700940 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800941 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700942 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700943 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800944 return f;
945 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100946 // Though GetDirectInterface() should not cause thread suspension when called
947 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700948 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800949 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000950 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
951 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
952 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700953 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
954 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800955 return f;
956 }
957 }
958 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700959 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800960}
961
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700962ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000963 ObjPtr<Class> klass,
Vladimir Markoe027d722019-02-05 10:13:49 +0000964 std::string_view name,
965 std::string_view type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800966 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000967 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800968 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700969 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700970 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800971 return f;
972 }
973 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700974 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800975 return f;
976 }
977 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000978 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
979 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
980 DCHECK(interface != nullptr);
981 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700982 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800983 return f;
984 }
985 }
986 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700987 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800988}
989
Andreas Gampe542451c2016-07-26 09:02:02 -0700990void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700991 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800992 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700993 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800994 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700995 }
996 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200997}
998
Ian Rogers1ff3c982014-08-12 02:30:58 -0700999const char* Class::GetDescriptor(std::string* storage) {
Vladimir Marko3892e622019-03-15 15:22:18 +00001000 size_t dim = 0u;
1001 ObjPtr<mirror::Class> klass = this;
1002 while (klass->IsArrayClass()) {
1003 ++dim;
Vladimir Markod355acf2019-03-21 17:09:40 +00001004 // No read barrier needed, we're reading a chain of constant references for comparison
1005 // with null. Then we follow up below with reading constant references to read constant
1006 // primitive data in both proxy and non-proxy paths. See ReadBarrierOption.
1007 klass = klass->GetComponentType<kDefaultVerifyFlags, kWithoutReadBarrier>();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001008 }
Vladimir Marko3892e622019-03-15 15:22:18 +00001009 if (klass->IsProxyClass()) {
1010 // No read barrier needed, the `name` field is constant for proxy classes and
1011 // the contents of the String are also constant. See ReadBarrierOption.
1012 ObjPtr<mirror::String> name = klass->GetName<kVerifyNone, kWithoutReadBarrier>();
1013 DCHECK(name != nullptr);
1014 *storage = DotToDescriptor(name->ToModifiedUtf8().c_str());
1015 } else {
1016 const char* descriptor;
1017 if (klass->IsPrimitive()) {
1018 descriptor = Primitive::Descriptor(klass->GetPrimitiveType());
1019 } else {
1020 const DexFile& dex_file = klass->GetDexFile();
1021 const dex::TypeId& type_id = dex_file.GetTypeId(klass->GetDexTypeIndex());
1022 descriptor = dex_file.GetTypeDescriptor(type_id);
1023 }
1024 if (dim == 0) {
1025 return descriptor;
1026 }
1027 *storage = descriptor;
1028 }
1029 storage->insert(0u, dim, '[');
Ian Rogers1ff3c982014-08-12 02:30:58 -07001030 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001031}
1032
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001033const dex::ClassDef* Class::GetClassDef() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001034 uint16_t class_def_idx = GetDexClassDefIndex();
1035 if (class_def_idx == DexFile::kDexNoIndex16) {
1036 return nullptr;
1037 }
1038 return &GetDexFile().GetClassDef(class_def_idx);
1039}
1040
Andreas Gampea5b09a62016-11-17 15:21:22 -08001041dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001042 DCHECK(!IsPrimitive());
1043 DCHECK(!IsArrayClass());
1044 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1045}
1046
Vladimir Marko19a4d372016-12-08 14:41:46 +00001047ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1048 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001049 DCHECK(!klass->IsPrimitive());
1050 if (klass->IsArrayClass()) {
1051 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001052 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1053 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001054 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001055 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001056 } else {
1057 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001058 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001059 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001060 DCHECK(interface != nullptr);
1061 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001062 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001063 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001064 DCHECK(interfaces != nullptr);
1065 return interfaces->Get(idx);
1066 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001067 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001068 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001069 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001070 return interface;
1071 }
1072}
1073
Vladimir Marko19a4d372016-12-08 14:41:46 +00001074ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1075 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1076 if (interface == nullptr) {
1077 DCHECK(!klass->IsArrayClass());
1078 DCHECK(!klass->IsProxyClass());
1079 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001080 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001081 CHECK(interface != nullptr || self->IsExceptionPending());
1082 }
1083 return interface;
1084}
1085
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001086ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001087 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001088 DCHECK(!klass->IsInterface());
1089 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001090 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001091 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001092 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001093 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001094 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001095 }
Calin Juravle52503d82015-11-11 16:58:31 +00001096 return common_super_class;
1097}
1098
Mathieu Chartierf8322842014-05-16 10:59:25 -07001099const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001100 const DexFile& dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001101 const dex::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001102 if (dex_class_def == nullptr) {
1103 // Generated classes have no class def.
1104 return nullptr;
1105 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001106 return dex_file.GetSourceFile(*dex_class_def);
1107}
1108
1109std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001110 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001111 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001112 return dex_cache->GetLocation()->ToModifiedUtf8();
1113 }
1114 // Arrays and proxies are generated and have no corresponding dex file location.
1115 return "generated class";
1116}
1117
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001118const dex::TypeList* Class::GetInterfaceTypeList() {
1119 const dex::ClassDef* class_def = GetClassDef();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001120 if (class_def == nullptr) {
1121 return nullptr;
1122 }
1123 return GetDexFile().GetInterfacesList(*class_def);
1124}
1125
Andreas Gampe542451c2016-07-26 09:02:02 -07001126void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Vladimir Markoc524e9e2019-03-26 10:54:50 +00001127 ObjPtr<PointerArray> table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001128 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129 const size_t table_length = table->GetLength();
1130 SetEmbeddedVTableLength(table_length);
1131 for (size_t i = 0; i < table_length; i++) {
1132 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001133 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001134 // Keep java.lang.Object class's vtable around for since it's easier
1135 // to be reused by array classes during their linking.
1136 if (!IsObjectClass()) {
1137 SetVTable(nullptr);
1138 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001139}
1140
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001141class ReadBarrierOnNativeRootsVisitor {
1142 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001143 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001144 MemberOffset offset ATTRIBUTE_UNUSED,
1145 bool is_static ATTRIBUTE_UNUSED) const {}
1146
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001147 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001148 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001149 if (!root->IsNull()) {
1150 VisitRoot(root);
1151 }
1152 }
1153
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001154 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001155 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001156 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1157 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001158 if (old_ref != new_ref) {
1159 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1160 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001161 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001162 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001163 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1164 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001165 }
1166 }
1167};
1168
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001169// The pre-fence visitor for Class::CopyOf().
1170class CopyClassVisitor {
1171 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001172 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001173 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001174 size_t new_length,
1175 size_t copy_bytes,
1176 ImTable* imt,
1177 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001178 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001179 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001180 }
1181
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001182 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001183 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001184 StackHandleScope<1> hs(self_);
1185 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001186 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001187 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001188 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1189 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001190 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001191 // Visit all of the references to make sure there is no from space references in the native
1192 // roots.
Vladimir Markod7e9bbf2019-03-28 13:18:57 +00001193 h_new_class_obj->Object::VisitReferences(ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001194 }
1195
1196 private:
1197 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001198 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001199 const size_t new_length_;
1200 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001201 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001202 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001203 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1204};
1205
Vladimir Marko3068d582019-05-28 16:39:29 +01001206ObjPtr<Class> Class::CopyOf(Handle<Class> h_this,
1207 Thread* self,
1208 int32_t new_length,
1209 ImTable* imt,
1210 PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001211 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1212 // We may get copied by a compacting GC.
Vladimir Marko317892b2018-05-31 11:11:32 +01001213 Runtime* runtime = Runtime::Current();
1214 gc::Heap* heap = runtime->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001215 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1216 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001217 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Vladimir Marko317892b2018-05-31 11:11:32 +01001218 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001219 ObjPtr<Object> new_class = kMovingClasses ?
Vladimir Marko991cd5c2019-05-30 14:23:39 +01001220 heap->AllocObject(self, java_lang_Class, new_length, visitor) :
1221 heap->AllocNonMovableObject(self, java_lang_Class, new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001222 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001223 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001224 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001225 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001226 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001227}
1228
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001229bool Class::ProxyDescriptorEquals(const char* match) {
1230 DCHECK(IsProxyClass());
Vladimir Marko3892e622019-03-15 15:22:18 +00001231 std::string storage;
1232 const char* descriptor = GetDescriptor(&storage);
1233 DCHECK(descriptor == storage.c_str());
1234 return storage == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001235}
1236
Mathieu Chartiere401d142015-04-22 13:56:20 -07001237// TODO: Move this to java_lang_Class.cc?
1238ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001239 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001240 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001241 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001242 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001243 continue;
1244 }
1245 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001246 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001247 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001248 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001249 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001250 return nullptr;
1251 }
1252 }
1253 return nullptr;
1254}
1255
Mathieu Chartiere401d142015-04-22 13:56:20 -07001256uint32_t Class::Depth() {
1257 uint32_t depth = 0;
Roland Levillaind32ead22018-05-30 17:38:21 +01001258 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001259 depth++;
1260 }
1261 return depth;
1262}
1263
Andreas Gampea5b09a62016-11-17 15:21:22 -08001264dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001265 std::string temp;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001266 const dex::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001267 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001268}
1269
David Brazdil4bcd6572019-02-02 20:08:44 +00001270ALWAYS_INLINE
1271static bool IsMethodPreferredOver(ArtMethod* orig_method,
1272 bool orig_method_hidden,
1273 ArtMethod* new_method,
1274 bool new_method_hidden) {
1275 DCHECK(new_method != nullptr);
1276
1277 // Is this the first result?
1278 if (orig_method == nullptr) {
1279 return true;
1280 }
1281
1282 // Original method is hidden, the new one is not?
1283 if (orig_method_hidden && !new_method_hidden) {
1284 return true;
1285 }
1286
1287 // We iterate over virtual methods first and then over direct ones,
1288 // so we can never be in situation where `orig_method` is direct and
1289 // `new_method` is virtual.
1290 DCHECK(!orig_method->IsDirect() || new_method->IsDirect());
1291
1292 // Original method is synthetic, the new one is not?
1293 if (orig_method->IsSynthetic() && !new_method->IsSynthetic()) {
1294 return true;
1295 }
1296
1297 return false;
1298}
1299
Andreas Gampe542451c2016-07-26 09:02:02 -07001300template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001301ObjPtr<Method> Class::GetDeclaredMethodInternal(
1302 Thread* self,
1303 ObjPtr<Class> klass,
1304 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001305 ObjPtr<ObjectArray<Class>> args,
1306 const std::function<hiddenapi::AccessContext()>& fn_get_access_context) {
1307 // Covariant return types (or smali) permit the class to define
1308 // multiple methods with the same name and parameter types.
1309 // Prefer (in decreasing order of importance):
1310 // 1) non-hidden method over hidden
1311 // 2) virtual methods over direct
1312 // 3) non-synthetic methods over synthetic
1313 // We never return miranda methods that were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001314 StackHandleScope<3> hs(self);
1315 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001316 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001317 ThrowNullPointerException("name == null");
1318 return nullptr;
1319 }
1320 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001321 Handle<Class> h_klass = hs.NewHandle(klass);
David Brazdil4bcd6572019-02-02 20:08:44 +00001322 constexpr hiddenapi::AccessMethod access_method = hiddenapi::AccessMethod::kNone;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001323 ArtMethod* result = nullptr;
David Brazdil4bcd6572019-02-02 20:08:44 +00001324 bool result_hidden = false;
Andreas Gampee01e3642016-07-25 13:06:04 -07001325 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
David Brazdil4bcd6572019-02-02 20:08:44 +00001326 if (m.IsMiranda()) {
1327 continue;
1328 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001329 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001330 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001331 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001332 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1333 if (UNLIKELY(self->IsExceptionPending())) {
1334 return nullptr;
1335 }
1336 continue;
1337 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001338 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1339 if (!m_hidden && !m.IsSynthetic()) {
1340 // Non-hidden, virtual, non-synthetic. Best possible result, exit early.
1341 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1342 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1343 // Remember as potential result.
1344 result = &m;
1345 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001346 }
1347 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001348
1349 if ((result != nullptr) && !result_hidden) {
1350 // We have not found a non-hidden, virtual, non-synthetic method, but
1351 // if we have found a non-hidden, virtual, synthetic method, we cannot
1352 // do better than that later.
1353 DCHECK(!result->IsDirect());
1354 DCHECK(result->IsSynthetic());
1355 } else {
Andreas Gampee01e3642016-07-25 13:06:04 -07001356 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001357 auto modifiers = m.GetAccessFlags();
1358 if ((modifiers & kAccConstructor) != 0) {
1359 continue;
1360 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001361 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001362 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001363 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001364 if (np_name == nullptr) {
1365 self->AssertPendingException();
1366 return nullptr;
1367 }
1368 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1369 if (UNLIKELY(self->IsExceptionPending())) {
1370 return nullptr;
1371 }
1372 continue;
1373 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001374 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
David Brazdil4bcd6572019-02-02 20:08:44 +00001375 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1376 if (!m_hidden && !m.IsSynthetic()) {
1377 // Non-hidden, direct, non-synthetic. Any virtual result could only have been
1378 // hidden, therefore this is the best possible match. Exit now.
1379 DCHECK((result == nullptr) || result_hidden);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001380 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
David Brazdil4bcd6572019-02-02 20:08:44 +00001381 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1382 // Remember as potential result.
1383 result = &m;
1384 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001385 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001386 }
1387 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001388
Andreas Gampebc4d2182016-02-22 10:03:12 -08001389 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001390 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001391 : nullptr;
1392}
1393
1394template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001395ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001396 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001397 ObjPtr<Class> klass,
1398 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001399 ObjPtr<ObjectArray<Class>> args,
1400 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001401template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001402ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001403 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001404 ObjPtr<Class> klass,
1405 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001406 ObjPtr<ObjectArray<Class>> args,
1407 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -07001408template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001409ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001410 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001411 ObjPtr<Class> klass,
1412 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001413 ObjPtr<ObjectArray<Class>> args,
1414 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -07001415template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001416ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001417 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001418 ObjPtr<Class> klass,
1419 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001420 ObjPtr<ObjectArray<Class>> args,
1421 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001422
Andreas Gampe542451c2016-07-26 09:02:02 -07001423template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001424ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001425 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001426 ObjPtr<Class> klass,
1427 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001428 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001429 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001430 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001431 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001432 : nullptr;
1433}
1434
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001435// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001436
Andreas Gampe542451c2016-07-26 09:02:02 -07001437template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001438ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001439 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001440 ObjPtr<Class> klass,
1441 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001442template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001443ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001444 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001445 ObjPtr<Class> klass,
1446 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001447template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001448ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001449 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001450 ObjPtr<Class> klass,
1451 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001452template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001453ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001454 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001455 ObjPtr<Class> klass,
1456 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001457
Andreas Gampe715fdc22016-04-18 17:07:30 -07001458int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1459 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1460 return default_value;
1461 }
1462 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001463 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001464 return default_value;
1465 }
1466 return flags;
1467}
1468
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001469void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1470 if (Runtime::Current()->IsActiveTransaction()) {
1471 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1472 } else {
1473 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1474 }
1475}
1476
David Sehr709b0702016-10-13 09:12:37 -07001477std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1478 if (klass == nullptr) {
1479 return "null";
1480 }
1481 return klass->PrettyDescriptor();
1482}
1483
1484std::string Class::PrettyDescriptor() {
1485 std::string temp;
1486 return art::PrettyDescriptor(GetDescriptor(&temp));
1487}
1488
1489std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1490 if (c == nullptr) {
1491 return "null";
1492 }
1493 return c->PrettyClass();
1494}
1495
1496std::string Class::PrettyClass() {
1497 std::string result;
1498 result += "java.lang.Class<";
1499 result += PrettyDescriptor();
1500 result += ">";
1501 return result;
1502}
1503
1504std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1505 if (c == nullptr) {
1506 return "null";
1507 }
1508 return c->PrettyClassAndClassLoader();
1509}
1510
1511std::string Class::PrettyClassAndClassLoader() {
1512 std::string result;
1513 result += "java.lang.Class<";
1514 result += PrettyDescriptor();
1515 result += ",";
1516 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1517 // TODO: add an identifying hash value for the loader
1518 result += ">";
1519 return result;
1520}
1521
Andreas Gampe90b936d2017-01-31 08:58:55 -08001522template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1523 // Check class is loaded/retired or this is java.lang.String that has a
1524 // circularity issue during loading the names of its members
1525 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1526 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001527 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001528 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1529 << " IsRetired=" << IsRetired<kVerifyFlags>()
1530 << " IsErroneous=" <<
1531 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001532 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001533 << " status= " << GetStatus<kVerifyFlags>()
1534 << " descriptor=" << PrettyDescriptor();
1535}
1536// Instantiate the common cases.
1537template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1538template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1539template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1540template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1541template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1542
Andreas Gampe62f6e902018-10-11 18:58:50 -07001543void Class::SetAccessFlagsDCheck(uint32_t new_access_flags) {
1544 uint32_t old_access_flags = GetField32<kVerifyNone>(AccessFlagsOffset());
1545 // kAccVerificationAttempted is retained.
1546 CHECK((old_access_flags & kAccVerificationAttempted) == 0 ||
1547 (new_access_flags & kAccVerificationAttempted) != 0);
1548}
1549
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001550} // namespace mirror
1551} // namespace art