blob: fcd3714b998f39e124a54195cc21b04b3b931905 [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"
Andreas Gampead1aa632019-01-02 10:30:54 -080033#include "dex/signature-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "dex_cache.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/card_table-inl.h"
Andreas Gampee15b9b12018-10-29 12:54:27 -070036#include "gc/heap-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070037#include "handle_scope-inl.h"
David Brazdil4bcd6572019-02-02 20:08:44 +000038#include "hidden_api.h"
Igor Murashkin86083f72017-10-27 10:59:04 -070039#include "subtype_check.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070040#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070041#include "object-inl.h"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080042#include "object-refvisitor-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070043#include "object_array-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070044#include "object_lock.h"
Vladimir Marko5924a4a2018-05-29 17:40:41 +010045#include "string-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070046#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047#include "thread.h"
48#include "throwable.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049#include "well_known_classes.h"
50
51namespace art {
Igor Murashkin86083f72017-10-27 10:59:04 -070052
53// TODO: move to own CC file?
54constexpr size_t BitString::kBitSizeAtPosition[BitString::kCapacity];
55constexpr size_t BitString::kCapacity;
56
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057namespace mirror {
58
Andreas Gampe46ee31b2016-12-14 10:11:49 -080059using android::base::StringPrintf;
60
Vladimir Marko7287c4d2018-02-15 10:41:07 +000061ObjPtr<mirror::Class> Class::GetPrimitiveClass(ObjPtr<mirror::String> name) {
62 const char* expected_name = nullptr;
Vladimir Markob4eb1b12018-05-24 11:09:38 +010063 ClassRoot class_root = ClassRoot::kJavaLangObject; // Invalid.
Vladimir Marko7287c4d2018-02-15 10:41:07 +000064 if (name != nullptr && name->GetLength() >= 2) {
65 // Perfect hash for the expected values: from the second letters of the primitive types,
66 // only 'y' has the bit 0x10 set, so use it to change 'b' to 'B'.
67 char hash = name->CharAt(0) ^ ((name->CharAt(1) & 0x10) << 1);
68 switch (hash) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010069 case 'b': expected_name = "boolean"; class_root = ClassRoot::kPrimitiveBoolean; break;
70 case 'B': expected_name = "byte"; class_root = ClassRoot::kPrimitiveByte; break;
71 case 'c': expected_name = "char"; class_root = ClassRoot::kPrimitiveChar; break;
72 case 'd': expected_name = "double"; class_root = ClassRoot::kPrimitiveDouble; break;
73 case 'f': expected_name = "float"; class_root = ClassRoot::kPrimitiveFloat; break;
74 case 'i': expected_name = "int"; class_root = ClassRoot::kPrimitiveInt; break;
75 case 'l': expected_name = "long"; class_root = ClassRoot::kPrimitiveLong; break;
76 case 's': expected_name = "short"; class_root = ClassRoot::kPrimitiveShort; break;
77 case 'v': expected_name = "void"; class_root = ClassRoot::kPrimitiveVoid; break;
Vladimir Marko7287c4d2018-02-15 10:41:07 +000078 default: break;
79 }
80 }
81 if (expected_name != nullptr && name->Equals(expected_name)) {
Vladimir Markob4eb1b12018-05-24 11:09:38 +010082 ObjPtr<mirror::Class> klass = GetClassRoot(class_root);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000083 DCHECK(klass != nullptr);
84 return klass;
85 } else {
86 Thread* self = Thread::Current();
87 if (name == nullptr) {
88 // Note: ThrowNullPointerException() requires a message which we deliberately want to omit.
Andreas Gampe98ea9d92018-10-19 14:06:15 -070089 self->ThrowNewException("Ljava/lang/NullPointerException;", /* msg= */ nullptr);
Vladimir Marko7287c4d2018-02-15 10:41:07 +000090 } else {
91 self->ThrowNewException("Ljava/lang/ClassNotFoundException;", name->ToModifiedUtf8().c_str());
92 }
93 return nullptr;
94 }
95}
96
Alex Light0273ad12016-11-02 11:19:31 -070097ClassExt* Class::EnsureExtDataPresent(Thread* self) {
98 ObjPtr<ClassExt> existing(GetExtData());
99 if (!existing.IsNull()) {
100 return existing.Ptr();
101 }
102 StackHandleScope<3> hs(self);
103 // Handlerize 'this' since we are allocating here.
104 Handle<Class> h_this(hs.NewHandle(this));
105 // Clear exception so we can allocate.
106 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
107 self->ClearException();
108 // Allocate the ClassExt
109 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800110 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700111 // OOM allocating the classExt.
112 // TODO Should we restore the suppressed exception?
113 self->AssertPendingOOMException();
114 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -0800115 } else {
Alex Light0273ad12016-11-02 11:19:31 -0700116 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
117 bool set;
118 // Set the ext_data_ field using CAS semantics.
119 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700120 set = h_this->CasFieldObject<true>(ext_offset,
121 nullptr,
122 new_ext.Get(),
123 CASMode::kStrong,
124 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700125 } else {
Mathieu Chartiera9746b92018-06-22 10:25:40 -0700126 set = h_this->CasFieldObject<false>(ext_offset,
127 nullptr,
128 new_ext.Get(),
129 CASMode::kStrong,
130 std::memory_order_seq_cst);
Alex Light0273ad12016-11-02 11:19:31 -0700131 }
132 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
133 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
134 CHECK(!ret.IsNull());
135 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800136 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700137 self->SetException(throwable.Get());
138 }
139 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800140 }
141}
142
Vladimir Marko2c64a832018-01-04 11:31:56 +0000143void Class::SetStatus(Handle<Class> h_this, ClassStatus new_status, Thread* self) {
144 ClassStatus old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700145 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
146 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700147 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000148 if (UNLIKELY(new_status <= old_status &&
Vladimir Marko2c64a832018-01-04 11:31:56 +0000149 new_status != ClassStatus::kErrorUnresolved &&
150 new_status != ClassStatus::kErrorResolved &&
151 new_status != ClassStatus::kRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700152 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700153 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700154 }
Vladimir Marko2c64a832018-01-04 11:31:56 +0000155 if (new_status >= ClassStatus::kResolved || old_status >= ClassStatus::kResolved) {
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700156 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700157 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700158 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700159 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700160 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000162 if (UNLIKELY(IsErroneous(new_status))) {
163 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700164 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000165 << h_this->PrettyClass()
166 << " old_status: " << old_status << " new_status: " << new_status;
Vladimir Marko2c64a832018-01-04 11:31:56 +0000167 CHECK_EQ(new_status == ClassStatus::kErrorResolved, old_status >= ClassStatus::kResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700168 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700169 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700170 if (self->IsExceptionPending()) {
171 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
172 }
173 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174
Alex Light0273ad12016-11-02 11:19:31 -0700175 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
176 if (!ext.IsNull()) {
177 self->AssertPendingException();
178 ext->SetVerifyError(self->GetException());
179 } else {
180 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700181 }
182 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183 }
Alex Light0273ad12016-11-02 11:19:31 -0700184
Vladimir Marko305c38b2018-02-14 11:50:07 +0000185 if (kBitstringSubtypeCheckEnabled) {
186 // FIXME: This looks broken with respect to aborted transactions.
Igor Murashkin86083f72017-10-27 10:59:04 -0700187 ObjPtr<mirror::Class> h_this_ptr = h_this.Get();
188 SubtypeCheck<ObjPtr<mirror::Class>>::WriteStatus(h_this_ptr, new_status);
Vladimir Marko305c38b2018-02-14 11:50:07 +0000189 } else {
190 // The ClassStatus is always in the 4 most-significant bits of status_.
191 static_assert(sizeof(status_) == sizeof(uint32_t), "Size of status_ not equal to uint32");
192 uint32_t new_status_value = static_cast<uint32_t>(new_status) << (32 - kClassStatusBitSize);
193 if (Runtime::Current()->IsActiveTransaction()) {
194 h_this->SetField32Volatile<true>(StatusOffset(), new_status_value);
195 } else {
196 h_this->SetField32Volatile<false>(StatusOffset(), new_status_value);
197 }
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700198 }
199
200 // Setting the object size alloc fast path needs to be after the status write so that if the
201 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
202 // load-acquire/fake dependency.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000203 if (new_status == ClassStatus::kInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700204 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
205 // Finalizable objects must always go slow path.
206 if (!h_this->IsFinalizable()) {
207 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700208 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100209 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700210
Andreas Gampe5b20b352018-10-11 19:03:20 -0700211 if (kIsDebugBuild && new_status >= ClassStatus::kInitialized) {
212 CHECK(h_this->WasVerificationAttempted()) << h_this->PrettyClassAndClassLoader();
213 }
214
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700215 if (!class_linker_initialized) {
216 // When the class linker is being initialized its single threaded and by definition there can be
217 // no waiters. During initialization classes may appear temporary but won't be retired as their
218 // size was statically computed.
219 } else {
220 // Classes that are being resolved or initialized need to notify waiters that the class status
221 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700222 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700223 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
224 // so that they can grab the new version of the class from the class linker's table.
Vladimir Marko2c64a832018-01-04 11:31:56 +0000225 CHECK_LT(new_status, ClassStatus::kResolved) << h_this->PrettyDescriptor();
226 if (new_status == ClassStatus::kRetired || new_status == ClassStatus::kErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700227 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700228 }
229 } else {
Vladimir Marko2c64a832018-01-04 11:31:56 +0000230 CHECK_NE(new_status, ClassStatus::kRetired);
231 if (old_status >= ClassStatus::kResolved || new_status >= ClassStatus::kResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700232 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700233 }
234 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700235 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236}
237
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700238void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700239 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240}
241
Ian Rogersef7d42f2014-01-06 12:55:46 -0800242void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700244 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
245 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700246 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700247 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700248 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249}
250
251// Return the class' name. The exact format is bizarre, but it's the specified behavior for
252// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
253// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
254// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700255String* Class::ComputeName(Handle<Class> h_this) {
256 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800257 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258 return name;
259 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700260 std::string temp;
261 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800262 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
264 // The descriptor indicates that this is the class for
265 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700266 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 switch (descriptor[0]) {
268 case 'Z': c_name = "boolean"; break;
269 case 'B': c_name = "byte"; break;
270 case 'C': c_name = "char"; break;
271 case 'S': c_name = "short"; break;
272 case 'I': c_name = "int"; break;
273 case 'J': c_name = "long"; break;
274 case 'F': c_name = "float"; break;
275 case 'D': c_name = "double"; break;
276 case 'V': c_name = "void"; break;
277 default:
278 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
279 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800280 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 } else {
282 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
283 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700284 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700286 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 return name;
288}
289
Ian Rogersef7d42f2014-01-06 12:55:46 -0800290void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700292 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 if ((flags & kDumpClassClassLoader) != 0) {
294 os << ' ' << GetClassLoader();
295 }
296 if ((flags & kDumpClassInitialized) != 0) {
297 os << ' ' << GetStatus();
298 }
299 os << "\n";
300 return;
301 }
302
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700304 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700305 Handle<Class> h_this(hs.NewHandle(this));
306 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700307 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700308
Ian Rogers1ff3c982014-08-12 02:30:58 -0700309 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700311 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800313 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 os << StringPrintf(" access=0x%04x.%04x\n",
315 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800316 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700317 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700318 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319 }
320 if (IsArrayClass()) {
321 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
322 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700323 const size_t num_direct_interfaces = NumDirectInterfaces();
324 if (num_direct_interfaces > 0) {
325 os << " interfaces (" << num_direct_interfaces << "):\n";
326 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000327 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700328 if (interface == nullptr) {
329 os << StringPrintf(" %2zd: nullptr!\n", i);
330 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700331 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
332 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700333 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 }
335 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700336 if (!IsLoaded()) {
337 os << " class not yet loaded";
338 } else {
339 // After this point, this may have moved due to GetDirectInterface.
340 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800341 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700342 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700343 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700346 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
347 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700348 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700350 }
351 if (h_this->NumStaticFields() > 0) {
352 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000353 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700354 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700355 os << StringPrintf(" %2zd: %s\n", i,
356 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700357 }
358 } else {
359 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700361 }
362 if (h_this->NumInstanceFields() > 0) {
363 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000364 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700365 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700366 os << StringPrintf(" %2zd: %s\n", i,
367 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700368 }
369 } else {
370 os << " <not yet available>";
371 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 }
373 }
374}
375
376void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 // Sanity check that the number of bits set in the reference offset bitmap
379 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700380 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700381 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 count += c->NumReferenceInstanceFieldsDuringLinking();
383 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700384 // +1 for the Class in Object.
385 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100387 // Not called within a transaction.
388 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700389 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390}
391
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
393 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700394 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
395 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 ++i;
397 }
398 if (descriptor1.find('/', i) != StringPiece::npos ||
399 descriptor2.find('/', i) != StringPiece::npos) {
400 return false;
401 } else {
402 return true;
403 }
404}
405
Mathieu Chartier3398c782016-09-30 10:27:43 -0700406bool Class::IsInSamePackage(ObjPtr<Class> that) {
407 ObjPtr<Class> klass1 = this;
408 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 if (klass1 == klass2) {
410 return true;
411 }
412 // Class loaders must match.
413 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
414 return false;
415 }
416 // Arrays are in the same package when their element classes are.
417 while (klass1->IsArrayClass()) {
418 klass1 = klass1->GetComponentType();
419 }
420 while (klass2->IsArrayClass()) {
421 klass2 = klass2->GetComponentType();
422 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700423 // trivial check again for array types
424 if (klass1 == klass2) {
425 return true;
426 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700428 std::string temp1, temp2;
429 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430}
431
Ian Rogersef7d42f2014-01-06 12:55:46 -0800432bool Class::IsThrowableClass() {
Vladimir Markoc13fbd82018-06-04 16:16:28 +0100433 return GetClassRoot<mirror::Throwable>()->IsAssignableFrom(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434}
435
Vladimir Markoba118822017-06-12 15:41:56 +0100436template <typename SignatureType>
437static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
438 const StringPiece& name,
439 const SignatureType& signature,
440 PointerSize pointer_size)
441 REQUIRES_SHARED(Locks::mutator_lock_) {
442 // If the current class is not an interface, skip the search of its declared methods;
443 // such lookup is used only to distinguish between IncompatibleClassChangeError and
444 // NoSuchMethodError and the caller has already tried to search methods in the class.
445 if (LIKELY(klass->IsInterface())) {
446 // Search declared methods, both direct and virtual.
447 // (This lookup is used also for invoke-static on interface classes.)
448 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
449 if (method.GetName() == name && method.GetSignature() == signature) {
450 return &method;
451 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800452 }
453 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454
Vladimir Markoba118822017-06-12 15:41:56 +0100455 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
456 // we should return it, otherwise an arbitrary one can be returned.
457 ObjPtr<IfTable> iftable = klass->GetIfTable();
458 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
459 ObjPtr<Class> iface = iftable->GetInterface(i);
460 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
461 if (method.GetName() == name && method.GetSignature() == signature) {
462 return &method;
463 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 }
465 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466
Vladimir Markoba118822017-06-12 15:41:56 +0100467 // Then search for public non-static methods in the java.lang.Object.
468 if (LIKELY(klass->IsInterface())) {
469 ObjPtr<Class> object_class = klass->GetSuperClass();
470 DCHECK(object_class->IsObjectClass());
471 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
472 if (method.IsPublic() && !method.IsStatic() &&
473 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800475 }
476 }
477 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700478 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800479}
480
Vladimir Markoba118822017-06-12 15:41:56 +0100481ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
482 const StringPiece& signature,
483 PointerSize pointer_size) {
484 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485}
486
Vladimir Markoba118822017-06-12 15:41:56 +0100487ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
488 const Signature& signature,
489 PointerSize pointer_size) {
490 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700491}
492
Vladimir Markoba118822017-06-12 15:41:56 +0100493ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
494 uint32_t dex_method_idx,
495 PointerSize pointer_size) {
496 // We always search by name and signature, ignoring the type index in the MethodId.
497 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800498 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +0100499 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
500 const Signature signature = dex_file.GetMethodSignature(method_id);
501 return FindInterfaceMethod(name, signature, pointer_size);
502}
503
Alex Lightafb66472017-08-01 09:54:49 -0700504static inline bool IsValidInheritanceCheck(ObjPtr<mirror::Class> klass,
505 ObjPtr<mirror::Class> declaring_class)
506 REQUIRES_SHARED(Locks::mutator_lock_) {
507 if (klass->IsArrayClass()) {
508 return declaring_class->IsObjectClass();
509 } else if (klass->IsInterface()) {
510 return declaring_class->IsObjectClass() || declaring_class == klass;
511 } else {
512 return klass->IsSubClass(declaring_class);
513 }
514}
515
Vladimir Markoba118822017-06-12 15:41:56 +0100516static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
517 ObjPtr<mirror::Class> declaring_class,
518 ArtMethod& method)
519 REQUIRES_SHARED(Locks::mutator_lock_) {
520 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
521 DCHECK_NE(klass, declaring_class);
Alex Lightafb66472017-08-01 09:54:49 -0700522 DCHECK(IsValidInheritanceCheck(klass, declaring_class));
Vladimir Markoba118822017-06-12 15:41:56 +0100523 uint32_t access_flags = method.GetAccessFlags();
524 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
525 return true;
526 }
527 if ((access_flags & kAccPrivate) != 0) {
528 return false;
529 }
530 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
531 if (!klass->IsInSamePackage(declaring_class)) {
532 return false;
533 }
534 }
535 return true;
536}
537
538template <typename SignatureType>
539static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
540 const StringPiece& name,
541 const SignatureType& signature,
542 PointerSize pointer_size)
543 REQUIRES_SHARED(Locks::mutator_lock_) {
544 // Search declared methods first.
545 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
546 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
547 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
548 return &method;
549 }
550 }
551
552 // Then search the superclass chain. If we find an inherited method, return it.
553 // If we find a method that's not inherited because of access restrictions,
554 // try to find a method inherited from an interface in copied methods.
555 ObjPtr<Class> klass = this_klass->GetSuperClass();
556 ArtMethod* uninherited_method = nullptr;
557 for (; klass != nullptr; klass = klass->GetSuperClass()) {
558 DCHECK(!klass->IsProxyClass());
559 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
560 if (method.GetName() == name && method.GetSignature() == signature) {
561 if (IsInheritedMethod(this_klass, klass, method)) {
562 return &method;
563 }
564 uninherited_method = &method;
565 break;
566 }
567 }
568 if (uninherited_method != nullptr) {
569 break;
570 }
571 }
572
573 // Then search copied methods.
574 // If we found a method that's not inherited, stop the search in its declaring class.
575 ObjPtr<Class> end_klass = klass;
576 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
577 klass = this_klass;
578 if (UNLIKELY(klass->IsProxyClass())) {
579 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
580 klass = klass->GetSuperClass();
581 }
582 for (; klass != end_klass; klass = klass->GetSuperClass()) {
583 DCHECK(!klass->IsProxyClass());
584 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
585 if (method.GetName() == name && method.GetSignature() == signature) {
586 return &method; // No further check needed, copied methods are inherited by definition.
587 }
588 }
589 }
590 return uninherited_method; // Return the `uninherited_method` if any.
591}
592
593
594ArtMethod* Class::FindClassMethod(const StringPiece& name,
595 const StringPiece& signature,
596 PointerSize pointer_size) {
597 return FindClassMethodWithSignature(this, name, signature, pointer_size);
598}
599
600ArtMethod* Class::FindClassMethod(const StringPiece& name,
601 const Signature& signature,
602 PointerSize pointer_size) {
603 return FindClassMethodWithSignature(this, name, signature, pointer_size);
604}
605
606ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
607 uint32_t dex_method_idx,
608 PointerSize pointer_size) {
609 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
610 DCHECK(!IsProxyClass());
611
612 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
613 ObjPtr<DexCache> this_dex_cache = GetDexCache();
614 if (this_dex_cache == dex_cache) {
615 // Lookup is always performed in the class referenced by the MethodId.
616 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
617 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
618 if (method.GetDexMethodIndex() == dex_method_idx) {
619 return &method;
620 }
621 }
622 }
623 // If not found, we need to search by name and signature.
624 const DexFile& dex_file = *dex_cache->GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800625 const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Vladimir Markoba118822017-06-12 15:41:56 +0100626 const Signature signature = dex_file.GetMethodSignature(method_id);
627 StringPiece name; // Delay strlen() until actually needed.
628 // If we do not have a dex_cache match, try to find the declared method in this class now.
629 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
630 DCHECK(name.empty());
David Srbecky39d8c872018-11-01 16:44:20 +0000631 // Avoid string comparisons by comparing the respective unicode lengths first.
632 uint32_t length, other_length; // UTF16 length.
633 name = dex_file.GetMethodName(method_id, &length);
Vladimir Markoba118822017-06-12 15:41:56 +0100634 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
David Srbecky39d8c872018-11-01 16:44:20 +0000635 DCHECK_NE(method.GetDexMethodIndex(), dex::kDexNoIndex);
636 const char* other_name = method.GetDexFile()->GetMethodName(
637 method.GetDexMethodIndex(), &other_length);
638 if (length == other_length && name == other_name && signature == method.GetSignature()) {
Vladimir Markoba118822017-06-12 15:41:56 +0100639 return &method;
640 }
641 }
642 }
643
644 // Then search the superclass chain. If we find an inherited method, return it.
645 // If we find a method that's not inherited because of access restrictions,
646 // try to find a method inherited from an interface in copied methods.
647 ArtMethod* uninherited_method = nullptr;
648 ObjPtr<Class> klass = GetSuperClass();
649 for (; klass != nullptr; klass = klass->GetSuperClass()) {
650 ArtMethod* candidate_method = nullptr;
651 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
652 if (klass->GetDexCache() == dex_cache) {
653 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
654 // the type index differs, so compare the name index and proto index.
655 for (ArtMethod& method : declared_methods) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800656 const dex::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
Vladimir Markoba118822017-06-12 15:41:56 +0100657 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
658 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
659 candidate_method = &method;
660 break;
661 }
662 }
663 } else {
664 if (!declared_methods.empty() && name.empty()) {
665 name = dex_file.StringDataByIdx(method_id.name_idx_);
666 }
667 for (ArtMethod& method : declared_methods) {
668 if (method.GetName() == name && method.GetSignature() == signature) {
669 candidate_method = &method;
670 break;
671 }
672 }
673 }
674 if (candidate_method != nullptr) {
675 if (IsInheritedMethod(this, klass, *candidate_method)) {
676 return candidate_method;
677 } else {
678 uninherited_method = candidate_method;
679 break;
680 }
681 }
682 }
683
684 // Then search copied methods.
685 // If we found a method that's not inherited, stop the search in its declaring class.
686 ObjPtr<Class> end_klass = klass;
687 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
688 // After we have searched the declared methods of the super-class chain,
689 // search copied methods which can contain methods from interfaces.
690 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
691 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
692 if (!copied_methods.empty() && name.empty()) {
693 name = dex_file.StringDataByIdx(method_id.name_idx_);
694 }
695 for (ArtMethod& method : copied_methods) {
696 if (method.GetName() == name && method.GetSignature() == signature) {
697 return &method; // No further check needed, copied methods are inherited by definition.
698 }
699 }
700 }
701 return uninherited_method; // Return the `uninherited_method` if any.
702}
703
704ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
705 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
706 DCHECK(!IsProxyClass());
707 StringPiece name("<init>");
708 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
709 if (method.GetName() == name && method.GetSignature() == signature) {
710 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800711 }
712 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700713 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800714}
715
Andreas Gampe542451c2016-07-26 09:02:02 -0700716ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
717 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000718 for (auto& method : GetDirectMethods(pointer_size)) {
719 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
720 if (name == np_method->GetName()) {
721 return &method;
722 }
723 }
724 return nullptr;
725}
726
Andreas Gampe542451c2016-07-26 09:02:02 -0700727ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
728 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000729 for (auto& method : GetVirtualMethods(pointer_size)) {
730 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
731 if (name == np_method->GetName()) {
732 return &method;
733 }
734 }
735 return nullptr;
736}
737
Andreas Gampe542451c2016-07-26 09:02:02 -0700738ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700739 DCHECK(method->GetDeclaringClass()->IsInterface());
740 DCHECK(IsInterface()) << "Should only be called on a interface class";
741 // Check if we have one defined on this interface first. This includes searching copied ones to
742 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
743 // don't do any indirect method checks here.
744 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
745 if (method->HasSameNameAndSignature(&iface_method)) {
746 return &iface_method;
747 }
748 }
749
750 std::vector<ArtMethod*> abstract_methods;
751 // Search through the IFTable for a working version. We don't need to check for conflicts
752 // because if there was one it would appear in this classes virtual_methods_ above.
753
754 Thread* self = Thread::Current();
755 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700756 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
757 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700758 size_t iftable_count = GetIfTableCount();
759 // Find the method. We don't need to check for conflicts because they would have been in the
760 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
761 // subtypiest interfaces get visited first.
762 for (size_t k = iftable_count; k != 0;) {
763 k--;
764 DCHECK_LT(k, iftable->Count());
765 iface.Assign(iftable->GetInterface(k));
766 // Iterate through every declared method on this interface. Each direct method's name/signature
767 // is unique so the order of the inner loop doesn't matter.
768 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
769 ArtMethod* current_method = &method_iter;
770 if (current_method->HasSameNameAndSignature(method)) {
771 if (current_method->IsDefault()) {
772 // Handle JLS soft errors, a default method from another superinterface tree can
773 // "override" an abstract method(s) from another superinterface tree(s). To do this,
774 // ignore any [default] method which are dominated by the abstract methods we've seen so
775 // far. Check if overridden by any in abstract_methods. We do not need to check for
776 // default_conflicts because we would hit those before we get to this loop.
777 bool overridden = false;
778 for (ArtMethod* possible_override : abstract_methods) {
779 DCHECK(possible_override->HasSameNameAndSignature(current_method));
780 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
781 overridden = true;
782 break;
783 }
784 }
785 if (!overridden) {
786 return current_method;
787 }
788 } else {
789 // Is not default.
790 // This might override another default method. Just stash it for now.
791 abstract_methods.push_back(current_method);
792 }
793 }
794 }
795 }
796 // If we reach here we either never found any declaration of the method (in which case
797 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
798 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
799 // of these arbitrarily.
800 return abstract_methods.empty() ? nullptr : abstract_methods[0];
801}
802
Andreas Gampe542451c2016-07-26 09:02:02 -0700803ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700804 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
805 if (method.IsClassInitializer()) {
806 DCHECK_STREQ(method.GetName(), "<clinit>");
807 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
808 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700809 }
810 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700811 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700812}
813
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700814// Custom binary search to avoid double comparisons from std::binary_search.
815static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
816 const StringPiece& name,
817 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700818 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700819 if (fields == nullptr) {
820 return nullptr;
821 }
822 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100823 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700824 ArtField* ret = nullptr;
825 while (low < high) {
826 size_t mid = (low + high) / 2;
827 ArtField& field = fields->At(mid);
828 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
829 // verifier. There can be multiple fields with the same in the same class name due to proguard.
830 int result = StringPiece(field.GetName()).Compare(name);
831 if (result == 0) {
832 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
833 }
834 if (result < 0) {
835 low = mid + 1;
836 } else if (result > 0) {
837 high = mid;
838 } else {
839 ret = &field;
840 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800841 }
842 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700843 if (kIsDebugBuild) {
844 ArtField* found = nullptr;
845 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
846 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
847 found = &field;
848 break;
849 }
850 }
David Sehr709b0702016-10-13 09:12:37 -0700851 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700852 }
853 return ret;
854}
855
856ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
857 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
858 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800859}
860
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700861ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800862 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700863 for (ArtField& field : GetIFields()) {
864 if (field.GetDexFieldIndex() == dex_field_idx) {
865 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800866 }
867 }
868 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700869 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800870}
871
Brian Carlstromea46f952013-07-30 01:26:50 -0700872ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800873 // Is the field in this class, or any of its superclasses?
874 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700875 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700876 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700877 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800878 return f;
879 }
880 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700881 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800882}
883
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700884ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
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(dex_cache, dex_field_idx);
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
Brian Carlstromea46f952013-07-30 01:26:50 -0700896ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700897 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700898 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800899}
900
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700901ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800902 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700903 for (ArtField& field : GetSFields()) {
904 if (field.GetDexFieldIndex() == dex_field_idx) {
905 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800906 }
907 }
908 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700909 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800910}
911
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700912ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000913 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700914 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700915 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800916 // Is the field in this class (or its interfaces), or any of its
917 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000918 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800919 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700920 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700921 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800922 return f;
923 }
924 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000925 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
926 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
927 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700928 f = FindStaticField(self, interface, name, type);
929 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800930 return f;
931 }
932 }
933 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700934 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800935}
936
Vladimir Markobb268b12016-06-30 15:52:56 +0100937ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700938 ObjPtr<Class> klass,
939 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700940 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700941 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800942 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700943 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700944 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800945 return f;
946 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100947 // Though GetDirectInterface() should not cause thread suspension when called
948 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700949 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800950 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000951 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
952 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
953 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700954 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
955 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800956 return f;
957 }
958 }
959 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700960 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800961}
962
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700963ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000964 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700965 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700966 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800967 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000968 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800969 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700970 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700971 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800972 return f;
973 }
974 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700975 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800976 return f;
977 }
978 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000979 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
980 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
981 DCHECK(interface != nullptr);
982 f = FindStaticField(self, interface, 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 }
987 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700988 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800989}
990
Andreas Gampe542451c2016-07-26 09:02:02 -0700991void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700992 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800993 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700994 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800995 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700996 }
997 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200998}
999
Ian Rogers1ff3c982014-08-12 02:30:58 -07001000const char* Class::GetDescriptor(std::string* storage) {
1001 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001002 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001003 } else if (IsArrayClass()) {
1004 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001005 } else if (IsProxyClass()) {
1006 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -07001007 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001008 } else {
1009 const DexFile& dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001010 const dex::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001011 return dex_file.GetTypeDescriptor(type_id);
1012 }
1013}
1014
Ian Rogers1ff3c982014-08-12 02:30:58 -07001015const char* Class::GetArrayDescriptor(std::string* storage) {
1016 std::string temp;
1017 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
1018 *storage = "[";
1019 *storage += elem_desc;
1020 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001021}
1022
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001023const dex::ClassDef* Class::GetClassDef() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001024 uint16_t class_def_idx = GetDexClassDefIndex();
1025 if (class_def_idx == DexFile::kDexNoIndex16) {
1026 return nullptr;
1027 }
1028 return &GetDexFile().GetClassDef(class_def_idx);
1029}
1030
Andreas Gampea5b09a62016-11-17 15:21:22 -08001031dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001032 DCHECK(!IsPrimitive());
1033 DCHECK(!IsArrayClass());
1034 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
1035}
1036
Vladimir Marko19a4d372016-12-08 14:41:46 +00001037ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
1038 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001039 DCHECK(!klass->IsPrimitive());
1040 if (klass->IsArrayClass()) {
1041 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +00001042 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
1043 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -07001044 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +00001045 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001046 } else {
1047 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +00001048 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -07001049 }
Vladimir Marko19a4d372016-12-08 14:41:46 +00001050 DCHECK(interface != nullptr);
1051 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001052 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001053 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001054 DCHECK(interfaces != nullptr);
1055 return interfaces->Get(idx);
1056 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001057 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001058 ObjPtr<Class> interface = Runtime::Current()->GetClassLinker()->LookupResolvedType(
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001059 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001060 return interface;
1061 }
1062}
1063
Vladimir Marko19a4d372016-12-08 14:41:46 +00001064ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1065 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1066 if (interface == nullptr) {
1067 DCHECK(!klass->IsArrayClass());
1068 DCHECK(!klass->IsProxyClass());
1069 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko666ee3d2017-12-11 18:37:36 +00001070 interface = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, klass.Get());
Vladimir Marko19a4d372016-12-08 14:41:46 +00001071 CHECK(interface != nullptr || self->IsExceptionPending());
1072 }
1073 return interface;
1074}
1075
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001076ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001077 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001078 DCHECK(!klass->IsInterface());
1079 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001080 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001081 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001082 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001083 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001084 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001085 }
Calin Juravle52503d82015-11-11 16:58:31 +00001086 return common_super_class;
1087}
1088
Mathieu Chartierf8322842014-05-16 10:59:25 -07001089const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001090 const DexFile& dex_file = GetDexFile();
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001091 const dex::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001092 if (dex_class_def == nullptr) {
1093 // Generated classes have no class def.
1094 return nullptr;
1095 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001096 return dex_file.GetSourceFile(*dex_class_def);
1097}
1098
1099std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001100 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001101 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001102 return dex_cache->GetLocation()->ToModifiedUtf8();
1103 }
1104 // Arrays and proxies are generated and have no corresponding dex file location.
1105 return "generated class";
1106}
1107
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001108const dex::TypeList* Class::GetInterfaceTypeList() {
1109 const dex::ClassDef* class_def = GetClassDef();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001110 if (class_def == nullptr) {
1111 return nullptr;
1112 }
1113 return GetDexFile().GetInterfacesList(*class_def);
1114}
1115
Andreas Gampe542451c2016-07-26 09:02:02 -07001116void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001117 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001118 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001119 const size_t table_length = table->GetLength();
1120 SetEmbeddedVTableLength(table_length);
1121 for (size_t i = 0; i < table_length; i++) {
1122 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001123 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001124 // Keep java.lang.Object class's vtable around for since it's easier
1125 // to be reused by array classes during their linking.
1126 if (!IsObjectClass()) {
1127 SetVTable(nullptr);
1128 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001129}
1130
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001131class ReadBarrierOnNativeRootsVisitor {
1132 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001133 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001134 MemberOffset offset ATTRIBUTE_UNUSED,
1135 bool is_static ATTRIBUTE_UNUSED) const {}
1136
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001137 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001138 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001139 if (!root->IsNull()) {
1140 VisitRoot(root);
1141 }
1142 }
1143
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001144 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001145 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001146 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1147 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001148 if (old_ref != new_ref) {
1149 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1150 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001151 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Orion Hodson4557b382018-01-03 11:47:54 +00001152 atomic_root->CompareAndSetStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001153 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1154 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001155 }
1156 }
1157};
1158
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001159// The pre-fence visitor for Class::CopyOf().
1160class CopyClassVisitor {
1161 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001162 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001163 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001164 size_t new_length,
1165 size_t copy_bytes,
1166 ImTable* imt,
1167 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001168 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001169 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001170 }
1171
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001172 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001173 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001174 StackHandleScope<1> hs(self_);
1175 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001176 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001177 Class::SetStatus(h_new_class_obj, ClassStatus::kResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001178 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1179 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001180 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001181 // Visit all of the references to make sure there is no from space references in the native
1182 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001183 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001184 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001185 }
1186
1187 private:
1188 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001189 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001190 const size_t new_length_;
1191 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001192 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001193 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001194 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1195};
1196
Andreas Gampe542451c2016-07-26 09:02:02 -07001197Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001198 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1199 // We may get copied by a compacting GC.
1200 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001201 Handle<Class> h_this(hs.NewHandle(this));
Vladimir Marko317892b2018-05-31 11:11:32 +01001202 Runtime* runtime = Runtime::Current();
1203 gc::Heap* heap = runtime->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001204 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1205 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001206 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Vladimir Marko317892b2018-05-31 11:11:32 +01001207 ObjPtr<mirror::Class> java_lang_Class = GetClassRoot<mirror::Class>(runtime->GetClassLinker());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001208 ObjPtr<Object> new_class = kMovingClasses ?
Vladimir Marko317892b2018-05-31 11:11:32 +01001209 heap->AllocObject<true>(self, java_lang_Class, new_length, visitor) :
1210 heap->AllocNonMovableObject<true>(self, java_lang_Class, new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001211 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001212 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001213 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001214 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001215 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001216}
1217
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001218bool Class::ProxyDescriptorEquals(const char* match) {
1219 DCHECK(IsProxyClass());
1220 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001221}
1222
Mathieu Chartiere401d142015-04-22 13:56:20 -07001223// TODO: Move this to java_lang_Class.cc?
1224ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001225 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001226 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001227 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001228 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001229 continue;
1230 }
1231 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001232 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001233 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001234 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001235 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001236 return nullptr;
1237 }
1238 }
1239 return nullptr;
1240}
1241
Mathieu Chartiere401d142015-04-22 13:56:20 -07001242uint32_t Class::Depth() {
1243 uint32_t depth = 0;
Roland Levillaind32ead22018-05-30 17:38:21 +01001244 for (ObjPtr<Class> cls = this; cls->GetSuperClass() != nullptr; cls = cls->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001245 depth++;
1246 }
1247 return depth;
1248}
1249
Andreas Gampea5b09a62016-11-17 15:21:22 -08001250dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001251 std::string temp;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -08001252 const dex::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001253 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001254}
1255
David Brazdil4bcd6572019-02-02 20:08:44 +00001256ALWAYS_INLINE
1257static bool IsMethodPreferredOver(ArtMethod* orig_method,
1258 bool orig_method_hidden,
1259 ArtMethod* new_method,
1260 bool new_method_hidden) {
1261 DCHECK(new_method != nullptr);
1262
1263 // Is this the first result?
1264 if (orig_method == nullptr) {
1265 return true;
1266 }
1267
1268 // Original method is hidden, the new one is not?
1269 if (orig_method_hidden && !new_method_hidden) {
1270 return true;
1271 }
1272
1273 // We iterate over virtual methods first and then over direct ones,
1274 // so we can never be in situation where `orig_method` is direct and
1275 // `new_method` is virtual.
1276 DCHECK(!orig_method->IsDirect() || new_method->IsDirect());
1277
1278 // Original method is synthetic, the new one is not?
1279 if (orig_method->IsSynthetic() && !new_method->IsSynthetic()) {
1280 return true;
1281 }
1282
1283 return false;
1284}
1285
Andreas Gampe542451c2016-07-26 09:02:02 -07001286template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001287ObjPtr<Method> Class::GetDeclaredMethodInternal(
1288 Thread* self,
1289 ObjPtr<Class> klass,
1290 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001291 ObjPtr<ObjectArray<Class>> args,
1292 const std::function<hiddenapi::AccessContext()>& fn_get_access_context) {
1293 // Covariant return types (or smali) permit the class to define
1294 // multiple methods with the same name and parameter types.
1295 // Prefer (in decreasing order of importance):
1296 // 1) non-hidden method over hidden
1297 // 2) virtual methods over direct
1298 // 3) non-synthetic methods over synthetic
1299 // We never return miranda methods that were synthesized by the runtime.
Andreas Gampebc4d2182016-02-22 10:03:12 -08001300 StackHandleScope<3> hs(self);
1301 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001302 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001303 ThrowNullPointerException("name == null");
1304 return nullptr;
1305 }
1306 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001307 Handle<Class> h_klass = hs.NewHandle(klass);
David Brazdil4bcd6572019-02-02 20:08:44 +00001308 constexpr hiddenapi::AccessMethod access_method = hiddenapi::AccessMethod::kNone;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001309 ArtMethod* result = nullptr;
David Brazdil4bcd6572019-02-02 20:08:44 +00001310 bool result_hidden = false;
Andreas Gampee01e3642016-07-25 13:06:04 -07001311 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
David Brazdil4bcd6572019-02-02 20:08:44 +00001312 if (m.IsMiranda()) {
1313 continue;
1314 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001315 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001316 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001317 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001318 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1319 if (UNLIKELY(self->IsExceptionPending())) {
1320 return nullptr;
1321 }
1322 continue;
1323 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001324 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1325 if (!m_hidden && !m.IsSynthetic()) {
1326 // Non-hidden, virtual, non-synthetic. Best possible result, exit early.
1327 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
1328 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1329 // Remember as potential result.
1330 result = &m;
1331 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001332 }
1333 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001334
1335 if ((result != nullptr) && !result_hidden) {
1336 // We have not found a non-hidden, virtual, non-synthetic method, but
1337 // if we have found a non-hidden, virtual, synthetic method, we cannot
1338 // do better than that later.
1339 DCHECK(!result->IsDirect());
1340 DCHECK(result->IsSynthetic());
1341 } else {
Andreas Gampee01e3642016-07-25 13:06:04 -07001342 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001343 auto modifiers = m.GetAccessFlags();
1344 if ((modifiers & kAccConstructor) != 0) {
1345 continue;
1346 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001347 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001348 // May cause thread suspension.
Vladimir Marko18090d12018-06-01 16:53:12 +01001349 ObjPtr<String> np_name = np_method->ResolveNameString();
Andreas Gampebc4d2182016-02-22 10:03:12 -08001350 if (np_name == nullptr) {
1351 self->AssertPendingException();
1352 return nullptr;
1353 }
1354 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1355 if (UNLIKELY(self->IsExceptionPending())) {
1356 return nullptr;
1357 }
1358 continue;
1359 }
Vladimir Markob0a6aee2017-10-27 10:34:04 +01001360 DCHECK(!m.IsMiranda()); // Direct methods cannot be miranda methods.
David Brazdil4bcd6572019-02-02 20:08:44 +00001361 bool m_hidden = hiddenapi::ShouldDenyAccessToMember(&m, fn_get_access_context, access_method);
1362 if (!m_hidden && !m.IsSynthetic()) {
1363 // Non-hidden, direct, non-synthetic. Any virtual result could only have been
1364 // hidden, therefore this is the best possible match. Exit now.
1365 DCHECK((result == nullptr) || result_hidden);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001366 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
David Brazdil4bcd6572019-02-02 20:08:44 +00001367 } else if (IsMethodPreferredOver(result, result_hidden, &m, m_hidden)) {
1368 // Remember as potential result.
1369 result = &m;
1370 result_hidden = m_hidden;
Andreas Gampebc4d2182016-02-22 10:03:12 -08001371 }
Andreas Gampebc4d2182016-02-22 10:03:12 -08001372 }
1373 }
David Brazdil4bcd6572019-02-02 20:08:44 +00001374
Andreas Gampebc4d2182016-02-22 10:03:12 -08001375 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001376 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001377 : nullptr;
1378}
1379
1380template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001381ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001382 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001383 ObjPtr<Class> klass,
1384 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001385 ObjPtr<ObjectArray<Class>> args,
1386 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001387template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001388ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001389 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001390 ObjPtr<Class> klass,
1391 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001392 ObjPtr<ObjectArray<Class>> args,
1393 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -07001394template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001395ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001396 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001397 ObjPtr<Class> klass,
1398 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001399 ObjPtr<ObjectArray<Class>> args,
1400 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -07001401template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001402ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001403 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001404 ObjPtr<Class> klass,
1405 ObjPtr<String> name,
David Brazdil4bcd6572019-02-02 20:08:44 +00001406 ObjPtr<ObjectArray<Class>> args,
1407 const std::function<hiddenapi::AccessContext()>& fn_get_access_context);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001408
Andreas Gampe542451c2016-07-26 09:02:02 -07001409template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001410ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001411 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001412 ObjPtr<Class> klass,
1413 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001414 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001415 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001416 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001417 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001418 : nullptr;
1419}
1420
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001421// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001422
Andreas Gampe542451c2016-07-26 09:02:02 -07001423template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001424ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001425 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001426 ObjPtr<Class> klass,
1427 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001428template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001429ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001430 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001431 ObjPtr<Class> klass,
1432 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001433template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001434ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001435 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001436 ObjPtr<Class> klass,
1437 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001438template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001439ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001440 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001441 ObjPtr<Class> klass,
1442 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001443
Andreas Gampe715fdc22016-04-18 17:07:30 -07001444int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1445 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1446 return default_value;
1447 }
1448 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001449 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001450 return default_value;
1451 }
1452 return flags;
1453}
1454
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001455void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1456 if (Runtime::Current()->IsActiveTransaction()) {
1457 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1458 } else {
1459 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1460 }
1461}
1462
David Sehr709b0702016-10-13 09:12:37 -07001463std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1464 if (klass == nullptr) {
1465 return "null";
1466 }
1467 return klass->PrettyDescriptor();
1468}
1469
1470std::string Class::PrettyDescriptor() {
1471 std::string temp;
1472 return art::PrettyDescriptor(GetDescriptor(&temp));
1473}
1474
1475std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1476 if (c == nullptr) {
1477 return "null";
1478 }
1479 return c->PrettyClass();
1480}
1481
1482std::string Class::PrettyClass() {
1483 std::string result;
1484 result += "java.lang.Class<";
1485 result += PrettyDescriptor();
1486 result += ">";
1487 return result;
1488}
1489
1490std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1491 if (c == nullptr) {
1492 return "null";
1493 }
1494 return c->PrettyClassAndClassLoader();
1495}
1496
1497std::string Class::PrettyClassAndClassLoader() {
1498 std::string result;
1499 result += "java.lang.Class<";
1500 result += PrettyDescriptor();
1501 result += ",";
1502 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1503 // TODO: add an identifying hash value for the loader
1504 result += ">";
1505 return result;
1506}
1507
Andreas Gampe90b936d2017-01-31 08:58:55 -08001508template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1509 // Check class is loaded/retired or this is java.lang.String that has a
1510 // circularity issue during loading the names of its members
1511 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1512 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
Vladimir Markoacb906d2018-05-30 10:23:49 +01001513 this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001514 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1515 << " IsRetired=" << IsRetired<kVerifyFlags>()
1516 << " IsErroneous=" <<
1517 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
Vladimir Markoacb906d2018-05-30 10:23:49 +01001518 << " IsString=" << (this == GetClassRoot<String>())
Andreas Gampe90b936d2017-01-31 08:58:55 -08001519 << " status= " << GetStatus<kVerifyFlags>()
1520 << " descriptor=" << PrettyDescriptor();
1521}
1522// Instantiate the common cases.
1523template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1524template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1525template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1526template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1527template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1528
Andreas Gampe62f6e902018-10-11 18:58:50 -07001529void Class::SetAccessFlagsDCheck(uint32_t new_access_flags) {
1530 uint32_t old_access_flags = GetField32<kVerifyNone>(AccessFlagsOffset());
1531 // kAccVerificationAttempted is retained.
1532 CHECK((old_access_flags & kAccVerificationAttempted) == 0 ||
1533 (new_access_flags & kAccVerificationAttempted) != 0);
1534}
1535
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001536} // namespace mirror
1537} // namespace art