blob: cb2708d0cb5f327d105a5a727f5f60a749ad4d71 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "class.h"
18
Andreas Gampe46ee31b2016-12-14 10:11:49 -080019#include "android-base/stringprintf.h"
20
Brian Carlstromea46f952013-07-30 01:26:50 -070021#include "art_field-inl.h"
22#include "art_method-inl.h"
Andreas Gampe170331f2017-12-07 18:41:03 -080023#include "base/logging.h" // For VLOG.
David Sehrc431b9d2018-03-02 12:01:51 -080024#include "base/utils.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070025#include "class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070026#include "class_ext.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010027#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "class_loader.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010029#include "class_root.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080030#include "dex/descriptors_names.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/dex_file-inl.h"
32#include "dex/dex_file_annotations.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "dex_cache.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070035#include "handle_scope-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070036#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070037#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070038#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080039#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070040#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070041#include "object_lock.h"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010042#include "string-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070043#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044#include "thread.h"
45#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046#include "well_known_classes.h"
47
48namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070049
50// TODO: move to own CC file?
51constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
52constexpr size_t BitString::kCapacity;
53
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054namespace mirror {
55
Andreas Gampe46ee31b2016-12-14 10:11:49 -080056using android::base::StringPrintf;
57
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070058GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080059
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070060void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070061 CHECK(java_lang_Class_.IsNull())
62 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070063 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070064 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070065 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070066 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067}
68
69void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070070 CHECK(!java_lang_Class_.IsNull());
71 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072}
73
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070074void Class::VisitRoots(RootVisitor* visitor) {
75 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080076}
77
Vladimir Marko7287c4d2018-02-15 10:41:07 +000078ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
79 const char* expected_name = nullptr;
Vladimir Markob4eb1b12018-05-24 11:09:38 +010080 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid.
Vladimir Marko7287c4d2018-02-15 10:41:07 +000081 if (name != nullptr && name->GetLength() >= 2) {
82 // Perfect hash for the expected values: from the second letters of the primitive types,
83 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
84 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
85 switch (hash) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010086 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break;
87 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break;
88 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break;
89 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break;
90 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break;
91 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break;
92 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break;
93 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break;
94 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break;
Vladimir Marko7287c4d2018-02-15 10:41:07 +000095 default: break;
96 }
97 }
98 if (expected_name != nullptr && name->Equals(expected_name)) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010099 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
Vladimir Marko7287c4d2018-02-15 10:41:07 +0000100 DCHECK(klass != nullptr);
101 return klass;
102 } else {
103 Thread* self = Thread::Current();
104 if (name == nullptr) {
105 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
106 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg */ nullptr);
107 } else {
108 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
109 }
110 return nullptr;
111 }
112}
113
Alex Light0273ad12016-11-02 11:19:31 -0700114ClassExt* Class::EnsureExtDataPresent(Thread* self) {
115 ObjPtr<ClassExt> existing(GetExtData());
116 if (!existing.IsNull()) {
117 return existing.Ptr();
118 }
119 StackHandleScope<3> hs(self);
120 // Handlerize 'this' since we are allocating here.
121 Handle<Class> h_this(hs.NewHandle(this));
122 // Clear exception so we can allocate.
123 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
124 self->ClearException();
125 // Allocate the ClassExt
126 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800127 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700128 // OOM allocating the classExt.
129 // TODO Should we restore the suppressed exception?
130 self->AssertPendingOOMException();
131 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800132 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700133 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
134 bool set;
135 // Set the ext_data_ field using CAS semantics.
136 if (Runtime::Current()->IsActiveTransaction()) {
137 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
138 ObjPtr<ClassExt>(nullptr),
139 new_ext.Get());
140 } else {
141 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
142 ObjPtr<ClassExt>(nullptr),
143 new_ext.Get());
144 }
145 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
146 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
147 CHECK(!ret.IsNull());
148 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800149 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700150 self->SetException(throwable.Get());
151 }
152 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800153 }
154}
155
Vladimir Marko2c64a832018-01-04 11:31:56 +0000156void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
157 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
159 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700160 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000161 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000162 new_status != ClassStatus::kErrorUnresolved &&
163 new_status != ClassStatus::kErrorResolved &&
164 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700165 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700166 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700167 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000168 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700169 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700170 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700171 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700172 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700173 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000175 if (UNLIKELY(IsErroneous(new_status))) {
176 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700177 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000178 << h_this->PrettyClass()
179 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000180 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700181 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700182 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700183 if (self->IsExceptionPending()) {
184 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
185 }
186 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187
Alex Light0273ad12016-11-02 11:19:31 -0700188 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
189 if (!ext.IsNull()) {
190 self->AssertPendingException();
191 ext->SetVerifyError(self->GetException());
192 } else {
193 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700194 }
195 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 }
Alex Light0273ad12016-11-02 11:19:31 -0700197
Vladimir Marko305c38b2018-02-14 11:50:07 +0000198 if (kBitstringSubtypeCheckEnabled) {
199 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700200 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
201 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000202 } else {
203 // The ClassStatus is always in the 4 most-significant bits of status_.
204 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
205 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
206 if (Runtime::Current()->IsActiveTransaction()) {
207 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
208 } else {
209 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
210 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700211 }
212
213 // Setting the object size alloc fast path needs to be after the status write so that if the
214 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
215 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000216 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700217 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
218 // Finalizable objects must always go slow path.
219 if (!h_this->IsFinalizable()) {
220 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700221 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100222 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223
224 if (!class_linker_initialized) {
225 // When the class linker is being initialized its single threaded and by definition there can be
226 // no waiters. During initialization classes may appear temporary but won't be retired as their
227 // size was statically computed.
228 } else {
229 // Classes that are being resolved or initialized need to notify waiters that the class status
230 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700231 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700232 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
233 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000234 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
235 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700236 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237 }
238 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000239 CHECK_NE(new_status, ClassStatus::kRetired);
240 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700241 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 }
243 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700244 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800245}
246
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700247void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700248 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249}
250
Ian Rogersef7d42f2014-01-06 12:55:46 -0800251void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700252 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700253 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
254 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700255 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700256 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700257 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258}
259
260// Return the class' name. The exact format is bizarre, but it's the specified behavior for
261// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
262// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
263// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700264String* Class::ComputeName(Handle<Class> h_this) {
265 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800266 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 return name;
268 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700269 std::string temp;
270 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800271 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
273 // The descriptor indicates that this is the class for
274 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700275 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 switch (descriptor[0]) {
277 case 'Z': c_name = "boolean"; break;
278 case 'B': c_name = "byte"; break;
279 case 'C': c_name = "char"; break;
280 case 'S': c_name = "short"; break;
281 case 'I': c_name = "int"; break;
282 case 'J': c_name = "long"; break;
283 case 'F': c_name = "float"; break;
284 case 'D': c_name = "double"; break;
285 case 'V': c_name = "void"; break;
286 default:
287 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
288 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800289 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 } else {
291 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
292 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700293 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700295 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 return name;
297}
298
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700301 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 if ((flags & kDumpClassClassLoader) != 0) {
303 os << ' ' << GetClassLoader();
304 }
305 if ((flags & kDumpClassInitialized) != 0) {
306 os << ' ' << GetStatus();
307 }
308 os << "\n";
309 return;
310 }
311
Mathieu Chartiere401d142015-04-22 13:56:20 -0700312 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700313 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700314 Handle<Class> h_this(hs.NewHandle(this));
315 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700316 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700317
Ian Rogers1ff3c982014-08-12 02:30:58 -0700318 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700320 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800321 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800322 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 os << StringPrintf(" access=0x%04x.%04x\n",
324 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800325 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700326 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700327 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 }
329 if (IsArrayClass()) {
330 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
331 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700332 const size_t num_direct_interfaces = NumDirectInterfaces();
333 if (num_direct_interfaces > 0) {
334 os << " interfaces (" << num_direct_interfaces << "):\n";
335 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000336 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700337 if (interface == nullptr) {
338 os << StringPrintf(" %2zd: nullptr!\n", i);
339 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700340 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
341 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700342 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 }
344 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700345 if (!IsLoaded()) {
346 os << " class not yet loaded";
347 } else {
348 // After this point, this may have moved due to GetDirectInterface.
349 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800350 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700351 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700352 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700353 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700355 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
356 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700357 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700359 }
360 if (h_this->NumStaticFields() > 0) {
361 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000362 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700363 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700364 os << StringPrintf(" %2zd: %s\n", i,
365 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700366 }
367 } else {
368 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700370 }
371 if (h_this->NumInstanceFields() > 0) {
372 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000373 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700374 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700375 os << StringPrintf(" %2zd: %s\n", i,
376 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700377 }
378 } else {
379 os << " <not yet available>";
380 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381 }
382 }
383}
384
385void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800387 // Sanity check that the number of bits set in the reference offset bitmap
388 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700389 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700390 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 count += c->NumReferenceInstanceFieldsDuringLinking();
392 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700393 // +1 for the Class in Object.
394 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100396 // Not called within a transaction.
397 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700398 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399}
400
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
402 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700403 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
404 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 ++i;
406 }
407 if (descriptor1.find('/', i) != StringPiece::npos ||
408 descriptor2.find('/', i) != StringPiece::npos) {
409 return false;
410 } else {
411 return true;
412 }
413}
414
Mathieu Chartier3398c782016-09-30 10:27:43 -0700415bool Class::IsInSamePackage(ObjPtr<Class> that) {
416 ObjPtr<Class> klass1 = this;
417 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800418 if (klass1 == klass2) {
419 return true;
420 }
421 // Class loaders must match.
422 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
423 return false;
424 }
425 // Arrays are in the same package when their element classes are.
426 while (klass1->IsArrayClass()) {
427 klass1 = klass1->GetComponentType();
428 }
429 while (klass2->IsArrayClass()) {
430 klass2 = klass2->GetComponentType();
431 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700432 // trivial check again for array types
433 if (klass1 == klass2) {
434 return true;
435 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700437 std::string temp1, temp2;
438 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439}
440
Ian Rogersef7d42f2014-01-06 12:55:46 -0800441bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
443}
444
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700445void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100446 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700447 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100448 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700449 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100450 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800451}
452
Vladimir Markoba118822017-06-12 15:41:56 +0100453template <typename SignatureType>
454static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
455 const StringPiece& name,
456 const SignatureType& signature,
457 PointerSize pointer_size)
458 REQUIRES_SHARED(Locks::mutator_lock_) {
459 // If the current class is not an interface, skip the search of its declared methods;
460 // such lookup is used only to distinguish between IncompatibleClassChangeError and
461 // NoSuchMethodError and the caller has already tried to search methods in the class.
462 if (LIKELY(klass->IsInterface())) {
463 // Search declared methods, both direct and virtual.
464 // (This lookup is used also for invoke-static on interface classes.)
465 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
466 if (method.GetName() == name && method.GetSignature() == signature) {
467 return &method;
468 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469 }
470 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700471
Vladimir Markoba118822017-06-12 15:41:56 +0100472 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
473 // we should return it, otherwise an arbitrary one can be returned.
474 ObjPtr<IfTable> iftable = klass->GetIfTable();
475 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
476 ObjPtr<Class> iface = iftable->GetInterface(i);
477 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
478 if (method.GetName() == name && method.GetSignature() == signature) {
479 return &method;
480 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700481 }
482 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483
Vladimir Markoba118822017-06-12 15:41:56 +0100484 // Then search for public non-static methods in the java.lang.Object.
485 if (LIKELY(klass->IsInterface())) {
486 ObjPtr<Class> object_class = klass->GetSuperClass();
487 DCHECK(object_class->IsObjectClass());
488 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
489 if (method.IsPublic() && !method.IsStatic() &&
490 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700491 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492 }
493 }
494 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700495 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496}
497
Vladimir Markoba118822017-06-12 15:41:56 +0100498ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
499 const StringPiece& signature,
500 PointerSize pointer_size) {
501 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502}
503
Vladimir Markoba118822017-06-12 15:41:56 +0100504ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
505 const Signature& signature,
506 PointerSize pointer_size) {
507 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700508}
509
Vladimir Markoba118822017-06-12 15:41:56 +0100510ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
511 uint32_t dex_method_idx,
512 PointerSize pointer_size) {
513 // We always search by name and signature, ignoring the type index in the MethodId.
514 const DexFile& dex_file = *dex_cache->GetDexFile();
515 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
516 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
517 const Signature signature = dex_file.GetMethodSignature(method_id);
518 return FindInterfaceMethod(name, signature, pointer_size);
519}
520
Alex Lightafb66472017-08-01 09:54:49 -0700521static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
522 ObjPtr<mirror::Class> declaring_class)
523 REQUIRES_SHARED(Locks::mutator_lock_) {
524 if (klass->IsArrayClass()) {
525 return declaring_class->IsObjectClass();
526 } else if (klass->IsInterface()) {
527 return declaring_class->IsObjectClass() || declaring_class == klass;
528 } else {
529 return klass->IsSubClass(declaring_class);
530 }
531}
532
Vladimir Markoba118822017-06-12 15:41:56 +0100533static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
534 ObjPtr<mirror::Class> declaring_class,
535 ArtMethod& method)
536 REQUIRES_SHARED(Locks::mutator_lock_) {
537 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
538 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700539 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100540 uint32_t access_flags = method.GetAccessFlags();
541 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
542 return true;
543 }
544 if ((access_flags & kAccPrivate) != 0) {
545 return false;
546 }
547 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
548 if (!klass->IsInSamePackage(declaring_class)) {
549 return false;
550 }
551 }
552 return true;
553}
554
555template <typename SignatureType>
556static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
557 const StringPiece& name,
558 const SignatureType& signature,
559 PointerSize pointer_size)
560 REQUIRES_SHARED(Locks::mutator_lock_) {
561 // Search declared methods first.
562 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
563 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
564 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
565 return &method;
566 }
567 }
568
569 // Then search the superclass chain. If we find an inherited method, return it.
570 // If we find a method that's not inherited because of access restrictions,
571 // try to find a method inherited from an interface in copied methods.
572 ObjPtr<Class> klass = this_klass->GetSuperClass();
573 ArtMethod* uninherited_method = nullptr;
574 for (; klass != nullptr; klass = klass->GetSuperClass()) {
575 DCHECK(!klass->IsProxyClass());
576 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
577 if (method.GetName() == name && method.GetSignature() == signature) {
578 if (IsInheritedMethod(this_klass, klass, method)) {
579 return &method;
580 }
581 uninherited_method = &method;
582 break;
583 }
584 }
585 if (uninherited_method != nullptr) {
586 break;
587 }
588 }
589
590 // Then search copied methods.
591 // If we found a method that's not inherited, stop the search in its declaring class.
592 ObjPtr<Class> end_klass = klass;
593 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
594 klass = this_klass;
595 if (UNLIKELY(klass->IsProxyClass())) {
596 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
597 klass = klass->GetSuperClass();
598 }
599 for (; klass != end_klass; klass = klass->GetSuperClass()) {
600 DCHECK(!klass->IsProxyClass());
601 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
602 if (method.GetName() == name && method.GetSignature() == signature) {
603 return &method; // No further check needed, copied methods are inherited by definition.
604 }
605 }
606 }
607 return uninherited_method; // Return the `uninherited_method` if any.
608}
609
610
611ArtMethod* Class::FindClassMethod(const StringPiece& name,
612 const StringPiece& signature,
613 PointerSize pointer_size) {
614 return FindClassMethodWithSignature(this, name, signature, pointer_size);
615}
616
617ArtMethod* Class::FindClassMethod(const StringPiece& name,
618 const Signature& signature,
619 PointerSize pointer_size) {
620 return FindClassMethodWithSignature(this, name, signature, pointer_size);
621}
622
623ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
624 uint32_t dex_method_idx,
625 PointerSize pointer_size) {
626 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
627 DCHECK(!IsProxyClass());
628
629 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
630 ObjPtr<DexCache> this_dex_cache = GetDexCache();
631 if (this_dex_cache == dex_cache) {
632 // Lookup is always performed in the class referenced by the MethodId.
633 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
634 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
635 if (method.GetDexMethodIndex() == dex_method_idx) {
636 return &method;
637 }
638 }
639 }
640 // If not found, we need to search by name and signature.
641 const DexFile& dex_file = *dex_cache->GetDexFile();
642 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
643 const Signature signature = dex_file.GetMethodSignature(method_id);
644 StringPiece name; // Delay strlen() until actually needed.
645 // If we do not have a dex_cache match, try to find the declared method in this class now.
646 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
647 DCHECK(name.empty());
648 name = dex_file.StringDataByIdx(method_id.name_idx_);
649 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
650 if (method.GetName() == name && method.GetSignature() == signature) {
651 return &method;
652 }
653 }
654 }
655
656 // Then search the superclass chain. If we find an inherited method, return it.
657 // If we find a method that's not inherited because of access restrictions,
658 // try to find a method inherited from an interface in copied methods.
659 ArtMethod* uninherited_method = nullptr;
660 ObjPtr<Class> klass = GetSuperClass();
661 for (; klass != nullptr; klass = klass->GetSuperClass()) {
662 ArtMethod* candidate_method = nullptr;
663 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
664 if (klass->GetDexCache() == dex_cache) {
665 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
666 // the type index differs, so compare the name index and proto index.
667 for (ArtMethod& method : declared_methods) {
668 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
669 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
670 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
671 candidate_method = &method;
672 break;
673 }
674 }
675 } else {
676 if (!declared_methods.empty() && name.empty()) {
677 name = dex_file.StringDataByIdx(method_id.name_idx_);
678 }
679 for (ArtMethod& method : declared_methods) {
680 if (method.GetName() == name && method.GetSignature() == signature) {
681 candidate_method = &method;
682 break;
683 }
684 }
685 }
686 if (candidate_method != nullptr) {
687 if (IsInheritedMethod(this, klass, *candidate_method)) {
688 return candidate_method;
689 } else {
690 uninherited_method = candidate_method;
691 break;
692 }
693 }
694 }
695
696 // Then search copied methods.
697 // If we found a method that's not inherited, stop the search in its declaring class.
698 ObjPtr<Class> end_klass = klass;
699 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
700 // After we have searched the declared methods of the super-class chain,
701 // search copied methods which can contain methods from interfaces.
702 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
703 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
704 if (!copied_methods.empty() && name.empty()) {
705 name = dex_file.StringDataByIdx(method_id.name_idx_);
706 }
707 for (ArtMethod& method : copied_methods) {
708 if (method.GetName() == name && method.GetSignature() == signature) {
709 return &method; // No further check needed, copied methods are inherited by definition.
710 }
711 }
712 }
713 return uninherited_method; // Return the `uninherited_method` if any.
714}
715
716ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
717 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
718 DCHECK(!IsProxyClass());
719 StringPiece name("<init>");
720 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
721 if (method.GetName() == name && method.GetSignature() == signature) {
722 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800723 }
724 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700725 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800726}
727
Andreas Gampe542451c2016-07-26 09:02:02 -0700728ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
729 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000730 for (auto& method : GetDirectMethods(pointer_size)) {
731 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
732 if (name == np_method->GetName()) {
733 return &method;
734 }
735 }
736 return nullptr;
737}
738
Andreas Gampe542451c2016-07-26 09:02:02 -0700739ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
740 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000741 for (auto& method : GetVirtualMethods(pointer_size)) {
742 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
743 if (name == np_method->GetName()) {
744 return &method;
745 }
746 }
747 return nullptr;
748}
749
Andreas Gampe542451c2016-07-26 09:02:02 -0700750ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700751 DCHECK(method->GetDeclaringClass()->IsInterface());
752 DCHECK(IsInterface()) << "Should only be called on a interface class";
753 // Check if we have one defined on this interface first. This includes searching copied ones to
754 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
755 // don't do any indirect method checks here.
756 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
757 if (method->HasSameNameAndSignature(&iface_method)) {
758 return &iface_method;
759 }
760 }
761
762 std::vector<ArtMethod*> abstract_methods;
763 // Search through the IFTable for a working version. We don't need to check for conflicts
764 // because if there was one it would appear in this classes virtual_methods_ above.
765
766 Thread* self = Thread::Current();
767 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700768 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
769 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700770 size_t iftable_count = GetIfTableCount();
771 // Find the method. We don't need to check for conflicts because they would have been in the
772 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
773 // subtypiest interfaces get visited first.
774 for (size_t k = iftable_count; k != 0;) {
775 k--;
776 DCHECK_LT(k, iftable->Count());
777 iface.Assign(iftable->GetInterface(k));
778 // Iterate through every declared method on this interface. Each direct method's name/signature
779 // is unique so the order of the inner loop doesn't matter.
780 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
781 ArtMethod* current_method = &method_iter;
782 if (current_method->HasSameNameAndSignature(method)) {
783 if (current_method->IsDefault()) {
784 // Handle JLS soft errors, a default method from another superinterface tree can
785 // "override" an abstract method(s) from another superinterface tree(s). To do this,
786 // ignore any [default] method which are dominated by the abstract methods we've seen so
787 // far. Check if overridden by any in abstract_methods. We do not need to check for
788 // default_conflicts because we would hit those before we get to this loop.
789 bool overridden = false;
790 for (ArtMethod* possible_override : abstract_methods) {
791 DCHECK(possible_override->HasSameNameAndSignature(current_method));
792 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
793 overridden = true;
794 break;
795 }
796 }
797 if (!overridden) {
798 return current_method;
799 }
800 } else {
801 // Is not default.
802 // This might override another default method. Just stash it for now.
803 abstract_methods.push_back(current_method);
804 }
805 }
806 }
807 }
808 // If we reach here we either never found any declaration of the method (in which case
809 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
810 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
811 // of these arbitrarily.
812 return abstract_methods.empty() ? nullptr : abstract_methods[0];
813}
814
Andreas Gampe542451c2016-07-26 09:02:02 -0700815ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700816 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
817 if (method.IsClassInitializer()) {
818 DCHECK_STREQ(method.GetName(), "<clinit>");
819 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
820 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700821 }
822 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700823 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700824}
825
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700826// Custom binary search to avoid double comparisons from std::binary_search.
827static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
828 const StringPiece& name,
829 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700830 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700831 if (fields == nullptr) {
832 return nullptr;
833 }
834 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100835 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700836 ArtField* ret = nullptr;
837 while (low < high) {
838 size_t mid = (low + high) / 2;
839 ArtField& field = fields->At(mid);
840 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
841 // verifier. There can be multiple fields with the same in the same class name due to proguard.
842 int result = StringPiece(field.GetName()).Compare(name);
843 if (result == 0) {
844 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
845 }
846 if (result < 0) {
847 low = mid + 1;
848 } else if (result > 0) {
849 high = mid;
850 } else {
851 ret = &field;
852 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800853 }
854 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700855 if (kIsDebugBuild) {
856 ArtField* found = nullptr;
857 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
858 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
859 found = &field;
860 break;
861 }
862 }
David Sehr709b0702016-10-13 09:12:37 -0700863 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700864 }
865 return ret;
866}
867
868ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
869 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
870 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800871}
872
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700873ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800874 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700875 for (ArtField& field : GetIFields()) {
876 if (field.GetDexFieldIndex() == dex_field_idx) {
877 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800878 }
879 }
880 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700881 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800882}
883
Brian Carlstromea46f952013-07-30 01:26:50 -0700884ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800885 // Is the field in this class, or any of its superclasses?
886 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700887 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700888 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700889 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800890 return f;
891 }
892 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700893 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800894}
895
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700896ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800897 // Is the field in this class, or any of its superclasses?
898 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700899 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700900 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700901 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800902 return f;
903 }
904 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700905 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800906}
907
Brian Carlstromea46f952013-07-30 01:26:50 -0700908ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700909 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700910 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800911}
912
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700913ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800914 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700915 for (ArtField& field : GetSFields()) {
916 if (field.GetDexFieldIndex() == dex_field_idx) {
917 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800918 }
919 }
920 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700921 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800922}
923
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700924ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000925 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700926 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700927 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800928 // Is the field in this class (or its interfaces), or any of its
929 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000930 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800931 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700932 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700933 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800934 return f;
935 }
936 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000937 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
938 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
939 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700940 f = FindStaticField(self, interface, name, type);
941 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800942 return f;
943 }
944 }
945 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700946 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800947}
948
Vladimir Markobb268b12016-06-30 15:52:56 +0100949ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700950 ObjPtr<Class> klass,
951 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700952 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700953 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800954 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700955 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700956 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800957 return f;
958 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100959 // Though GetDirectInterface() should not cause thread suspension when called
960 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700961 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800962 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000963 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
964 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
965 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700966 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
967 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800968 return f;
969 }
970 }
971 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700972 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800973}
974
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700975ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000976 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700977 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700978 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800979 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000980 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800981 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700982 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700983 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800984 return f;
985 }
986 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700987 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800988 return f;
989 }
990 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000991 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
992 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
993 DCHECK(interface != nullptr);
994 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700995 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800996 return f;
997 }
998 }
999 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001000 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001001}
1002
Andreas Gampe542451c2016-07-26 09:02:02 -07001003void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001004 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -08001005 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -07001006 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001007 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001008 }
1009 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +02001010}
1011
Ian Rogers1ff3c982014-08-12 02:30:58 -07001012const char* Class::GetDescriptor(std::string* storage) {
1013 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001014 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001015 } else if (IsArrayClass()) {
1016 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001017 } else if (IsProxyClass()) {
1018 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001019 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001020 } else {
1021 const DexFile& dex_file = GetDexFile();
1022 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
1023 return dex_file.GetTypeDescriptor(type_id);
1024 }
1025}
1026
Ian Rogers1ff3c982014-08-12 02:30:58 -07001027const char* Class::GetArrayDescriptor(std::string* storage) {
1028 std::string temp;
1029 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1030 *storage = "[";
1031 *storage += elem_desc;
1032 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001033}
1034
1035const DexFile::ClassDef* Class::GetClassDef() {
1036 uint16_t class_def_idx = GetDexClassDefIndex();
1037 if (class_def_idx == DexFile::kDexNoIndex16) {
1038 return nullptr;
1039 }
1040 return &GetDexFile().GetClassDef(class_def_idx);
1041}
1042
Andreas Gampea5b09a62016-11-17 15:21:22 -08001043dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001044 DCHECK(!IsPrimitive());
1045 DCHECK(!IsArrayClass());
1046 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1047}
1048
Vladimir Marko19a4d372016-12-08 14:41:46 +00001049ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1050 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001051 DCHECK(!klass->IsPrimitive());
1052 if (klass->IsArrayClass()) {
1053 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001054 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1055 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001056 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001057 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001058 } else {
1059 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001060 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001061 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001062 DCHECK(interface != nullptr);
1063 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001064 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001065 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001066 DCHECK(interfaces != nullptr);
1067 return interfaces->Get(idx);
1068 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001069 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001070 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001071 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001072 return interface;
1073 }
1074}
1075
Vladimir Marko19a4d372016-12-08 14:41:46 +00001076ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1077 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1078 if (interface == nullptr) {
1079 DCHECK(!klass->IsArrayClass());
1080 DCHECK(!klass->IsProxyClass());
1081 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001082 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001083 CHECK(interface != nullptr || self->IsExceptionPending());
1084 }
1085 return interface;
1086}
1087
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001088ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001089 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001090 DCHECK(!klass->IsInterface());
1091 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001092 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001093 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001094 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001095 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001096 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001097 }
Calin Juravle52503d82015-11-11 16:58:31 +00001098 return common_super_class;
1099}
1100
Mathieu Chartierf8322842014-05-16 10:59:25 -07001101const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001102 const DexFile& dex_file = GetDexFile();
1103 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001104 if (dex_class_def == nullptr) {
1105 // Generated classes have no class def.
1106 return nullptr;
1107 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001108 return dex_file.GetSourceFile(*dex_class_def);
1109}
1110
1111std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001112 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001113 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001114 return dex_cache->GetLocation()->ToModifiedUtf8();
1115 }
1116 // Arrays and proxies are generated and have no corresponding dex file location.
1117 return "generated class";
1118}
1119
1120const DexFile::TypeList* Class::GetInterfaceTypeList() {
1121 const DexFile::ClassDef* class_def = GetClassDef();
1122 if (class_def == nullptr) {
1123 return nullptr;
1124 }
1125 return GetDexFile().GetInterfacesList(*class_def);
1126}
1127
Andreas Gampe542451c2016-07-26 09:02:02 -07001128void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001130 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001131 const size_t table_length = table->GetLength();
1132 SetEmbeddedVTableLength(table_length);
1133 for (size_t i = 0; i < table_length; i++) {
1134 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001135 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001136 // Keep java.lang.Object class's vtable around for since it's easier
1137 // to be reused by array classes during their linking.
1138 if (!IsObjectClass()) {
1139 SetVTable(nullptr);
1140 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001141}
1142
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001143class ReadBarrierOnNativeRootsVisitor {
1144 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001145 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001146 MemberOffset offset ATTRIBUTE_UNUSED,
1147 bool is_static ATTRIBUTE_UNUSED) const {}
1148
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001149 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001150 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001151 if (!root->IsNull()) {
1152 VisitRoot(root);
1153 }
1154 }
1155
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001156 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001157 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001158 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1159 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001160 if (old_ref != new_ref) {
1161 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1162 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001163 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001164 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001165 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1166 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001167 }
1168 }
1169};
1170
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001171// The pre-fence visitor for Class::CopyOf().
1172class CopyClassVisitor {
1173 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001174 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001175 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001176 size_t new_length,
1177 size_t copy_bytes,
1178 ImTable* imt,
1179 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001180 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001181 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001182 }
1183
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001184 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001185 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001186 StackHandleScope<1> hs(self_);
1187 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001188 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001189 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001190 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1191 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001192 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001193 // Visit all of the references to make sure there is no from space references in the native
1194 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001195 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001196 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001197 }
1198
1199 private:
1200 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001201 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001202 const size_t new_length_;
1203 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001204 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001205 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001206 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1207};
1208
Andreas Gampe542451c2016-07-26 09:02:02 -07001209Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001210 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1211 // We may get copied by a compacting GC.
1212 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001213 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001214 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001215 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1216 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001217 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001218 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001219 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1220 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001221 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001222 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001223 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001224 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001225 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001226}
1227
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001228bool Class::ProxyDescriptorEquals(const char* match) {
1229 DCHECK(IsProxyClass());
1230 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001231}
1232
Mathieu Chartiere401d142015-04-22 13:56:20 -07001233// TODO: Move this to java_lang_Class.cc?
1234ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001235 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001236 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001237 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001238 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001239 continue;
1240 }
1241 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001242 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001243 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001244 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001245 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001246 return nullptr;
1247 }
1248 }
1249 return nullptr;
1250}
1251
Mathieu Chartiere401d142015-04-22 13:56:20 -07001252uint32_t Class::Depth() {
1253 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001254 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001255 depth++;
1256 }
1257 return depth;
1258}
1259
Andreas Gampea5b09a62016-11-17 15:21:22 -08001260dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001261 std::string temp;
1262 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001263 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001264}
1265
Andreas Gampe542451c2016-07-26 09:02:02 -07001266template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001267ObjPtr<Method> Class::GetDeclaredMethodInternal(
1268 Thread* self,
1269 ObjPtr<Class> klass,
1270 ObjPtr<String> name,
1271 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001272 // Covariant return types permit the class to define multiple
1273 // methods with the same name and parameter types. Prefer to
1274 // return a non-synthetic method in such situations. We may
1275 // still return a synthetic method to handle situations like
1276 // escalated visibility. We never return miranda methods that
1277 // were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001278 StackHandleScope<3> hs(self);
1279 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001280 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001281 ThrowNullPointerException("name == null");
1282 return nullptr;
1283 }
1284 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001285 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001286 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001287 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1288 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001289 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001290 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001291 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1292 if (UNLIKELY(self->IsExceptionPending())) {
1293 return nullptr;
1294 }
1295 continue;
1296 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001297 if (!m.IsMiranda()) {
1298 if (!m.IsSynthetic()) {
1299 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1300 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001301 result = &m; // Remember as potential result if it's not a miranda method.
1302 }
1303 }
1304 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001305 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001306 auto modifiers = m.GetAccessFlags();
1307 if ((modifiers & kAccConstructor) != 0) {
1308 continue;
1309 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001310 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001311 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001312 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001313 if (np_name == nullptr) {
1314 self->AssertPendingException();
1315 return nullptr;
1316 }
1317 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1318 if (UNLIKELY(self->IsExceptionPending())) {
1319 return nullptr;
1320 }
1321 continue;
1322 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001323 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
1324 if ((modifiers & kAccSynthetic) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001325 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001326 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001327 result = &m; // Remember as potential result.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001328 }
1329 }
1330 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001331 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001332 : nullptr;
1333}
1334
1335template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001336ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001337 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001338 ObjPtr<Class> klass,
1339 ObjPtr<String> name,
1340 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001341template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001342ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001343 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001344 ObjPtr<Class> klass,
1345 ObjPtr<String> name,
1346 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001347template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001348ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001349 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001350 ObjPtr<Class> klass,
1351 ObjPtr<String> name,
1352 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001353template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001354ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001355 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001356 ObjPtr<Class> klass,
1357 ObjPtr<String> name,
1358 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001359
Andreas Gampe542451c2016-07-26 09:02:02 -07001360template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001361ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001362 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001363 ObjPtr<Class> klass,
1364 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001365 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001366 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001367 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001368 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001369 : nullptr;
1370}
1371
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001372// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001373
Andreas Gampe542451c2016-07-26 09:02:02 -07001374template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001375ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001376 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001377 ObjPtr<Class> klass,
1378 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001379template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001380ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001381 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001382 ObjPtr<Class> klass,
1383 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001384template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001385ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001386 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001387 ObjPtr<Class> klass,
1388 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001389template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001390ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001391 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001392 ObjPtr<Class> klass,
1393 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001394
Andreas Gampe715fdc22016-04-18 17:07:30 -07001395int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1396 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1397 return default_value;
1398 }
1399 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001400 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001401 return default_value;
1402 }
1403 return flags;
1404}
1405
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001406void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1407 if (Runtime::Current()->IsActiveTransaction()) {
1408 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1409 } else {
1410 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1411 }
1412}
1413
David Sehr709b0702016-10-13 09:12:37 -07001414std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1415 if (klass == nullptr) {
1416 return "null";
1417 }
1418 return klass->PrettyDescriptor();
1419}
1420
1421std::string Class::PrettyDescriptor() {
1422 std::string temp;
1423 return art::PrettyDescriptor(GetDescriptor(&temp));
1424}
1425
1426std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1427 if (c == nullptr) {
1428 return "null";
1429 }
1430 return c->PrettyClass();
1431}
1432
1433std::string Class::PrettyClass() {
1434 std::string result;
1435 result += "java.lang.Class<";
1436 result += PrettyDescriptor();
1437 result += ">";
1438 return result;
1439}
1440
1441std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1442 if (c == nullptr) {
1443 return "null";
1444 }
1445 return c->PrettyClassAndClassLoader();
1446}
1447
1448std::string Class::PrettyClassAndClassLoader() {
1449 std::string result;
1450 result += "java.lang.Class<";
1451 result += PrettyDescriptor();
1452 result += ",";
1453 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1454 // TODO: add an identifying hash value for the loader
1455 result += ">";
1456 return result;
1457}
1458
Andreas Gampe90b936d2017-01-31 08:58:55 -08001459template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1460 // Check class is loaded/retired or this is java.lang.String that has a
1461 // circularity issue during loading the names of its members
1462 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1463 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001464 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001465 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1466 << " IsRetired=" << IsRetired<kVerifyFlags>()
1467 << " IsErroneous=" <<
1468 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001469 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001470 << " status= " << GetStatus<kVerifyFlags>()
1471 << " descriptor=" << PrettyDescriptor();
1472}
1473// Instantiate the common cases.
1474template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1475template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1476template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1477template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1478template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1479
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001480} // namespace mirror
1481} // namespace art