blob: e33e407149e86d4e54c14c90abfba68a4b062f33 [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"
Andreas Gampee15b9b12018-10-29 12:54:27 -070035#include "gc/heap-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070036#include "handle_scope-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070037#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070038#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070039#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080040#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070041#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070042#include "object_lock.h"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010043#include "string-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070044#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "thread.h"
46#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "well_known_classes.h"
48
49namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070050
51// TODO: move to own CC file?
52constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
53constexpr size_t BitString::kCapacity;
54
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055namespace mirror {
56
Andreas Gampe46ee31b2016-12-14 10:11:49 -080057using android::base::StringPrintf;
58
Vladimir Marko7287c4d2018-02-15 10:41:07 +000059ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
60 const char* expected_name = nullptr;
Vladimir Markob4eb1b12018-05-24 11:09:38 +010061 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid.
Vladimir Marko7287c4d2018-02-15 10:41:07 +000062 if (name != nullptr && name->GetLength() >= 2) {
63 // Perfect hash for the expected values: from the second letters of the primitive types,
64 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
65 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
66 switch (hash) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010067 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break;
68 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break;
69 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break;
70 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break;
71 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break;
72 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break;
73 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break;
74 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break;
75 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break;
Vladimir Marko7287c4d2018-02-15 10:41:07 +000076 default: break;
77 }
78 }
79 if (expected_name != nullptr && name->Equals(expected_name)) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010080 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000081 DCHECK(klass != nullptr);
82 return klass;
83 } else {
84 Thread* self = Thread::Current();
85 if (name == nullptr) {
86 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
Andreas Gampe98ea9d92018-10-19 14:06:15 -070087 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg= */ nullptr);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000088 } else {
89 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
90 }
91 return nullptr;
92 }
93}
94
Alex Light0273ad12016-11-02 11:19:31 -070095ClassExt* Class::EnsureExtDataPresent(Thread* self) {
96 ObjPtr<ClassExt> existing(GetExtData());
97 if (!existing.IsNull()) {
98 return existing.Ptr();
99 }
100 StackHandleScope<3> hs(self);
101 // Handlerize 'this' since we are allocating here.
102 Handle<Class> h_this(hs.NewHandle(this));
103 // Clear exception so we can allocate.
104 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
105 self->ClearException();
106 // Allocate the ClassExt
107 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800108 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700109 // OOM allocating the classExt.
110 // TODO Should we restore the suppressed exception?
111 self->AssertPendingOOMException();
112 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800113 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700114 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
115 bool set;
116 // Set the ext_data_ field using CAS semantics.
117 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700118 set = h_this->CasFieldObject<true>(ext_offset,
119 nullptr,
120 new_ext.Get(),
121 CASMode::kStrong,
122 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700123 } else {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700124 set = h_this->CasFieldObject<false>(ext_offset,
125 nullptr,
126 new_ext.Get(),
127 CASMode::kStrong,
128 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700129 }
130 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
131 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
132 CHECK(!ret.IsNull());
133 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800134 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700135 self->SetException(throwable.Get());
136 }
137 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800138 }
139}
140
Vladimir Marko2c64a832018-01-04 11:31:56 +0000141void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
142 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700143 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
144 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700145 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000146 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000147 new_status != ClassStatus::kErrorUnresolved &&
148 new_status != ClassStatus::kErrorResolved &&
149 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700150 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700151 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700152 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000153 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700154 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700155 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700156 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700157 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700158 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800159 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000160 if (UNLIKELY(IsErroneous(new_status))) {
161 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700162 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000163 << h_this->PrettyClass()
164 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000165 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700166 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700167 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700168 if (self->IsExceptionPending()) {
169 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
170 }
171 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172
Alex Light0273ad12016-11-02 11:19:31 -0700173 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
174 if (!ext.IsNull()) {
175 self->AssertPendingException();
176 ext->SetVerifyError(self->GetException());
177 } else {
178 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700179 }
180 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 }
Alex Light0273ad12016-11-02 11:19:31 -0700182
Vladimir Marko305c38b2018-02-14 11:50:07 +0000183 if (kBitstringSubtypeCheckEnabled) {
184 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700185 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
186 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000187 } else {
188 // The ClassStatus is always in the 4 most-significant bits of status_.
189 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
190 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
191 if (Runtime::Current()->IsActiveTransaction()) {
192 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
193 } else {
194 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
195 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700196 }
197
198 // Setting the object size alloc fast path needs to be after the status write so that if the
199 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
200 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000201 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700202 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
203 // Finalizable objects must always go slow path.
204 if (!h_this->IsFinalizable()) {
205 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700206 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100207 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700208
209 if (!class_linker_initialized) {
210 // When the class linker is being initialized its single threaded and by definition there can be
211 // no waiters. During initialization classes may appear temporary but won't be retired as their
212 // size was statically computed.
213 } else {
214 // Classes that are being resolved or initialized need to notify waiters that the class status
215 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700216 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700217 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
218 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000219 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
220 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700221 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700222 }
223 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000224 CHECK_NE(new_status, ClassStatus::kRetired);
225 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700226 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700227 }
228 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700229 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230}
231
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700232void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700233 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234}
235
Ian Rogersef7d42f2014-01-06 12:55:46 -0800236void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700238 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
239 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700240 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700241 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700242 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243}
244
245// Return the class' name. The exact format is bizarre, but it's the specified behavior for
246// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
247// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
248// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700249String* Class::ComputeName(Handle<Class> h_this) {
250 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800251 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 return name;
253 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700254 std::string temp;
255 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800256 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
258 // The descriptor indicates that this is the class for
259 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700260 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 switch (descriptor[0]) {
262 case 'Z': c_name = "boolean"; break;
263 case 'B': c_name = "byte"; break;
264 case 'C': c_name = "char"; break;
265 case 'S': c_name = "short"; break;
266 case 'I': c_name = "int"; break;
267 case 'J': c_name = "long"; break;
268 case 'F': c_name = "float"; break;
269 case 'D': c_name = "double"; break;
270 case 'V': c_name = "void"; break;
271 default:
272 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
273 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800274 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 } else {
276 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
277 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700278 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700280 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 return name;
282}
283
Ian Rogersef7d42f2014-01-06 12:55:46 -0800284void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700286 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 if ((flags & kDumpClassClassLoader) != 0) {
288 os << ' ' << GetClassLoader();
289 }
290 if ((flags & kDumpClassInitialized) != 0) {
291 os << ' ' << GetStatus();
292 }
293 os << "\n";
294 return;
295 }
296
Mathieu Chartiere401d142015-04-22 13:56:20 -0700297 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700298 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700299 Handle<Class> h_this(hs.NewHandle(this));
300 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700302
Ian Rogers1ff3c982014-08-12 02:30:58 -0700303 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700305 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800307 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 os << StringPrintf(" access=0x%04x.%04x\n",
309 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800310 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700311 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700312 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 }
314 if (IsArrayClass()) {
315 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
316 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700317 const size_t num_direct_interfaces = NumDirectInterfaces();
318 if (num_direct_interfaces > 0) {
319 os << " interfaces (" << num_direct_interfaces << "):\n";
320 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000321 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700322 if (interface == nullptr) {
323 os << StringPrintf(" %2zd: nullptr!\n", i);
324 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700325 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
326 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700327 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 }
329 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700330 if (!IsLoaded()) {
331 os << " class not yet loaded";
332 } else {
333 // After this point, this may have moved due to GetDirectInterface.
334 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800335 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700336 for (size_t i = 0; i < NumVirtualMethods(); ++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->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700340 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
341 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700342 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700344 }
345 if (h_this->NumStaticFields() > 0) {
346 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000347 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700348 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700349 os << StringPrintf(" %2zd: %s\n", i,
350 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700351 }
352 } else {
353 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700355 }
356 if (h_this->NumInstanceFields() > 0) {
357 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000358 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700359 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700360 os << StringPrintf(" %2zd: %s\n", i,
361 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700362 }
363 } else {
364 os << " <not yet available>";
365 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 }
367 }
368}
369
370void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 // Sanity check that the number of bits set in the reference offset bitmap
373 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700374 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700375 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800376 count += c->NumReferenceInstanceFieldsDuringLinking();
377 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700378 // +1 for the Class in Object.
379 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100381 // Not called within a transaction.
382 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700383 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384}
385
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
387 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700388 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
389 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 ++i;
391 }
392 if (descriptor1.find('/', i) != StringPiece::npos ||
393 descriptor2.find('/', i) != StringPiece::npos) {
394 return false;
395 } else {
396 return true;
397 }
398}
399
Mathieu Chartier3398c782016-09-30 10:27:43 -0700400bool Class::IsInSamePackage(ObjPtr<Class> that) {
401 ObjPtr<Class> klass1 = this;
402 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 if (klass1 == klass2) {
404 return true;
405 }
406 // Class loaders must match.
407 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
408 return false;
409 }
410 // Arrays are in the same package when their element classes are.
411 while (klass1->IsArrayClass()) {
412 klass1 = klass1->GetComponentType();
413 }
414 while (klass2->IsArrayClass()) {
415 klass2 = klass2->GetComponentType();
416 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700417 // trivial check again for array types
418 if (klass1 == klass2) {
419 return true;
420 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700422 std::string temp1, temp2;
423 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424}
425
Ian Rogersef7d42f2014-01-06 12:55:46 -0800426bool Class::IsThrowableClass() {
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100427 return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428}
429
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700430void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100431 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700432 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100433 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700434 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100435 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436}
437
Vladimir Markoba118822017-06-12 15:41:56 +0100438template <typename SignatureType>
439static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
440 const StringPiece& name,
441 const SignatureType& signature,
442 PointerSize pointer_size)
443 REQUIRES_SHARED(Locks::mutator_lock_) {
444 // If the current class is not an interface, skip the search of its declared methods;
445 // such lookup is used only to distinguish between IncompatibleClassChangeError and
446 // NoSuchMethodError and the caller has already tried to search methods in the class.
447 if (LIKELY(klass->IsInterface())) {
448 // Search declared methods, both direct and virtual.
449 // (This lookup is used also for invoke-static on interface classes.)
450 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
451 if (method.GetName() == name && method.GetSignature() == signature) {
452 return &method;
453 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 }
455 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700456
Vladimir Markoba118822017-06-12 15:41:56 +0100457 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
458 // we should return it, otherwise an arbitrary one can be returned.
459 ObjPtr<IfTable> iftable = klass->GetIfTable();
460 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
461 ObjPtr<Class> iface = iftable->GetInterface(i);
462 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
463 if (method.GetName() == name && method.GetSignature() == signature) {
464 return &method;
465 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700466 }
467 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468
Vladimir Markoba118822017-06-12 15:41:56 +0100469 // Then search for public non-static methods in the java.lang.Object.
470 if (LIKELY(klass->IsInterface())) {
471 ObjPtr<Class> object_class = klass->GetSuperClass();
472 DCHECK(object_class->IsObjectClass());
473 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
474 if (method.IsPublic() && !method.IsStatic() &&
475 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700476 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477 }
478 }
479 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700480 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481}
482
Vladimir Markoba118822017-06-12 15:41:56 +0100483ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
484 const StringPiece& signature,
485 PointerSize pointer_size) {
486 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487}
488
Vladimir Markoba118822017-06-12 15:41:56 +0100489ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
490 const Signature& signature,
491 PointerSize pointer_size) {
492 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700493}
494
Vladimir Markoba118822017-06-12 15:41:56 +0100495ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
496 uint32_t dex_method_idx,
497 PointerSize pointer_size) {
498 // We always search by name and signature, ignoring the type index in the MethodId.
499 const DexFile& dex_file = *dex_cache->GetDexFile();
500 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
501 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
502 const Signature signature = dex_file.GetMethodSignature(method_id);
503 return FindInterfaceMethod(name, signature, pointer_size);
504}
505
Alex Lightafb66472017-08-01 09:54:49 -0700506static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
507 ObjPtr<mirror::Class> declaring_class)
508 REQUIRES_SHARED(Locks::mutator_lock_) {
509 if (klass->IsArrayClass()) {
510 return declaring_class->IsObjectClass();
511 } else if (klass->IsInterface()) {
512 return declaring_class->IsObjectClass() || declaring_class == klass;
513 } else {
514 return klass->IsSubClass(declaring_class);
515 }
516}
517
Vladimir Markoba118822017-06-12 15:41:56 +0100518static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
519 ObjPtr<mirror::Class> declaring_class,
520 ArtMethod& method)
521 REQUIRES_SHARED(Locks::mutator_lock_) {
522 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
523 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700524 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100525 uint32_t access_flags = method.GetAccessFlags();
526 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
527 return true;
528 }
529 if ((access_flags & kAccPrivate) != 0) {
530 return false;
531 }
532 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
533 if (!klass->IsInSamePackage(declaring_class)) {
534 return false;
535 }
536 }
537 return true;
538}
539
540template <typename SignatureType>
541static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
542 const StringPiece& name,
543 const SignatureType& signature,
544 PointerSize pointer_size)
545 REQUIRES_SHARED(Locks::mutator_lock_) {
546 // Search declared methods first.
547 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
548 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
549 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
550 return &method;
551 }
552 }
553
554 // Then search the superclass chain. If we find an inherited method, return it.
555 // If we find a method that's not inherited because of access restrictions,
556 // try to find a method inherited from an interface in copied methods.
557 ObjPtr<Class> klass = this_klass->GetSuperClass();
558 ArtMethod* uninherited_method = nullptr;
559 for (; klass != nullptr; klass = klass->GetSuperClass()) {
560 DCHECK(!klass->IsProxyClass());
561 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
562 if (method.GetName() == name && method.GetSignature() == signature) {
563 if (IsInheritedMethod(this_klass, klass, method)) {
564 return &method;
565 }
566 uninherited_method = &method;
567 break;
568 }
569 }
570 if (uninherited_method != nullptr) {
571 break;
572 }
573 }
574
575 // Then search copied methods.
576 // If we found a method that's not inherited, stop the search in its declaring class.
577 ObjPtr<Class> end_klass = klass;
578 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
579 klass = this_klass;
580 if (UNLIKELY(klass->IsProxyClass())) {
581 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
582 klass = klass->GetSuperClass();
583 }
584 for (; klass != end_klass; klass = klass->GetSuperClass()) {
585 DCHECK(!klass->IsProxyClass());
586 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
587 if (method.GetName() == name && method.GetSignature() == signature) {
588 return &method; // No further check needed, copied methods are inherited by definition.
589 }
590 }
591 }
592 return uninherited_method; // Return the `uninherited_method` if any.
593}
594
595
596ArtMethod* Class::FindClassMethod(const StringPiece& name,
597 const StringPiece& signature,
598 PointerSize pointer_size) {
599 return FindClassMethodWithSignature(this, name, signature, pointer_size);
600}
601
602ArtMethod* Class::FindClassMethod(const StringPiece& name,
603 const Signature& signature,
604 PointerSize pointer_size) {
605 return FindClassMethodWithSignature(this, name, signature, pointer_size);
606}
607
608ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
609 uint32_t dex_method_idx,
610 PointerSize pointer_size) {
611 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
612 DCHECK(!IsProxyClass());
613
614 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
615 ObjPtr<DexCache> this_dex_cache = GetDexCache();
616 if (this_dex_cache == dex_cache) {
617 // Lookup is always performed in the class referenced by the MethodId.
618 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
619 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
620 if (method.GetDexMethodIndex() == dex_method_idx) {
621 return &method;
622 }
623 }
624 }
625 // If not found, we need to search by name and signature.
626 const DexFile& dex_file = *dex_cache->GetDexFile();
627 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
628 const Signature signature = dex_file.GetMethodSignature(method_id);
629 StringPiece name; // Delay strlen() until actually needed.
630 // If we do not have a dex_cache match, try to find the declared method in this class now.
631 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
632 DCHECK(name.empty());
David Srbecky39d8c872018-11-01 16:44:20 +0000633 // Avoid string comparisons by comparing the respective unicode lengths first.
634 uint32_t length, other_length; // UTF16 length.
635 name = dex_file.GetMethodName(method_id, &length);
Vladimir Markoba118822017-06-12 15:41:56 +0100636 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
David Srbecky39d8c872018-11-01 16:44:20 +0000637 DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex);
638 const char* other_name = method.GetDexFile()->GetMethodName(
639 method.GetDexMethodIndex(), &other_length);
640 if (length == other_length && name == other_name && signature == method.GetSignature()) {
Vladimir Markoba118822017-06-12 15:41:56 +0100641 return &method;
642 }
643 }
644 }
645
646 // Then search the superclass chain. If we find an inherited method, return it.
647 // If we find a method that's not inherited because of access restrictions,
648 // try to find a method inherited from an interface in copied methods.
649 ArtMethod* uninherited_method = nullptr;
650 ObjPtr<Class> klass = GetSuperClass();
651 for (; klass != nullptr; klass = klass->GetSuperClass()) {
652 ArtMethod* candidate_method = nullptr;
653 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
654 if (klass->GetDexCache() == dex_cache) {
655 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
656 // the type index differs, so compare the name index and proto index.
657 for (ArtMethod& method : declared_methods) {
658 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
659 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
660 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
661 candidate_method = &method;
662 break;
663 }
664 }
665 } else {
666 if (!declared_methods.empty() && name.empty()) {
667 name = dex_file.StringDataByIdx(method_id.name_idx_);
668 }
669 for (ArtMethod& method : declared_methods) {
670 if (method.GetName() == name && method.GetSignature() == signature) {
671 candidate_method = &method;
672 break;
673 }
674 }
675 }
676 if (candidate_method != nullptr) {
677 if (IsInheritedMethod(this, klass, *candidate_method)) {
678 return candidate_method;
679 } else {
680 uninherited_method = candidate_method;
681 break;
682 }
683 }
684 }
685
686 // Then search copied methods.
687 // If we found a method that's not inherited, stop the search in its declaring class.
688 ObjPtr<Class> end_klass = klass;
689 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
690 // After we have searched the declared methods of the super-class chain,
691 // search copied methods which can contain methods from interfaces.
692 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
693 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
694 if (!copied_methods.empty() && name.empty()) {
695 name = dex_file.StringDataByIdx(method_id.name_idx_);
696 }
697 for (ArtMethod& method : copied_methods) {
698 if (method.GetName() == name && method.GetSignature() == signature) {
699 return &method; // No further check needed, copied methods are inherited by definition.
700 }
701 }
702 }
703 return uninherited_method; // Return the `uninherited_method` if any.
704}
705
706ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
707 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
708 DCHECK(!IsProxyClass());
709 StringPiece name("<init>");
710 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
711 if (method.GetName() == name && method.GetSignature() == signature) {
712 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800713 }
714 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700715 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800716}
717
Andreas Gampe542451c2016-07-26 09:02:02 -0700718ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
719 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000720 for (auto& method : GetDirectMethods(pointer_size)) {
721 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
722 if (name == np_method->GetName()) {
723 return &method;
724 }
725 }
726 return nullptr;
727}
728
Andreas Gampe542451c2016-07-26 09:02:02 -0700729ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
730 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000731 for (auto& method : GetVirtualMethods(pointer_size)) {
732 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
733 if (name == np_method->GetName()) {
734 return &method;
735 }
736 }
737 return nullptr;
738}
739
Andreas Gampe542451c2016-07-26 09:02:02 -0700740ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700741 DCHECK(method->GetDeclaringClass()->IsInterface());
742 DCHECK(IsInterface()) << "Should only be called on a interface class";
743 // Check if we have one defined on this interface first. This includes searching copied ones to
744 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
745 // don't do any indirect method checks here.
746 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
747 if (method->HasSameNameAndSignature(&iface_method)) {
748 return &iface_method;
749 }
750 }
751
752 std::vector<ArtMethod*> abstract_methods;
753 // Search through the IFTable for a working version. We don't need to check for conflicts
754 // because if there was one it would appear in this classes virtual_methods_ above.
755
756 Thread* self = Thread::Current();
757 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700758 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
759 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700760 size_t iftable_count = GetIfTableCount();
761 // Find the method. We don't need to check for conflicts because they would have been in the
762 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
763 // subtypiest interfaces get visited first.
764 for (size_t k = iftable_count; k != 0;) {
765 k--;
766 DCHECK_LT(k, iftable->Count());
767 iface.Assign(iftable->GetInterface(k));
768 // Iterate through every declared method on this interface. Each direct method's name/signature
769 // is unique so the order of the inner loop doesn't matter.
770 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
771 ArtMethod* current_method = &method_iter;
772 if (current_method->HasSameNameAndSignature(method)) {
773 if (current_method->IsDefault()) {
774 // Handle JLS soft errors, a default method from another superinterface tree can
775 // "override" an abstract method(s) from another superinterface tree(s). To do this,
776 // ignore any [default] method which are dominated by the abstract methods we've seen so
777 // far. Check if overridden by any in abstract_methods. We do not need to check for
778 // default_conflicts because we would hit those before we get to this loop.
779 bool overridden = false;
780 for (ArtMethod* possible_override : abstract_methods) {
781 DCHECK(possible_override->HasSameNameAndSignature(current_method));
782 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
783 overridden = true;
784 break;
785 }
786 }
787 if (!overridden) {
788 return current_method;
789 }
790 } else {
791 // Is not default.
792 // This might override another default method. Just stash it for now.
793 abstract_methods.push_back(current_method);
794 }
795 }
796 }
797 }
798 // If we reach here we either never found any declaration of the method (in which case
799 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
800 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
801 // of these arbitrarily.
802 return abstract_methods.empty() ? nullptr : abstract_methods[0];
803}
804
Andreas Gampe542451c2016-07-26 09:02:02 -0700805ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700806 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
807 if (method.IsClassInitializer()) {
808 DCHECK_STREQ(method.GetName(), "<clinit>");
809 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
810 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700811 }
812 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700813 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700814}
815
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700816// Custom binary search to avoid double comparisons from std::binary_search.
817static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
818 const StringPiece& name,
819 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700820 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700821 if (fields == nullptr) {
822 return nullptr;
823 }
824 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100825 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700826 ArtField* ret = nullptr;
827 while (low < high) {
828 size_t mid = (low + high) / 2;
829 ArtField& field = fields->At(mid);
830 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
831 // verifier. There can be multiple fields with the same in the same class name due to proguard.
832 int result = StringPiece(field.GetName()).Compare(name);
833 if (result == 0) {
834 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
835 }
836 if (result < 0) {
837 low = mid + 1;
838 } else if (result > 0) {
839 high = mid;
840 } else {
841 ret = &field;
842 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843 }
844 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700845 if (kIsDebugBuild) {
846 ArtField* found = nullptr;
847 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
848 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
849 found = &field;
850 break;
851 }
852 }
David Sehr709b0702016-10-13 09:12:37 -0700853 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700854 }
855 return ret;
856}
857
858ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
859 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
860 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800861}
862
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700863ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800864 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700865 for (ArtField& field : GetIFields()) {
866 if (field.GetDexFieldIndex() == dex_field_idx) {
867 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800868 }
869 }
870 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700871 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800872}
873
Brian Carlstromea46f952013-07-30 01:26:50 -0700874ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800875 // Is the field in this class, or any of its superclasses?
876 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700877 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700878 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700879 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800880 return f;
881 }
882 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700883 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800884}
885
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700886ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800887 // Is the field in this class, or any of its superclasses?
888 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700889 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700890 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700891 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800892 return f;
893 }
894 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700895 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800896}
897
Brian Carlstromea46f952013-07-30 01:26:50 -0700898ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700899 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700900 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901}
902
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700903ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800904 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700905 for (ArtField& field : GetSFields()) {
906 if (field.GetDexFieldIndex() == dex_field_idx) {
907 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800908 }
909 }
910 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700911 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800912}
913
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700914ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000915 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700916 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700917 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800918 // Is the field in this class (or its interfaces), or any of its
919 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000920 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800921 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700922 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700923 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800924 return f;
925 }
926 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000927 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
928 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
929 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700930 f = FindStaticField(self, interface, name, type);
931 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800932 return f;
933 }
934 }
935 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700936 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800937}
938
Vladimir Markobb268b12016-06-30 15:52:56 +0100939ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700940 ObjPtr<Class> klass,
941 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700942 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700943 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800944 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700945 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700946 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800947 return f;
948 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100949 // Though GetDirectInterface() should not cause thread suspension when called
950 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700951 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800952 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000953 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
954 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
955 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700956 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
957 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800958 return f;
959 }
960 }
961 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700962 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800963}
964
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700965ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000966 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700967 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700968 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800969 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000970 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800971 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700972 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700973 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800974 return f;
975 }
976 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700977 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800978 return f;
979 }
980 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000981 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
982 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
983 DCHECK(interface != nullptr);
984 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700985 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800986 return f;
987 }
988 }
989 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700990 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800991}
992
Andreas Gampe542451c2016-07-26 09:02:02 -0700993void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700994 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800995 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700996 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800997 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700998 }
999 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +02001000}
1001
Ian Rogers1ff3c982014-08-12 02:30:58 -07001002const char* Class::GetDescriptor(std::string* storage) {
1003 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001004 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001005 } else if (IsArrayClass()) {
1006 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001007 } else if (IsProxyClass()) {
1008 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001009 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001010 } else {
1011 const DexFile& dex_file = GetDexFile();
1012 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
1013 return dex_file.GetTypeDescriptor(type_id);
1014 }
1015}
1016
Ian Rogers1ff3c982014-08-12 02:30:58 -07001017const char* Class::GetArrayDescriptor(std::string* storage) {
1018 std::string temp;
1019 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1020 *storage = "[";
1021 *storage += elem_desc;
1022 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001023}
1024
1025const DexFile::ClassDef* Class::GetClassDef() {
1026 uint16_t class_def_idx = GetDexClassDefIndex();
1027 if (class_def_idx == DexFile::kDexNoIndex16) {
1028 return nullptr;
1029 }
1030 return &GetDexFile().GetClassDef(class_def_idx);
1031}
1032
Andreas Gampea5b09a62016-11-17 15:21:22 -08001033dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001034 DCHECK(!IsPrimitive());
1035 DCHECK(!IsArrayClass());
1036 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1037}
1038
Vladimir Marko19a4d372016-12-08 14:41:46 +00001039ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1040 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001041 DCHECK(!klass->IsPrimitive());
1042 if (klass->IsArrayClass()) {
1043 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001044 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1045 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001046 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001047 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001048 } else {
1049 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001050 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001051 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001052 DCHECK(interface != nullptr);
1053 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001054 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001055 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001056 DCHECK(interfaces != nullptr);
1057 return interfaces->Get(idx);
1058 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001059 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001060 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001061 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001062 return interface;
1063 }
1064}
1065
Vladimir Marko19a4d372016-12-08 14:41:46 +00001066ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1067 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1068 if (interface == nullptr) {
1069 DCHECK(!klass->IsArrayClass());
1070 DCHECK(!klass->IsProxyClass());
1071 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001072 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001073 CHECK(interface != nullptr || self->IsExceptionPending());
1074 }
1075 return interface;
1076}
1077
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001078ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001079 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001080 DCHECK(!klass->IsInterface());
1081 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001082 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001083 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001084 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001085 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001086 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001087 }
Calin Juravle52503d82015-11-11 16:58:31 +00001088 return common_super_class;
1089}
1090
Mathieu Chartierf8322842014-05-16 10:59:25 -07001091const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001092 const DexFile& dex_file = GetDexFile();
1093 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001094 if (dex_class_def == nullptr) {
1095 // Generated classes have no class def.
1096 return nullptr;
1097 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001098 return dex_file.GetSourceFile(*dex_class_def);
1099}
1100
1101std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001102 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001103 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001104 return dex_cache->GetLocation()->ToModifiedUtf8();
1105 }
1106 // Arrays and proxies are generated and have no corresponding dex file location.
1107 return "generated class";
1108}
1109
1110const DexFile::TypeList* Class::GetInterfaceTypeList() {
1111 const DexFile::ClassDef* class_def = GetClassDef();
1112 if (class_def == nullptr) {
1113 return nullptr;
1114 }
1115 return GetDexFile().GetInterfacesList(*class_def);
1116}
1117
Andreas Gampe542451c2016-07-26 09:02:02 -07001118void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001119 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001120 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001121 const size_t table_length = table->GetLength();
1122 SetEmbeddedVTableLength(table_length);
1123 for (size_t i = 0; i < table_length; i++) {
1124 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001125 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001126 // Keep java.lang.Object class's vtable around for since it's easier
1127 // to be reused by array classes during their linking.
1128 if (!IsObjectClass()) {
1129 SetVTable(nullptr);
1130 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001131}
1132
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001133class ReadBarrierOnNativeRootsVisitor {
1134 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001135 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001136 MemberOffset offset ATTRIBUTE_UNUSED,
1137 bool is_static ATTRIBUTE_UNUSED) const {}
1138
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001139 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001140 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001141 if (!root->IsNull()) {
1142 VisitRoot(root);
1143 }
1144 }
1145
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001146 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001147 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001148 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1149 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001150 if (old_ref != new_ref) {
1151 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1152 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001153 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001154 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001155 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1156 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001157 }
1158 }
1159};
1160
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001161// The pre-fence visitor for Class::CopyOf().
1162class CopyClassVisitor {
1163 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001164 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001165 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001166 size_t new_length,
1167 size_t copy_bytes,
1168 ImTable* imt,
1169 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001170 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001171 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001172 }
1173
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001174 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001175 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001176 StackHandleScope<1> hs(self_);
1177 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001178 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001179 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001180 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1181 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001182 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001183 // Visit all of the references to make sure there is no from space references in the native
1184 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001185 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001186 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001187 }
1188
1189 private:
1190 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001191 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001192 const size_t new_length_;
1193 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001194 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001195 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001196 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1197};
1198
Andreas Gampe542451c2016-07-26 09:02:02 -07001199Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001200 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1201 // We may get copied by a compacting GC.
1202 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001203 Handle<Class> h_this(hs.NewHandle(this));
Vladimir Marko317892b2018-05-31 11:11:32 +01001204 Runtime* runtime = Runtime::Current();
1205 gc::Heap* heap = runtime->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001206 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1207 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001208 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Vladimir Marko317892b2018-05-31 11:11:32 +01001209 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001210 ObjPtr<Object> new_class = kMovingClasses ?
Vladimir Marko317892b2018-05-31 11:11:32 +01001211 heap->AllocObject<true>(self, java_lang_Class, new_length, visitor) :
1212 heap->AllocNonMovableObject<true>(self, java_lang_Class, new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001213 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001214 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001215 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001216 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001217 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001218}
1219
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001220bool Class::ProxyDescriptorEquals(const char* match) {
1221 DCHECK(IsProxyClass());
1222 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001223}
1224
Mathieu Chartiere401d142015-04-22 13:56:20 -07001225// TODO: Move this to java_lang_Class.cc?
1226ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001227 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001228 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001229 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001230 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001231 continue;
1232 }
1233 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001234 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001235 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001236 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001237 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001238 return nullptr;
1239 }
1240 }
1241 return nullptr;
1242}
1243
Mathieu Chartiere401d142015-04-22 13:56:20 -07001244uint32_t Class::Depth() {
1245 uint32_t depth = 0;
Roland Levillaind32ead22018-05-30 17:38:21 +01001246 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001247 depth++;
1248 }
1249 return depth;
1250}
1251
Andreas Gampea5b09a62016-11-17 15:21:22 -08001252dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001253 std::string temp;
1254 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001255 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001256}
1257
Andreas Gampe542451c2016-07-26 09:02:02 -07001258template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001259ObjPtr<Method> Class::GetDeclaredMethodInternal(
1260 Thread* self,
1261 ObjPtr<Class> klass,
1262 ObjPtr<String> name,
1263 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001264 // Covariant return types permit the class to define multiple
1265 // methods with the same name and parameter types. Prefer to
1266 // return a non-synthetic method in such situations. We may
1267 // still return a synthetic method to handle situations like
1268 // escalated visibility. We never return miranda methods that
1269 // were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001270 StackHandleScope<3> hs(self);
1271 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001272 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001273 ThrowNullPointerException("name == null");
1274 return nullptr;
1275 }
1276 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001277 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001278 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001279 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1280 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001281 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001282 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001283 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1284 if (UNLIKELY(self->IsExceptionPending())) {
1285 return nullptr;
1286 }
1287 continue;
1288 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001289 if (!m.IsMiranda()) {
1290 if (!m.IsSynthetic()) {
1291 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1292 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001293 result = &m; // Remember as potential result if it's not a miranda method.
1294 }
1295 }
1296 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001297 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001298 auto modifiers = m.GetAccessFlags();
1299 if ((modifiers & kAccConstructor) != 0) {
1300 continue;
1301 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001302 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001303 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001304 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001305 if (np_name == nullptr) {
1306 self->AssertPendingException();
1307 return nullptr;
1308 }
1309 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1310 if (UNLIKELY(self->IsExceptionPending())) {
1311 return nullptr;
1312 }
1313 continue;
1314 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001315 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
1316 if ((modifiers & kAccSynthetic) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001317 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001318 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001319 result = &m; // Remember as potential result.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001320 }
1321 }
1322 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001323 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001324 : nullptr;
1325}
1326
1327template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001328ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
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 Gampebc4d2182016-02-22 10:03:12 -08001333template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001334ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
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, false>(
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 Gampee01e3642016-07-25 13:06:04 -07001345template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001346ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001347 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001348 ObjPtr<Class> klass,
1349 ObjPtr<String> name,
1350 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001351
Andreas Gampe542451c2016-07-26 09:02:02 -07001352template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001353ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001354 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001355 ObjPtr<Class> klass,
1356 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001357 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001358 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001359 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001360 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001361 : nullptr;
1362}
1363
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001364// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001365
Andreas Gampe542451c2016-07-26 09:02:02 -07001366template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001367ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -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::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001373 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001374 ObjPtr<Class> klass,
1375 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001376template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001377ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001378 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001379 ObjPtr<Class> klass,
1380 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001381template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001382ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001383 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001384 ObjPtr<Class> klass,
1385 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001386
Andreas Gampe715fdc22016-04-18 17:07:30 -07001387int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1388 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1389 return default_value;
1390 }
1391 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001392 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001393 return default_value;
1394 }
1395 return flags;
1396}
1397
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001398void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1399 if (Runtime::Current()->IsActiveTransaction()) {
1400 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1401 } else {
1402 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1403 }
1404}
1405
David Sehr709b0702016-10-13 09:12:37 -07001406std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1407 if (klass == nullptr) {
1408 return "null";
1409 }
1410 return klass->PrettyDescriptor();
1411}
1412
1413std::string Class::PrettyDescriptor() {
1414 std::string temp;
1415 return art::PrettyDescriptor(GetDescriptor(&temp));
1416}
1417
1418std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1419 if (c == nullptr) {
1420 return "null";
1421 }
1422 return c->PrettyClass();
1423}
1424
1425std::string Class::PrettyClass() {
1426 std::string result;
1427 result += "java.lang.Class<";
1428 result += PrettyDescriptor();
1429 result += ">";
1430 return result;
1431}
1432
1433std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1434 if (c == nullptr) {
1435 return "null";
1436 }
1437 return c->PrettyClassAndClassLoader();
1438}
1439
1440std::string Class::PrettyClassAndClassLoader() {
1441 std::string result;
1442 result += "java.lang.Class<";
1443 result += PrettyDescriptor();
1444 result += ",";
1445 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1446 // TODO: add an identifying hash value for the loader
1447 result += ">";
1448 return result;
1449}
1450
Andreas Gampe90b936d2017-01-31 08:58:55 -08001451template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1452 // Check class is loaded/retired or this is java.lang.String that has a
1453 // circularity issue during loading the names of its members
1454 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1455 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001456 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001457 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1458 << " IsRetired=" << IsRetired<kVerifyFlags>()
1459 << " IsErroneous=" <<
1460 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001461 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001462 << " status= " << GetStatus<kVerifyFlags>()
1463 << " descriptor=" << PrettyDescriptor();
1464}
1465// Instantiate the common cases.
1466template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1467template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1468template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1469template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1470template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1471
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001472} // namespace mirror
1473} // namespace art