blob: 31a83f8e4871db4c673119f7ca26e35b5be4d59d [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()) {
117 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
118 ObjPtr<ClassExt>(nullptr),
119 new_ext.Get());
120 } else {
121 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
122 ObjPtr<ClassExt>(nullptr),
123 new_ext.Get());
124 }
125 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
126 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
127 CHECK(!ret.IsNull());
128 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800129 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700130 self->SetException(throwable.Get());
131 }
132 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800133 }
134}
135
Vladimir Marko2c64a832018-01-04 11:31:56 +0000136void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
137 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700138 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
139 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700140 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000141 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000142 new_status != ClassStatus::kErrorUnresolved &&
143 new_status != ClassStatus::kErrorResolved &&
144 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700145 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700146 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700147 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000148 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700149 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700150 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700151 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700152 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700153 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000155 if (UNLIKELY(IsErroneous(new_status))) {
156 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700157 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000158 << h_this->PrettyClass()
159 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000160 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700161 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700162 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700163 if (self->IsExceptionPending()) {
164 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
165 }
166 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167
Alex Light0273ad12016-11-02 11:19:31 -0700168 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
169 if (!ext.IsNull()) {
170 self->AssertPendingException();
171 ext->SetVerifyError(self->GetException());
172 } else {
173 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700174 }
175 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 }
Alex Light0273ad12016-11-02 11:19:31 -0700177
Vladimir Marko305c38b2018-02-14 11:50:07 +0000178 if (kBitstringSubtypeCheckEnabled) {
179 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700180 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
181 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000182 } else {
183 // The ClassStatus is always in the 4 most-significant bits of status_.
184 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
185 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
186 if (Runtime::Current()->IsActiveTransaction()) {
187 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
188 } else {
189 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
190 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700191 }
192
193 // Setting the object size alloc fast path needs to be after the status write so that if the
194 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
195 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000196 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700197 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
198 // Finalizable objects must always go slow path.
199 if (!h_this->IsFinalizable()) {
200 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700201 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100202 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700203
204 if (!class_linker_initialized) {
205 // When the class linker is being initialized its single threaded and by definition there can be
206 // no waiters. During initialization classes may appear temporary but won't be retired as their
207 // size was statically computed.
208 } else {
209 // Classes that are being resolved or initialized need to notify waiters that the class status
210 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700211 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700212 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
213 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000214 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
215 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700216 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700217 }
218 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000219 CHECK_NE(new_status, ClassStatus::kRetired);
220 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700221 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700222 }
223 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700224 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225}
226
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700227void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700228 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229}
230
Ian Rogersef7d42f2014-01-06 12:55:46 -0800231void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700232 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700233 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
234 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700235 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700236 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700237 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238}
239
240// Return the class' name. The exact format is bizarre, but it's the specified behavior for
241// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
242// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
243// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700244String* Class::ComputeName(Handle<Class> h_this) {
245 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800246 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 return name;
248 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700249 std::string temp;
250 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800251 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
253 // The descriptor indicates that this is the class for
254 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700255 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 switch (descriptor[0]) {
257 case 'Z': c_name = "boolean"; break;
258 case 'B': c_name = "byte"; break;
259 case 'C': c_name = "char"; break;
260 case 'S': c_name = "short"; break;
261 case 'I': c_name = "int"; break;
262 case 'J': c_name = "long"; break;
263 case 'F': c_name = "float"; break;
264 case 'D': c_name = "double"; break;
265 case 'V': c_name = "void"; break;
266 default:
267 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
268 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800269 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 } else {
271 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
272 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700273 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700275 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 return name;
277}
278
Ian Rogersef7d42f2014-01-06 12:55:46 -0800279void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700281 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 if ((flags & kDumpClassClassLoader) != 0) {
283 os << ' ' << GetClassLoader();
284 }
285 if ((flags & kDumpClassInitialized) != 0) {
286 os << ' ' << GetStatus();
287 }
288 os << "\n";
289 return;
290 }
291
Mathieu Chartiere401d142015-04-22 13:56:20 -0700292 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700293 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700294 Handle<Class> h_this(hs.NewHandle(this));
295 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700297
Ian Rogers1ff3c982014-08-12 02:30:58 -0700298 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700300 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800302 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 os << StringPrintf(" access=0x%04x.%04x\n",
304 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800305 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700306 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700307 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 }
309 if (IsArrayClass()) {
310 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
311 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700312 const size_t num_direct_interfaces = NumDirectInterfaces();
313 if (num_direct_interfaces > 0) {
314 os << " interfaces (" << num_direct_interfaces << "):\n";
315 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000316 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700317 if (interface == nullptr) {
318 os << StringPrintf(" %2zd: nullptr!\n", i);
319 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700320 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
321 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700322 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 }
324 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700325 if (!IsLoaded()) {
326 os << " class not yet loaded";
327 } else {
328 // After this point, this may have moved due to GetDirectInterface.
329 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800330 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700331 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700332 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700335 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
336 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700337 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700339 }
340 if (h_this->NumStaticFields() > 0) {
341 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000342 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700343 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700344 os << StringPrintf(" %2zd: %s\n", i,
345 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700346 }
347 } else {
348 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700350 }
351 if (h_this->NumInstanceFields() > 0) {
352 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000353 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700354 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700355 os << StringPrintf(" %2zd: %s\n", i,
356 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700357 }
358 } else {
359 os << " <not yet available>";
360 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 }
362 }
363}
364
365void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 // Sanity check that the number of bits set in the reference offset bitmap
368 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700369 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700370 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 count += c->NumReferenceInstanceFieldsDuringLinking();
372 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700373 // +1 for the Class in Object.
374 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100376 // Not called within a transaction.
377 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700378 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379}
380
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
382 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700383 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
384 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 ++i;
386 }
387 if (descriptor1.find('/', i) != StringPiece::npos ||
388 descriptor2.find('/', i) != StringPiece::npos) {
389 return false;
390 } else {
391 return true;
392 }
393}
394
Mathieu Chartier3398c782016-09-30 10:27:43 -0700395bool Class::IsInSamePackage(ObjPtr<Class> that) {
396 ObjPtr<Class> klass1 = this;
397 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 if (klass1 == klass2) {
399 return true;
400 }
401 // Class loaders must match.
402 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
403 return false;
404 }
405 // Arrays are in the same package when their element classes are.
406 while (klass1->IsArrayClass()) {
407 klass1 = klass1->GetComponentType();
408 }
409 while (klass2->IsArrayClass()) {
410 klass2 = klass2->GetComponentType();
411 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700412 // trivial check again for array types
413 if (klass1 == klass2) {
414 return true;
415 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700417 std::string temp1, temp2;
418 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419}
420
Ian Rogersef7d42f2014-01-06 12:55:46 -0800421bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
423}
424
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700425void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100426 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700427 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100428 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700429 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100430 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431}
432
Vladimir Markoba118822017-06-12 15:41:56 +0100433template <typename SignatureType>
434static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
435 const StringPiece& name,
436 const SignatureType& signature,
437 PointerSize pointer_size)
438 REQUIRES_SHARED(Locks::mutator_lock_) {
439 // If the current class is not an interface, skip the search of its declared methods;
440 // such lookup is used only to distinguish between IncompatibleClassChangeError and
441 // NoSuchMethodError and the caller has already tried to search methods in the class.
442 if (LIKELY(klass->IsInterface())) {
443 // Search declared methods, both direct and virtual.
444 // (This lookup is used also for invoke-static on interface classes.)
445 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
446 if (method.GetName() == name && method.GetSignature() == signature) {
447 return &method;
448 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449 }
450 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451
Vladimir Markoba118822017-06-12 15:41:56 +0100452 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
453 // we should return it, otherwise an arbitrary one can be returned.
454 ObjPtr<IfTable> iftable = klass->GetIfTable();
455 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
456 ObjPtr<Class> iface = iftable->GetInterface(i);
457 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
458 if (method.GetName() == name && method.GetSignature() == signature) {
459 return &method;
460 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700461 }
462 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800463
Vladimir Markoba118822017-06-12 15:41:56 +0100464 // Then search for public non-static methods in the java.lang.Object.
465 if (LIKELY(klass->IsInterface())) {
466 ObjPtr<Class> object_class = klass->GetSuperClass();
467 DCHECK(object_class->IsObjectClass());
468 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
469 if (method.IsPublic() && !method.IsStatic() &&
470 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 }
473 }
474 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700475 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476}
477
Vladimir Markoba118822017-06-12 15:41:56 +0100478ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
479 const StringPiece& signature,
480 PointerSize pointer_size) {
481 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482}
483
Vladimir Markoba118822017-06-12 15:41:56 +0100484ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
485 const Signature& signature,
486 PointerSize pointer_size) {
487 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700488}
489
Vladimir Markoba118822017-06-12 15:41:56 +0100490ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
491 uint32_t dex_method_idx,
492 PointerSize pointer_size) {
493 // We always search by name and signature, ignoring the type index in the MethodId.
494 const DexFile& dex_file = *dex_cache->GetDexFile();
495 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
496 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
497 const Signature signature = dex_file.GetMethodSignature(method_id);
498 return FindInterfaceMethod(name, signature, pointer_size);
499}
500
Alex Lightafb66472017-08-01 09:54:49 -0700501static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
502 ObjPtr<mirror::Class> declaring_class)
503 REQUIRES_SHARED(Locks::mutator_lock_) {
504 if (klass->IsArrayClass()) {
505 return declaring_class->IsObjectClass();
506 } else if (klass->IsInterface()) {
507 return declaring_class->IsObjectClass() || declaring_class == klass;
508 } else {
509 return klass->IsSubClass(declaring_class);
510 }
511}
512
Vladimir Markoba118822017-06-12 15:41:56 +0100513static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
514 ObjPtr<mirror::Class> declaring_class,
515 ArtMethod& method)
516 REQUIRES_SHARED(Locks::mutator_lock_) {
517 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
518 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700519 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100520 uint32_t access_flags = method.GetAccessFlags();
521 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
522 return true;
523 }
524 if ((access_flags & kAccPrivate) != 0) {
525 return false;
526 }
527 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
528 if (!klass->IsInSamePackage(declaring_class)) {
529 return false;
530 }
531 }
532 return true;
533}
534
535template <typename SignatureType>
536static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
537 const StringPiece& name,
538 const SignatureType& signature,
539 PointerSize pointer_size)
540 REQUIRES_SHARED(Locks::mutator_lock_) {
541 // Search declared methods first.
542 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
543 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
544 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
545 return &method;
546 }
547 }
548
549 // Then search the superclass chain. If we find an inherited method, return it.
550 // If we find a method that's not inherited because of access restrictions,
551 // try to find a method inherited from an interface in copied methods.
552 ObjPtr<Class> klass = this_klass->GetSuperClass();
553 ArtMethod* uninherited_method = nullptr;
554 for (; klass != nullptr; klass = klass->GetSuperClass()) {
555 DCHECK(!klass->IsProxyClass());
556 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
557 if (method.GetName() == name && method.GetSignature() == signature) {
558 if (IsInheritedMethod(this_klass, klass, method)) {
559 return &method;
560 }
561 uninherited_method = &method;
562 break;
563 }
564 }
565 if (uninherited_method != nullptr) {
566 break;
567 }
568 }
569
570 // Then search copied methods.
571 // If we found a method that's not inherited, stop the search in its declaring class.
572 ObjPtr<Class> end_klass = klass;
573 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
574 klass = this_klass;
575 if (UNLIKELY(klass->IsProxyClass())) {
576 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
577 klass = klass->GetSuperClass();
578 }
579 for (; klass != end_klass; klass = klass->GetSuperClass()) {
580 DCHECK(!klass->IsProxyClass());
581 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
582 if (method.GetName() == name && method.GetSignature() == signature) {
583 return &method; // No further check needed, copied methods are inherited by definition.
584 }
585 }
586 }
587 return uninherited_method; // Return the `uninherited_method` if any.
588}
589
590
591ArtMethod* Class::FindClassMethod(const StringPiece& name,
592 const StringPiece& signature,
593 PointerSize pointer_size) {
594 return FindClassMethodWithSignature(this, name, signature, pointer_size);
595}
596
597ArtMethod* Class::FindClassMethod(const StringPiece& name,
598 const Signature& signature,
599 PointerSize pointer_size) {
600 return FindClassMethodWithSignature(this, name, signature, pointer_size);
601}
602
603ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
604 uint32_t dex_method_idx,
605 PointerSize pointer_size) {
606 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
607 DCHECK(!IsProxyClass());
608
609 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
610 ObjPtr<DexCache> this_dex_cache = GetDexCache();
611 if (this_dex_cache == dex_cache) {
612 // Lookup is always performed in the class referenced by the MethodId.
613 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
614 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
615 if (method.GetDexMethodIndex() == dex_method_idx) {
616 return &method;
617 }
618 }
619 }
620 // If not found, we need to search by name and signature.
621 const DexFile& dex_file = *dex_cache->GetDexFile();
622 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
623 const Signature signature = dex_file.GetMethodSignature(method_id);
624 StringPiece name; // Delay strlen() until actually needed.
625 // If we do not have a dex_cache match, try to find the declared method in this class now.
626 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
627 DCHECK(name.empty());
628 name = dex_file.StringDataByIdx(method_id.name_idx_);
629 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
630 if (method.GetName() == name && method.GetSignature() == signature) {
631 return &method;
632 }
633 }
634 }
635
636 // Then search the superclass chain. If we find an inherited method, return it.
637 // If we find a method that's not inherited because of access restrictions,
638 // try to find a method inherited from an interface in copied methods.
639 ArtMethod* uninherited_method = nullptr;
640 ObjPtr<Class> klass = GetSuperClass();
641 for (; klass != nullptr; klass = klass->GetSuperClass()) {
642 ArtMethod* candidate_method = nullptr;
643 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
644 if (klass->GetDexCache() == dex_cache) {
645 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
646 // the type index differs, so compare the name index and proto index.
647 for (ArtMethod& method : declared_methods) {
648 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
649 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
650 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
651 candidate_method = &method;
652 break;
653 }
654 }
655 } else {
656 if (!declared_methods.empty() && name.empty()) {
657 name = dex_file.StringDataByIdx(method_id.name_idx_);
658 }
659 for (ArtMethod& method : declared_methods) {
660 if (method.GetName() == name && method.GetSignature() == signature) {
661 candidate_method = &method;
662 break;
663 }
664 }
665 }
666 if (candidate_method != nullptr) {
667 if (IsInheritedMethod(this, klass, *candidate_method)) {
668 return candidate_method;
669 } else {
670 uninherited_method = candidate_method;
671 break;
672 }
673 }
674 }
675
676 // Then search copied methods.
677 // If we found a method that's not inherited, stop the search in its declaring class.
678 ObjPtr<Class> end_klass = klass;
679 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
680 // After we have searched the declared methods of the super-class chain,
681 // search copied methods which can contain methods from interfaces.
682 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
683 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
684 if (!copied_methods.empty() && name.empty()) {
685 name = dex_file.StringDataByIdx(method_id.name_idx_);
686 }
687 for (ArtMethod& method : copied_methods) {
688 if (method.GetName() == name && method.GetSignature() == signature) {
689 return &method; // No further check needed, copied methods are inherited by definition.
690 }
691 }
692 }
693 return uninherited_method; // Return the `uninherited_method` if any.
694}
695
696ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
697 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
698 DCHECK(!IsProxyClass());
699 StringPiece name("<init>");
700 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
701 if (method.GetName() == name && method.GetSignature() == signature) {
702 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800703 }
704 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700705 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800706}
707
Andreas Gampe542451c2016-07-26 09:02:02 -0700708ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
709 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000710 for (auto& method : GetDirectMethods(pointer_size)) {
711 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
712 if (name == np_method->GetName()) {
713 return &method;
714 }
715 }
716 return nullptr;
717}
718
Andreas Gampe542451c2016-07-26 09:02:02 -0700719ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
720 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000721 for (auto& method : GetVirtualMethods(pointer_size)) {
722 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
723 if (name == np_method->GetName()) {
724 return &method;
725 }
726 }
727 return nullptr;
728}
729
Andreas Gampe542451c2016-07-26 09:02:02 -0700730ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700731 DCHECK(method->GetDeclaringClass()->IsInterface());
732 DCHECK(IsInterface()) << "Should only be called on a interface class";
733 // Check if we have one defined on this interface first. This includes searching copied ones to
734 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
735 // don't do any indirect method checks here.
736 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
737 if (method->HasSameNameAndSignature(&iface_method)) {
738 return &iface_method;
739 }
740 }
741
742 std::vector<ArtMethod*> abstract_methods;
743 // Search through the IFTable for a working version. We don't need to check for conflicts
744 // because if there was one it would appear in this classes virtual_methods_ above.
745
746 Thread* self = Thread::Current();
747 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700748 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
749 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700750 size_t iftable_count = GetIfTableCount();
751 // Find the method. We don't need to check for conflicts because they would have been in the
752 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
753 // subtypiest interfaces get visited first.
754 for (size_t k = iftable_count; k != 0;) {
755 k--;
756 DCHECK_LT(k, iftable->Count());
757 iface.Assign(iftable->GetInterface(k));
758 // Iterate through every declared method on this interface. Each direct method's name/signature
759 // is unique so the order of the inner loop doesn't matter.
760 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
761 ArtMethod* current_method = &method_iter;
762 if (current_method->HasSameNameAndSignature(method)) {
763 if (current_method->IsDefault()) {
764 // Handle JLS soft errors, a default method from another superinterface tree can
765 // "override" an abstract method(s) from another superinterface tree(s). To do this,
766 // ignore any [default] method which are dominated by the abstract methods we've seen so
767 // far. Check if overridden by any in abstract_methods. We do not need to check for
768 // default_conflicts because we would hit those before we get to this loop.
769 bool overridden = false;
770 for (ArtMethod* possible_override : abstract_methods) {
771 DCHECK(possible_override->HasSameNameAndSignature(current_method));
772 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
773 overridden = true;
774 break;
775 }
776 }
777 if (!overridden) {
778 return current_method;
779 }
780 } else {
781 // Is not default.
782 // This might override another default method. Just stash it for now.
783 abstract_methods.push_back(current_method);
784 }
785 }
786 }
787 }
788 // If we reach here we either never found any declaration of the method (in which case
789 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
790 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
791 // of these arbitrarily.
792 return abstract_methods.empty() ? nullptr : abstract_methods[0];
793}
794
Andreas Gampe542451c2016-07-26 09:02:02 -0700795ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700796 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
797 if (method.IsClassInitializer()) {
798 DCHECK_STREQ(method.GetName(), "<clinit>");
799 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
800 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700801 }
802 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700803 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700804}
805
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700806// Custom binary search to avoid double comparisons from std::binary_search.
807static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
808 const StringPiece& name,
809 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700810 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700811 if (fields == nullptr) {
812 return nullptr;
813 }
814 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100815 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700816 ArtField* ret = nullptr;
817 while (low < high) {
818 size_t mid = (low + high) / 2;
819 ArtField& field = fields->At(mid);
820 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
821 // verifier. There can be multiple fields with the same in the same class name due to proguard.
822 int result = StringPiece(field.GetName()).Compare(name);
823 if (result == 0) {
824 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
825 }
826 if (result < 0) {
827 low = mid + 1;
828 } else if (result > 0) {
829 high = mid;
830 } else {
831 ret = &field;
832 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800833 }
834 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700835 if (kIsDebugBuild) {
836 ArtField* found = nullptr;
837 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
838 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
839 found = &field;
840 break;
841 }
842 }
David Sehr709b0702016-10-13 09:12:37 -0700843 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700844 }
845 return ret;
846}
847
848ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
849 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
850 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800851}
852
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700853ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800854 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700855 for (ArtField& field : GetIFields()) {
856 if (field.GetDexFieldIndex() == dex_field_idx) {
857 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800858 }
859 }
860 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700861 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800862}
863
Brian Carlstromea46f952013-07-30 01:26:50 -0700864ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800865 // Is the field in this class, or any of its superclasses?
866 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700867 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700868 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700869 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800870 return f;
871 }
872 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700873 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800874}
875
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700876ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800877 // Is the field in this class, or any of its superclasses?
878 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700879 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700880 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700881 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800882 return f;
883 }
884 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700885 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800886}
887
Brian Carlstromea46f952013-07-30 01:26:50 -0700888ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700889 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700890 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800891}
892
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700893ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800894 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700895 for (ArtField& field : GetSFields()) {
896 if (field.GetDexFieldIndex() == dex_field_idx) {
897 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800898 }
899 }
900 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700901 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800902}
903
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700904ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000905 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700906 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700907 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800908 // Is the field in this class (or its interfaces), or any of its
909 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000910 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800911 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700912 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700913 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800914 return f;
915 }
916 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000917 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
918 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
919 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700920 f = FindStaticField(self, interface, name, type);
921 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800922 return f;
923 }
924 }
925 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700926 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800927}
928
Vladimir Markobb268b12016-06-30 15:52:56 +0100929ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700930 ObjPtr<Class> klass,
931 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700932 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700933 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800934 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700935 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700936 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800937 return f;
938 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100939 // Though GetDirectInterface() should not cause thread suspension when called
940 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700941 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800942 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000943 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
944 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
945 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700946 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
947 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800948 return f;
949 }
950 }
951 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700952 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800953}
954
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700955ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000956 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700957 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700958 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800959 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000960 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800961 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700962 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700963 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800964 return f;
965 }
966 f = k->FindDeclaredStaticField(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 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000971 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
972 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
973 DCHECK(interface != nullptr);
974 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700975 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800976 return f;
977 }
978 }
979 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700980 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800981}
982
Andreas Gampe542451c2016-07-26 09:02:02 -0700983void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700984 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800985 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700986 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800987 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700988 }
989 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200990}
991
Ian Rogers1ff3c982014-08-12 02:30:58 -0700992const char* Class::GetDescriptor(std::string* storage) {
993 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700994 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700995 } else if (IsArrayClass()) {
996 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000997 } else if (IsProxyClass()) {
998 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700999 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001000 } else {
1001 const DexFile& dex_file = GetDexFile();
1002 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
1003 return dex_file.GetTypeDescriptor(type_id);
1004 }
1005}
1006
Ian Rogers1ff3c982014-08-12 02:30:58 -07001007const char* Class::GetArrayDescriptor(std::string* storage) {
1008 std::string temp;
1009 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1010 *storage = "[";
1011 *storage += elem_desc;
1012 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001013}
1014
1015const DexFile::ClassDef* Class::GetClassDef() {
1016 uint16_t class_def_idx = GetDexClassDefIndex();
1017 if (class_def_idx == DexFile::kDexNoIndex16) {
1018 return nullptr;
1019 }
1020 return &GetDexFile().GetClassDef(class_def_idx);
1021}
1022
Andreas Gampea5b09a62016-11-17 15:21:22 -08001023dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001024 DCHECK(!IsPrimitive());
1025 DCHECK(!IsArrayClass());
1026 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1027}
1028
Vladimir Marko19a4d372016-12-08 14:41:46 +00001029ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1030 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001031 DCHECK(!klass->IsPrimitive());
1032 if (klass->IsArrayClass()) {
1033 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001034 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1035 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001036 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001037 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001038 } else {
1039 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001040 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001041 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001042 DCHECK(interface != nullptr);
1043 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001044 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001045 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001046 DCHECK(interfaces != nullptr);
1047 return interfaces->Get(idx);
1048 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001049 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001050 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001051 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001052 return interface;
1053 }
1054}
1055
Vladimir Marko19a4d372016-12-08 14:41:46 +00001056ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1057 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1058 if (interface == nullptr) {
1059 DCHECK(!klass->IsArrayClass());
1060 DCHECK(!klass->IsProxyClass());
1061 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001062 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001063 CHECK(interface != nullptr || self->IsExceptionPending());
1064 }
1065 return interface;
1066}
1067
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001068ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001069 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001070 DCHECK(!klass->IsInterface());
1071 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001072 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001073 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001074 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001075 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001076 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001077 }
Calin Juravle52503d82015-11-11 16:58:31 +00001078 return common_super_class;
1079}
1080
Mathieu Chartierf8322842014-05-16 10:59:25 -07001081const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001082 const DexFile& dex_file = GetDexFile();
1083 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001084 if (dex_class_def == nullptr) {
1085 // Generated classes have no class def.
1086 return nullptr;
1087 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001088 return dex_file.GetSourceFile(*dex_class_def);
1089}
1090
1091std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001092 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001093 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001094 return dex_cache->GetLocation()->ToModifiedUtf8();
1095 }
1096 // Arrays and proxies are generated and have no corresponding dex file location.
1097 return "generated class";
1098}
1099
1100const DexFile::TypeList* Class::GetInterfaceTypeList() {
1101 const DexFile::ClassDef* class_def = GetClassDef();
1102 if (class_def == nullptr) {
1103 return nullptr;
1104 }
1105 return GetDexFile().GetInterfacesList(*class_def);
1106}
1107
Andreas Gampe542451c2016-07-26 09:02:02 -07001108void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001109 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001110 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001111 const size_t table_length = table->GetLength();
1112 SetEmbeddedVTableLength(table_length);
1113 for (size_t i = 0; i < table_length; i++) {
1114 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001115 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001116 // Keep java.lang.Object class's vtable around for since it's easier
1117 // to be reused by array classes during their linking.
1118 if (!IsObjectClass()) {
1119 SetVTable(nullptr);
1120 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001121}
1122
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001123class ReadBarrierOnNativeRootsVisitor {
1124 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001125 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001126 MemberOffset offset ATTRIBUTE_UNUSED,
1127 bool is_static ATTRIBUTE_UNUSED) const {}
1128
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001129 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001130 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001131 if (!root->IsNull()) {
1132 VisitRoot(root);
1133 }
1134 }
1135
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001136 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001137 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001138 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1139 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001140 if (old_ref != new_ref) {
1141 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1142 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001143 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001144 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001145 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1146 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001147 }
1148 }
1149};
1150
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001151// The pre-fence visitor for Class::CopyOf().
1152class CopyClassVisitor {
1153 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001154 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001155 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001156 size_t new_length,
1157 size_t copy_bytes,
1158 ImTable* imt,
1159 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001160 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001161 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001162 }
1163
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001164 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001165 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001166 StackHandleScope<1> hs(self_);
1167 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001168 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001169 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001170 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1171 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001172 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001173 // Visit all of the references to make sure there is no from space references in the native
1174 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001175 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001176 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001177 }
1178
1179 private:
1180 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001181 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001182 const size_t new_length_;
1183 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001184 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001185 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001186 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1187};
1188
Andreas Gampe542451c2016-07-26 09:02:02 -07001189Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001190 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1191 // We may get copied by a compacting GC.
1192 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001193 Handle<Class> h_this(hs.NewHandle(this));
Vladimir Marko317892b2018-05-31 11:11:32 +01001194 Runtime* runtime = Runtime::Current();
1195 gc::Heap* heap = runtime->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001196 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1197 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001198 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Vladimir Marko317892b2018-05-31 11:11:32 +01001199 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001200 ObjPtr<Object> new_class = kMovingClasses ?
Vladimir Marko317892b2018-05-31 11:11:32 +01001201 heap->AllocObject<true>(self, java_lang_Class, new_length, visitor) :
1202 heap->AllocNonMovableObject<true>(self, java_lang_Class, new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001203 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001204 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001205 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001206 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001207 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001208}
1209
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001210bool Class::ProxyDescriptorEquals(const char* match) {
1211 DCHECK(IsProxyClass());
1212 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001213}
1214
Mathieu Chartiere401d142015-04-22 13:56:20 -07001215// TODO: Move this to java_lang_Class.cc?
1216ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001217 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001218 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001219 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001220 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001221 continue;
1222 }
1223 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001224 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001225 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001226 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001227 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001228 return nullptr;
1229 }
1230 }
1231 return nullptr;
1232}
1233
Mathieu Chartiere401d142015-04-22 13:56:20 -07001234uint32_t Class::Depth() {
1235 uint32_t depth = 0;
Roland Levillaind32ead22018-05-30 17:38:21 +01001236 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001237 depth++;
1238 }
1239 return depth;
1240}
1241
Andreas Gampea5b09a62016-11-17 15:21:22 -08001242dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001243 std::string temp;
1244 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001245 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001246}
1247
Andreas Gampe542451c2016-07-26 09:02:02 -07001248template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001249ObjPtr<Method> Class::GetDeclaredMethodInternal(
1250 Thread* self,
1251 ObjPtr<Class> klass,
1252 ObjPtr<String> name,
1253 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001254 // Covariant return types permit the class to define multiple
1255 // methods with the same name and parameter types. Prefer to
1256 // return a non-synthetic method in such situations. We may
1257 // still return a synthetic method to handle situations like
1258 // escalated visibility. We never return miranda methods that
1259 // were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001260 StackHandleScope<3> hs(self);
1261 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001262 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001263 ThrowNullPointerException("name == null");
1264 return nullptr;
1265 }
1266 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001267 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001268 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001269 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1270 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001271 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001272 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001273 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1274 if (UNLIKELY(self->IsExceptionPending())) {
1275 return nullptr;
1276 }
1277 continue;
1278 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001279 if (!m.IsMiranda()) {
1280 if (!m.IsSynthetic()) {
1281 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1282 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001283 result = &m; // Remember as potential result if it's not a miranda method.
1284 }
1285 }
1286 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001287 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001288 auto modifiers = m.GetAccessFlags();
1289 if ((modifiers & kAccConstructor) != 0) {
1290 continue;
1291 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001292 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001293 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001294 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001295 if (np_name == nullptr) {
1296 self->AssertPendingException();
1297 return nullptr;
1298 }
1299 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1300 if (UNLIKELY(self->IsExceptionPending())) {
1301 return nullptr;
1302 }
1303 continue;
1304 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001305 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
1306 if ((modifiers & kAccSynthetic) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001307 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001308 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001309 result = &m; // Remember as potential result.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001310 }
1311 }
1312 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001313 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001314 : nullptr;
1315}
1316
1317template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001318ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001319 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001320 ObjPtr<Class> klass,
1321 ObjPtr<String> name,
1322 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001323template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001324ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001325 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001326 ObjPtr<Class> klass,
1327 ObjPtr<String> name,
1328 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001329template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001330ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001331 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001332 ObjPtr<Class> klass,
1333 ObjPtr<String> name,
1334 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001335template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001336ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001337 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001338 ObjPtr<Class> klass,
1339 ObjPtr<String> name,
1340 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001341
Andreas Gampe542451c2016-07-26 09:02:02 -07001342template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001343ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001344 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001345 ObjPtr<Class> klass,
1346 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001347 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001348 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001349 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001350 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001351 : nullptr;
1352}
1353
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001354// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001355
Andreas Gampe542451c2016-07-26 09:02:02 -07001356template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001357ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001358 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001359 ObjPtr<Class> klass,
1360 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001361template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001362ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001363 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001364 ObjPtr<Class> klass,
1365 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001366template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001367ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001368 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001369 ObjPtr<Class> klass,
1370 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001371template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001372ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001373 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001374 ObjPtr<Class> klass,
1375 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001376
Andreas Gampe715fdc22016-04-18 17:07:30 -07001377int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1378 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1379 return default_value;
1380 }
1381 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001382 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001383 return default_value;
1384 }
1385 return flags;
1386}
1387
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001388void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1389 if (Runtime::Current()->IsActiveTransaction()) {
1390 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1391 } else {
1392 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1393 }
1394}
1395
David Sehr709b0702016-10-13 09:12:37 -07001396std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1397 if (klass == nullptr) {
1398 return "null";
1399 }
1400 return klass->PrettyDescriptor();
1401}
1402
1403std::string Class::PrettyDescriptor() {
1404 std::string temp;
1405 return art::PrettyDescriptor(GetDescriptor(&temp));
1406}
1407
1408std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1409 if (c == nullptr) {
1410 return "null";
1411 }
1412 return c->PrettyClass();
1413}
1414
1415std::string Class::PrettyClass() {
1416 std::string result;
1417 result += "java.lang.Class<";
1418 result += PrettyDescriptor();
1419 result += ">";
1420 return result;
1421}
1422
1423std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1424 if (c == nullptr) {
1425 return "null";
1426 }
1427 return c->PrettyClassAndClassLoader();
1428}
1429
1430std::string Class::PrettyClassAndClassLoader() {
1431 std::string result;
1432 result += "java.lang.Class<";
1433 result += PrettyDescriptor();
1434 result += ",";
1435 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1436 // TODO: add an identifying hash value for the loader
1437 result += ">";
1438 return result;
1439}
1440
Andreas Gampe90b936d2017-01-31 08:58:55 -08001441template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1442 // Check class is loaded/retired or this is java.lang.String that has a
1443 // circularity issue during loading the names of its members
1444 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1445 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001446 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001447 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1448 << " IsRetired=" << IsRetired<kVerifyFlags>()
1449 << " IsErroneous=" <<
1450 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001451 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001452 << " status= " << GetStatus<kVerifyFlags>()
1453 << " descriptor=" << PrettyDescriptor();
1454}
1455// Instantiate the common cases.
1456template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1457template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1458template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1459template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1460template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1461
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001462} // namespace mirror
1463} // namespace art