blob: 03d648764fc4b6aa1bd9eea57bec67293b92d999 [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
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_field-inl.h"
20#include "art_method-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070021#include "class_ext.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010022#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "class_loader.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070024#include "class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070027#include "dex_file_annotations.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "handle_scope-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070030#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070031#include "object_array-inl.h"
32#include "object-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070033#include "object_lock.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070034#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "thread.h"
36#include "throwable.h"
37#include "utils.h"
38#include "well_known_classes.h"
39
40namespace art {
41namespace mirror {
42
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070043GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080044
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070045void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070046 CHECK(java_lang_Class_.IsNull())
47 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070048 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070049 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070050 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070051 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
54void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 CHECK(!java_lang_Class_.IsNull());
56 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057}
58
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070059void Class::VisitRoots(RootVisitor* visitor) {
60 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080061}
62
Alex Lightd6251582016-10-31 11:12:30 -070063ClassExt* Class::GetExtData() {
64 return GetFieldObject<ClassExt>(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
65}
66
67void Class::SetExtData(ObjPtr<ClassExt> ext) {
68 CHECK(ext != nullptr) << PrettyClass();
69 // TODO It might be wise to just create an internal (global?) mutex that we synchronize on instead
70 // to prevent any possibility of deadlocks with java code. Alternatively we might want to come up
71 // with some other abstraction.
72 DCHECK_EQ(GetLockOwnerThreadId(), Thread::Current()->GetThreadId())
73 << "The " << PrettyClass() << " object should be locked when writing to the extData field.";
74 DCHECK(GetExtData() == nullptr)
75 << "The extData for " << PrettyClass() << " has already been set!";
Andreas Gampe99babb62015-11-02 16:20:00 -080076 if (Runtime::Current()->IsActiveTransaction()) {
Alex Lightd6251582016-10-31 11:12:30 -070077 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_), ext);
Andreas Gampe99babb62015-11-02 16:20:00 -080078 } else {
Alex Lightd6251582016-10-31 11:12:30 -070079 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_), ext);
Andreas Gampe99babb62015-11-02 16:20:00 -080080 }
81}
82
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070083void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
84 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070085 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
86 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070087 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070088 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
89 new_status != kStatusRetired)) {
David Sehr709b0702016-10-13 09:12:37 -070090 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070091 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070092 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070093 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
94 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070095 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070096 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -070097 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070098 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 }
Ian Rogers98379392014-02-24 16:53:16 -0800100 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700101 CHECK_NE(h_this->GetStatus(), kStatusError)
102 << "Attempt to set as erroneous an already erroneous class "
David Sehr709b0702016-10-13 09:12:37 -0700103 << h_this->PrettyClass();
Andreas Gampe31decb12015-08-24 21:09:05 -0700104 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700105 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700106 if (self->IsExceptionPending()) {
107 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
108 }
109 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110
Alex Lightd6251582016-10-31 11:12:30 -0700111 {
112 // Ensure we lock around 'this' when we set the ClassExt.
113 ObjectLock<mirror::Class> lock(self, h_this);
114 StackHandleScope<2> hs(self);
115 // Remember the current exception.
116 Handle<Throwable> exception(hs.NewHandle(self->GetException()));
117 CHECK(exception.Get() != nullptr);
118 MutableHandle<ClassExt> ext(hs.NewHandle(h_this->GetExtData()));
119 if (ext.Get() == nullptr) {
120 // Cannot have exception while allocating.
121 self->ClearException();
122 ext.Assign(ClassExt::Alloc(self));
123 DCHECK(ext.Get() == nullptr || ext->GetVerifyError() == nullptr);
124 if (ext.Get() != nullptr) {
125 self->AssertNoPendingException();
126 h_this->SetExtData(ext.Get());
127 self->SetException(exception.Get());
128 } else {
129 // TODO Should we restore the old exception anyway?
130 self->AssertPendingOOMException();
131 }
132 }
133 if (ext.Get() != nullptr) {
134 ext->SetVerifyError(self->GetException());
135 }
136 }
137 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800139 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100140 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700141 h_this->SetField32Volatile<true>(StatusOffset(), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142 } else {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700143 h_this->SetField32Volatile<false>(StatusOffset(), new_status);
144 }
145
146 // Setting the object size alloc fast path needs to be after the status write so that if the
147 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
148 // load-acquire/fake dependency.
149 if (new_status == kStatusInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700150 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
151 // Finalizable objects must always go slow path.
152 if (!h_this->IsFinalizable()) {
153 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700154 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100155 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700156
157 if (!class_linker_initialized) {
158 // When the class linker is being initialized its single threaded and by definition there can be
159 // no waiters. During initialization classes may appear temporary but won't be retired as their
160 // size was statically computed.
161 } else {
162 // Classes that are being resolved or initialized need to notify waiters that the class status
163 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700164 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700165 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
166 // so that they can grab the new version of the class from the class linker's table.
David Sehr709b0702016-10-13 09:12:37 -0700167 CHECK_LT(new_status, kStatusResolved) << h_this->PrettyDescriptor();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700168 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700169 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700170 }
171 } else {
172 CHECK_NE(new_status, kStatusRetired);
173 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700174 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700175 }
176 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700177 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178}
179
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700180void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700181 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800182 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183}
184
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700186 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700187 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
188 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700189 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700190 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100191 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700192 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193}
194
195// Return the class' name. The exact format is bizarre, but it's the specified behavior for
196// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
197// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
198// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700199String* Class::ComputeName(Handle<Class> h_this) {
200 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800201 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 return name;
203 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700204 std::string temp;
205 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800206 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
208 // The descriptor indicates that this is the class for
209 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700210 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 switch (descriptor[0]) {
212 case 'Z': c_name = "boolean"; break;
213 case 'B': c_name = "byte"; break;
214 case 'C': c_name = "char"; break;
215 case 'S': c_name = "short"; break;
216 case 'I': c_name = "int"; break;
217 case 'J': c_name = "long"; break;
218 case 'F': c_name = "float"; break;
219 case 'D': c_name = "double"; break;
220 case 'V': c_name = "void"; break;
221 default:
222 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
223 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800224 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 } else {
226 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
227 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700228 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700230 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 return name;
232}
233
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800235 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700236 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 if ((flags & kDumpClassClassLoader) != 0) {
238 os << ' ' << GetClassLoader();
239 }
240 if ((flags & kDumpClassInitialized) != 0) {
241 os << ' ' << GetStatus();
242 }
243 os << "\n";
244 return;
245 }
246
Mathieu Chartiere401d142015-04-22 13:56:20 -0700247 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700248 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700249 Handle<Class> h_this(hs.NewHandle(this));
250 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700252
Ian Rogers1ff3c982014-08-12 02:30:58 -0700253 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700255 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700257 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258 os << StringPrintf(" access=0x%04x.%04x\n",
259 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700260 if (h_super.Get() != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700261 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700262 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 }
264 if (IsArrayClass()) {
265 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
266 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700267 const size_t num_direct_interfaces = NumDirectInterfaces();
268 if (num_direct_interfaces > 0) {
269 os << " interfaces (" << num_direct_interfaces << "):\n";
270 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700271 ObjPtr<Class> interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700272 if (interface == nullptr) {
273 os << StringPrintf(" %2zd: nullptr!\n", i);
274 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700275 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
276 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700277 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 }
279 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700280 if (!IsLoaded()) {
281 os << " class not yet loaded";
282 } else {
283 // After this point, this may have moved due to GetDirectInterface.
284 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
285 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
286 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700287 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700290 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
291 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700292 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700294 }
295 if (h_this->NumStaticFields() > 0) {
296 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
297 if (h_this->IsResolved() || h_this->IsErroneous()) {
298 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700299 os << StringPrintf(" %2zd: %s\n", i,
300 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700301 }
302 } else {
303 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700305 }
306 if (h_this->NumInstanceFields() > 0) {
307 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
308 if (h_this->IsResolved() || h_this->IsErroneous()) {
309 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700310 os << StringPrintf(" %2zd: %s\n", i,
311 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700312 }
313 } else {
314 os << " <not yet available>";
315 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 }
317 }
318}
319
320void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 // Sanity check that the number of bits set in the reference offset bitmap
323 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700324 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700325 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 count += c->NumReferenceInstanceFieldsDuringLinking();
327 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700328 // +1 for the Class in Object.
329 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100331 // Not called within a transaction.
332 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700333 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334}
335
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
337 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700338 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
339 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 ++i;
341 }
342 if (descriptor1.find('/', i) != StringPiece::npos ||
343 descriptor2.find('/', i) != StringPiece::npos) {
344 return false;
345 } else {
346 return true;
347 }
348}
349
Mathieu Chartier3398c782016-09-30 10:27:43 -0700350bool Class::IsInSamePackage(ObjPtr<Class> that) {
351 ObjPtr<Class> klass1 = this;
352 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 if (klass1 == klass2) {
354 return true;
355 }
356 // Class loaders must match.
357 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
358 return false;
359 }
360 // Arrays are in the same package when their element classes are.
361 while (klass1->IsArrayClass()) {
362 klass1 = klass1->GetComponentType();
363 }
364 while (klass2->IsArrayClass()) {
365 klass2 = klass2->GetComponentType();
366 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700367 // trivial check again for array types
368 if (klass1 == klass2) {
369 return true;
370 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700372 std::string temp1, temp2;
373 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374}
375
Ian Rogersef7d42f2014-01-06 12:55:46 -0800376bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
378}
379
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700380void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100381 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700382 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100383 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700384 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100385 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386}
387
Andreas Gampe542451c2016-07-26 09:02:02 -0700388ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
389 const StringPiece& signature,
390 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700393 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 return method;
395 }
396
397 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700398 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700399 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700401 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 return method;
403 }
404 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700405 return nullptr;
406}
407
Andreas Gampe542451c2016-07-26 09:02:02 -0700408ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
409 const Signature& signature,
410 PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700411 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700413 if (method != nullptr) {
414 return method;
415 }
416
417 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700418 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700419 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700420 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700421 if (method != nullptr) {
422 return method;
423 }
424 }
425 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426}
427
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700428ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700429 uint32_t dex_method_idx,
430 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700433 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 return method;
435 }
436
437 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700438 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700439 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
441 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 return method;
444 }
445 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447}
448
Andreas Gampe542451c2016-07-26 09:02:02 -0700449ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
450 const StringPiece& signature,
451 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 for (auto& method : GetDirectMethods(pointer_size)) {
453 if (name == method.GetName() && method.GetSignature() == signature) {
454 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700455 }
456 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700458}
459
Andreas Gampe542451c2016-07-26 09:02:02 -0700460ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
461 const Signature& signature,
462 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 for (auto& method : GetDirectMethods(pointer_size)) {
464 if (name == method.GetName() && signature == method.GetSignature()) {
465 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466 }
467 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469}
470
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700471ArtMethod* Class::FindDeclaredDirectMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700472 uint32_t dex_method_idx,
473 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800474 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475 for (auto& method : GetDirectMethods(pointer_size)) {
476 if (method.GetDexMethodIndex() == dex_method_idx) {
477 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 }
479 }
480 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700481 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482}
483
Andreas Gampe542451c2016-07-26 09:02:02 -0700484ArtMethod* Class::FindDirectMethod(const StringPiece& name,
485 const StringPiece& signature,
486 PointerSize pointer_size) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700487 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700489 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 return method;
491 }
492 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700493 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494}
495
Andreas Gampe542451c2016-07-26 09:02:02 -0700496ArtMethod* Class::FindDirectMethod(const StringPiece& name,
497 const Signature& signature,
498 PointerSize pointer_size) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700499 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700500 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700501 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700502 return method;
503 }
504 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700505 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700506}
507
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700508ArtMethod* Class::FindDirectMethod(ObjPtr<DexCache> dex_cache,
509 uint32_t dex_method_idx,
510 PointerSize pointer_size) {
511 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514 return method;
515 }
516 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518}
519
Andreas Gampe542451c2016-07-26 09:02:02 -0700520ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
521 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000522 for (auto& method : GetDirectMethods(pointer_size)) {
523 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
524 if (name == np_method->GetName()) {
525 return &method;
526 }
527 }
528 return nullptr;
529}
530
Alex Lighte64300b2015-12-15 15:02:47 -0800531// TODO These should maybe be changed to be named FindOwnedVirtualMethod or something similar
532// because they do not only find 'declared' methods and will return copied methods. This behavior is
533// desired and correct but the naming can lead to confusion because in the java language declared
534// excludes interface methods which might be found by this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700535ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
536 const StringPiece& signature,
537 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700538 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700539 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
540 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700541 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700542 }
543 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700544 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700545}
546
Andreas Gampe542451c2016-07-26 09:02:02 -0700547ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
548 const Signature& signature,
549 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700550 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700551 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
552 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 }
555 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700556 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557}
558
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700559ArtMethod* Class::FindDeclaredVirtualMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700560 uint32_t dex_method_idx,
561 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 if (GetDexCache() == dex_cache) {
Alex Lighte64300b2015-12-15 15:02:47 -0800563 for (auto& method : GetDeclaredVirtualMethods(pointer_size)) {
564 if (method.GetDexMethodIndex() == dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700565 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 }
567 }
568 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700569 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570}
571
Andreas Gampe542451c2016-07-26 09:02:02 -0700572ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
573 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000574 for (auto& method : GetVirtualMethods(pointer_size)) {
575 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
576 if (name == np_method->GetName()) {
577 return &method;
578 }
579 }
580 return nullptr;
581}
582
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700583ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
584 const StringPiece& signature,
585 PointerSize pointer_size) {
586 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700587 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700588 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800589 return method;
590 }
591 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700592 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593}
594
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700595ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
596 const Signature& signature,
597 PointerSize pointer_size) {
598 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700600 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700601 return method;
602 }
603 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700604 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700605}
606
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700607ArtMethod* Class::FindVirtualMethod(ObjPtr<DexCache> dex_cache,
608 uint32_t dex_method_idx,
609 PointerSize pointer_size) {
610 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700611 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700612 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800613 return method;
614 }
615 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700616 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617}
618
Andreas Gampe542451c2016-07-26 09:02:02 -0700619ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700620 DCHECK(method->GetDeclaringClass()->IsInterface());
621 DCHECK(IsInterface()) << "Should only be called on a interface class";
622 // Check if we have one defined on this interface first. This includes searching copied ones to
623 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
624 // don't do any indirect method checks here.
625 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
626 if (method->HasSameNameAndSignature(&iface_method)) {
627 return &iface_method;
628 }
629 }
630
631 std::vector<ArtMethod*> abstract_methods;
632 // Search through the IFTable for a working version. We don't need to check for conflicts
633 // because if there was one it would appear in this classes virtual_methods_ above.
634
635 Thread* self = Thread::Current();
636 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700637 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
638 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700639 size_t iftable_count = GetIfTableCount();
640 // Find the method. We don't need to check for conflicts because they would have been in the
641 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
642 // subtypiest interfaces get visited first.
643 for (size_t k = iftable_count; k != 0;) {
644 k--;
645 DCHECK_LT(k, iftable->Count());
646 iface.Assign(iftable->GetInterface(k));
647 // Iterate through every declared method on this interface. Each direct method's name/signature
648 // is unique so the order of the inner loop doesn't matter.
649 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
650 ArtMethod* current_method = &method_iter;
651 if (current_method->HasSameNameAndSignature(method)) {
652 if (current_method->IsDefault()) {
653 // Handle JLS soft errors, a default method from another superinterface tree can
654 // "override" an abstract method(s) from another superinterface tree(s). To do this,
655 // ignore any [default] method which are dominated by the abstract methods we've seen so
656 // far. Check if overridden by any in abstract_methods. We do not need to check for
657 // default_conflicts because we would hit those before we get to this loop.
658 bool overridden = false;
659 for (ArtMethod* possible_override : abstract_methods) {
660 DCHECK(possible_override->HasSameNameAndSignature(current_method));
661 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
662 overridden = true;
663 break;
664 }
665 }
666 if (!overridden) {
667 return current_method;
668 }
669 } else {
670 // Is not default.
671 // This might override another default method. Just stash it for now.
672 abstract_methods.push_back(current_method);
673 }
674 }
675 }
676 }
677 // If we reach here we either never found any declaration of the method (in which case
678 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
679 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
680 // of these arbitrarily.
681 return abstract_methods.empty() ? nullptr : abstract_methods[0];
682}
683
Andreas Gampe542451c2016-07-26 09:02:02 -0700684ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700685 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
686 if (method.IsClassInitializer()) {
687 DCHECK_STREQ(method.GetName(), "<clinit>");
688 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
689 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700690 }
691 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700692 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700693}
694
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700695// Custom binary search to avoid double comparisons from std::binary_search.
696static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
697 const StringPiece& name,
698 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700699 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700700 if (fields == nullptr) {
701 return nullptr;
702 }
703 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100704 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700705 ArtField* ret = nullptr;
706 while (low < high) {
707 size_t mid = (low + high) / 2;
708 ArtField& field = fields->At(mid);
709 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
710 // verifier. There can be multiple fields with the same in the same class name due to proguard.
711 int result = StringPiece(field.GetName()).Compare(name);
712 if (result == 0) {
713 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
714 }
715 if (result < 0) {
716 low = mid + 1;
717 } else if (result > 0) {
718 high = mid;
719 } else {
720 ret = &field;
721 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800722 }
723 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700724 if (kIsDebugBuild) {
725 ArtField* found = nullptr;
726 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
727 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
728 found = &field;
729 break;
730 }
731 }
David Sehr709b0702016-10-13 09:12:37 -0700732 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700733 }
734 return ret;
735}
736
737ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
738 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
739 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800740}
741
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700742ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800743 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700744 for (ArtField& field : GetIFields()) {
745 if (field.GetDexFieldIndex() == dex_field_idx) {
746 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800747 }
748 }
749 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700750 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800751}
752
Brian Carlstromea46f952013-07-30 01:26:50 -0700753ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800754 // Is the field in this class, or any of its superclasses?
755 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700756 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700757 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700758 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800759 return f;
760 }
761 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700762 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800763}
764
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700765ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800766 // Is the field in this class, or any of its superclasses?
767 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700768 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700769 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700770 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800771 return f;
772 }
773 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700774 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800775}
776
Brian Carlstromea46f952013-07-30 01:26:50 -0700777ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700778 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700779 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800780}
781
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700782ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800783 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700784 for (ArtField& field : GetSFields()) {
785 if (field.GetDexFieldIndex() == dex_field_idx) {
786 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800787 }
788 }
789 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700790 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800791}
792
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700793ArtField* Class::FindStaticField(Thread* self,
794 Handle<Class> klass,
795 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700796 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800797 // Is the field in this class (or its interfaces), or any of its
798 // superclasses (or their interfaces)?
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700799 for (ObjPtr<Class> k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800800 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700801 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700802 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800803 return f;
804 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700805 // Wrap k incase it moves during GetDirectInterface.
806 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700807 HandleWrapperObjPtr<Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800808 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700809 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800810 StackHandleScope<1> hs2(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700811 Handle<Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700812 f = FindStaticField(self, interface, name, type);
813 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800814 return f;
815 }
816 }
817 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700818 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800819}
820
Vladimir Markobb268b12016-06-30 15:52:56 +0100821ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700822 ObjPtr<Class> klass,
823 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700824 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700825 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800826 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700827 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700828 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800829 return f;
830 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100831 // Though GetDirectInterface() should not cause thread suspension when called
832 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700833 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700834 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700835 Handle<Class> h_k(hs.NewHandle(k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800836 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700837 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700838 ObjPtr<Class> interface = GetDirectInterface(self, h_k, i);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700839 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
840 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800841 return f;
842 }
843 }
844 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700845 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800846}
847
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700848ArtField* Class::FindField(Thread* self,
849 Handle<Class> klass,
850 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700851 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800852 // Find a field using the JLS field resolution order
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700853 for (ObjPtr<Class> k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800854 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700855 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700856 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800857 return f;
858 }
859 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700860 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800861 return f;
862 }
863 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700864 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700865 HandleWrapperObjPtr<Class> h_k(hs.NewHandleWrapper(&k));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700866 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800867 StackHandleScope<1> hs2(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700868 Handle<Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700869 f = interface->FindStaticField(self, interface, name, type);
870 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800871 return f;
872 }
873 }
874 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700875 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800876}
877
Andreas Gampe542451c2016-07-26 09:02:02 -0700878void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700879 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800880 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700881 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800882 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700883 }
884 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200885}
886
Ian Rogers1ff3c982014-08-12 02:30:58 -0700887const char* Class::GetDescriptor(std::string* storage) {
888 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700889 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700890 } else if (IsArrayClass()) {
891 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000892 } else if (IsProxyClass()) {
893 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700894 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700895 } else {
896 const DexFile& dex_file = GetDexFile();
897 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
898 return dex_file.GetTypeDescriptor(type_id);
899 }
900}
901
Ian Rogers1ff3c982014-08-12 02:30:58 -0700902const char* Class::GetArrayDescriptor(std::string* storage) {
903 std::string temp;
904 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
905 *storage = "[";
906 *storage += elem_desc;
907 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700908}
909
910const DexFile::ClassDef* Class::GetClassDef() {
911 uint16_t class_def_idx = GetDexClassDefIndex();
912 if (class_def_idx == DexFile::kDexNoIndex16) {
913 return nullptr;
914 }
915 return &GetDexFile().GetClassDef(class_def_idx);
916}
917
Mathieu Chartierf8322842014-05-16 10:59:25 -0700918uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
919 DCHECK(!IsPrimitive());
920 DCHECK(!IsArrayClass());
921 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
922}
923
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700924ObjPtr<Class> Class::GetDirectInterface(Thread* self,
925 Handle<Class> klass,
926 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700927 DCHECK(klass.Get() != nullptr);
928 DCHECK(!klass->IsPrimitive());
929 if (klass->IsArrayClass()) {
930 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
931 if (idx == 0) {
932 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
933 } else {
934 DCHECK_EQ(1U, idx);
935 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
936 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000937 } else if (klass->IsProxyClass()) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700938 ObjPtr<ObjectArray<Class>> interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700939 DCHECK(interfaces != nullptr);
940 return interfaces->Get(idx);
941 } else {
942 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700943 ObjPtr<Class> interface = klass->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700944 if (interface == nullptr) {
945 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
946 klass.Get());
947 CHECK(interface != nullptr || self->IsExceptionPending());
948 }
949 return interface;
950 }
951}
952
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700953ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Calin Juravle52503d82015-11-11 16:58:31 +0000954 DCHECK(klass.Get() != nullptr);
955 DCHECK(!klass->IsInterface());
956 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700957 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +0000958 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700959 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -0700960 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -0700961 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +0000962 }
Calin Juravle52503d82015-11-11 16:58:31 +0000963 return common_super_class;
964}
965
Mathieu Chartierf8322842014-05-16 10:59:25 -0700966const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700967 const DexFile& dex_file = GetDexFile();
968 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200969 if (dex_class_def == nullptr) {
970 // Generated classes have no class def.
971 return nullptr;
972 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700973 return dex_file.GetSourceFile(*dex_class_def);
974}
975
976std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700977 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000978 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700979 return dex_cache->GetLocation()->ToModifiedUtf8();
980 }
981 // Arrays and proxies are generated and have no corresponding dex file location.
982 return "generated class";
983}
984
985const DexFile::TypeList* Class::GetInterfaceTypeList() {
986 const DexFile::ClassDef* class_def = GetClassDef();
987 if (class_def == nullptr) {
988 return nullptr;
989 }
990 return GetDexFile().GetInterfacesList(*class_def);
991}
992
Andreas Gampe542451c2016-07-26 09:02:02 -0700993void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700994 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -0700995 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700996 const size_t table_length = table->GetLength();
997 SetEmbeddedVTableLength(table_length);
998 for (size_t i = 0; i < table_length; i++) {
999 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001000 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001001 // Keep java.lang.Object class's vtable around for since it's easier
1002 // to be reused by array classes during their linking.
1003 if (!IsObjectClass()) {
1004 SetVTable(nullptr);
1005 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001006}
1007
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001008class ReadBarrierOnNativeRootsVisitor {
1009 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001010 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001011 MemberOffset offset ATTRIBUTE_UNUSED,
1012 bool is_static ATTRIBUTE_UNUSED) const {}
1013
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001014 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001015 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001016 if (!root->IsNull()) {
1017 VisitRoot(root);
1018 }
1019 }
1020
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001021 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001022 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001023 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1024 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001025 if (old_ref != new_ref) {
1026 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1027 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001028 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001029 atomic_root->CompareExchangeStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001030 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1031 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001032 }
1033 }
1034};
1035
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001036// The pre-fence visitor for Class::CopyOf().
1037class CopyClassVisitor {
1038 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001039 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001040 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001041 size_t new_length,
1042 size_t copy_bytes,
1043 ImTable* imt,
1044 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001045 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001046 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001047 }
1048
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001049 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001050 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001051 StackHandleScope<1> hs(self_);
1052 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001053 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
1054 Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001055 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1056 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001057 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001058 // Visit all of the references to make sure there is no from space references in the native
1059 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001060 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001061 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001062 }
1063
1064 private:
1065 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001066 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001067 const size_t new_length_;
1068 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001069 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001070 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001071 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1072};
1073
Andreas Gampe542451c2016-07-26 09:02:02 -07001074Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001075 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1076 // We may get copied by a compacting GC.
1077 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001078 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001079 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001080 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1081 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001082 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001083 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001084 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1085 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001086 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001087 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001088 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001089 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001090 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001091}
1092
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001093bool Class::ProxyDescriptorEquals(const char* match) {
1094 DCHECK(IsProxyClass());
1095 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001096}
1097
Mathieu Chartiere401d142015-04-22 13:56:20 -07001098// TODO: Move this to java_lang_Class.cc?
1099ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001100 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001101 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001102 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001103 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001104 continue;
1105 }
1106 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001107 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001108 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001109 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001110 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001111 return nullptr;
1112 }
1113 }
1114 return nullptr;
1115}
1116
Mathieu Chartiere401d142015-04-22 13:56:20 -07001117uint32_t Class::Depth() {
1118 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001119 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001120 depth++;
1121 }
1122 return depth;
1123}
1124
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001125uint32_t Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
1126 std::string temp;
1127 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
1128 return (type_id == nullptr) ? DexFile::kDexNoIndex : dex_file.GetIndexForTypeId(*type_id);
1129}
1130
Andreas Gampe542451c2016-07-26 09:02:02 -07001131template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001132ObjPtr<Method> Class::GetDeclaredMethodInternal(
1133 Thread* self,
1134 ObjPtr<Class> klass,
1135 ObjPtr<String> name,
1136 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001137 // Covariant return types permit the class to define multiple
1138 // methods with the same name and parameter types. Prefer to
1139 // return a non-synthetic method in such situations. We may
1140 // still return a synthetic method to handle situations like
1141 // escalated visibility. We never return miranda methods that
1142 // were synthesized by the runtime.
1143 constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
1144 StackHandleScope<3> hs(self);
1145 auto h_method_name = hs.NewHandle(name);
1146 if (UNLIKELY(h_method_name.Get() == nullptr)) {
1147 ThrowNullPointerException("name == null");
1148 return nullptr;
1149 }
1150 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001151 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001152 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001153 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1154 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001155 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001156 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001157 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1158 if (UNLIKELY(self->IsExceptionPending())) {
1159 return nullptr;
1160 }
1161 continue;
1162 }
1163 auto modifiers = m.GetAccessFlags();
1164 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001165 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001166 }
1167 if ((modifiers & kAccMiranda) == 0) {
1168 result = &m; // Remember as potential result if it's not a miranda method.
1169 }
1170 }
1171 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001172 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001173 auto modifiers = m.GetAccessFlags();
1174 if ((modifiers & kAccConstructor) != 0) {
1175 continue;
1176 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001177 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001178 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001179 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001180 if (np_name == nullptr) {
1181 self->AssertPendingException();
1182 return nullptr;
1183 }
1184 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1185 if (UNLIKELY(self->IsExceptionPending())) {
1186 return nullptr;
1187 }
1188 continue;
1189 }
1190 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001191 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001192 }
1193 // Direct methods cannot be miranda methods, so this potential result must be synthetic.
1194 result = &m;
1195 }
1196 }
1197 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001198 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001199 : nullptr;
1200}
1201
1202template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001203ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001204 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001205 ObjPtr<Class> klass,
1206 ObjPtr<String> name,
1207 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001208template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001209ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001210 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001211 ObjPtr<Class> klass,
1212 ObjPtr<String> name,
1213 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001214template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001215ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001216 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001217 ObjPtr<Class> klass,
1218 ObjPtr<String> name,
1219 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001220template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001221ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001222 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001223 ObjPtr<Class> klass,
1224 ObjPtr<String> name,
1225 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001226
Andreas Gampe542451c2016-07-26 09:02:02 -07001227template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001228ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001229 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001230 ObjPtr<Class> klass,
1231 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001232 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001233 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001234 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001235 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001236 : nullptr;
1237}
1238
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001239// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001240
Andreas Gampe542451c2016-07-26 09:02:02 -07001241template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001242ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001243 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001244 ObjPtr<Class> klass,
1245 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001246template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001247ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001248 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001249 ObjPtr<Class> klass,
1250 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001251template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001252ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001253 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001254 ObjPtr<Class> klass,
1255 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001256template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001257ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001258 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001259 ObjPtr<Class> klass,
1260 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001261
Andreas Gampe715fdc22016-04-18 17:07:30 -07001262int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1263 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1264 return default_value;
1265 }
1266 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001267 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001268 return default_value;
1269 }
1270 return flags;
1271}
1272
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001273void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1274 if (Runtime::Current()->IsActiveTransaction()) {
1275 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1276 } else {
1277 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1278 }
1279}
1280
David Sehr709b0702016-10-13 09:12:37 -07001281std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1282 if (klass == nullptr) {
1283 return "null";
1284 }
1285 return klass->PrettyDescriptor();
1286}
1287
1288std::string Class::PrettyDescriptor() {
1289 std::string temp;
1290 return art::PrettyDescriptor(GetDescriptor(&temp));
1291}
1292
1293std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1294 if (c == nullptr) {
1295 return "null";
1296 }
1297 return c->PrettyClass();
1298}
1299
1300std::string Class::PrettyClass() {
1301 std::string result;
1302 result += "java.lang.Class<";
1303 result += PrettyDescriptor();
1304 result += ">";
1305 return result;
1306}
1307
1308std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1309 if (c == nullptr) {
1310 return "null";
1311 }
1312 return c->PrettyClassAndClassLoader();
1313}
1314
1315std::string Class::PrettyClassAndClassLoader() {
1316 std::string result;
1317 result += "java.lang.Class<";
1318 result += PrettyDescriptor();
1319 result += ",";
1320 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1321 // TODO: add an identifying hash value for the loader
1322 result += ">";
1323 return result;
1324}
1325
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001326} // namespace mirror
1327} // namespace art