blob: 26dba024c640a6f9f94c0fd58844aa0114d90930 [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"
Alex Lightd6251582016-10-31 11:12:30 -070026#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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "dex_cache.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070035#include "handle_scope-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070036#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070037#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070038#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080039#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070041#include "object_lock.h"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010042#include "string-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070043#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "thread.h"
45#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "well_known_classes.h"
47
48namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070049
50// TODO: move to own CC file?
51constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
52constexpr size_t BitString::kCapacity;
53
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054namespace mirror {
55
Andreas Gampe46ee31b2016-12-14 10:11:49 -080056using android::base::StringPrintf;
57
Vladimir Marko7287c4d2018-02-15 10:41:07 +000058ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
59 const char* expected_name = nullptr;
Vladimir Markob4eb1b12018-05-24 11:09:38 +010060 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid.
Vladimir Marko7287c4d2018-02-15 10:41:07 +000061 if (name != nullptr && name->GetLength() >= 2) {
62 // Perfect hash for the expected values: from the second letters of the primitive types,
63 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
64 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
65 switch (hash) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010066 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break;
67 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break;
68 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break;
69 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break;
70 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break;
71 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break;
72 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break;
73 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break;
74 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break;
Vladimir Marko7287c4d2018-02-15 10:41:07 +000075 default: break;
76 }
77 }
78 if (expected_name != nullptr && name->Equals(expected_name)) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010079 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000080 DCHECK(klass != nullptr);
81 return klass;
82 } else {
83 Thread* self = Thread::Current();
84 if (name == nullptr) {
85 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
86 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg */ nullptr);
87 } else {
88 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
89 }
90 return nullptr;
91 }
92}
93
Alex Light0273ad12016-11-02 11:19:31 -070094ClassExt* Class::EnsureExtDataPresent(Thread* self) {
95 ObjPtr<ClassExt> existing(GetExtData());
96 if (!existing.IsNull()) {
97 return existing.Ptr();
98 }
99 StackHandleScope<3> hs(self);
100 // Handlerize 'this' since we are allocating here.
101 Handle<Class> h_this(hs.NewHandle(this));
102 // Clear exception so we can allocate.
103 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
104 self->ClearException();
105 // Allocate the ClassExt
106 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800107 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700108 // OOM allocating the classExt.
109 // TODO Should we restore the suppressed exception?
110 self->AssertPendingOOMException();
111 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800112 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700113 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
114 bool set;
115 // Set the ext_data_ field using CAS semantics.
116 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700117 set = h_this->CasFieldObject<true>(ext_offset,
118 nullptr,
119 new_ext.Get(),
120 CASMode::kStrong,
121 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700122 } else {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700123 set = h_this->CasFieldObject<false>(ext_offset,
124 nullptr,
125 new_ext.Get(),
126 CASMode::kStrong,
127 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700128 }
129 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
130 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
131 CHECK(!ret.IsNull());
132 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800133 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700134 self->SetException(throwable.Get());
135 }
136 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800137 }
138}
139
Vladimir Marko2c64a832018-01-04 11:31:56 +0000140void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
141 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700142 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
143 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700144 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000145 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000146 new_status != ClassStatus::kErrorUnresolved &&
147 new_status != ClassStatus::kErrorResolved &&
148 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700149 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700150 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700151 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000152 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700153 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700154 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700155 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700156 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700157 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000159 if (UNLIKELY(IsErroneous(new_status))) {
160 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700161 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000162 << h_this->PrettyClass()
163 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000164 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700165 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700166 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700167 if (self->IsExceptionPending()) {
168 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
169 }
170 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171
Alex Light0273ad12016-11-02 11:19:31 -0700172 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
173 if (!ext.IsNull()) {
174 self->AssertPendingException();
175 ext->SetVerifyError(self->GetException());
176 } else {
177 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700178 }
179 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 }
Alex Light0273ad12016-11-02 11:19:31 -0700181
Vladimir Marko305c38b2018-02-14 11:50:07 +0000182 if (kBitstringSubtypeCheckEnabled) {
183 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700184 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
185 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000186 } else {
187 // The ClassStatus is always in the 4 most-significant bits of status_.
188 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
189 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
190 if (Runtime::Current()->IsActiveTransaction()) {
191 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
192 } else {
193 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
194 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700195 }
196
197 // Setting the object size alloc fast path needs to be after the status write so that if the
198 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
199 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000200 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700201 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
202 // Finalizable objects must always go slow path.
203 if (!h_this->IsFinalizable()) {
204 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700205 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100206 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700207
208 if (!class_linker_initialized) {
209 // When the class linker is being initialized its single threaded and by definition there can be
210 // no waiters. During initialization classes may appear temporary but won't be retired as their
211 // size was statically computed.
212 } else {
213 // Classes that are being resolved or initialized need to notify waiters that the class status
214 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700215 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700216 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
217 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000218 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
219 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700220 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221 }
222 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000223 CHECK_NE(new_status, ClassStatus::kRetired);
224 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700225 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700226 }
227 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700228 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229}
230
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700231void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700232 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233}
234
Ian Rogersef7d42f2014-01-06 12:55:46 -0800235void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700237 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
238 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700239 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700240 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700241 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242}
243
244// Return the class' name. The exact format is bizarre, but it's the specified behavior for
245// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
246// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
247// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700248String* Class::ComputeName(Handle<Class> h_this) {
249 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800250 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 return name;
252 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700253 std::string temp;
254 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800255 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
257 // The descriptor indicates that this is the class for
258 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700259 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260 switch (descriptor[0]) {
261 case 'Z': c_name = "boolean"; break;
262 case 'B': c_name = "byte"; break;
263 case 'C': c_name = "char"; break;
264 case 'S': c_name = "short"; break;
265 case 'I': c_name = "int"; break;
266 case 'J': c_name = "long"; break;
267 case 'F': c_name = "float"; break;
268 case 'D': c_name = "double"; break;
269 case 'V': c_name = "void"; break;
270 default:
271 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
272 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800273 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 } else {
275 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
276 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700277 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700279 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 return name;
281}
282
Ian Rogersef7d42f2014-01-06 12:55:46 -0800283void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700285 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 if ((flags & kDumpClassClassLoader) != 0) {
287 os << ' ' << GetClassLoader();
288 }
289 if ((flags & kDumpClassInitialized) != 0) {
290 os << ' ' << GetStatus();
291 }
292 os << "\n";
293 return;
294 }
295
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700297 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700298 Handle<Class> h_this(hs.NewHandle(this));
299 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700301
Ian Rogers1ff3c982014-08-12 02:30:58 -0700302 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700304 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800306 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 os << StringPrintf(" access=0x%04x.%04x\n",
308 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800309 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700310 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700311 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 }
313 if (IsArrayClass()) {
314 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
315 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700316 const size_t num_direct_interfaces = NumDirectInterfaces();
317 if (num_direct_interfaces > 0) {
318 os << " interfaces (" << num_direct_interfaces << "):\n";
319 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000320 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700321 if (interface == nullptr) {
322 os << StringPrintf(" %2zd: nullptr!\n", i);
323 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700324 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
325 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700326 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 }
328 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700329 if (!IsLoaded()) {
330 os << " class not yet loaded";
331 } else {
332 // After this point, this may have moved due to GetDirectInterface.
333 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800334 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700335 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700336 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700339 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
340 for (size_t i = 0; i < h_this->NumDirectMethods(); ++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->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700343 }
344 if (h_this->NumStaticFields() > 0) {
345 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000346 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700347 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700348 os << StringPrintf(" %2zd: %s\n", i,
349 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700350 }
351 } else {
352 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700354 }
355 if (h_this->NumInstanceFields() > 0) {
356 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000357 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700358 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700359 os << StringPrintf(" %2zd: %s\n", i,
360 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700361 }
362 } else {
363 os << " <not yet available>";
364 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 }
366 }
367}
368
369void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700370 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 // Sanity check that the number of bits set in the reference offset bitmap
372 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700373 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700374 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 count += c->NumReferenceInstanceFieldsDuringLinking();
376 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700377 // +1 for the Class in Object.
378 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100380 // Not called within a transaction.
381 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700382 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383}
384
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
386 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700387 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
388 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 ++i;
390 }
391 if (descriptor1.find('/', i) != StringPiece::npos ||
392 descriptor2.find('/', i) != StringPiece::npos) {
393 return false;
394 } else {
395 return true;
396 }
397}
398
Mathieu Chartier3398c782016-09-30 10:27:43 -0700399bool Class::IsInSamePackage(ObjPtr<Class> that) {
400 ObjPtr<Class> klass1 = this;
401 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 if (klass1 == klass2) {
403 return true;
404 }
405 // Class loaders must match.
406 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
407 return false;
408 }
409 // Arrays are in the same package when their element classes are.
410 while (klass1->IsArrayClass()) {
411 klass1 = klass1->GetComponentType();
412 }
413 while (klass2->IsArrayClass()) {
414 klass2 = klass2->GetComponentType();
415 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700416 // trivial check again for array types
417 if (klass1 == klass2) {
418 return true;
419 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700421 std::string temp1, temp2;
422 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423}
424
Ian Rogersef7d42f2014-01-06 12:55:46 -0800425bool Class::IsThrowableClass() {
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100426 return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427}
428
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700429void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100430 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700431 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100432 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700433 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100434 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435}
436
Vladimir Markoba118822017-06-12 15:41:56 +0100437template <typename SignatureType>
438static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
439 const StringPiece& name,
440 const SignatureType& signature,
441 PointerSize pointer_size)
442 REQUIRES_SHARED(Locks::mutator_lock_) {
443 // If the current class is not an interface, skip the search of its declared methods;
444 // such lookup is used only to distinguish between IncompatibleClassChangeError and
445 // NoSuchMethodError and the caller has already tried to search methods in the class.
446 if (LIKELY(klass->IsInterface())) {
447 // Search declared methods, both direct and virtual.
448 // (This lookup is used also for invoke-static on interface classes.)
449 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
450 if (method.GetName() == name && method.GetSignature() == signature) {
451 return &method;
452 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 }
454 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700455
Vladimir Markoba118822017-06-12 15:41:56 +0100456 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
457 // we should return it, otherwise an arbitrary one can be returned.
458 ObjPtr<IfTable> iftable = klass->GetIfTable();
459 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
460 ObjPtr<Class> iface = iftable->GetInterface(i);
461 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
462 if (method.GetName() == name && method.GetSignature() == signature) {
463 return &method;
464 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700465 }
466 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467
Vladimir Markoba118822017-06-12 15:41:56 +0100468 // Then search for public non-static methods in the java.lang.Object.
469 if (LIKELY(klass->IsInterface())) {
470 ObjPtr<Class> object_class = klass->GetSuperClass();
471 DCHECK(object_class->IsObjectClass());
472 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
473 if (method.IsPublic() && !method.IsStatic() &&
474 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476 }
477 }
478 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700479 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480}
481
Vladimir Markoba118822017-06-12 15:41:56 +0100482ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
483 const StringPiece& signature,
484 PointerSize pointer_size) {
485 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486}
487
Vladimir Markoba118822017-06-12 15:41:56 +0100488ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
489 const Signature& signature,
490 PointerSize pointer_size) {
491 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700492}
493
Vladimir Markoba118822017-06-12 15:41:56 +0100494ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
495 uint32_t dex_method_idx,
496 PointerSize pointer_size) {
497 // We always search by name and signature, ignoring the type index in the MethodId.
498 const DexFile& dex_file = *dex_cache->GetDexFile();
499 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
500 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
501 const Signature signature = dex_file.GetMethodSignature(method_id);
502 return FindInterfaceMethod(name, signature, pointer_size);
503}
504
Alex Lightafb66472017-08-01 09:54:49 -0700505static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
506 ObjPtr<mirror::Class> declaring_class)
507 REQUIRES_SHARED(Locks::mutator_lock_) {
508 if (klass->IsArrayClass()) {
509 return declaring_class->IsObjectClass();
510 } else if (klass->IsInterface()) {
511 return declaring_class->IsObjectClass() || declaring_class == klass;
512 } else {
513 return klass->IsSubClass(declaring_class);
514 }
515}
516
Vladimir Markoba118822017-06-12 15:41:56 +0100517static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
518 ObjPtr<mirror::Class> declaring_class,
519 ArtMethod& method)
520 REQUIRES_SHARED(Locks::mutator_lock_) {
521 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
522 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700523 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100524 uint32_t access_flags = method.GetAccessFlags();
525 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
526 return true;
527 }
528 if ((access_flags & kAccPrivate) != 0) {
529 return false;
530 }
531 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
532 if (!klass->IsInSamePackage(declaring_class)) {
533 return false;
534 }
535 }
536 return true;
537}
538
539template <typename SignatureType>
540static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
541 const StringPiece& name,
542 const SignatureType& signature,
543 PointerSize pointer_size)
544 REQUIRES_SHARED(Locks::mutator_lock_) {
545 // Search declared methods first.
546 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
547 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
548 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
549 return &method;
550 }
551 }
552
553 // Then search the superclass chain. If we find an inherited method, return it.
554 // If we find a method that's not inherited because of access restrictions,
555 // try to find a method inherited from an interface in copied methods.
556 ObjPtr<Class> klass = this_klass->GetSuperClass();
557 ArtMethod* uninherited_method = nullptr;
558 for (; klass != nullptr; klass = klass->GetSuperClass()) {
559 DCHECK(!klass->IsProxyClass());
560 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
561 if (method.GetName() == name && method.GetSignature() == signature) {
562 if (IsInheritedMethod(this_klass, klass, method)) {
563 return &method;
564 }
565 uninherited_method = &method;
566 break;
567 }
568 }
569 if (uninherited_method != nullptr) {
570 break;
571 }
572 }
573
574 // Then search copied methods.
575 // If we found a method that's not inherited, stop the search in its declaring class.
576 ObjPtr<Class> end_klass = klass;
577 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
578 klass = this_klass;
579 if (UNLIKELY(klass->IsProxyClass())) {
580 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
581 klass = klass->GetSuperClass();
582 }
583 for (; klass != end_klass; klass = klass->GetSuperClass()) {
584 DCHECK(!klass->IsProxyClass());
585 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
586 if (method.GetName() == name && method.GetSignature() == signature) {
587 return &method; // No further check needed, copied methods are inherited by definition.
588 }
589 }
590 }
591 return uninherited_method; // Return the `uninherited_method` if any.
592}
593
594
595ArtMethod* Class::FindClassMethod(const StringPiece& name,
596 const StringPiece& signature,
597 PointerSize pointer_size) {
598 return FindClassMethodWithSignature(this, name, signature, pointer_size);
599}
600
601ArtMethod* Class::FindClassMethod(const StringPiece& name,
602 const Signature& signature,
603 PointerSize pointer_size) {
604 return FindClassMethodWithSignature(this, name, signature, pointer_size);
605}
606
607ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
608 uint32_t dex_method_idx,
609 PointerSize pointer_size) {
610 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
611 DCHECK(!IsProxyClass());
612
613 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
614 ObjPtr<DexCache> this_dex_cache = GetDexCache();
615 if (this_dex_cache == dex_cache) {
616 // Lookup is always performed in the class referenced by the MethodId.
617 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
618 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
619 if (method.GetDexMethodIndex() == dex_method_idx) {
620 return &method;
621 }
622 }
623 }
624 // If not found, we need to search by name and signature.
625 const DexFile& dex_file = *dex_cache->GetDexFile();
626 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
627 const Signature signature = dex_file.GetMethodSignature(method_id);
628 StringPiece name; // Delay strlen() until actually needed.
629 // If we do not have a dex_cache match, try to find the declared method in this class now.
630 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
631 DCHECK(name.empty());
632 name = dex_file.StringDataByIdx(method_id.name_idx_);
633 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
634 if (method.GetName() == name && method.GetSignature() == signature) {
635 return &method;
636 }
637 }
638 }
639
640 // Then search the superclass chain. If we find an inherited method, return it.
641 // If we find a method that's not inherited because of access restrictions,
642 // try to find a method inherited from an interface in copied methods.
643 ArtMethod* uninherited_method = nullptr;
644 ObjPtr<Class> klass = GetSuperClass();
645 for (; klass != nullptr; klass = klass->GetSuperClass()) {
646 ArtMethod* candidate_method = nullptr;
647 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
648 if (klass->GetDexCache() == dex_cache) {
649 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
650 // the type index differs, so compare the name index and proto index.
651 for (ArtMethod& method : declared_methods) {
652 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
653 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
654 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
655 candidate_method = &method;
656 break;
657 }
658 }
659 } else {
660 if (!declared_methods.empty() && name.empty()) {
661 name = dex_file.StringDataByIdx(method_id.name_idx_);
662 }
663 for (ArtMethod& method : declared_methods) {
664 if (method.GetName() == name && method.GetSignature() == signature) {
665 candidate_method = &method;
666 break;
667 }
668 }
669 }
670 if (candidate_method != nullptr) {
671 if (IsInheritedMethod(this, klass, *candidate_method)) {
672 return candidate_method;
673 } else {
674 uninherited_method = candidate_method;
675 break;
676 }
677 }
678 }
679
680 // Then search copied methods.
681 // If we found a method that's not inherited, stop the search in its declaring class.
682 ObjPtr<Class> end_klass = klass;
683 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
684 // After we have searched the declared methods of the super-class chain,
685 // search copied methods which can contain methods from interfaces.
686 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
687 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
688 if (!copied_methods.empty() && name.empty()) {
689 name = dex_file.StringDataByIdx(method_id.name_idx_);
690 }
691 for (ArtMethod& method : copied_methods) {
692 if (method.GetName() == name && method.GetSignature() == signature) {
693 return &method; // No further check needed, copied methods are inherited by definition.
694 }
695 }
696 }
697 return uninherited_method; // Return the `uninherited_method` if any.
698}
699
700ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
701 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
702 DCHECK(!IsProxyClass());
703 StringPiece name("<init>");
704 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
705 if (method.GetName() == name && method.GetSignature() == signature) {
706 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800707 }
708 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700709 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800710}
711
Andreas Gampe542451c2016-07-26 09:02:02 -0700712ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
713 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000714 for (auto& method : GetDirectMethods(pointer_size)) {
715 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
716 if (name == np_method->GetName()) {
717 return &method;
718 }
719 }
720 return nullptr;
721}
722
Andreas Gampe542451c2016-07-26 09:02:02 -0700723ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
724 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,
812 const StringPiece& name,
813 const StringPiece& 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.
826 int result = StringPiece(field.GetName()).Compare(name);
827 if (result == 0) {
828 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
829 }
830 if (result < 0) {
831 low = mid + 1;
832 } else if (result > 0) {
833 high = mid;
834 } else {
835 ret = &field;
836 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800837 }
838 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700839 if (kIsDebugBuild) {
840 ArtField* found = nullptr;
841 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
842 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
843 found = &field;
844 break;
845 }
846 }
David Sehr709b0702016-10-13 09:12:37 -0700847 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700848 }
849 return ret;
850}
851
852ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
853 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
854 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800855}
856
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700857ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800858 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700859 for (ArtField& field : GetIFields()) {
860 if (field.GetDexFieldIndex() == dex_field_idx) {
861 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800862 }
863 }
864 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700865 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800866}
867
Brian Carlstromea46f952013-07-30 01:26:50 -0700868ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800869 // Is the field in this class, or any of its superclasses?
870 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700871 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700872 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700873 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800874 return f;
875 }
876 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700877 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800878}
879
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700880ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800881 // Is the field in this class, or any of its superclasses?
882 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700883 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700884 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700885 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800886 return f;
887 }
888 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700889 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800890}
891
Brian Carlstromea46f952013-07-30 01:26:50 -0700892ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700893 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700894 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800895}
896
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700897ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800898 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700899 for (ArtField& field : GetSFields()) {
900 if (field.GetDexFieldIndex() == dex_field_idx) {
901 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800902 }
903 }
904 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700905 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800906}
907
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700908ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000909 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700910 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700911 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800912 // Is the field in this class (or its interfaces), or any of its
913 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000914 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800915 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700916 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700917 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800918 return f;
919 }
920 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000921 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
922 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
923 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700924 f = FindStaticField(self, interface, name, type);
925 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800926 return f;
927 }
928 }
929 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700930 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800931}
932
Vladimir Markobb268b12016-06-30 15:52:56 +0100933ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700934 ObjPtr<Class> klass,
935 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700936 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700937 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800938 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700939 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700940 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800941 return f;
942 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100943 // Though GetDirectInterface() should not cause thread suspension when called
944 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700945 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800946 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000947 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
948 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
949 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700950 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
951 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800952 return f;
953 }
954 }
955 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700956 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800957}
958
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700959ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000960 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700961 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700962 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800963 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000964 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800965 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700966 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700967 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800968 return f;
969 }
970 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700971 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800972 return f;
973 }
974 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000975 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
976 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
977 DCHECK(interface != nullptr);
978 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700979 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800980 return f;
981 }
982 }
983 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700984 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800985}
986
Andreas Gampe542451c2016-07-26 09:02:02 -0700987void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700988 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800989 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700990 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800991 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700992 }
993 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200994}
995
Ian Rogers1ff3c982014-08-12 02:30:58 -0700996const char* Class::GetDescriptor(std::string* storage) {
997 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700998 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700999 } else if (IsArrayClass()) {
1000 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001001 } else if (IsProxyClass()) {
1002 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001003 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001004 } else {
1005 const DexFile& dex_file = GetDexFile();
1006 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
1007 return dex_file.GetTypeDescriptor(type_id);
1008 }
1009}
1010
Ian Rogers1ff3c982014-08-12 02:30:58 -07001011const char* Class::GetArrayDescriptor(std::string* storage) {
1012 std::string temp;
1013 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1014 *storage = "[";
1015 *storage += elem_desc;
1016 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001017}
1018
1019const DexFile::ClassDef* Class::GetClassDef() {
1020 uint16_t class_def_idx = GetDexClassDefIndex();
1021 if (class_def_idx == DexFile::kDexNoIndex16) {
1022 return nullptr;
1023 }
1024 return &GetDexFile().GetClassDef(class_def_idx);
1025}
1026
Andreas Gampea5b09a62016-11-17 15:21:22 -08001027dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001028 DCHECK(!IsPrimitive());
1029 DCHECK(!IsArrayClass());
1030 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1031}
1032
Vladimir Marko19a4d372016-12-08 14:41:46 +00001033ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1034 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001035 DCHECK(!klass->IsPrimitive());
1036 if (klass->IsArrayClass()) {
1037 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001038 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1039 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001040 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001041 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001042 } else {
1043 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001044 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001045 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001046 DCHECK(interface != nullptr);
1047 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001048 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001049 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001050 DCHECK(interfaces != nullptr);
1051 return interfaces->Get(idx);
1052 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001053 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001054 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001055 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001056 return interface;
1057 }
1058}
1059
Vladimir Marko19a4d372016-12-08 14:41:46 +00001060ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1061 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1062 if (interface == nullptr) {
1063 DCHECK(!klass->IsArrayClass());
1064 DCHECK(!klass->IsProxyClass());
1065 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001066 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001067 CHECK(interface != nullptr || self->IsExceptionPending());
1068 }
1069 return interface;
1070}
1071
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001072ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001073 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001074 DCHECK(!klass->IsInterface());
1075 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001076 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001077 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001078 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001079 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001080 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001081 }
Calin Juravle52503d82015-11-11 16:58:31 +00001082 return common_super_class;
1083}
1084
Mathieu Chartierf8322842014-05-16 10:59:25 -07001085const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001086 const DexFile& dex_file = GetDexFile();
1087 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001088 if (dex_class_def == nullptr) {
1089 // Generated classes have no class def.
1090 return nullptr;
1091 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001092 return dex_file.GetSourceFile(*dex_class_def);
1093}
1094
1095std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001096 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001097 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001098 return dex_cache->GetLocation()->ToModifiedUtf8();
1099 }
1100 // Arrays and proxies are generated and have no corresponding dex file location.
1101 return "generated class";
1102}
1103
1104const DexFile::TypeList* Class::GetInterfaceTypeList() {
1105 const DexFile::ClassDef* class_def = GetClassDef();
1106 if (class_def == nullptr) {
1107 return nullptr;
1108 }
1109 return GetDexFile().GetInterfacesList(*class_def);
1110}
1111
Andreas Gampe542451c2016-07-26 09:02:02 -07001112void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001113 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001114 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001115 const size_t table_length = table->GetLength();
1116 SetEmbeddedVTableLength(table_length);
1117 for (size_t i = 0; i < table_length; i++) {
1118 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001119 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001120 // Keep java.lang.Object class's vtable around for since it's easier
1121 // to be reused by array classes during their linking.
1122 if (!IsObjectClass()) {
1123 SetVTable(nullptr);
1124 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001125}
1126
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001127class ReadBarrierOnNativeRootsVisitor {
1128 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001129 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001130 MemberOffset offset ATTRIBUTE_UNUSED,
1131 bool is_static ATTRIBUTE_UNUSED) const {}
1132
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001133 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001134 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001135 if (!root->IsNull()) {
1136 VisitRoot(root);
1137 }
1138 }
1139
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001140 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001141 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001142 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1143 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001144 if (old_ref != new_ref) {
1145 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1146 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001147 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001148 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001149 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1150 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001151 }
1152 }
1153};
1154
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001155// The pre-fence visitor for Class::CopyOf().
1156class CopyClassVisitor {
1157 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001158 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001159 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001160 size_t new_length,
1161 size_t copy_bytes,
1162 ImTable* imt,
1163 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001164 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001165 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001166 }
1167
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001168 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001169 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001170 StackHandleScope<1> hs(self_);
1171 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001172 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001173 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001174 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1175 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001176 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001177 // Visit all of the references to make sure there is no from space references in the native
1178 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001179 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001180 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001181 }
1182
1183 private:
1184 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001185 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001186 const size_t new_length_;
1187 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001188 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001189 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001190 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1191};
1192
Andreas Gampe542451c2016-07-26 09:02:02 -07001193Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001194 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1195 // We may get copied by a compacting GC.
1196 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001197 Handle<Class> h_this(hs.NewHandle(this));
Vladimir Marko317892b2018-05-31 11:11:32 +01001198 Runtime* runtime = Runtime::Current();
1199 gc::Heap* heap = runtime->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001200 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1201 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001202 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Vladimir Marko317892b2018-05-31 11:11:32 +01001203 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001204 ObjPtr<Object> new_class = kMovingClasses ?
Vladimir Marko317892b2018-05-31 11:11:32 +01001205 heap->AllocObject<true>(self, java_lang_Class, new_length, visitor) :
1206 heap->AllocNonMovableObject<true>(self, java_lang_Class, new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001207 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001208 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001209 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001210 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001211 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001212}
1213
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001214bool Class::ProxyDescriptorEquals(const char* match) {
1215 DCHECK(IsProxyClass());
1216 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001217}
1218
Mathieu Chartiere401d142015-04-22 13:56:20 -07001219// TODO: Move this to java_lang_Class.cc?
1220ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001221 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001222 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001223 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001224 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001225 continue;
1226 }
1227 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001228 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001229 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001230 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001231 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001232 return nullptr;
1233 }
1234 }
1235 return nullptr;
1236}
1237
Mathieu Chartiere401d142015-04-22 13:56:20 -07001238uint32_t Class::Depth() {
1239 uint32_t depth = 0;
Roland Levillaind32ead22018-05-30 17:38:21 +01001240 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001241 depth++;
1242 }
1243 return depth;
1244}
1245
Andreas Gampea5b09a62016-11-17 15:21:22 -08001246dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001247 std::string temp;
1248 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001249 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001250}
1251
Andreas Gampe542451c2016-07-26 09:02:02 -07001252template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001253ObjPtr<Method> Class::GetDeclaredMethodInternal(
1254 Thread* self,
1255 ObjPtr<Class> klass,
1256 ObjPtr<String> name,
1257 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001258 // Covariant return types permit the class to define multiple
1259 // methods with the same name and parameter types. Prefer to
1260 // return a non-synthetic method in such situations. We may
1261 // still return a synthetic method to handle situations like
1262 // escalated visibility. We never return miranda methods that
1263 // were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001264 StackHandleScope<3> hs(self);
1265 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001266 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001267 ThrowNullPointerException("name == null");
1268 return nullptr;
1269 }
1270 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001271 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001272 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001273 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1274 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001275 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001276 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001277 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1278 if (UNLIKELY(self->IsExceptionPending())) {
1279 return nullptr;
1280 }
1281 continue;
1282 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001283 if (!m.IsMiranda()) {
1284 if (!m.IsSynthetic()) {
1285 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1286 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001287 result = &m; // Remember as potential result if it's not a miranda method.
1288 }
1289 }
1290 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001291 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001292 auto modifiers = m.GetAccessFlags();
1293 if ((modifiers & kAccConstructor) != 0) {
1294 continue;
1295 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001296 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001297 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001298 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001299 if (np_name == nullptr) {
1300 self->AssertPendingException();
1301 return nullptr;
1302 }
1303 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1304 if (UNLIKELY(self->IsExceptionPending())) {
1305 return nullptr;
1306 }
1307 continue;
1308 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001309 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
1310 if ((modifiers & kAccSynthetic) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001311 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001312 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001313 result = &m; // Remember as potential result.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001314 }
1315 }
1316 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001317 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001318 : nullptr;
1319}
1320
1321template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001322ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001323 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001324 ObjPtr<Class> klass,
1325 ObjPtr<String> name,
1326 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001327template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001328ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001329 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001330 ObjPtr<Class> klass,
1331 ObjPtr<String> name,
1332 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001333template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001334ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001335 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001336 ObjPtr<Class> klass,
1337 ObjPtr<String> name,
1338 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001339template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001340ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001341 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001342 ObjPtr<Class> klass,
1343 ObjPtr<String> name,
1344 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001345
Andreas Gampe542451c2016-07-26 09:02:02 -07001346template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001347ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001348 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001349 ObjPtr<Class> klass,
1350 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001351 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001352 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001353 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001354 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001355 : nullptr;
1356}
1357
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001358// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001359
Andreas Gampe542451c2016-07-26 09:02:02 -07001360template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001361ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001362 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001363 ObjPtr<Class> klass,
1364 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001365template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001366ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001367 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001368 ObjPtr<Class> klass,
1369 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001370template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001371ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001372 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001373 ObjPtr<Class> klass,
1374 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001375template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001376ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001377 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001378 ObjPtr<Class> klass,
1379 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001380
Andreas Gampe715fdc22016-04-18 17:07:30 -07001381int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1382 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1383 return default_value;
1384 }
1385 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001386 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001387 return default_value;
1388 }
1389 return flags;
1390}
1391
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001392void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1393 if (Runtime::Current()->IsActiveTransaction()) {
1394 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1395 } else {
1396 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1397 }
1398}
1399
David Sehr709b0702016-10-13 09:12:37 -07001400std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1401 if (klass == nullptr) {
1402 return "null";
1403 }
1404 return klass->PrettyDescriptor();
1405}
1406
1407std::string Class::PrettyDescriptor() {
1408 std::string temp;
1409 return art::PrettyDescriptor(GetDescriptor(&temp));
1410}
1411
1412std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1413 if (c == nullptr) {
1414 return "null";
1415 }
1416 return c->PrettyClass();
1417}
1418
1419std::string Class::PrettyClass() {
1420 std::string result;
1421 result += "java.lang.Class<";
1422 result += PrettyDescriptor();
1423 result += ">";
1424 return result;
1425}
1426
1427std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1428 if (c == nullptr) {
1429 return "null";
1430 }
1431 return c->PrettyClassAndClassLoader();
1432}
1433
1434std::string Class::PrettyClassAndClassLoader() {
1435 std::string result;
1436 result += "java.lang.Class<";
1437 result += PrettyDescriptor();
1438 result += ",";
1439 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1440 // TODO: add an identifying hash value for the loader
1441 result += ">";
1442 return result;
1443}
1444
Andreas Gampe90b936d2017-01-31 08:58:55 -08001445template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1446 // Check class is loaded/retired or this is java.lang.String that has a
1447 // circularity issue during loading the names of its members
1448 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1449 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001450 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001451 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1452 << " IsRetired=" << IsRetired<kVerifyFlags>()
1453 << " IsErroneous=" <<
1454 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001455 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001456 << " status= " << GetStatus<kVerifyFlags>()
1457 << " descriptor=" << PrettyDescriptor();
1458}
1459// Instantiate the common cases.
1460template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1461template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1462template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1463template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1464template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1465
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001466} // namespace mirror
1467} // namespace art