blob: 3f4e841f866a5d556e54963f0eaa0da3ce5f66f2 [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"
David Sehrb2ec9f52018-02-21 13:20:31 -080029#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080030#include "dex/dex_file-inl.h"
31#include "dex/dex_file_annotations.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "dex_cache.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070033#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070034#include "handle_scope-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070035#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070036#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070037#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080038#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070039#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070040#include "object_lock.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070041#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "thread.h"
43#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "well_known_classes.h"
45
46namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070047
48// TODO: move to own CC file?
49constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
50constexpr size_t BitString::kCapacity;
51
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052namespace mirror {
53
Andreas Gampe46ee31b2016-12-14 10:11:49 -080054using android::base::StringPrintf;
55
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070056GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070058void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070059 CHECK(java_lang_Class_.IsNull())
60 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070061 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070062 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070063 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070064 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080065}
66
67void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070068 CHECK(!java_lang_Class_.IsNull());
69 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070}
71
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070072void Class::VisitRoots(RootVisitor* visitor) {
73 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080074}
75
Vladimir Marko7287c4d2018-02-15 10:41:07 +000076ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
77 const char* expected_name = nullptr;
78 ClassLinker::ClassRoot class_root = ClassLinker::kJavaLangObject; // Invalid.
79 if (name != nullptr && name->GetLength() >= 2) {
80 // Perfect hash for the expected values: from the second letters of the primitive types,
81 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
82 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
83 switch (hash) {
84 case 'b': expected_name = "boolean"; class_root = ClassLinker::kPrimitiveBoolean; break;
85 case 'B': expected_name = "byte"; class_root = ClassLinker::kPrimitiveByte; break;
86 case 'c': expected_name = "char"; class_root = ClassLinker::kPrimitiveChar; break;
87 case 'd': expected_name = "double"; class_root = ClassLinker::kPrimitiveDouble; break;
88 case 'f': expected_name = "float"; class_root = ClassLinker::kPrimitiveFloat; break;
89 case 'i': expected_name = "int"; class_root = ClassLinker::kPrimitiveInt; break;
90 case 'l': expected_name = "long"; class_root = ClassLinker::kPrimitiveLong; break;
91 case 's': expected_name = "short"; class_root = ClassLinker::kPrimitiveShort; break;
92 case 'v': expected_name = "void"; class_root = ClassLinker::kPrimitiveVoid; break;
93 default: break;
94 }
95 }
96 if (expected_name != nullptr && name->Equals(expected_name)) {
97 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->GetClassRoot(class_root);
98 DCHECK(klass != nullptr);
99 return klass;
100 } else {
101 Thread* self = Thread::Current();
102 if (name == nullptr) {
103 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
104 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg */ nullptr);
105 } else {
106 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
107 }
108 return nullptr;
109 }
110}
111
Alex Light0273ad12016-11-02 11:19:31 -0700112ClassExt* Class::EnsureExtDataPresent(Thread* self) {
113 ObjPtr<ClassExt> existing(GetExtData());
114 if (!existing.IsNull()) {
115 return existing.Ptr();
116 }
117 StackHandleScope<3> hs(self);
118 // Handlerize 'this' since we are allocating here.
119 Handle<Class> h_this(hs.NewHandle(this));
120 // Clear exception so we can allocate.
121 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
122 self->ClearException();
123 // Allocate the ClassExt
124 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800125 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700126 // OOM allocating the classExt.
127 // TODO Should we restore the suppressed exception?
128 self->AssertPendingOOMException();
129 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800130 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700131 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
132 bool set;
133 // Set the ext_data_ field using CAS semantics.
134 if (Runtime::Current()->IsActiveTransaction()) {
135 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
136 ObjPtr<ClassExt>(nullptr),
137 new_ext.Get());
138 } else {
139 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
140 ObjPtr<ClassExt>(nullptr),
141 new_ext.Get());
142 }
143 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
144 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
145 CHECK(!ret.IsNull());
146 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800147 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700148 self->SetException(throwable.Get());
149 }
150 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800151 }
152}
153
Vladimir Marko2c64a832018-01-04 11:31:56 +0000154void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
155 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700156 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
157 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700158 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000159 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000160 new_status != ClassStatus::kErrorUnresolved &&
161 new_status != ClassStatus::kErrorResolved &&
162 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700163 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700164 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700165 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000166 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700167 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700168 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700169 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700170 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700171 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000173 if (UNLIKELY(IsErroneous(new_status))) {
174 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700175 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000176 << h_this->PrettyClass()
177 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000178 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700179 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700180 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700181 if (self->IsExceptionPending()) {
182 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
183 }
184 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185
Alex Light0273ad12016-11-02 11:19:31 -0700186 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
187 if (!ext.IsNull()) {
188 self->AssertPendingException();
189 ext->SetVerifyError(self->GetException());
190 } else {
191 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700192 }
193 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 }
Alex Light0273ad12016-11-02 11:19:31 -0700195
Vladimir Marko305c38b2018-02-14 11:50:07 +0000196 if (kBitstringSubtypeCheckEnabled) {
197 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700198 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
199 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000200 } else {
201 // The ClassStatus is always in the 4 most-significant bits of status_.
202 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
203 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
204 if (Runtime::Current()->IsActiveTransaction()) {
205 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
206 } else {
207 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
208 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700209 }
210
211 // Setting the object size alloc fast path needs to be after the status write so that if the
212 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
213 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000214 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700215 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
216 // Finalizable objects must always go slow path.
217 if (!h_this->IsFinalizable()) {
218 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700219 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100220 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700221
222 if (!class_linker_initialized) {
223 // When the class linker is being initialized its single threaded and by definition there can be
224 // no waiters. During initialization classes may appear temporary but won't be retired as their
225 // size was statically computed.
226 } else {
227 // Classes that are being resolved or initialized need to notify waiters that the class status
228 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700229 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700230 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
231 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000232 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
233 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700234 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235 }
236 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000237 CHECK_NE(new_status, ClassStatus::kRetired);
238 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700239 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700240 }
241 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700242 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243}
244
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700245void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700246 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247}
248
Ian Rogersef7d42f2014-01-06 12:55:46 -0800249void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700251 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
252 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700253 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700254 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700255 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256}
257
258// Return the class' name. The exact format is bizarre, but it's the specified behavior for
259// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
260// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
261// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700262String* Class::ComputeName(Handle<Class> h_this) {
263 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800264 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 return name;
266 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700267 std::string temp;
268 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800269 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
271 // The descriptor indicates that this is the class for
272 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700273 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 switch (descriptor[0]) {
275 case 'Z': c_name = "boolean"; break;
276 case 'B': c_name = "byte"; break;
277 case 'C': c_name = "char"; break;
278 case 'S': c_name = "short"; break;
279 case 'I': c_name = "int"; break;
280 case 'J': c_name = "long"; break;
281 case 'F': c_name = "float"; break;
282 case 'D': c_name = "double"; break;
283 case 'V': c_name = "void"; break;
284 default:
285 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
286 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800287 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288 } else {
289 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
290 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700291 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800292 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700293 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 return name;
295}
296
Ian Rogersef7d42f2014-01-06 12:55:46 -0800297void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700299 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 if ((flags & kDumpClassClassLoader) != 0) {
301 os << ' ' << GetClassLoader();
302 }
303 if ((flags & kDumpClassInitialized) != 0) {
304 os << ' ' << GetStatus();
305 }
306 os << "\n";
307 return;
308 }
309
Mathieu Chartiere401d142015-04-22 13:56:20 -0700310 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700311 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700312 Handle<Class> h_this(hs.NewHandle(this));
313 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700314 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700315
Ian Rogers1ff3c982014-08-12 02:30:58 -0700316 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700318 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800320 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800321 os << StringPrintf(" access=0x%04x.%04x\n",
322 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800323 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700324 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700325 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 }
327 if (IsArrayClass()) {
328 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
329 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700330 const size_t num_direct_interfaces = NumDirectInterfaces();
331 if (num_direct_interfaces > 0) {
332 os << " interfaces (" << num_direct_interfaces << "):\n";
333 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000334 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700335 if (interface == nullptr) {
336 os << StringPrintf(" %2zd: nullptr!\n", i);
337 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700338 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
339 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700340 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 }
342 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700343 if (!IsLoaded()) {
344 os << " class not yet loaded";
345 } else {
346 // After this point, this may have moved due to GetDirectInterface.
347 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800348 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700349 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700350 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700353 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
354 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700355 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700356 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700357 }
358 if (h_this->NumStaticFields() > 0) {
359 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000360 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700361 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700362 os << StringPrintf(" %2zd: %s\n", i,
363 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700364 }
365 } else {
366 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700368 }
369 if (h_this->NumInstanceFields() > 0) {
370 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000371 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700372 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700373 os << StringPrintf(" %2zd: %s\n", i,
374 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700375 }
376 } else {
377 os << " <not yet available>";
378 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379 }
380 }
381}
382
383void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700384 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 // Sanity check that the number of bits set in the reference offset bitmap
386 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700387 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700388 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 count += c->NumReferenceInstanceFieldsDuringLinking();
390 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700391 // +1 for the Class in Object.
392 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100394 // Not called within a transaction.
395 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700396 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397}
398
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
400 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700401 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
402 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 ++i;
404 }
405 if (descriptor1.find('/', i) != StringPiece::npos ||
406 descriptor2.find('/', i) != StringPiece::npos) {
407 return false;
408 } else {
409 return true;
410 }
411}
412
Mathieu Chartier3398c782016-09-30 10:27:43 -0700413bool Class::IsInSamePackage(ObjPtr<Class> that) {
414 ObjPtr<Class> klass1 = this;
415 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 if (klass1 == klass2) {
417 return true;
418 }
419 // Class loaders must match.
420 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
421 return false;
422 }
423 // Arrays are in the same package when their element classes are.
424 while (klass1->IsArrayClass()) {
425 klass1 = klass1->GetComponentType();
426 }
427 while (klass2->IsArrayClass()) {
428 klass2 = klass2->GetComponentType();
429 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700430 // trivial check again for array types
431 if (klass1 == klass2) {
432 return true;
433 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700435 std::string temp1, temp2;
436 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437}
438
Ian Rogersef7d42f2014-01-06 12:55:46 -0800439bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800440 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
441}
442
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700443void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100444 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700445 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100446 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700447 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100448 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449}
450
Vladimir Markoba118822017-06-12 15:41:56 +0100451template <typename SignatureType>
452static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
453 const StringPiece& name,
454 const SignatureType& signature,
455 PointerSize pointer_size)
456 REQUIRES_SHARED(Locks::mutator_lock_) {
457 // If the current class is not an interface, skip the search of its declared methods;
458 // such lookup is used only to distinguish between IncompatibleClassChangeError and
459 // NoSuchMethodError and the caller has already tried to search methods in the class.
460 if (LIKELY(klass->IsInterface())) {
461 // Search declared methods, both direct and virtual.
462 // (This lookup is used also for invoke-static on interface classes.)
463 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
464 if (method.GetName() == name && method.GetSignature() == signature) {
465 return &method;
466 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 }
468 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700469
Vladimir Markoba118822017-06-12 15:41:56 +0100470 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
471 // we should return it, otherwise an arbitrary one can be returned.
472 ObjPtr<IfTable> iftable = klass->GetIfTable();
473 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
474 ObjPtr<Class> iface = iftable->GetInterface(i);
475 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
476 if (method.GetName() == name && method.GetSignature() == signature) {
477 return &method;
478 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700479 }
480 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481
Vladimir Markoba118822017-06-12 15:41:56 +0100482 // Then search for public non-static methods in the java.lang.Object.
483 if (LIKELY(klass->IsInterface())) {
484 ObjPtr<Class> object_class = klass->GetSuperClass();
485 DCHECK(object_class->IsObjectClass());
486 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
487 if (method.IsPublic() && !method.IsStatic() &&
488 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700489 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 }
491 }
492 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700493 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494}
495
Vladimir Markoba118822017-06-12 15:41:56 +0100496ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
497 const StringPiece& signature,
498 PointerSize pointer_size) {
499 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800500}
501
Vladimir Markoba118822017-06-12 15:41:56 +0100502ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
503 const Signature& signature,
504 PointerSize pointer_size) {
505 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700506}
507
Vladimir Markoba118822017-06-12 15:41:56 +0100508ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
509 uint32_t dex_method_idx,
510 PointerSize pointer_size) {
511 // We always search by name and signature, ignoring the type index in the MethodId.
512 const DexFile& dex_file = *dex_cache->GetDexFile();
513 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
514 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
515 const Signature signature = dex_file.GetMethodSignature(method_id);
516 return FindInterfaceMethod(name, signature, pointer_size);
517}
518
Alex Lightafb66472017-08-01 09:54:49 -0700519static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
520 ObjPtr<mirror::Class> declaring_class)
521 REQUIRES_SHARED(Locks::mutator_lock_) {
522 if (klass->IsArrayClass()) {
523 return declaring_class->IsObjectClass();
524 } else if (klass->IsInterface()) {
525 return declaring_class->IsObjectClass() || declaring_class == klass;
526 } else {
527 return klass->IsSubClass(declaring_class);
528 }
529}
530
Vladimir Markoba118822017-06-12 15:41:56 +0100531static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
532 ObjPtr<mirror::Class> declaring_class,
533 ArtMethod& method)
534 REQUIRES_SHARED(Locks::mutator_lock_) {
535 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
536 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700537 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100538 uint32_t access_flags = method.GetAccessFlags();
539 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
540 return true;
541 }
542 if ((access_flags & kAccPrivate) != 0) {
543 return false;
544 }
545 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
546 if (!klass->IsInSamePackage(declaring_class)) {
547 return false;
548 }
549 }
550 return true;
551}
552
553template <typename SignatureType>
554static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
555 const StringPiece& name,
556 const SignatureType& signature,
557 PointerSize pointer_size)
558 REQUIRES_SHARED(Locks::mutator_lock_) {
559 // Search declared methods first.
560 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
561 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
562 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
563 return &method;
564 }
565 }
566
567 // Then search the superclass chain. If we find an inherited method, return it.
568 // If we find a method that's not inherited because of access restrictions,
569 // try to find a method inherited from an interface in copied methods.
570 ObjPtr<Class> klass = this_klass->GetSuperClass();
571 ArtMethod* uninherited_method = nullptr;
572 for (; klass != nullptr; klass = klass->GetSuperClass()) {
573 DCHECK(!klass->IsProxyClass());
574 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
575 if (method.GetName() == name && method.GetSignature() == signature) {
576 if (IsInheritedMethod(this_klass, klass, method)) {
577 return &method;
578 }
579 uninherited_method = &method;
580 break;
581 }
582 }
583 if (uninherited_method != nullptr) {
584 break;
585 }
586 }
587
588 // Then search copied methods.
589 // If we found a method that's not inherited, stop the search in its declaring class.
590 ObjPtr<Class> end_klass = klass;
591 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
592 klass = this_klass;
593 if (UNLIKELY(klass->IsProxyClass())) {
594 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
595 klass = klass->GetSuperClass();
596 }
597 for (; klass != end_klass; klass = klass->GetSuperClass()) {
598 DCHECK(!klass->IsProxyClass());
599 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
600 if (method.GetName() == name && method.GetSignature() == signature) {
601 return &method; // No further check needed, copied methods are inherited by definition.
602 }
603 }
604 }
605 return uninherited_method; // Return the `uninherited_method` if any.
606}
607
608
609ArtMethod* Class::FindClassMethod(const StringPiece& name,
610 const StringPiece& signature,
611 PointerSize pointer_size) {
612 return FindClassMethodWithSignature(this, name, signature, pointer_size);
613}
614
615ArtMethod* Class::FindClassMethod(const StringPiece& name,
616 const Signature& signature,
617 PointerSize pointer_size) {
618 return FindClassMethodWithSignature(this, name, signature, pointer_size);
619}
620
621ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
622 uint32_t dex_method_idx,
623 PointerSize pointer_size) {
624 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
625 DCHECK(!IsProxyClass());
626
627 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
628 ObjPtr<DexCache> this_dex_cache = GetDexCache();
629 if (this_dex_cache == dex_cache) {
630 // Lookup is always performed in the class referenced by the MethodId.
631 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
632 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
633 if (method.GetDexMethodIndex() == dex_method_idx) {
634 return &method;
635 }
636 }
637 }
638 // If not found, we need to search by name and signature.
639 const DexFile& dex_file = *dex_cache->GetDexFile();
640 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
641 const Signature signature = dex_file.GetMethodSignature(method_id);
642 StringPiece name; // Delay strlen() until actually needed.
643 // If we do not have a dex_cache match, try to find the declared method in this class now.
644 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
645 DCHECK(name.empty());
646 name = dex_file.StringDataByIdx(method_id.name_idx_);
647 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
648 if (method.GetName() == name && method.GetSignature() == signature) {
649 return &method;
650 }
651 }
652 }
653
654 // Then search the superclass chain. If we find an inherited method, return it.
655 // If we find a method that's not inherited because of access restrictions,
656 // try to find a method inherited from an interface in copied methods.
657 ArtMethod* uninherited_method = nullptr;
658 ObjPtr<Class> klass = GetSuperClass();
659 for (; klass != nullptr; klass = klass->GetSuperClass()) {
660 ArtMethod* candidate_method = nullptr;
661 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
662 if (klass->GetDexCache() == dex_cache) {
663 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
664 // the type index differs, so compare the name index and proto index.
665 for (ArtMethod& method : declared_methods) {
666 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
667 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
668 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
669 candidate_method = &method;
670 break;
671 }
672 }
673 } else {
674 if (!declared_methods.empty() && name.empty()) {
675 name = dex_file.StringDataByIdx(method_id.name_idx_);
676 }
677 for (ArtMethod& method : declared_methods) {
678 if (method.GetName() == name && method.GetSignature() == signature) {
679 candidate_method = &method;
680 break;
681 }
682 }
683 }
684 if (candidate_method != nullptr) {
685 if (IsInheritedMethod(this, klass, *candidate_method)) {
686 return candidate_method;
687 } else {
688 uninherited_method = candidate_method;
689 break;
690 }
691 }
692 }
693
694 // Then search copied methods.
695 // If we found a method that's not inherited, stop the search in its declaring class.
696 ObjPtr<Class> end_klass = klass;
697 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
698 // After we have searched the declared methods of the super-class chain,
699 // search copied methods which can contain methods from interfaces.
700 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
701 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
702 if (!copied_methods.empty() && name.empty()) {
703 name = dex_file.StringDataByIdx(method_id.name_idx_);
704 }
705 for (ArtMethod& method : copied_methods) {
706 if (method.GetName() == name && method.GetSignature() == signature) {
707 return &method; // No further check needed, copied methods are inherited by definition.
708 }
709 }
710 }
711 return uninherited_method; // Return the `uninherited_method` if any.
712}
713
714ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
715 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
716 DCHECK(!IsProxyClass());
717 StringPiece name("<init>");
718 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
719 if (method.GetName() == name && method.GetSignature() == signature) {
720 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800721 }
722 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700723 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800724}
725
Andreas Gampe542451c2016-07-26 09:02:02 -0700726ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
727 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000728 for (auto& method : GetDirectMethods(pointer_size)) {
729 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
730 if (name == np_method->GetName()) {
731 return &method;
732 }
733 }
734 return nullptr;
735}
736
Andreas Gampe542451c2016-07-26 09:02:02 -0700737ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
738 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000739 for (auto& method : GetVirtualMethods(pointer_size)) {
740 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
741 if (name == np_method->GetName()) {
742 return &method;
743 }
744 }
745 return nullptr;
746}
747
Andreas Gampe542451c2016-07-26 09:02:02 -0700748ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700749 DCHECK(method->GetDeclaringClass()->IsInterface());
750 DCHECK(IsInterface()) << "Should only be called on a interface class";
751 // Check if we have one defined on this interface first. This includes searching copied ones to
752 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
753 // don't do any indirect method checks here.
754 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
755 if (method->HasSameNameAndSignature(&iface_method)) {
756 return &iface_method;
757 }
758 }
759
760 std::vector<ArtMethod*> abstract_methods;
761 // Search through the IFTable for a working version. We don't need to check for conflicts
762 // because if there was one it would appear in this classes virtual_methods_ above.
763
764 Thread* self = Thread::Current();
765 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700766 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
767 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700768 size_t iftable_count = GetIfTableCount();
769 // Find the method. We don't need to check for conflicts because they would have been in the
770 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
771 // subtypiest interfaces get visited first.
772 for (size_t k = iftable_count; k != 0;) {
773 k--;
774 DCHECK_LT(k, iftable->Count());
775 iface.Assign(iftable->GetInterface(k));
776 // Iterate through every declared method on this interface. Each direct method's name/signature
777 // is unique so the order of the inner loop doesn't matter.
778 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
779 ArtMethod* current_method = &method_iter;
780 if (current_method->HasSameNameAndSignature(method)) {
781 if (current_method->IsDefault()) {
782 // Handle JLS soft errors, a default method from another superinterface tree can
783 // "override" an abstract method(s) from another superinterface tree(s). To do this,
784 // ignore any [default] method which are dominated by the abstract methods we've seen so
785 // far. Check if overridden by any in abstract_methods. We do not need to check for
786 // default_conflicts because we would hit those before we get to this loop.
787 bool overridden = false;
788 for (ArtMethod* possible_override : abstract_methods) {
789 DCHECK(possible_override->HasSameNameAndSignature(current_method));
790 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
791 overridden = true;
792 break;
793 }
794 }
795 if (!overridden) {
796 return current_method;
797 }
798 } else {
799 // Is not default.
800 // This might override another default method. Just stash it for now.
801 abstract_methods.push_back(current_method);
802 }
803 }
804 }
805 }
806 // If we reach here we either never found any declaration of the method (in which case
807 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
808 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
809 // of these arbitrarily.
810 return abstract_methods.empty() ? nullptr : abstract_methods[0];
811}
812
Andreas Gampe542451c2016-07-26 09:02:02 -0700813ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700814 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
815 if (method.IsClassInitializer()) {
816 DCHECK_STREQ(method.GetName(), "<clinit>");
817 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
818 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700819 }
820 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700821 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700822}
823
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700824// Custom binary search to avoid double comparisons from std::binary_search.
825static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
826 const StringPiece& name,
827 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700828 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700829 if (fields == nullptr) {
830 return nullptr;
831 }
832 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100833 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700834 ArtField* ret = nullptr;
835 while (low < high) {
836 size_t mid = (low + high) / 2;
837 ArtField& field = fields->At(mid);
838 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
839 // verifier. There can be multiple fields with the same in the same class name due to proguard.
840 int result = StringPiece(field.GetName()).Compare(name);
841 if (result == 0) {
842 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
843 }
844 if (result < 0) {
845 low = mid + 1;
846 } else if (result > 0) {
847 high = mid;
848 } else {
849 ret = &field;
850 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800851 }
852 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700853 if (kIsDebugBuild) {
854 ArtField* found = nullptr;
855 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
856 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
857 found = &field;
858 break;
859 }
860 }
David Sehr709b0702016-10-13 09:12:37 -0700861 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700862 }
863 return ret;
864}
865
866ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
867 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
868 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800869}
870
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700871ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800872 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700873 for (ArtField& field : GetIFields()) {
874 if (field.GetDexFieldIndex() == dex_field_idx) {
875 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800876 }
877 }
878 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700879 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800880}
881
Brian Carlstromea46f952013-07-30 01:26:50 -0700882ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800883 // Is the field in this class, or any of its superclasses?
884 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700885 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700886 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700887 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800888 return f;
889 }
890 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700891 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800892}
893
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700894ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800895 // Is the field in this class, or any of its superclasses?
896 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700897 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700898 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700899 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800900 return f;
901 }
902 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700903 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800904}
905
Brian Carlstromea46f952013-07-30 01:26:50 -0700906ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700907 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700908 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800909}
910
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700911ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800912 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700913 for (ArtField& field : GetSFields()) {
914 if (field.GetDexFieldIndex() == dex_field_idx) {
915 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800916 }
917 }
918 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700919 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800920}
921
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700922ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000923 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700924 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700925 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800926 // Is the field in this class (or its interfaces), or any of its
927 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000928 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800929 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700930 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700931 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800932 return f;
933 }
934 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000935 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
936 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
937 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700938 f = FindStaticField(self, interface, name, type);
939 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800940 return f;
941 }
942 }
943 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700944 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800945}
946
Vladimir Markobb268b12016-06-30 15:52:56 +0100947ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700948 ObjPtr<Class> klass,
949 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700950 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700951 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800952 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700953 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700954 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800955 return f;
956 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100957 // Though GetDirectInterface() should not cause thread suspension when called
958 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700959 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800960 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000961 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
962 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
963 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700964 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
965 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800966 return f;
967 }
968 }
969 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700970 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800971}
972
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700973ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000974 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700975 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700976 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800977 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000978 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800979 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700980 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700981 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800982 return f;
983 }
984 f = k->FindDeclaredStaticField(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 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000989 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
990 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
991 DCHECK(interface != nullptr);
992 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700993 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800994 return f;
995 }
996 }
997 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700998 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800999}
1000
Andreas Gampe542451c2016-07-26 09:02:02 -07001001void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001002 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -08001003 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -07001004 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001005 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001006 }
1007 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +02001008}
1009
Ian Rogers1ff3c982014-08-12 02:30:58 -07001010const char* Class::GetDescriptor(std::string* storage) {
1011 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001012 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001013 } else if (IsArrayClass()) {
1014 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001015 } else if (IsProxyClass()) {
1016 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001017 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001018 } else {
1019 const DexFile& dex_file = GetDexFile();
1020 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
1021 return dex_file.GetTypeDescriptor(type_id);
1022 }
1023}
1024
Ian Rogers1ff3c982014-08-12 02:30:58 -07001025const char* Class::GetArrayDescriptor(std::string* storage) {
1026 std::string temp;
1027 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1028 *storage = "[";
1029 *storage += elem_desc;
1030 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001031}
1032
1033const DexFile::ClassDef* Class::GetClassDef() {
1034 uint16_t class_def_idx = GetDexClassDefIndex();
1035 if (class_def_idx == DexFile::kDexNoIndex16) {
1036 return nullptr;
1037 }
1038 return &GetDexFile().GetClassDef(class_def_idx);
1039}
1040
Andreas Gampea5b09a62016-11-17 15:21:22 -08001041dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001042 DCHECK(!IsPrimitive());
1043 DCHECK(!IsArrayClass());
1044 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1045}
1046
Vladimir Marko19a4d372016-12-08 14:41:46 +00001047ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1048 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001049 DCHECK(!klass->IsPrimitive());
1050 if (klass->IsArrayClass()) {
1051 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001052 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1053 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001054 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001055 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001056 } else {
1057 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001058 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001059 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001060 DCHECK(interface != nullptr);
1061 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001062 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001063 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001064 DCHECK(interfaces != nullptr);
1065 return interfaces->Get(idx);
1066 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001067 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001068 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001069 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001070 return interface;
1071 }
1072}
1073
Vladimir Marko19a4d372016-12-08 14:41:46 +00001074ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1075 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1076 if (interface == nullptr) {
1077 DCHECK(!klass->IsArrayClass());
1078 DCHECK(!klass->IsProxyClass());
1079 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001080 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001081 CHECK(interface != nullptr || self->IsExceptionPending());
1082 }
1083 return interface;
1084}
1085
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001086ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001087 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001088 DCHECK(!klass->IsInterface());
1089 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001090 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001091 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001092 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001093 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001094 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001095 }
Calin Juravle52503d82015-11-11 16:58:31 +00001096 return common_super_class;
1097}
1098
Mathieu Chartierf8322842014-05-16 10:59:25 -07001099const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001100 const DexFile& dex_file = GetDexFile();
1101 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001102 if (dex_class_def == nullptr) {
1103 // Generated classes have no class def.
1104 return nullptr;
1105 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001106 return dex_file.GetSourceFile(*dex_class_def);
1107}
1108
1109std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001110 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001111 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001112 return dex_cache->GetLocation()->ToModifiedUtf8();
1113 }
1114 // Arrays and proxies are generated and have no corresponding dex file location.
1115 return "generated class";
1116}
1117
1118const DexFile::TypeList* Class::GetInterfaceTypeList() {
1119 const DexFile::ClassDef* class_def = GetClassDef();
1120 if (class_def == nullptr) {
1121 return nullptr;
1122 }
1123 return GetDexFile().GetInterfacesList(*class_def);
1124}
1125
Andreas Gampe542451c2016-07-26 09:02:02 -07001126void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001127 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001128 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129 const size_t table_length = table->GetLength();
1130 SetEmbeddedVTableLength(table_length);
1131 for (size_t i = 0; i < table_length; i++) {
1132 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001133 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001134 // Keep java.lang.Object class's vtable around for since it's easier
1135 // to be reused by array classes during their linking.
1136 if (!IsObjectClass()) {
1137 SetVTable(nullptr);
1138 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001139}
1140
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001141class ReadBarrierOnNativeRootsVisitor {
1142 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001143 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001144 MemberOffset offset ATTRIBUTE_UNUSED,
1145 bool is_static ATTRIBUTE_UNUSED) const {}
1146
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001147 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001148 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001149 if (!root->IsNull()) {
1150 VisitRoot(root);
1151 }
1152 }
1153
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001154 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001155 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001156 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1157 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001158 if (old_ref != new_ref) {
1159 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1160 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001161 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001162 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001163 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1164 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001165 }
1166 }
1167};
1168
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001169// The pre-fence visitor for Class::CopyOf().
1170class CopyClassVisitor {
1171 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001172 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001173 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001174 size_t new_length,
1175 size_t copy_bytes,
1176 ImTable* imt,
1177 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001178 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001179 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001180 }
1181
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001182 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001183 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001184 StackHandleScope<1> hs(self_);
1185 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001186 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001187 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001188 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1189 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001190 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001191 // Visit all of the references to make sure there is no from space references in the native
1192 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001193 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001194 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001195 }
1196
1197 private:
1198 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001199 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001200 const size_t new_length_;
1201 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001202 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001203 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001204 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1205};
1206
Andreas Gampe542451c2016-07-26 09:02:02 -07001207Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001208 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1209 // We may get copied by a compacting GC.
1210 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001211 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001212 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001213 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1214 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001215 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001216 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001217 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1218 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001219 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001220 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001221 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001222 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001223 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001224}
1225
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001226bool Class::ProxyDescriptorEquals(const char* match) {
1227 DCHECK(IsProxyClass());
1228 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001229}
1230
Mathieu Chartiere401d142015-04-22 13:56:20 -07001231// TODO: Move this to java_lang_Class.cc?
1232ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001233 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001234 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001235 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001236 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001237 continue;
1238 }
1239 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001240 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001241 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001242 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001243 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001244 return nullptr;
1245 }
1246 }
1247 return nullptr;
1248}
1249
Mathieu Chartiere401d142015-04-22 13:56:20 -07001250uint32_t Class::Depth() {
1251 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001252 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001253 depth++;
1254 }
1255 return depth;
1256}
1257
Andreas Gampea5b09a62016-11-17 15:21:22 -08001258dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001259 std::string temp;
1260 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001261 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001262}
1263
Andreas Gampe542451c2016-07-26 09:02:02 -07001264template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001265ObjPtr<Method> Class::GetDeclaredMethodInternal(
1266 Thread* self,
1267 ObjPtr<Class> klass,
1268 ObjPtr<String> name,
1269 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001270 // Covariant return types permit the class to define multiple
1271 // methods with the same name and parameter types. Prefer to
1272 // return a non-synthetic method in such situations. We may
1273 // still return a synthetic method to handle situations like
1274 // escalated visibility. We never return miranda methods that
1275 // were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001276 StackHandleScope<3> hs(self);
1277 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001278 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001279 ThrowNullPointerException("name == null");
1280 return nullptr;
1281 }
1282 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001283 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001284 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001285 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1286 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001287 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001288 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001289 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1290 if (UNLIKELY(self->IsExceptionPending())) {
1291 return nullptr;
1292 }
1293 continue;
1294 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001295 if (!m.IsMiranda()) {
1296 if (!m.IsSynthetic()) {
1297 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1298 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001299 result = &m; // Remember as potential result if it's not a miranda method.
1300 }
1301 }
1302 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001303 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001304 auto modifiers = m.GetAccessFlags();
1305 if ((modifiers & kAccConstructor) != 0) {
1306 continue;
1307 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001308 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001309 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001310 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001311 if (np_name == nullptr) {
1312 self->AssertPendingException();
1313 return nullptr;
1314 }
1315 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1316 if (UNLIKELY(self->IsExceptionPending())) {
1317 return nullptr;
1318 }
1319 continue;
1320 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001321 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
1322 if ((modifiers & kAccSynthetic) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001323 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001324 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001325 result = &m; // Remember as potential result.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001326 }
1327 }
1328 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001329 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001330 : nullptr;
1331}
1332
1333template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001334ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001335 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001336 ObjPtr<Class> klass,
1337 ObjPtr<String> name,
1338 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001339template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001340ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001341 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001342 ObjPtr<Class> klass,
1343 ObjPtr<String> name,
1344 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001345template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001346ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
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 Gampee01e3642016-07-25 13:06:04 -07001351template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001352ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001353 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001354 ObjPtr<Class> klass,
1355 ObjPtr<String> name,
1356 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001357
Andreas Gampe542451c2016-07-26 09:02:02 -07001358template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001359ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001360 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001361 ObjPtr<Class> klass,
1362 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001363 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001364 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001365 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001366 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001367 : nullptr;
1368}
1369
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001370// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001371
Andreas Gampe542451c2016-07-26 09:02:02 -07001372template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001373ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001374 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001375 ObjPtr<Class> klass,
1376 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001377template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001378ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001379 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001380 ObjPtr<Class> klass,
1381 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001382template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001383ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001384 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001385 ObjPtr<Class> klass,
1386 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001387template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001388ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001389 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001390 ObjPtr<Class> klass,
1391 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001392
Andreas Gampe715fdc22016-04-18 17:07:30 -07001393int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1394 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1395 return default_value;
1396 }
1397 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001398 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001399 return default_value;
1400 }
1401 return flags;
1402}
1403
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001404void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1405 if (Runtime::Current()->IsActiveTransaction()) {
1406 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1407 } else {
1408 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1409 }
1410}
1411
David Sehr709b0702016-10-13 09:12:37 -07001412std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1413 if (klass == nullptr) {
1414 return "null";
1415 }
1416 return klass->PrettyDescriptor();
1417}
1418
1419std::string Class::PrettyDescriptor() {
1420 std::string temp;
1421 return art::PrettyDescriptor(GetDescriptor(&temp));
1422}
1423
1424std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1425 if (c == nullptr) {
1426 return "null";
1427 }
1428 return c->PrettyClass();
1429}
1430
1431std::string Class::PrettyClass() {
1432 std::string result;
1433 result += "java.lang.Class<";
1434 result += PrettyDescriptor();
1435 result += ">";
1436 return result;
1437}
1438
1439std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1440 if (c == nullptr) {
1441 return "null";
1442 }
1443 return c->PrettyClassAndClassLoader();
1444}
1445
1446std::string Class::PrettyClassAndClassLoader() {
1447 std::string result;
1448 result += "java.lang.Class<";
1449 result += PrettyDescriptor();
1450 result += ",";
1451 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1452 // TODO: add an identifying hash value for the loader
1453 result += ">";
1454 return result;
1455}
1456
Andreas Gampe90b936d2017-01-31 08:58:55 -08001457template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1458 // Check class is loaded/retired or this is java.lang.String that has a
1459 // circularity issue during loading the names of its members
1460 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1461 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
1462 this == String::GetJavaLangString())
1463 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1464 << " IsRetired=" << IsRetired<kVerifyFlags>()
1465 << " IsErroneous=" <<
1466 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
1467 << " IsString=" << (this == String::GetJavaLangString())
1468 << " status= " << GetStatus<kVerifyFlags>()
1469 << " descriptor=" << PrettyDescriptor();
1470}
1471// Instantiate the common cases.
1472template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1473template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1474template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1475template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1476template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1477
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001478} // namespace mirror
1479} // namespace art