blob: 5d730ce0b0db9ea47a8300dc697fc2ff622fc60f [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.
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "class-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070025#include "class_ext.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010026#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "class_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file-inl.h"
29#include "dex/dex_file_annotations.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080030#include "dex_cache.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032#include "handle_scope-inl.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070033#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070034#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070035#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080036#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070037#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070038#include "object_lock.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070039#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040#include "thread.h"
41#include "throwable.h"
42#include "utils.h"
43#include "well_known_classes.h"
44
45namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070046
47// TODO: move to own CC file?
48constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
49constexpr size_t BitString::kCapacity;
50
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080051namespace mirror {
52
Andreas Gampe46ee31b2016-12-14 10:11:49 -080053using android::base::StringPrintf;
54
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070057void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070058 CHECK(java_lang_Class_.IsNull())
59 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070060 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070061 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070062 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070063 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064}
65
66void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070067 CHECK(!java_lang_Class_.IsNull());
68 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080069}
70
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070071void Class::VisitRoots(RootVisitor* visitor) {
72 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080073}
74
Vladimir Marko7287c4d2018-02-15 10:41:07 +000075ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
76 const char* expected_name = nullptr;
77 ClassLinker::ClassRoot class_root = ClassLinker::kJavaLangObject; // Invalid.
78 if (name != nullptr && name->GetLength() >= 2) {
79 // Perfect hash for the expected values: from the second letters of the primitive types,
80 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
81 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
82 switch (hash) {
83 case 'b': expected_name = "boolean"; class_root = ClassLinker::kPrimitiveBoolean; break;
84 case 'B': expected_name = "byte"; class_root = ClassLinker::kPrimitiveByte; break;
85 case 'c': expected_name = "char"; class_root = ClassLinker::kPrimitiveChar; break;
86 case 'd': expected_name = "double"; class_root = ClassLinker::kPrimitiveDouble; break;
87 case 'f': expected_name = "float"; class_root = ClassLinker::kPrimitiveFloat; break;
88 case 'i': expected_name = "int"; class_root = ClassLinker::kPrimitiveInt; break;
89 case 'l': expected_name = "long"; class_root = ClassLinker::kPrimitiveLong; break;
90 case 's': expected_name = "short"; class_root = ClassLinker::kPrimitiveShort; break;
91 case 'v': expected_name = "void"; class_root = ClassLinker::kPrimitiveVoid; break;
92 default: break;
93 }
94 }
95 if (expected_name != nullptr && name->Equals(expected_name)) {
96 ObjPtr<mirror::Class> klass = Runtime::Current()->GetClassLinker()->GetClassRoot(class_root);
97 DCHECK(klass != nullptr);
98 return klass;
99 } else {
100 Thread* self = Thread::Current();
101 if (name == nullptr) {
102 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
103 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg */ nullptr);
104 } else {
105 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
106 }
107 return nullptr;
108 }
109}
110
Alex Light0273ad12016-11-02 11:19:31 -0700111ClassExt* Class::EnsureExtDataPresent(Thread* self) {
112 ObjPtr<ClassExt> existing(GetExtData());
113 if (!existing.IsNull()) {
114 return existing.Ptr();
115 }
116 StackHandleScope<3> hs(self);
117 // Handlerize 'this' since we are allocating here.
118 Handle<Class> h_this(hs.NewHandle(this));
119 // Clear exception so we can allocate.
120 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
121 self->ClearException();
122 // Allocate the ClassExt
123 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800124 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700125 // OOM allocating the classExt.
126 // TODO Should we restore the suppressed exception?
127 self->AssertPendingOOMException();
128 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800129 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700130 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
131 bool set;
132 // Set the ext_data_ field using CAS semantics.
133 if (Runtime::Current()->IsActiveTransaction()) {
134 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
135 ObjPtr<ClassExt>(nullptr),
136 new_ext.Get());
137 } else {
138 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
139 ObjPtr<ClassExt>(nullptr),
140 new_ext.Get());
141 }
142 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
143 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
144 CHECK(!ret.IsNull());
145 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800146 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700147 self->SetException(throwable.Get());
148 }
149 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800150 }
151}
152
Vladimir Marko2c64a832018-01-04 11:31:56 +0000153void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
154 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700155 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
156 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700157 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000158 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000159 new_status != ClassStatus::kErrorUnresolved &&
160 new_status != ClassStatus::kErrorResolved &&
161 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700162 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700163 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700164 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000165 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700166 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700167 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700168 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700169 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700170 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000172 if (UNLIKELY(IsErroneous(new_status))) {
173 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700174 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000175 << h_this->PrettyClass()
176 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000177 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700178 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700179 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700180 if (self->IsExceptionPending()) {
181 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
182 }
183 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184
Alex Light0273ad12016-11-02 11:19:31 -0700185 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
186 if (!ext.IsNull()) {
187 self->AssertPendingException();
188 ext->SetVerifyError(self->GetException());
189 } else {
190 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700191 }
192 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 }
Alex Light0273ad12016-11-02 11:19:31 -0700194
Vladimir Marko305c38b2018-02-14 11:50:07 +0000195 if (kBitstringSubtypeCheckEnabled) {
196 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700197 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
198 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000199 } else {
200 // The ClassStatus is always in the 4 most-significant bits of status_.
201 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
202 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
203 if (Runtime::Current()->IsActiveTransaction()) {
204 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
205 } else {
206 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
207 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700208 }
209
210 // Setting the object size alloc fast path needs to be after the status write so that if the
211 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
212 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000213 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700214 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
215 // Finalizable objects must always go slow path.
216 if (!h_this->IsFinalizable()) {
217 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700218 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100219 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700220
221 if (!class_linker_initialized) {
222 // When the class linker is being initialized its single threaded and by definition there can be
223 // no waiters. During initialization classes may appear temporary but won't be retired as their
224 // size was statically computed.
225 } else {
226 // Classes that are being resolved or initialized need to notify waiters that the class status
227 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700228 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700229 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
230 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000231 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
232 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700233 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234 }
235 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000236 CHECK_NE(new_status, ClassStatus::kRetired);
237 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700238 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700239 }
240 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700241 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242}
243
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700244void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700245 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246}
247
Ian Rogersef7d42f2014-01-06 12:55:46 -0800248void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700250 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
251 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700252 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700253 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700254 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255}
256
257// Return the class' name. The exact format is bizarre, but it's the specified behavior for
258// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
259// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
260// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700261String* Class::ComputeName(Handle<Class> h_this) {
262 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800263 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 return name;
265 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700266 std::string temp;
267 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800268 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
270 // The descriptor indicates that this is the class for
271 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700272 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 switch (descriptor[0]) {
274 case 'Z': c_name = "boolean"; break;
275 case 'B': c_name = "byte"; break;
276 case 'C': c_name = "char"; break;
277 case 'S': c_name = "short"; break;
278 case 'I': c_name = "int"; break;
279 case 'J': c_name = "long"; break;
280 case 'F': c_name = "float"; break;
281 case 'D': c_name = "double"; break;
282 case 'V': c_name = "void"; break;
283 default:
284 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
285 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800286 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 } else {
288 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
289 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700290 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700292 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 return name;
294}
295
Ian Rogersef7d42f2014-01-06 12:55:46 -0800296void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700298 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 if ((flags & kDumpClassClassLoader) != 0) {
300 os << ' ' << GetClassLoader();
301 }
302 if ((flags & kDumpClassInitialized) != 0) {
303 os << ' ' << GetStatus();
304 }
305 os << "\n";
306 return;
307 }
308
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700310 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700311 Handle<Class> h_this(hs.NewHandle(this));
312 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700314
Ian Rogers1ff3c982014-08-12 02:30:58 -0700315 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700317 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800319 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 os << StringPrintf(" access=0x%04x.%04x\n",
321 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800322 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700323 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700324 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325 }
326 if (IsArrayClass()) {
327 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
328 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700329 const size_t num_direct_interfaces = NumDirectInterfaces();
330 if (num_direct_interfaces > 0) {
331 os << " interfaces (" << num_direct_interfaces << "):\n";
332 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000333 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700334 if (interface == nullptr) {
335 os << StringPrintf(" %2zd: nullptr!\n", i);
336 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700337 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
338 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700339 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 }
341 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700342 if (!IsLoaded()) {
343 os << " class not yet loaded";
344 } else {
345 // After this point, this may have moved due to GetDirectInterface.
346 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800347 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700348 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700349 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700350 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700352 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
353 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700354 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700356 }
357 if (h_this->NumStaticFields() > 0) {
358 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000359 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700360 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700361 os << StringPrintf(" %2zd: %s\n", i,
362 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700363 }
364 } else {
365 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700367 }
368 if (h_this->NumInstanceFields() > 0) {
369 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000370 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700371 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700372 os << StringPrintf(" %2zd: %s\n", i,
373 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700374 }
375 } else {
376 os << " <not yet available>";
377 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 }
379 }
380}
381
382void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384 // Sanity check that the number of bits set in the reference offset bitmap
385 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700386 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700387 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 count += c->NumReferenceInstanceFieldsDuringLinking();
389 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700390 // +1 for the Class in Object.
391 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100393 // Not called within a transaction.
394 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700395 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396}
397
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
399 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700400 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
401 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 ++i;
403 }
404 if (descriptor1.find('/', i) != StringPiece::npos ||
405 descriptor2.find('/', i) != StringPiece::npos) {
406 return false;
407 } else {
408 return true;
409 }
410}
411
Mathieu Chartier3398c782016-09-30 10:27:43 -0700412bool Class::IsInSamePackage(ObjPtr<Class> that) {
413 ObjPtr<Class> klass1 = this;
414 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415 if (klass1 == klass2) {
416 return true;
417 }
418 // Class loaders must match.
419 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
420 return false;
421 }
422 // Arrays are in the same package when their element classes are.
423 while (klass1->IsArrayClass()) {
424 klass1 = klass1->GetComponentType();
425 }
426 while (klass2->IsArrayClass()) {
427 klass2 = klass2->GetComponentType();
428 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700429 // trivial check again for array types
430 if (klass1 == klass2) {
431 return true;
432 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700434 std::string temp1, temp2;
435 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436}
437
Ian Rogersef7d42f2014-01-06 12:55:46 -0800438bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
440}
441
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700442void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100443 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700444 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100445 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700446 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100447 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448}
449
Vladimir Markoba118822017-06-12 15:41:56 +0100450template <typename SignatureType>
451static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
452 const StringPiece& name,
453 const SignatureType& signature,
454 PointerSize pointer_size)
455 REQUIRES_SHARED(Locks::mutator_lock_) {
456 // If the current class is not an interface, skip the search of its declared methods;
457 // such lookup is used only to distinguish between IncompatibleClassChangeError and
458 // NoSuchMethodError and the caller has already tried to search methods in the class.
459 if (LIKELY(klass->IsInterface())) {
460 // Search declared methods, both direct and virtual.
461 // (This lookup is used also for invoke-static on interface classes.)
462 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
463 if (method.GetName() == name && method.GetSignature() == signature) {
464 return &method;
465 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466 }
467 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468
Vladimir Markoba118822017-06-12 15:41:56 +0100469 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
470 // we should return it, otherwise an arbitrary one can be returned.
471 ObjPtr<IfTable> iftable = klass->GetIfTable();
472 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
473 ObjPtr<Class> iface = iftable->GetInterface(i);
474 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
475 if (method.GetName() == name && method.GetSignature() == signature) {
476 return &method;
477 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700478 }
479 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480
Vladimir Markoba118822017-06-12 15:41:56 +0100481 // Then search for public non-static methods in the java.lang.Object.
482 if (LIKELY(klass->IsInterface())) {
483 ObjPtr<Class> object_class = klass->GetSuperClass();
484 DCHECK(object_class->IsObjectClass());
485 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
486 if (method.IsPublic() && !method.IsStatic() &&
487 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 }
490 }
491 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700492 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493}
494
Vladimir Markoba118822017-06-12 15:41:56 +0100495ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
496 const StringPiece& signature,
497 PointerSize pointer_size) {
498 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499}
500
Vladimir Markoba118822017-06-12 15:41:56 +0100501ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
502 const Signature& signature,
503 PointerSize pointer_size) {
504 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700505}
506
Vladimir Markoba118822017-06-12 15:41:56 +0100507ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
508 uint32_t dex_method_idx,
509 PointerSize pointer_size) {
510 // We always search by name and signature, ignoring the type index in the MethodId.
511 const DexFile& dex_file = *dex_cache->GetDexFile();
512 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
513 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
514 const Signature signature = dex_file.GetMethodSignature(method_id);
515 return FindInterfaceMethod(name, signature, pointer_size);
516}
517
Alex Lightafb66472017-08-01 09:54:49 -0700518static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
519 ObjPtr<mirror::Class> declaring_class)
520 REQUIRES_SHARED(Locks::mutator_lock_) {
521 if (klass->IsArrayClass()) {
522 return declaring_class->IsObjectClass();
523 } else if (klass->IsInterface()) {
524 return declaring_class->IsObjectClass() || declaring_class == klass;
525 } else {
526 return klass->IsSubClass(declaring_class);
527 }
528}
529
Vladimir Markoba118822017-06-12 15:41:56 +0100530static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
531 ObjPtr<mirror::Class> declaring_class,
532 ArtMethod& method)
533 REQUIRES_SHARED(Locks::mutator_lock_) {
534 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
535 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700536 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100537 uint32_t access_flags = method.GetAccessFlags();
538 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
539 return true;
540 }
541 if ((access_flags & kAccPrivate) != 0) {
542 return false;
543 }
544 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
545 if (!klass->IsInSamePackage(declaring_class)) {
546 return false;
547 }
548 }
549 return true;
550}
551
552template <typename SignatureType>
553static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
554 const StringPiece& name,
555 const SignatureType& signature,
556 PointerSize pointer_size)
557 REQUIRES_SHARED(Locks::mutator_lock_) {
558 // Search declared methods first.
559 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
560 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
561 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
562 return &method;
563 }
564 }
565
566 // Then search the superclass chain. If we find an inherited method, return it.
567 // If we find a method that's not inherited because of access restrictions,
568 // try to find a method inherited from an interface in copied methods.
569 ObjPtr<Class> klass = this_klass->GetSuperClass();
570 ArtMethod* uninherited_method = nullptr;
571 for (; klass != nullptr; klass = klass->GetSuperClass()) {
572 DCHECK(!klass->IsProxyClass());
573 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
574 if (method.GetName() == name && method.GetSignature() == signature) {
575 if (IsInheritedMethod(this_klass, klass, method)) {
576 return &method;
577 }
578 uninherited_method = &method;
579 break;
580 }
581 }
582 if (uninherited_method != nullptr) {
583 break;
584 }
585 }
586
587 // Then search copied methods.
588 // If we found a method that's not inherited, stop the search in its declaring class.
589 ObjPtr<Class> end_klass = klass;
590 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
591 klass = this_klass;
592 if (UNLIKELY(klass->IsProxyClass())) {
593 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
594 klass = klass->GetSuperClass();
595 }
596 for (; klass != end_klass; klass = klass->GetSuperClass()) {
597 DCHECK(!klass->IsProxyClass());
598 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
599 if (method.GetName() == name && method.GetSignature() == signature) {
600 return &method; // No further check needed, copied methods are inherited by definition.
601 }
602 }
603 }
604 return uninherited_method; // Return the `uninherited_method` if any.
605}
606
607
608ArtMethod* Class::FindClassMethod(const StringPiece& name,
609 const StringPiece& signature,
610 PointerSize pointer_size) {
611 return FindClassMethodWithSignature(this, name, signature, pointer_size);
612}
613
614ArtMethod* Class::FindClassMethod(const StringPiece& name,
615 const Signature& signature,
616 PointerSize pointer_size) {
617 return FindClassMethodWithSignature(this, name, signature, pointer_size);
618}
619
620ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
621 uint32_t dex_method_idx,
622 PointerSize pointer_size) {
623 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
624 DCHECK(!IsProxyClass());
625
626 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
627 ObjPtr<DexCache> this_dex_cache = GetDexCache();
628 if (this_dex_cache == dex_cache) {
629 // Lookup is always performed in the class referenced by the MethodId.
630 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
631 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
632 if (method.GetDexMethodIndex() == dex_method_idx) {
633 return &method;
634 }
635 }
636 }
637 // If not found, we need to search by name and signature.
638 const DexFile& dex_file = *dex_cache->GetDexFile();
639 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
640 const Signature signature = dex_file.GetMethodSignature(method_id);
641 StringPiece name; // Delay strlen() until actually needed.
642 // If we do not have a dex_cache match, try to find the declared method in this class now.
643 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
644 DCHECK(name.empty());
645 name = dex_file.StringDataByIdx(method_id.name_idx_);
646 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
647 if (method.GetName() == name && method.GetSignature() == signature) {
648 return &method;
649 }
650 }
651 }
652
653 // Then search the superclass chain. If we find an inherited method, return it.
654 // If we find a method that's not inherited because of access restrictions,
655 // try to find a method inherited from an interface in copied methods.
656 ArtMethod* uninherited_method = nullptr;
657 ObjPtr<Class> klass = GetSuperClass();
658 for (; klass != nullptr; klass = klass->GetSuperClass()) {
659 ArtMethod* candidate_method = nullptr;
660 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
661 if (klass->GetDexCache() == dex_cache) {
662 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
663 // the type index differs, so compare the name index and proto index.
664 for (ArtMethod& method : declared_methods) {
665 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
666 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
667 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
668 candidate_method = &method;
669 break;
670 }
671 }
672 } else {
673 if (!declared_methods.empty() && name.empty()) {
674 name = dex_file.StringDataByIdx(method_id.name_idx_);
675 }
676 for (ArtMethod& method : declared_methods) {
677 if (method.GetName() == name && method.GetSignature() == signature) {
678 candidate_method = &method;
679 break;
680 }
681 }
682 }
683 if (candidate_method != nullptr) {
684 if (IsInheritedMethod(this, klass, *candidate_method)) {
685 return candidate_method;
686 } else {
687 uninherited_method = candidate_method;
688 break;
689 }
690 }
691 }
692
693 // Then search copied methods.
694 // If we found a method that's not inherited, stop the search in its declaring class.
695 ObjPtr<Class> end_klass = klass;
696 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
697 // After we have searched the declared methods of the super-class chain,
698 // search copied methods which can contain methods from interfaces.
699 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
700 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
701 if (!copied_methods.empty() && name.empty()) {
702 name = dex_file.StringDataByIdx(method_id.name_idx_);
703 }
704 for (ArtMethod& method : copied_methods) {
705 if (method.GetName() == name && method.GetSignature() == signature) {
706 return &method; // No further check needed, copied methods are inherited by definition.
707 }
708 }
709 }
710 return uninherited_method; // Return the `uninherited_method` if any.
711}
712
713ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
714 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
715 DCHECK(!IsProxyClass());
716 StringPiece name("<init>");
717 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
718 if (method.GetName() == name && method.GetSignature() == signature) {
719 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800720 }
721 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700722 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800723}
724
Andreas Gampe542451c2016-07-26 09:02:02 -0700725ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
726 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000727 for (auto& method : GetDirectMethods(pointer_size)) {
728 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
729 if (name == np_method->GetName()) {
730 return &method;
731 }
732 }
733 return nullptr;
734}
735
Andreas Gampe542451c2016-07-26 09:02:02 -0700736ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
737 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000738 for (auto& method : GetVirtualMethods(pointer_size)) {
739 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
740 if (name == np_method->GetName()) {
741 return &method;
742 }
743 }
744 return nullptr;
745}
746
Andreas Gampe542451c2016-07-26 09:02:02 -0700747ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700748 DCHECK(method->GetDeclaringClass()->IsInterface());
749 DCHECK(IsInterface()) << "Should only be called on a interface class";
750 // Check if we have one defined on this interface first. This includes searching copied ones to
751 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
752 // don't do any indirect method checks here.
753 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
754 if (method->HasSameNameAndSignature(&iface_method)) {
755 return &iface_method;
756 }
757 }
758
759 std::vector<ArtMethod*> abstract_methods;
760 // Search through the IFTable for a working version. We don't need to check for conflicts
761 // because if there was one it would appear in this classes virtual_methods_ above.
762
763 Thread* self = Thread::Current();
764 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700765 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
766 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700767 size_t iftable_count = GetIfTableCount();
768 // Find the method. We don't need to check for conflicts because they would have been in the
769 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
770 // subtypiest interfaces get visited first.
771 for (size_t k = iftable_count; k != 0;) {
772 k--;
773 DCHECK_LT(k, iftable->Count());
774 iface.Assign(iftable->GetInterface(k));
775 // Iterate through every declared method on this interface. Each direct method's name/signature
776 // is unique so the order of the inner loop doesn't matter.
777 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
778 ArtMethod* current_method = &method_iter;
779 if (current_method->HasSameNameAndSignature(method)) {
780 if (current_method->IsDefault()) {
781 // Handle JLS soft errors, a default method from another superinterface tree can
782 // "override" an abstract method(s) from another superinterface tree(s). To do this,
783 // ignore any [default] method which are dominated by the abstract methods we've seen so
784 // far. Check if overridden by any in abstract_methods. We do not need to check for
785 // default_conflicts because we would hit those before we get to this loop.
786 bool overridden = false;
787 for (ArtMethod* possible_override : abstract_methods) {
788 DCHECK(possible_override->HasSameNameAndSignature(current_method));
789 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
790 overridden = true;
791 break;
792 }
793 }
794 if (!overridden) {
795 return current_method;
796 }
797 } else {
798 // Is not default.
799 // This might override another default method. Just stash it for now.
800 abstract_methods.push_back(current_method);
801 }
802 }
803 }
804 }
805 // If we reach here we either never found any declaration of the method (in which case
806 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
807 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
808 // of these arbitrarily.
809 return abstract_methods.empty() ? nullptr : abstract_methods[0];
810}
811
Andreas Gampe542451c2016-07-26 09:02:02 -0700812ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700813 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
814 if (method.IsClassInitializer()) {
815 DCHECK_STREQ(method.GetName(), "<clinit>");
816 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
817 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700818 }
819 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700820 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700821}
822
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700823// Custom binary search to avoid double comparisons from std::binary_search.
824static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
825 const StringPiece& name,
826 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700827 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700828 if (fields == nullptr) {
829 return nullptr;
830 }
831 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100832 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700833 ArtField* ret = nullptr;
834 while (low < high) {
835 size_t mid = (low + high) / 2;
836 ArtField& field = fields->At(mid);
837 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
838 // verifier. There can be multiple fields with the same in the same class name due to proguard.
839 int result = StringPiece(field.GetName()).Compare(name);
840 if (result == 0) {
841 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
842 }
843 if (result < 0) {
844 low = mid + 1;
845 } else if (result > 0) {
846 high = mid;
847 } else {
848 ret = &field;
849 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800850 }
851 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700852 if (kIsDebugBuild) {
853 ArtField* found = nullptr;
854 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
855 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
856 found = &field;
857 break;
858 }
859 }
David Sehr709b0702016-10-13 09:12:37 -0700860 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700861 }
862 return ret;
863}
864
865ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
866 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
867 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800868}
869
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700870ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800871 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700872 for (ArtField& field : GetIFields()) {
873 if (field.GetDexFieldIndex() == dex_field_idx) {
874 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800875 }
876 }
877 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700878 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800879}
880
Brian Carlstromea46f952013-07-30 01:26:50 -0700881ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800882 // Is the field in this class, or any of its superclasses?
883 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700884 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700885 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700886 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800887 return f;
888 }
889 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700890 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800891}
892
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700893ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800894 // Is the field in this class, or any of its superclasses?
895 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700896 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700897 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700898 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800899 return f;
900 }
901 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700902 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800903}
904
Brian Carlstromea46f952013-07-30 01:26:50 -0700905ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700906 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700907 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800908}
909
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700910ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800911 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700912 for (ArtField& field : GetSFields()) {
913 if (field.GetDexFieldIndex() == dex_field_idx) {
914 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800915 }
916 }
917 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700918 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800919}
920
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700921ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000922 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700923 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700924 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800925 // Is the field in this class (or its interfaces), or any of its
926 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000927 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800928 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700929 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700930 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800931 return f;
932 }
933 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000934 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
935 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
936 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700937 f = FindStaticField(self, interface, name, type);
938 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800939 return f;
940 }
941 }
942 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700943 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800944}
945
Vladimir Markobb268b12016-06-30 15:52:56 +0100946ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700947 ObjPtr<Class> klass,
948 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700949 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700950 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800951 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700952 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700953 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800954 return f;
955 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100956 // Though GetDirectInterface() should not cause thread suspension when called
957 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700958 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800959 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000960 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
961 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
962 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700963 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
964 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800965 return f;
966 }
967 }
968 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700969 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800970}
971
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700972ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000973 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700974 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700975 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800976 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000977 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800978 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700979 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700980 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800981 return f;
982 }
983 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700984 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800985 return f;
986 }
987 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000988 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
989 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
990 DCHECK(interface != nullptr);
991 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700992 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800993 return f;
994 }
995 }
996 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700997 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800998}
999
Andreas Gampe542451c2016-07-26 09:02:02 -07001000void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001001 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -08001002 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -07001003 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -08001004 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001005 }
1006 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +02001007}
1008
Ian Rogers1ff3c982014-08-12 02:30:58 -07001009const char* Class::GetDescriptor(std::string* storage) {
1010 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001011 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001012 } else if (IsArrayClass()) {
1013 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001014 } else if (IsProxyClass()) {
1015 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001016 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001017 } else {
1018 const DexFile& dex_file = GetDexFile();
1019 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
1020 return dex_file.GetTypeDescriptor(type_id);
1021 }
1022}
1023
Ian Rogers1ff3c982014-08-12 02:30:58 -07001024const char* Class::GetArrayDescriptor(std::string* storage) {
1025 std::string temp;
1026 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1027 *storage = "[";
1028 *storage += elem_desc;
1029 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001030}
1031
1032const DexFile::ClassDef* Class::GetClassDef() {
1033 uint16_t class_def_idx = GetDexClassDefIndex();
1034 if (class_def_idx == DexFile::kDexNoIndex16) {
1035 return nullptr;
1036 }
1037 return &GetDexFile().GetClassDef(class_def_idx);
1038}
1039
Andreas Gampea5b09a62016-11-17 15:21:22 -08001040dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001041 DCHECK(!IsPrimitive());
1042 DCHECK(!IsArrayClass());
1043 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1044}
1045
Vladimir Marko19a4d372016-12-08 14:41:46 +00001046ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1047 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001048 DCHECK(!klass->IsPrimitive());
1049 if (klass->IsArrayClass()) {
1050 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001051 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1052 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001053 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001054 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001055 } else {
1056 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001057 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001058 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001059 DCHECK(interface != nullptr);
1060 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001061 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001062 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001063 DCHECK(interfaces != nullptr);
1064 return interfaces->Get(idx);
1065 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001066 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001067 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001068 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001069 return interface;
1070 }
1071}
1072
Vladimir Marko19a4d372016-12-08 14:41:46 +00001073ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1074 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1075 if (interface == nullptr) {
1076 DCHECK(!klass->IsArrayClass());
1077 DCHECK(!klass->IsProxyClass());
1078 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001079 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001080 CHECK(interface != nullptr || self->IsExceptionPending());
1081 }
1082 return interface;
1083}
1084
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001085ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001086 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001087 DCHECK(!klass->IsInterface());
1088 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001089 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001090 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001091 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001092 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001093 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001094 }
Calin Juravle52503d82015-11-11 16:58:31 +00001095 return common_super_class;
1096}
1097
Mathieu Chartierf8322842014-05-16 10:59:25 -07001098const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001099 const DexFile& dex_file = GetDexFile();
1100 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001101 if (dex_class_def == nullptr) {
1102 // Generated classes have no class def.
1103 return nullptr;
1104 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001105 return dex_file.GetSourceFile(*dex_class_def);
1106}
1107
1108std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001109 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001110 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001111 return dex_cache->GetLocation()->ToModifiedUtf8();
1112 }
1113 // Arrays and proxies are generated and have no corresponding dex file location.
1114 return "generated class";
1115}
1116
1117const DexFile::TypeList* Class::GetInterfaceTypeList() {
1118 const DexFile::ClassDef* class_def = GetClassDef();
1119 if (class_def == nullptr) {
1120 return nullptr;
1121 }
1122 return GetDexFile().GetInterfacesList(*class_def);
1123}
1124
Andreas Gampe542451c2016-07-26 09:02:02 -07001125void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001126 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001127 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001128 const size_t table_length = table->GetLength();
1129 SetEmbeddedVTableLength(table_length);
1130 for (size_t i = 0; i < table_length; i++) {
1131 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001132 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001133 // Keep java.lang.Object class's vtable around for since it's easier
1134 // to be reused by array classes during their linking.
1135 if (!IsObjectClass()) {
1136 SetVTable(nullptr);
1137 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001138}
1139
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001140class ReadBarrierOnNativeRootsVisitor {
1141 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001142 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001143 MemberOffset offset ATTRIBUTE_UNUSED,
1144 bool is_static ATTRIBUTE_UNUSED) const {}
1145
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001146 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001147 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001148 if (!root->IsNull()) {
1149 VisitRoot(root);
1150 }
1151 }
1152
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001153 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001154 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001155 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1156 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001157 if (old_ref != new_ref) {
1158 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1159 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001160 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001161 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001162 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1163 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001164 }
1165 }
1166};
1167
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001168// The pre-fence visitor for Class::CopyOf().
1169class CopyClassVisitor {
1170 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001171 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001172 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001173 size_t new_length,
1174 size_t copy_bytes,
1175 ImTable* imt,
1176 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001177 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001178 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001179 }
1180
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001181 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001182 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001183 StackHandleScope<1> hs(self_);
1184 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001185 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001186 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001187 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1188 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001189 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001190 // Visit all of the references to make sure there is no from space references in the native
1191 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001192 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001193 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001194 }
1195
1196 private:
1197 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001198 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001199 const size_t new_length_;
1200 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001201 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001202 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001203 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1204};
1205
Andreas Gampe542451c2016-07-26 09:02:02 -07001206Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001207 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1208 // We may get copied by a compacting GC.
1209 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001210 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001211 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001212 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1213 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001214 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001215 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001216 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1217 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001218 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001219 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001220 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001221 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001222 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001223}
1224
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001225bool Class::ProxyDescriptorEquals(const char* match) {
1226 DCHECK(IsProxyClass());
1227 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001228}
1229
Mathieu Chartiere401d142015-04-22 13:56:20 -07001230// TODO: Move this to java_lang_Class.cc?
1231ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001232 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001233 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001234 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001235 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001236 continue;
1237 }
1238 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001239 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001240 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001241 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001242 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001243 return nullptr;
1244 }
1245 }
1246 return nullptr;
1247}
1248
Mathieu Chartiere401d142015-04-22 13:56:20 -07001249uint32_t Class::Depth() {
1250 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001251 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001252 depth++;
1253 }
1254 return depth;
1255}
1256
Andreas Gampea5b09a62016-11-17 15:21:22 -08001257dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001258 std::string temp;
1259 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001260 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001261}
1262
Andreas Gampe542451c2016-07-26 09:02:02 -07001263template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001264ObjPtr<Method> Class::GetDeclaredMethodInternal(
1265 Thread* self,
1266 ObjPtr<Class> klass,
1267 ObjPtr<String> name,
1268 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001269 // Covariant return types permit the class to define multiple
1270 // methods with the same name and parameter types. Prefer to
1271 // return a non-synthetic method in such situations. We may
1272 // still return a synthetic method to handle situations like
1273 // escalated visibility. We never return miranda methods that
1274 // were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001275 StackHandleScope<3> hs(self);
1276 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001277 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001278 ThrowNullPointerException("name == null");
1279 return nullptr;
1280 }
1281 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001282 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001283 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001284 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1285 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001286 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001287 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001288 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1289 if (UNLIKELY(self->IsExceptionPending())) {
1290 return nullptr;
1291 }
1292 continue;
1293 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001294 if (!m.IsMiranda()) {
1295 if (!m.IsSynthetic()) {
1296 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1297 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001298 result = &m; // Remember as potential result if it's not a miranda method.
1299 }
1300 }
1301 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001302 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001303 auto modifiers = m.GetAccessFlags();
1304 if ((modifiers & kAccConstructor) != 0) {
1305 continue;
1306 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001307 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001308 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001309 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001310 if (np_name == nullptr) {
1311 self->AssertPendingException();
1312 return nullptr;
1313 }
1314 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1315 if (UNLIKELY(self->IsExceptionPending())) {
1316 return nullptr;
1317 }
1318 continue;
1319 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001320 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
1321 if ((modifiers & kAccSynthetic) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001322 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001323 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001324 result = &m; // Remember as potential result.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001325 }
1326 }
1327 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001328 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001329 : nullptr;
1330}
1331
1332template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001333ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001334 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001335 ObjPtr<Class> klass,
1336 ObjPtr<String> name,
1337 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001338template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001339ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001340 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001341 ObjPtr<Class> klass,
1342 ObjPtr<String> name,
1343 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001344template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001345ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001346 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001347 ObjPtr<Class> klass,
1348 ObjPtr<String> name,
1349 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001350template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001351ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001352 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001353 ObjPtr<Class> klass,
1354 ObjPtr<String> name,
1355 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001356
Andreas Gampe542451c2016-07-26 09:02:02 -07001357template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001358ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001359 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001360 ObjPtr<Class> klass,
1361 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001362 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001363 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001364 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001365 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001366 : nullptr;
1367}
1368
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001369// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001370
Andreas Gampe542451c2016-07-26 09:02:02 -07001371template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001372ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001373 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001374 ObjPtr<Class> klass,
1375 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001376template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001377ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001378 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001379 ObjPtr<Class> klass,
1380 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001381template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001382ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001383 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001384 ObjPtr<Class> klass,
1385 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001386template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001387ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001388 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001389 ObjPtr<Class> klass,
1390 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001391
Andreas Gampe715fdc22016-04-18 17:07:30 -07001392int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1393 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1394 return default_value;
1395 }
1396 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001397 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001398 return default_value;
1399 }
1400 return flags;
1401}
1402
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001403void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1404 if (Runtime::Current()->IsActiveTransaction()) {
1405 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1406 } else {
1407 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1408 }
1409}
1410
David Sehr709b0702016-10-13 09:12:37 -07001411std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1412 if (klass == nullptr) {
1413 return "null";
1414 }
1415 return klass->PrettyDescriptor();
1416}
1417
1418std::string Class::PrettyDescriptor() {
1419 std::string temp;
1420 return art::PrettyDescriptor(GetDescriptor(&temp));
1421}
1422
1423std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1424 if (c == nullptr) {
1425 return "null";
1426 }
1427 return c->PrettyClass();
1428}
1429
1430std::string Class::PrettyClass() {
1431 std::string result;
1432 result += "java.lang.Class<";
1433 result += PrettyDescriptor();
1434 result += ">";
1435 return result;
1436}
1437
1438std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1439 if (c == nullptr) {
1440 return "null";
1441 }
1442 return c->PrettyClassAndClassLoader();
1443}
1444
1445std::string Class::PrettyClassAndClassLoader() {
1446 std::string result;
1447 result += "java.lang.Class<";
1448 result += PrettyDescriptor();
1449 result += ",";
1450 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1451 // TODO: add an identifying hash value for the loader
1452 result += ">";
1453 return result;
1454}
1455
Andreas Gampe90b936d2017-01-31 08:58:55 -08001456template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1457 // Check class is loaded/retired or this is java.lang.String that has a
1458 // circularity issue during loading the names of its members
1459 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1460 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
1461 this == String::GetJavaLangString())
1462 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1463 << " IsRetired=" << IsRetired<kVerifyFlags>()
1464 << " IsErroneous=" <<
1465 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
1466 << " IsString=" << (this == String::GetJavaLangString())
1467 << " status= " << GetStatus<kVerifyFlags>()
1468 << " descriptor=" << PrettyDescriptor();
1469}
1470// Instantiate the common cases.
1471template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1472template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1473template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1474template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1475template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1476
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001477} // namespace mirror
1478} // namespace art