blob: ad6b37b86aecd5bb6c4d074ed0457c0900c200fd [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "class.h"
18
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_field-inl.h"
22#include "art_method-inl.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080023#include "base/logging.h" // For VLOG.
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070026#include "class_ext.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010027#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_loader.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010029#include "class_root.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080030#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file-inl.h"
32#include "dex/dex_file_annotations.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "dex_cache.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070035#include "handle_scope-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070036#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070037#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070038#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080039#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070041#include "object_lock.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070042#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080043#include "thread.h"
44#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080045#include "well_known_classes.h"
46
47namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070048
49// TODO: move to own CC file?
50constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
51constexpr size_t BitString::kCapacity;
52
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053namespace mirror {
54
Andreas Gampe46ee31b2016-12-14 10:11:49 -080055using android::base::StringPrintf;
56
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070057GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080058
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070059void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070060 CHECK(java_lang_Class_.IsNull())
61 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070062 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070063 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070064 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070065 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080066}
67
68void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070069 CHECK(!java_lang_Class_.IsNull());
70 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071}
72
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070073void Class::VisitRoots(RootVisitor* visitor) {
74 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080075}
76
Vladimir Marko7287c4d2018-02-15 10:41:07 +000077ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
78 const char* expected_name = nullptr;
Vladimir Markob4eb1b12018-05-24 11:09:38 +010079 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid.
Vladimir Marko7287c4d2018-02-15 10:41:07 +000080 if (name != nullptr && name->GetLength() >= 2) {
81 // Perfect hash for the expected values: from the second letters of the primitive types,
82 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
83 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
84 switch (hash) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010085 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break;
86 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break;
87 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break;
88 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break;
89 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break;
90 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break;
91 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break;
92 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break;
93 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break;
Vladimir Marko7287c4d2018-02-15 10:41:07 +000094 default: break;
95 }
96 }
97 if (expected_name != nullptr && name->Equals(expected_name)) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010098 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000099 DCHECK(klass != nullptr);
100 return klass;
101 } else {
102 Thread* self = Thread::Current();
103 if (name == nullptr) {
104 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
105 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg */ nullptr);
106 } else {
107 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
108 }
109 return nullptr;
110 }
111}
112
Alex Light0273ad12016-11-02 11:19:31 -0700113ClassExt* Class::EnsureExtDataPresent(Thread* self) {
114 ObjPtr<ClassExt> existing(GetExtData());
115 if (!existing.IsNull()) {
116 return existing.Ptr();
117 }
118 StackHandleScope<3> hs(self);
119 // Handlerize 'this' since we are allocating here.
120 Handle<Class> h_this(hs.NewHandle(this));
121 // Clear exception so we can allocate.
122 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
123 self->ClearException();
124 // Allocate the ClassExt
125 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800126 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700127 // OOM allocating the classExt.
128 // TODO Should we restore the suppressed exception?
129 self->AssertPendingOOMException();
130 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800131 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700132 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
133 bool set;
134 // Set the ext_data_ field using CAS semantics.
135 if (Runtime::Current()->IsActiveTransaction()) {
136 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
137 ObjPtr<ClassExt>(nullptr),
138 new_ext.Get());
139 } else {
140 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
141 ObjPtr<ClassExt>(nullptr),
142 new_ext.Get());
143 }
144 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
145 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
146 CHECK(!ret.IsNull());
147 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800148 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700149 self->SetException(throwable.Get());
150 }
151 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800152 }
153}
154
Vladimir Marko2c64a832018-01-04 11:31:56 +0000155void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
156 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700157 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
158 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700159 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000160 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000161 new_status != ClassStatus::kErrorUnresolved &&
162 new_status != ClassStatus::kErrorResolved &&
163 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700164 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700165 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700166 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000167 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700168 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700169 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700170 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700171 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700172 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000174 if (UNLIKELY(IsErroneous(new_status))) {
175 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700176 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000177 << h_this->PrettyClass()
178 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000179 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700180 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700181 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700182 if (self->IsExceptionPending()) {
183 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
184 }
185 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186
Alex Light0273ad12016-11-02 11:19:31 -0700187 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
188 if (!ext.IsNull()) {
189 self->AssertPendingException();
190 ext->SetVerifyError(self->GetException());
191 } else {
192 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700193 }
194 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 }
Alex Light0273ad12016-11-02 11:19:31 -0700196
Vladimir Marko305c38b2018-02-14 11:50:07 +0000197 if (kBitstringSubtypeCheckEnabled) {
198 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700199 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
200 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000201 } else {
202 // The ClassStatus is always in the 4 most-significant bits of status_.
203 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
204 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
205 if (Runtime::Current()->IsActiveTransaction()) {
206 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
207 } else {
208 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
209 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700210 }
211
212 // Setting the object size alloc fast path needs to be after the status write so that if the
213 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
214 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000215 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700216 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
217 // Finalizable objects must always go slow path.
218 if (!h_this->IsFinalizable()) {
219 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700220 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100221 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700222
223 if (!class_linker_initialized) {
224 // When the class linker is being initialized its single threaded and by definition there can be
225 // no waiters. During initialization classes may appear temporary but won't be retired as their
226 // size was statically computed.
227 } else {
228 // Classes that are being resolved or initialized need to notify waiters that the class status
229 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700230 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
232 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000233 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
234 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700235 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700236 }
237 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000238 CHECK_NE(new_status, ClassStatus::kRetired);
239 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700240 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241 }
242 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700243 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244}
245
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700246void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700247 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248}
249
Ian Rogersef7d42f2014-01-06 12:55:46 -0800250void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700252 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
253 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700254 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700256 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257}
258
259// Return the class' name. The exact format is bizarre, but it's the specified behavior for
260// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
261// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
262// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700263String* Class::ComputeName(Handle<Class> h_this) {
264 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800265 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 return name;
267 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700268 std::string temp;
269 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800270 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
272 // The descriptor indicates that this is the class for
273 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700274 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 switch (descriptor[0]) {
276 case 'Z': c_name = "boolean"; break;
277 case 'B': c_name = "byte"; break;
278 case 'C': c_name = "char"; break;
279 case 'S': c_name = "short"; break;
280 case 'I': c_name = "int"; break;
281 case 'J': c_name = "long"; break;
282 case 'F': c_name = "float"; break;
283 case 'D': c_name = "double"; break;
284 case 'V': c_name = "void"; break;
285 default:
286 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
287 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800288 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 } else {
290 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
291 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700292 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700294 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295 return name;
296}
297
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700300 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 if ((flags & kDumpClassClassLoader) != 0) {
302 os << ' ' << GetClassLoader();
303 }
304 if ((flags & kDumpClassInitialized) != 0) {
305 os << ' ' << GetStatus();
306 }
307 os << "\n";
308 return;
309 }
310
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700312 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700313 Handle<Class> h_this(hs.NewHandle(this));
314 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700316
Ian Rogers1ff3c982014-08-12 02:30:58 -0700317 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700319 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800321 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 os << StringPrintf(" access=0x%04x.%04x\n",
323 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800324 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700325 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700326 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 }
328 if (IsArrayClass()) {
329 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
330 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700331 const size_t num_direct_interfaces = NumDirectInterfaces();
332 if (num_direct_interfaces > 0) {
333 os << " interfaces (" << num_direct_interfaces << "):\n";
334 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000335 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700336 if (interface == nullptr) {
337 os << StringPrintf(" %2zd: nullptr!\n", i);
338 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700339 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
340 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700341 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 }
343 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700344 if (!IsLoaded()) {
345 os << " class not yet loaded";
346 } else {
347 // After this point, this may have moved due to GetDirectInterface.
348 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800349 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700350 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700351 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700354 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
355 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700356 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700357 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700358 }
359 if (h_this->NumStaticFields() > 0) {
360 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000361 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700362 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700363 os << StringPrintf(" %2zd: %s\n", i,
364 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700365 }
366 } else {
367 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700369 }
370 if (h_this->NumInstanceFields() > 0) {
371 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000372 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700373 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700374 os << StringPrintf(" %2zd: %s\n", i,
375 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700376 }
377 } else {
378 os << " <not yet available>";
379 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 }
381 }
382}
383
384void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386 // Sanity check that the number of bits set in the reference offset bitmap
387 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700388 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700389 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 count += c->NumReferenceInstanceFieldsDuringLinking();
391 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700392 // +1 for the Class in Object.
393 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100395 // Not called within a transaction.
396 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700397 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398}
399
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
401 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700402 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
403 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404 ++i;
405 }
406 if (descriptor1.find('/', i) != StringPiece::npos ||
407 descriptor2.find('/', i) != StringPiece::npos) {
408 return false;
409 } else {
410 return true;
411 }
412}
413
Mathieu Chartier3398c782016-09-30 10:27:43 -0700414bool Class::IsInSamePackage(ObjPtr<Class> that) {
415 ObjPtr<Class> klass1 = this;
416 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 if (klass1 == klass2) {
418 return true;
419 }
420 // Class loaders must match.
421 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
422 return false;
423 }
424 // Arrays are in the same package when their element classes are.
425 while (klass1->IsArrayClass()) {
426 klass1 = klass1->GetComponentType();
427 }
428 while (klass2->IsArrayClass()) {
429 klass2 = klass2->GetComponentType();
430 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700431 // trivial check again for array types
432 if (klass1 == klass2) {
433 return true;
434 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700436 std::string temp1, temp2;
437 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438}
439
Ian Rogersef7d42f2014-01-06 12:55:46 -0800440bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
442}
443
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700444void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100445 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700446 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100447 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700448 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100449 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450}
451
Vladimir Markoba118822017-06-12 15:41:56 +0100452template <typename SignatureType>
453static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
454 const StringPiece& name,
455 const SignatureType& signature,
456 PointerSize pointer_size)
457 REQUIRES_SHARED(Locks::mutator_lock_) {
458 // If the current class is not an interface, skip the search of its declared methods;
459 // such lookup is used only to distinguish between IncompatibleClassChangeError and
460 // NoSuchMethodError and the caller has already tried to search methods in the class.
461 if (LIKELY(klass->IsInterface())) {
462 // Search declared methods, both direct and virtual.
463 // (This lookup is used also for invoke-static on interface classes.)
464 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
465 if (method.GetName() == name && method.GetSignature() == signature) {
466 return &method;
467 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468 }
469 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700470
Vladimir Markoba118822017-06-12 15:41:56 +0100471 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
472 // we should return it, otherwise an arbitrary one can be returned.
473 ObjPtr<IfTable> iftable = klass->GetIfTable();
474 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
475 ObjPtr<Class> iface = iftable->GetInterface(i);
476 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
477 if (method.GetName() == name && method.GetSignature() == signature) {
478 return &method;
479 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700480 }
481 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482
Vladimir Markoba118822017-06-12 15:41:56 +0100483 // Then search for public non-static methods in the java.lang.Object.
484 if (LIKELY(klass->IsInterface())) {
485 ObjPtr<Class> object_class = klass->GetSuperClass();
486 DCHECK(object_class->IsObjectClass());
487 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
488 if (method.IsPublic() && !method.IsStatic() &&
489 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700490 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491 }
492 }
493 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700494 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495}
496
Vladimir Markoba118822017-06-12 15:41:56 +0100497ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
498 const StringPiece& signature,
499 PointerSize pointer_size) {
500 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800501}
502
Vladimir Markoba118822017-06-12 15:41:56 +0100503ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
504 const Signature& signature,
505 PointerSize pointer_size) {
506 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700507}
508
Vladimir Markoba118822017-06-12 15:41:56 +0100509ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
510 uint32_t dex_method_idx,
511 PointerSize pointer_size) {
512 // We always search by name and signature, ignoring the type index in the MethodId.
513 const DexFile& dex_file = *dex_cache->GetDexFile();
514 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
515 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
516 const Signature signature = dex_file.GetMethodSignature(method_id);
517 return FindInterfaceMethod(name, signature, pointer_size);
518}
519
Alex Lightafb66472017-08-01 09:54:49 -0700520static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
521 ObjPtr<mirror::Class> declaring_class)
522 REQUIRES_SHARED(Locks::mutator_lock_) {
523 if (klass->IsArrayClass()) {
524 return declaring_class->IsObjectClass();
525 } else if (klass->IsInterface()) {
526 return declaring_class->IsObjectClass() || declaring_class == klass;
527 } else {
528 return klass->IsSubClass(declaring_class);
529 }
530}
531
Vladimir Markoba118822017-06-12 15:41:56 +0100532static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
533 ObjPtr<mirror::Class> declaring_class,
534 ArtMethod& method)
535 REQUIRES_SHARED(Locks::mutator_lock_) {
536 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
537 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700538 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100539 uint32_t access_flags = method.GetAccessFlags();
540 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
541 return true;
542 }
543 if ((access_flags & kAccPrivate) != 0) {
544 return false;
545 }
546 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
547 if (!klass->IsInSamePackage(declaring_class)) {
548 return false;
549 }
550 }
551 return true;
552}
553
554template <typename SignatureType>
555static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
556 const StringPiece& name,
557 const SignatureType& signature,
558 PointerSize pointer_size)
559 REQUIRES_SHARED(Locks::mutator_lock_) {
560 // Search declared methods first.
561 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
562 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
563 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
564 return &method;
565 }
566 }
567
568 // Then search the superclass chain. If we find an inherited method, return it.
569 // If we find a method that's not inherited because of access restrictions,
570 // try to find a method inherited from an interface in copied methods.
571 ObjPtr<Class> klass = this_klass->GetSuperClass();
572 ArtMethod* uninherited_method = nullptr;
573 for (; klass != nullptr; klass = klass->GetSuperClass()) {
574 DCHECK(!klass->IsProxyClass());
575 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
576 if (method.GetName() == name && method.GetSignature() == signature) {
577 if (IsInheritedMethod(this_klass, klass, method)) {
578 return &method;
579 }
580 uninherited_method = &method;
581 break;
582 }
583 }
584 if (uninherited_method != nullptr) {
585 break;
586 }
587 }
588
589 // Then search copied methods.
590 // If we found a method that's not inherited, stop the search in its declaring class.
591 ObjPtr<Class> end_klass = klass;
592 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
593 klass = this_klass;
594 if (UNLIKELY(klass->IsProxyClass())) {
595 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
596 klass = klass->GetSuperClass();
597 }
598 for (; klass != end_klass; klass = klass->GetSuperClass()) {
599 DCHECK(!klass->IsProxyClass());
600 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
601 if (method.GetName() == name && method.GetSignature() == signature) {
602 return &method; // No further check needed, copied methods are inherited by definition.
603 }
604 }
605 }
606 return uninherited_method; // Return the `uninherited_method` if any.
607}
608
609
610ArtMethod* Class::FindClassMethod(const StringPiece& name,
611 const StringPiece& signature,
612 PointerSize pointer_size) {
613 return FindClassMethodWithSignature(this, name, signature, pointer_size);
614}
615
616ArtMethod* Class::FindClassMethod(const StringPiece& name,
617 const Signature& signature,
618 PointerSize pointer_size) {
619 return FindClassMethodWithSignature(this, name, signature, pointer_size);
620}
621
622ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
623 uint32_t dex_method_idx,
624 PointerSize pointer_size) {
625 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
626 DCHECK(!IsProxyClass());
627
628 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
629 ObjPtr<DexCache> this_dex_cache = GetDexCache();
630 if (this_dex_cache == dex_cache) {
631 // Lookup is always performed in the class referenced by the MethodId.
632 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
633 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
634 if (method.GetDexMethodIndex() == dex_method_idx) {
635 return &method;
636 }
637 }
638 }
639 // If not found, we need to search by name and signature.
640 const DexFile& dex_file = *dex_cache->GetDexFile();
641 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
642 const Signature signature = dex_file.GetMethodSignature(method_id);
643 StringPiece name; // Delay strlen() until actually needed.
644 // If we do not have a dex_cache match, try to find the declared method in this class now.
645 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
646 DCHECK(name.empty());
647 name = dex_file.StringDataByIdx(method_id.name_idx_);
648 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
649 if (method.GetName() == name && method.GetSignature() == signature) {
650 return &method;
651 }
652 }
653 }
654
655 // Then search the superclass chain. If we find an inherited method, return it.
656 // If we find a method that's not inherited because of access restrictions,
657 // try to find a method inherited from an interface in copied methods.
658 ArtMethod* uninherited_method = nullptr;
659 ObjPtr<Class> klass = GetSuperClass();
660 for (; klass != nullptr; klass = klass->GetSuperClass()) {
661 ArtMethod* candidate_method = nullptr;
662 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
663 if (klass->GetDexCache() == dex_cache) {
664 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
665 // the type index differs, so compare the name index and proto index.
666 for (ArtMethod& method : declared_methods) {
667 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
668 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
669 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
670 candidate_method = &method;
671 break;
672 }
673 }
674 } else {
675 if (!declared_methods.empty() && name.empty()) {
676 name = dex_file.StringDataByIdx(method_id.name_idx_);
677 }
678 for (ArtMethod& method : declared_methods) {
679 if (method.GetName() == name && method.GetSignature() == signature) {
680 candidate_method = &method;
681 break;
682 }
683 }
684 }
685 if (candidate_method != nullptr) {
686 if (IsInheritedMethod(this, klass, *candidate_method)) {
687 return candidate_method;
688 } else {
689 uninherited_method = candidate_method;
690 break;
691 }
692 }
693 }
694
695 // Then search copied methods.
696 // If we found a method that's not inherited, stop the search in its declaring class.
697 ObjPtr<Class> end_klass = klass;
698 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
699 // After we have searched the declared methods of the super-class chain,
700 // search copied methods which can contain methods from interfaces.
701 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
702 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
703 if (!copied_methods.empty() && name.empty()) {
704 name = dex_file.StringDataByIdx(method_id.name_idx_);
705 }
706 for (ArtMethod& method : copied_methods) {
707 if (method.GetName() == name && method.GetSignature() == signature) {
708 return &method; // No further check needed, copied methods are inherited by definition.
709 }
710 }
711 }
712 return uninherited_method; // Return the `uninherited_method` if any.
713}
714
715ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
716 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
717 DCHECK(!IsProxyClass());
718 StringPiece name("<init>");
719 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
720 if (method.GetName() == name && method.GetSignature() == signature) {
721 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800722 }
723 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700724 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800725}
726
Andreas Gampe542451c2016-07-26 09:02:02 -0700727ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
728 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000729 for (auto& method : GetDirectMethods(pointer_size)) {
730 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
731 if (name == np_method->GetName()) {
732 return &method;
733 }
734 }
735 return nullptr;
736}
737
Andreas Gampe542451c2016-07-26 09:02:02 -0700738ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
739 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000740 for (auto& method : GetVirtualMethods(pointer_size)) {
741 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
742 if (name == np_method->GetName()) {
743 return &method;
744 }
745 }
746 return nullptr;
747}
748
Andreas Gampe542451c2016-07-26 09:02:02 -0700749ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700750 DCHECK(method->GetDeclaringClass()->IsInterface());
751 DCHECK(IsInterface()) << "Should only be called on a interface class";
752 // Check if we have one defined on this interface first. This includes searching copied ones to
753 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
754 // don't do any indirect method checks here.
755 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
756 if (method->HasSameNameAndSignature(&iface_method)) {
757 return &iface_method;
758 }
759 }
760
761 std::vector<ArtMethod*> abstract_methods;
762 // Search through the IFTable for a working version. We don't need to check for conflicts
763 // because if there was one it would appear in this classes virtual_methods_ above.
764
765 Thread* self = Thread::Current();
766 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700767 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
768 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700769 size_t iftable_count = GetIfTableCount();
770 // Find the method. We don't need to check for conflicts because they would have been in the
771 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
772 // subtypiest interfaces get visited first.
773 for (size_t k = iftable_count; k != 0;) {
774 k--;
775 DCHECK_LT(k, iftable->Count());
776 iface.Assign(iftable->GetInterface(k));
777 // Iterate through every declared method on this interface. Each direct method's name/signature
778 // is unique so the order of the inner loop doesn't matter.
779 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
780 ArtMethod* current_method = &method_iter;
781 if (current_method->HasSameNameAndSignature(method)) {
782 if (current_method->IsDefault()) {
783 // Handle JLS soft errors, a default method from another superinterface tree can
784 // "override" an abstract method(s) from another superinterface tree(s). To do this,
785 // ignore any [default] method which are dominated by the abstract methods we've seen so
786 // far. Check if overridden by any in abstract_methods. We do not need to check for
787 // default_conflicts because we would hit those before we get to this loop.
788 bool overridden = false;
789 for (ArtMethod* possible_override : abstract_methods) {
790 DCHECK(possible_override->HasSameNameAndSignature(current_method));
791 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
792 overridden = true;
793 break;
794 }
795 }
796 if (!overridden) {
797 return current_method;
798 }
799 } else {
800 // Is not default.
801 // This might override another default method. Just stash it for now.
802 abstract_methods.push_back(current_method);
803 }
804 }
805 }
806 }
807 // If we reach here we either never found any declaration of the method (in which case
808 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
809 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
810 // of these arbitrarily.
811 return abstract_methods.empty() ? nullptr : abstract_methods[0];
812}
813
Andreas Gampe542451c2016-07-26 09:02:02 -0700814ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700815 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
816 if (method.IsClassInitializer()) {
817 DCHECK_STREQ(method.GetName(), "<clinit>");
818 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
819 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700820 }
821 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700822 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700823}
824
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700825// Custom binary search to avoid double comparisons from std::binary_search.
826static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
827 const StringPiece& name,
828 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700829 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700830 if (fields == nullptr) {
831 return nullptr;
832 }
833 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100834 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700835 ArtField* ret = nullptr;
836 while (low < high) {
837 size_t mid = (low + high) / 2;
838 ArtField& field = fields->At(mid);
839 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
840 // verifier. There can be multiple fields with the same in the same class name due to proguard.
841 int result = StringPiece(field.GetName()).Compare(name);
842 if (result == 0) {
843 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
844 }
845 if (result < 0) {
846 low = mid + 1;
847 } else if (result > 0) {
848 high = mid;
849 } else {
850 ret = &field;
851 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800852 }
853 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700854 if (kIsDebugBuild) {
855 ArtField* found = nullptr;
856 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
857 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
858 found = &field;
859 break;
860 }
861 }
David Sehr709b0702016-10-13 09:12:37 -0700862 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700863 }
864 return ret;
865}
866
867ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
868 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
869 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800870}
871
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700872ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800873 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700874 for (ArtField& field : GetIFields()) {
875 if (field.GetDexFieldIndex() == dex_field_idx) {
876 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800877 }
878 }
879 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700880 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800881}
882
Brian Carlstromea46f952013-07-30 01:26:50 -0700883ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800884 // Is the field in this class, or any of its superclasses?
885 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700886 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700887 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700888 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800889 return f;
890 }
891 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700892 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800893}
894
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700895ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800896 // Is the field in this class, or any of its superclasses?
897 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700898 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700899 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700900 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901 return f;
902 }
903 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700904 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800905}
906
Brian Carlstromea46f952013-07-30 01:26:50 -0700907ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700908 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700909 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800910}
911
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700912ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800913 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700914 for (ArtField& field : GetSFields()) {
915 if (field.GetDexFieldIndex() == dex_field_idx) {
916 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800917 }
918 }
919 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700920 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800921}
922
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700923ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000924 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700925 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700926 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800927 // Is the field in this class (or its interfaces), or any of its
928 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000929 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800930 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700931 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700932 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800933 return f;
934 }
935 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000936 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
937 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
938 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700939 f = FindStaticField(self, interface, name, type);
940 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800941 return f;
942 }
943 }
944 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700945 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800946}
947
Vladimir Markobb268b12016-06-30 15:52:56 +0100948ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700949 ObjPtr<Class> klass,
950 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700951 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700952 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800953 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700954 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700955 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800956 return f;
957 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100958 // Though GetDirectInterface() should not cause thread suspension when called
959 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700960 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800961 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000962 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
963 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
964 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700965 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
966 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800967 return f;
968 }
969 }
970 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700971 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800972}
973
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700974ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000975 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700976 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700977 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800978 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000979 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800980 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700981 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700982 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800983 return f;
984 }
985 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700986 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800987 return f;
988 }
989 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000990 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
991 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
992 DCHECK(interface != nullptr);
993 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700994 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800995 return f;
996 }
997 }
998 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700999 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001000}
1001
Andreas Gampe542451c2016-07-26 09:02:02 -07001002void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001003 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -08001004 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -07001005 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001006 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001007 }
1008 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +02001009}
1010
Ian Rogers1ff3c982014-08-12 02:30:58 -07001011const char* Class::GetDescriptor(std::string* storage) {
1012 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001013 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001014 } else if (IsArrayClass()) {
1015 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001016 } else if (IsProxyClass()) {
1017 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001018 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001019 } else {
1020 const DexFile& dex_file = GetDexFile();
1021 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
1022 return dex_file.GetTypeDescriptor(type_id);
1023 }
1024}
1025
Ian Rogers1ff3c982014-08-12 02:30:58 -07001026const char* Class::GetArrayDescriptor(std::string* storage) {
1027 std::string temp;
1028 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1029 *storage = "[";
1030 *storage += elem_desc;
1031 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001032}
1033
1034const DexFile::ClassDef* Class::GetClassDef() {
1035 uint16_t class_def_idx = GetDexClassDefIndex();
1036 if (class_def_idx == DexFile::kDexNoIndex16) {
1037 return nullptr;
1038 }
1039 return &GetDexFile().GetClassDef(class_def_idx);
1040}
1041
Andreas Gampea5b09a62016-11-17 15:21:22 -08001042dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001043 DCHECK(!IsPrimitive());
1044 DCHECK(!IsArrayClass());
1045 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1046}
1047
Vladimir Marko19a4d372016-12-08 14:41:46 +00001048ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1049 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001050 DCHECK(!klass->IsPrimitive());
1051 if (klass->IsArrayClass()) {
1052 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001053 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1054 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001055 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001056 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001057 } else {
1058 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001059 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001060 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001061 DCHECK(interface != nullptr);
1062 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001063 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001064 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001065 DCHECK(interfaces != nullptr);
1066 return interfaces->Get(idx);
1067 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001068 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001069 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001070 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001071 return interface;
1072 }
1073}
1074
Vladimir Marko19a4d372016-12-08 14:41:46 +00001075ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1076 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1077 if (interface == nullptr) {
1078 DCHECK(!klass->IsArrayClass());
1079 DCHECK(!klass->IsProxyClass());
1080 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001081 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001082 CHECK(interface != nullptr || self->IsExceptionPending());
1083 }
1084 return interface;
1085}
1086
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001087ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001088 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001089 DCHECK(!klass->IsInterface());
1090 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001091 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001092 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001093 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001094 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001095 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001096 }
Calin Juravle52503d82015-11-11 16:58:31 +00001097 return common_super_class;
1098}
1099
Mathieu Chartierf8322842014-05-16 10:59:25 -07001100const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001101 const DexFile& dex_file = GetDexFile();
1102 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001103 if (dex_class_def == nullptr) {
1104 // Generated classes have no class def.
1105 return nullptr;
1106 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001107 return dex_file.GetSourceFile(*dex_class_def);
1108}
1109
1110std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001111 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001112 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001113 return dex_cache->GetLocation()->ToModifiedUtf8();
1114 }
1115 // Arrays and proxies are generated and have no corresponding dex file location.
1116 return "generated class";
1117}
1118
1119const DexFile::TypeList* Class::GetInterfaceTypeList() {
1120 const DexFile::ClassDef* class_def = GetClassDef();
1121 if (class_def == nullptr) {
1122 return nullptr;
1123 }
1124 return GetDexFile().GetInterfacesList(*class_def);
1125}
1126
Andreas Gampe542451c2016-07-26 09:02:02 -07001127void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001128 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001129 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130 const size_t table_length = table->GetLength();
1131 SetEmbeddedVTableLength(table_length);
1132 for (size_t i = 0; i < table_length; i++) {
1133 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001134 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001135 // Keep java.lang.Object class's vtable around for since it's easier
1136 // to be reused by array classes during their linking.
1137 if (!IsObjectClass()) {
1138 SetVTable(nullptr);
1139 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001140}
1141
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001142class ReadBarrierOnNativeRootsVisitor {
1143 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001144 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001145 MemberOffset offset ATTRIBUTE_UNUSED,
1146 bool is_static ATTRIBUTE_UNUSED) const {}
1147
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001148 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001149 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001150 if (!root->IsNull()) {
1151 VisitRoot(root);
1152 }
1153 }
1154
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001155 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001156 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001157 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1158 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001159 if (old_ref != new_ref) {
1160 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1161 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001162 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001163 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001164 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1165 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001166 }
1167 }
1168};
1169
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001170// The pre-fence visitor for Class::CopyOf().
1171class CopyClassVisitor {
1172 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001173 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001174 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001175 size_t new_length,
1176 size_t copy_bytes,
1177 ImTable* imt,
1178 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001179 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001180 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001181 }
1182
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001183 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001184 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001185 StackHandleScope<1> hs(self_);
1186 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001187 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001188 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001189 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1190 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001191 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001192 // Visit all of the references to make sure there is no from space references in the native
1193 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001194 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001195 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001196 }
1197
1198 private:
1199 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001200 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001201 const size_t new_length_;
1202 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001203 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001204 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001205 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1206};
1207
Andreas Gampe542451c2016-07-26 09:02:02 -07001208Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001209 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1210 // We may get copied by a compacting GC.
1211 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001212 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001213 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001214 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1215 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001216 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001217 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001218 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1219 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001220 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001221 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001222 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001223 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001224 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001225}
1226
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001227bool Class::ProxyDescriptorEquals(const char* match) {
1228 DCHECK(IsProxyClass());
1229 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001230}
1231
Mathieu Chartiere401d142015-04-22 13:56:20 -07001232// TODO: Move this to java_lang_Class.cc?
1233ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001234 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001235 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001236 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001237 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001238 continue;
1239 }
1240 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001241 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001242 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001243 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001244 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001245 return nullptr;
1246 }
1247 }
1248 return nullptr;
1249}
1250
Mathieu Chartiere401d142015-04-22 13:56:20 -07001251uint32_t Class::Depth() {
1252 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001253 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001254 depth++;
1255 }
1256 return depth;
1257}
1258
Andreas Gampea5b09a62016-11-17 15:21:22 -08001259dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001260 std::string temp;
1261 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001262 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001263}
1264
Andreas Gampe542451c2016-07-26 09:02:02 -07001265template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001266ObjPtr<Method> Class::GetDeclaredMethodInternal(
1267 Thread* self,
1268 ObjPtr<Class> klass,
1269 ObjPtr<String> name,
1270 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001271 // Covariant return types permit the class to define multiple
1272 // methods with the same name and parameter types. Prefer to
1273 // return a non-synthetic method in such situations. We may
1274 // still return a synthetic method to handle situations like
1275 // escalated visibility. We never return miranda methods that
1276 // were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001277 StackHandleScope<3> hs(self);
1278 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001279 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001280 ThrowNullPointerException("name == null");
1281 return nullptr;
1282 }
1283 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001284 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001285 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001286 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1287 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001288 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001289 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001290 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1291 if (UNLIKELY(self->IsExceptionPending())) {
1292 return nullptr;
1293 }
1294 continue;
1295 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001296 if (!m.IsMiranda()) {
1297 if (!m.IsSynthetic()) {
1298 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1299 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001300 result = &m; // Remember as potential result if it's not a miranda method.
1301 }
1302 }
1303 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001304 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001305 auto modifiers = m.GetAccessFlags();
1306 if ((modifiers & kAccConstructor) != 0) {
1307 continue;
1308 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001309 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001310 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001311 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001312 if (np_name == nullptr) {
1313 self->AssertPendingException();
1314 return nullptr;
1315 }
1316 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1317 if (UNLIKELY(self->IsExceptionPending())) {
1318 return nullptr;
1319 }
1320 continue;
1321 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001322 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
1323 if ((modifiers & kAccSynthetic) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001324 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001325 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001326 result = &m; // Remember as potential result.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001327 }
1328 }
1329 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001330 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001331 : nullptr;
1332}
1333
1334template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001335ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001336 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001337 ObjPtr<Class> klass,
1338 ObjPtr<String> name,
1339 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001340template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001341ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001342 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001343 ObjPtr<Class> klass,
1344 ObjPtr<String> name,
1345 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001346template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001347ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001348 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001349 ObjPtr<Class> klass,
1350 ObjPtr<String> name,
1351 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001352template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001353ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001354 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001355 ObjPtr<Class> klass,
1356 ObjPtr<String> name,
1357 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001358
Andreas Gampe542451c2016-07-26 09:02:02 -07001359template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001360ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001361 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001362 ObjPtr<Class> klass,
1363 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001364 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001365 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001366 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001367 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001368 : nullptr;
1369}
1370
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001371// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001372
Andreas Gampe542451c2016-07-26 09:02:02 -07001373template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001374ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001375 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001376 ObjPtr<Class> klass,
1377 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001378template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001379ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001380 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001381 ObjPtr<Class> klass,
1382 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001383template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001384ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001385 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001386 ObjPtr<Class> klass,
1387 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001388template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001389ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001390 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001391 ObjPtr<Class> klass,
1392 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001393
Andreas Gampe715fdc22016-04-18 17:07:30 -07001394int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1395 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1396 return default_value;
1397 }
1398 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001399 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001400 return default_value;
1401 }
1402 return flags;
1403}
1404
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001405void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1406 if (Runtime::Current()->IsActiveTransaction()) {
1407 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1408 } else {
1409 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1410 }
1411}
1412
David Sehr709b0702016-10-13 09:12:37 -07001413std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1414 if (klass == nullptr) {
1415 return "null";
1416 }
1417 return klass->PrettyDescriptor();
1418}
1419
1420std::string Class::PrettyDescriptor() {
1421 std::string temp;
1422 return art::PrettyDescriptor(GetDescriptor(&temp));
1423}
1424
1425std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1426 if (c == nullptr) {
1427 return "null";
1428 }
1429 return c->PrettyClass();
1430}
1431
1432std::string Class::PrettyClass() {
1433 std::string result;
1434 result += "java.lang.Class<";
1435 result += PrettyDescriptor();
1436 result += ">";
1437 return result;
1438}
1439
1440std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1441 if (c == nullptr) {
1442 return "null";
1443 }
1444 return c->PrettyClassAndClassLoader();
1445}
1446
1447std::string Class::PrettyClassAndClassLoader() {
1448 std::string result;
1449 result += "java.lang.Class<";
1450 result += PrettyDescriptor();
1451 result += ",";
1452 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1453 // TODO: add an identifying hash value for the loader
1454 result += ">";
1455 return result;
1456}
1457
Andreas Gampe90b936d2017-01-31 08:58:55 -08001458template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1459 // Check class is loaded/retired or this is java.lang.String that has a
1460 // circularity issue during loading the names of its members
1461 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1462 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
1463 this == String::GetJavaLangString())
1464 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1465 << " IsRetired=" << IsRetired<kVerifyFlags>()
1466 << " IsErroneous=" <<
1467 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
1468 << " IsString=" << (this == String::GetJavaLangString())
1469 << " status= " << GetStatus<kVerifyFlags>()
1470 << " descriptor=" << PrettyDescriptor();
1471}
1472// Instantiate the common cases.
1473template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1474template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1475template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1476template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1477template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1478
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001479} // namespace mirror
1480} // namespace art