blob: c1565df76b5667054d0a057cb78100cb21a6a496 [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"
Alex Lightd6251582016-10-31 11:12:30 -070023#include "class_ext.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010024#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "class_loader.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070026#include "class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070029#include "dex_file_annotations.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031#include "handle_scope-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070032#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070033#include "object_array-inl.h"
34#include "object-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070035#include "object_lock.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070036#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "thread.h"
38#include "throwable.h"
39#include "utils.h"
40#include "well_known_classes.h"
41
42namespace art {
43namespace mirror {
44
Andreas Gampe46ee31b2016-12-14 10:11:49 -080045using android::base::StringPrintf;
46
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070047GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070049void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070050 CHECK(java_lang_Class_.IsNull())
51 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070052 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070053 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070054 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080056}
57
58void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070059 CHECK(!java_lang_Class_.IsNull());
60 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061}
62
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070063void Class::VisitRoots(RootVisitor* visitor) {
64 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080065}
66
Alex Lightd6251582016-10-31 11:12:30 -070067ClassExt* Class::GetExtData() {
68 return GetFieldObject<ClassExt>(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
69}
70
Alex Light0273ad12016-11-02 11:19:31 -070071ClassExt* Class::EnsureExtDataPresent(Thread* self) {
72 ObjPtr<ClassExt> existing(GetExtData());
73 if (!existing.IsNull()) {
74 return existing.Ptr();
75 }
76 StackHandleScope<3> hs(self);
77 // Handlerize 'this' since we are allocating here.
78 Handle<Class> h_this(hs.NewHandle(this));
79 // Clear exception so we can allocate.
80 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
81 self->ClearException();
82 // Allocate the ClassExt
83 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
84 if (new_ext.Get() == nullptr) {
85 // OOM allocating the classExt.
86 // TODO Should we restore the suppressed exception?
87 self->AssertPendingOOMException();
88 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -080089 } else {
Alex Light0273ad12016-11-02 11:19:31 -070090 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
91 bool set;
92 // Set the ext_data_ field using CAS semantics.
93 if (Runtime::Current()->IsActiveTransaction()) {
94 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
95 ObjPtr<ClassExt>(nullptr),
96 new_ext.Get());
97 } else {
98 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
99 ObjPtr<ClassExt>(nullptr),
100 new_ext.Get());
101 }
102 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
103 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
104 CHECK(!ret.IsNull());
105 // Restore the exception if there was one.
106 if (throwable.Get() != nullptr) {
107 self->SetException(throwable.Get());
108 }
109 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800110 }
111}
112
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700113void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
114 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700115 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
116 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700117 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
119 new_status != kStatusRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700120 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700121 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700122 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700123 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
124 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700125 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700126 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700127 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700128 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 }
Ian Rogers98379392014-02-24 16:53:16 -0800130 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700131 CHECK_NE(h_this->GetStatus(), kStatusError)
132 << "Attempt to set as erroneous an already erroneous class "
David Sehr709b0702016-10-13 09:12:37 -0700133 << h_this->PrettyClass();
Andreas Gampe31decb12015-08-24 21:09:05 -0700134 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700135 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700136 if (self->IsExceptionPending()) {
137 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
138 }
139 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140
Alex Light0273ad12016-11-02 11:19:31 -0700141 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
142 if (!ext.IsNull()) {
143 self->AssertPendingException();
144 ext->SetVerifyError(self->GetException());
145 } else {
146 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700147 }
148 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 }
Alex Light0273ad12016-11-02 11:19:31 -0700150
Andreas Gampe575e78c2014-11-03 23:41:03 -0800151 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100152 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700153 h_this->SetField32Volatile<true>(StatusOffset(), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154 } else {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700155 h_this->SetField32Volatile<false>(StatusOffset(), new_status);
156 }
157
158 // Setting the object size alloc fast path needs to be after the status write so that if the
159 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
160 // load-acquire/fake dependency.
161 if (new_status == kStatusInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700162 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
163 // Finalizable objects must always go slow path.
164 if (!h_this->IsFinalizable()) {
165 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700166 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100167 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700168
169 if (!class_linker_initialized) {
170 // When the class linker is being initialized its single threaded and by definition there can be
171 // no waiters. During initialization classes may appear temporary but won't be retired as their
172 // size was statically computed.
173 } else {
174 // Classes that are being resolved or initialized need to notify waiters that the class status
175 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700176 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700177 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
178 // so that they can grab the new version of the class from the class linker's table.
David Sehr709b0702016-10-13 09:12:37 -0700179 CHECK_LT(new_status, kStatusResolved) << h_this->PrettyDescriptor();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700180 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700181 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700182 }
183 } else {
184 CHECK_NE(new_status, kStatusRetired);
185 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700186 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700187 }
188 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700189 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190}
191
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700192void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700193 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800194 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195}
196
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700198 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700199 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
200 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700201 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700202 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100203 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700204 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205}
206
207// Return the class' name. The exact format is bizarre, but it's the specified behavior for
208// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
209// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
210// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700211String* Class::ComputeName(Handle<Class> h_this) {
212 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800213 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 return name;
215 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 std::string temp;
217 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800218 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
220 // The descriptor indicates that this is the class for
221 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700222 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 switch (descriptor[0]) {
224 case 'Z': c_name = "boolean"; break;
225 case 'B': c_name = "byte"; break;
226 case 'C': c_name = "char"; break;
227 case 'S': c_name = "short"; break;
228 case 'I': c_name = "int"; break;
229 case 'J': c_name = "long"; break;
230 case 'F': c_name = "float"; break;
231 case 'D': c_name = "double"; break;
232 case 'V': c_name = "void"; break;
233 default:
234 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
235 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800236 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 } else {
238 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
239 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700240 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700242 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 return name;
244}
245
Ian Rogersef7d42f2014-01-06 12:55:46 -0800246void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700248 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 if ((flags & kDumpClassClassLoader) != 0) {
250 os << ' ' << GetClassLoader();
251 }
252 if ((flags & kDumpClassInitialized) != 0) {
253 os << ' ' << GetStatus();
254 }
255 os << "\n";
256 return;
257 }
258
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700260 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700261 Handle<Class> h_this(hs.NewHandle(this));
262 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700264
Ian Rogers1ff3c982014-08-12 02:30:58 -0700265 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700267 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700269 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 os << StringPrintf(" access=0x%04x.%04x\n",
271 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700272 if (h_super.Get() != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700273 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700274 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 }
276 if (IsArrayClass()) {
277 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
278 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700279 const size_t num_direct_interfaces = NumDirectInterfaces();
280 if (num_direct_interfaces > 0) {
281 os << " interfaces (" << num_direct_interfaces << "):\n";
282 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000283 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700284 if (interface == nullptr) {
285 os << StringPrintf(" %2zd: nullptr!\n", i);
286 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700287 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
288 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700289 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 }
291 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700292 if (!IsLoaded()) {
293 os << " class not yet loaded";
294 } else {
295 // After this point, this may have moved due to GetDirectInterface.
296 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
297 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
298 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700299 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700302 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
303 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700304 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700305 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700306 }
307 if (h_this->NumStaticFields() > 0) {
308 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
309 if (h_this->IsResolved() || h_this->IsErroneous()) {
310 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700311 os << StringPrintf(" %2zd: %s\n", i,
312 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700313 }
314 } else {
315 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700317 }
318 if (h_this->NumInstanceFields() > 0) {
319 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
320 if (h_this->IsResolved() || h_this->IsErroneous()) {
321 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700322 os << StringPrintf(" %2zd: %s\n", i,
323 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700324 }
325 } else {
326 os << " <not yet available>";
327 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 }
329 }
330}
331
332void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 // Sanity check that the number of bits set in the reference offset bitmap
335 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700336 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700337 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 count += c->NumReferenceInstanceFieldsDuringLinking();
339 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700340 // +1 for the Class in Object.
341 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 // Not called within a transaction.
344 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700345 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346}
347
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
349 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700350 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
351 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 ++i;
353 }
354 if (descriptor1.find('/', i) != StringPiece::npos ||
355 descriptor2.find('/', i) != StringPiece::npos) {
356 return false;
357 } else {
358 return true;
359 }
360}
361
Mathieu Chartier3398c782016-09-30 10:27:43 -0700362bool Class::IsInSamePackage(ObjPtr<Class> that) {
363 ObjPtr<Class> klass1 = this;
364 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 if (klass1 == klass2) {
366 return true;
367 }
368 // Class loaders must match.
369 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
370 return false;
371 }
372 // Arrays are in the same package when their element classes are.
373 while (klass1->IsArrayClass()) {
374 klass1 = klass1->GetComponentType();
375 }
376 while (klass2->IsArrayClass()) {
377 klass2 = klass2->GetComponentType();
378 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700379 // trivial check again for array types
380 if (klass1 == klass2) {
381 return true;
382 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700384 std::string temp1, temp2;
385 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386}
387
Ian Rogersef7d42f2014-01-06 12:55:46 -0800388bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
390}
391
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700392void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100393 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700394 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100395 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700396 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100397 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398}
399
Andreas Gampe542451c2016-07-26 09:02:02 -0700400ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
401 const StringPiece& signature,
402 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700405 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 return method;
407 }
408
409 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700410 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700411 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700413 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414 return method;
415 }
416 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700417 return nullptr;
418}
419
Andreas Gampe542451c2016-07-26 09:02:02 -0700420ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
421 const Signature& signature,
422 PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700423 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700424 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700425 if (method != nullptr) {
426 return method;
427 }
428
429 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700430 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700431 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700432 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700433 if (method != nullptr) {
434 return method;
435 }
436 }
437 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438}
439
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700440ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700441 uint32_t dex_method_idx,
442 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700444 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700445 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446 return method;
447 }
448
449 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700450 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
453 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455 return method;
456 }
457 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459}
460
Andreas Gampe542451c2016-07-26 09:02:02 -0700461ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
462 const StringPiece& signature,
463 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 for (auto& method : GetDirectMethods(pointer_size)) {
465 if (name == method.GetName() && method.GetSignature() == signature) {
466 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700467 }
468 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700469 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700470}
471
Andreas Gampe542451c2016-07-26 09:02:02 -0700472ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
473 const Signature& signature,
474 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475 for (auto& method : GetDirectMethods(pointer_size)) {
476 if (name == method.GetName() && signature == method.GetSignature()) {
477 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 }
479 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700480 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481}
482
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700483ArtMethod* Class::FindDeclaredDirectMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700484 uint32_t dex_method_idx,
485 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 for (auto& method : GetDirectMethods(pointer_size)) {
488 if (method.GetDexMethodIndex() == dex_method_idx) {
489 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 }
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 StringPiece& 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800502 return method;
503 }
504 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700505 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800506}
507
Andreas Gampe542451c2016-07-26 09:02:02 -0700508ArtMethod* Class::FindDirectMethod(const StringPiece& name,
509 const Signature& signature,
510 PointerSize pointer_size) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700511 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700514 return method;
515 }
516 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700518}
519
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700520ArtMethod* Class::FindDirectMethod(ObjPtr<DexCache> dex_cache,
521 uint32_t dex_method_idx,
522 PointerSize pointer_size) {
523 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700524 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700525 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800526 return method;
527 }
528 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700529 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530}
531
Andreas Gampe542451c2016-07-26 09:02:02 -0700532ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
533 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000534 for (auto& method : GetDirectMethods(pointer_size)) {
535 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
536 if (name == np_method->GetName()) {
537 return &method;
538 }
539 }
540 return nullptr;
541}
542
Alex Lighte64300b2015-12-15 15:02:47 -0800543// TODO These should maybe be changed to be named FindOwnedVirtualMethod or something similar
544// because they do not only find 'declared' methods and will return copied methods. This behavior is
545// desired and correct but the naming can lead to confusion because in the java language declared
546// excludes interface methods which might be found by this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700547ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
548 const StringPiece& 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() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700554 }
555 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700556 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700557}
558
Andreas Gampe542451c2016-07-26 09:02:02 -0700559ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
560 const Signature& signature,
561 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700562 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700563 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
564 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700565 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 }
567 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700568 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569}
570
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700571ArtMethod* Class::FindDeclaredVirtualMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700572 uint32_t dex_method_idx,
573 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 if (GetDexCache() == dex_cache) {
Alex Lighte64300b2015-12-15 15:02:47 -0800575 for (auto& method : GetDeclaredVirtualMethods(pointer_size)) {
576 if (method.GetDexMethodIndex() == dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700577 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 }
579 }
580 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700581 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800582}
583
Andreas Gampe542451c2016-07-26 09:02:02 -0700584ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
585 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000586 for (auto& method : GetVirtualMethods(pointer_size)) {
587 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
588 if (name == np_method->GetName()) {
589 return &method;
590 }
591 }
592 return nullptr;
593}
594
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700595ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
596 const StringPiece& 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800601 return method;
602 }
603 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700604 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605}
606
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700607ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
608 const Signature& signature,
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(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700612 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700613 return method;
614 }
615 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700616 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700617}
618
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700619ArtMethod* Class::FindVirtualMethod(ObjPtr<DexCache> dex_cache,
620 uint32_t dex_method_idx,
621 PointerSize pointer_size) {
622 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700623 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700624 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 return method;
626 }
627 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700628 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800629}
630
Andreas Gampe542451c2016-07-26 09:02:02 -0700631ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700632 DCHECK(method->GetDeclaringClass()->IsInterface());
633 DCHECK(IsInterface()) << "Should only be called on a interface class";
634 // Check if we have one defined on this interface first. This includes searching copied ones to
635 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
636 // don't do any indirect method checks here.
637 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
638 if (method->HasSameNameAndSignature(&iface_method)) {
639 return &iface_method;
640 }
641 }
642
643 std::vector<ArtMethod*> abstract_methods;
644 // Search through the IFTable for a working version. We don't need to check for conflicts
645 // because if there was one it would appear in this classes virtual_methods_ above.
646
647 Thread* self = Thread::Current();
648 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700649 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
650 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700651 size_t iftable_count = GetIfTableCount();
652 // Find the method. We don't need to check for conflicts because they would have been in the
653 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
654 // subtypiest interfaces get visited first.
655 for (size_t k = iftable_count; k != 0;) {
656 k--;
657 DCHECK_LT(k, iftable->Count());
658 iface.Assign(iftable->GetInterface(k));
659 // Iterate through every declared method on this interface. Each direct method's name/signature
660 // is unique so the order of the inner loop doesn't matter.
661 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
662 ArtMethod* current_method = &method_iter;
663 if (current_method->HasSameNameAndSignature(method)) {
664 if (current_method->IsDefault()) {
665 // Handle JLS soft errors, a default method from another superinterface tree can
666 // "override" an abstract method(s) from another superinterface tree(s). To do this,
667 // ignore any [default] method which are dominated by the abstract methods we've seen so
668 // far. Check if overridden by any in abstract_methods. We do not need to check for
669 // default_conflicts because we would hit those before we get to this loop.
670 bool overridden = false;
671 for (ArtMethod* possible_override : abstract_methods) {
672 DCHECK(possible_override->HasSameNameAndSignature(current_method));
673 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
674 overridden = true;
675 break;
676 }
677 }
678 if (!overridden) {
679 return current_method;
680 }
681 } else {
682 // Is not default.
683 // This might override another default method. Just stash it for now.
684 abstract_methods.push_back(current_method);
685 }
686 }
687 }
688 }
689 // If we reach here we either never found any declaration of the method (in which case
690 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
691 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
692 // of these arbitrarily.
693 return abstract_methods.empty() ? nullptr : abstract_methods[0];
694}
695
Andreas Gampe542451c2016-07-26 09:02:02 -0700696ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700697 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
698 if (method.IsClassInitializer()) {
699 DCHECK_STREQ(method.GetName(), "<clinit>");
700 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
701 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700702 }
703 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700704 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700705}
706
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700707// Custom binary search to avoid double comparisons from std::binary_search.
708static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
709 const StringPiece& name,
710 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700711 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700712 if (fields == nullptr) {
713 return nullptr;
714 }
715 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100716 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700717 ArtField* ret = nullptr;
718 while (low < high) {
719 size_t mid = (low + high) / 2;
720 ArtField& field = fields->At(mid);
721 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
722 // verifier. There can be multiple fields with the same in the same class name due to proguard.
723 int result = StringPiece(field.GetName()).Compare(name);
724 if (result == 0) {
725 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
726 }
727 if (result < 0) {
728 low = mid + 1;
729 } else if (result > 0) {
730 high = mid;
731 } else {
732 ret = &field;
733 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800734 }
735 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700736 if (kIsDebugBuild) {
737 ArtField* found = nullptr;
738 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
739 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
740 found = &field;
741 break;
742 }
743 }
David Sehr709b0702016-10-13 09:12:37 -0700744 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700745 }
746 return ret;
747}
748
749ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
750 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
751 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800752}
753
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700754ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800755 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700756 for (ArtField& field : GetIFields()) {
757 if (field.GetDexFieldIndex() == dex_field_idx) {
758 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800759 }
760 }
761 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700762 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800763}
764
Brian Carlstromea46f952013-07-30 01:26:50 -0700765ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
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(name, type);
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
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700777ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800778 // Is the field in this class, or any of its superclasses?
779 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700780 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700781 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700782 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800783 return f;
784 }
785 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700786 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800787}
788
Brian Carlstromea46f952013-07-30 01:26:50 -0700789ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700790 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700791 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800792}
793
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700794ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800795 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700796 for (ArtField& field : GetSFields()) {
797 if (field.GetDexFieldIndex() == dex_field_idx) {
798 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800799 }
800 }
801 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700802 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800803}
804
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700805ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000806 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700807 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700808 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800809 // Is the field in this class (or its interfaces), or any of its
810 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000811 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800812 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700813 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700814 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800815 return f;
816 }
817 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000818 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
819 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
820 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700821 f = FindStaticField(self, interface, name, type);
822 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800823 return f;
824 }
825 }
826 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700827 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800828}
829
Vladimir Markobb268b12016-06-30 15:52:56 +0100830ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700831 ObjPtr<Class> klass,
832 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700833 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700834 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800835 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700836 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700837 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800838 return f;
839 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100840 // Though GetDirectInterface() should not cause thread suspension when called
841 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700842 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000844 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
845 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
846 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700847 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
848 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800849 return f;
850 }
851 }
852 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700853 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800854}
855
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700856ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000857 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700858 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700859 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800860 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000861 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800862 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700863 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700864 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800865 return f;
866 }
867 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700868 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800869 return f;
870 }
871 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000872 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
873 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
874 DCHECK(interface != nullptr);
875 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700876 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800877 return f;
878 }
879 }
880 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700881 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800882}
883
Andreas Gampe542451c2016-07-26 09:02:02 -0700884void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700885 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800886 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700887 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800888 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700889 }
890 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200891}
892
Ian Rogers1ff3c982014-08-12 02:30:58 -0700893const char* Class::GetDescriptor(std::string* storage) {
894 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700895 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700896 } else if (IsArrayClass()) {
897 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000898 } else if (IsProxyClass()) {
899 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700900 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700901 } else {
902 const DexFile& dex_file = GetDexFile();
903 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
904 return dex_file.GetTypeDescriptor(type_id);
905 }
906}
907
Ian Rogers1ff3c982014-08-12 02:30:58 -0700908const char* Class::GetArrayDescriptor(std::string* storage) {
909 std::string temp;
910 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
911 *storage = "[";
912 *storage += elem_desc;
913 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700914}
915
916const DexFile::ClassDef* Class::GetClassDef() {
917 uint16_t class_def_idx = GetDexClassDefIndex();
918 if (class_def_idx == DexFile::kDexNoIndex16) {
919 return nullptr;
920 }
921 return &GetDexFile().GetClassDef(class_def_idx);
922}
923
Andreas Gampea5b09a62016-11-17 15:21:22 -0800924dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700925 DCHECK(!IsPrimitive());
926 DCHECK(!IsArrayClass());
927 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
928}
929
Vladimir Marko19a4d372016-12-08 14:41:46 +0000930ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
931 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700932 DCHECK(!klass->IsPrimitive());
933 if (klass->IsArrayClass()) {
934 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +0000935 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
936 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700937 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000938 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700939 } else {
940 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000941 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700942 }
Vladimir Marko19a4d372016-12-08 14:41:46 +0000943 DCHECK(interface != nullptr);
944 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000945 } else if (klass->IsProxyClass()) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000946 ObjPtr<ObjectArray<Class>> interfaces = klass->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700947 DCHECK(interfaces != nullptr);
948 return interfaces->Get(idx);
949 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -0800950 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700951 ObjPtr<Class> interface = klass->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700952 return interface;
953 }
954}
955
Vladimir Marko19a4d372016-12-08 14:41:46 +0000956ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
957 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
958 if (interface == nullptr) {
959 DCHECK(!klass->IsArrayClass());
960 DCHECK(!klass->IsProxyClass());
961 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
962 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(),
963 type_idx,
964 klass.Get());
965 CHECK(interface != nullptr || self->IsExceptionPending());
966 }
967 return interface;
968}
969
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700970ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Calin Juravle52503d82015-11-11 16:58:31 +0000971 DCHECK(klass.Get() != nullptr);
972 DCHECK(!klass->IsInterface());
973 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700974 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +0000975 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700976 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -0700977 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -0700978 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +0000979 }
Calin Juravle52503d82015-11-11 16:58:31 +0000980 return common_super_class;
981}
982
Mathieu Chartierf8322842014-05-16 10:59:25 -0700983const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700984 const DexFile& dex_file = GetDexFile();
985 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200986 if (dex_class_def == nullptr) {
987 // Generated classes have no class def.
988 return nullptr;
989 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700990 return dex_file.GetSourceFile(*dex_class_def);
991}
992
993std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700994 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000995 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700996 return dex_cache->GetLocation()->ToModifiedUtf8();
997 }
998 // Arrays and proxies are generated and have no corresponding dex file location.
999 return "generated class";
1000}
1001
1002const DexFile::TypeList* Class::GetInterfaceTypeList() {
1003 const DexFile::ClassDef* class_def = GetClassDef();
1004 if (class_def == nullptr) {
1005 return nullptr;
1006 }
1007 return GetDexFile().GetInterfacesList(*class_def);
1008}
1009
Andreas Gampe542451c2016-07-26 09:02:02 -07001010void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001011 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001012 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001013 const size_t table_length = table->GetLength();
1014 SetEmbeddedVTableLength(table_length);
1015 for (size_t i = 0; i < table_length; i++) {
1016 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001017 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001018 // Keep java.lang.Object class's vtable around for since it's easier
1019 // to be reused by array classes during their linking.
1020 if (!IsObjectClass()) {
1021 SetVTable(nullptr);
1022 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001023}
1024
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001025class ReadBarrierOnNativeRootsVisitor {
1026 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001027 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001028 MemberOffset offset ATTRIBUTE_UNUSED,
1029 bool is_static ATTRIBUTE_UNUSED) const {}
1030
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001031 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001032 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001033 if (!root->IsNull()) {
1034 VisitRoot(root);
1035 }
1036 }
1037
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001038 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001039 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001040 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1041 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001042 if (old_ref != new_ref) {
1043 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1044 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001045 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001046 atomic_root->CompareExchangeStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001047 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1048 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001049 }
1050 }
1051};
1052
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001053// The pre-fence visitor for Class::CopyOf().
1054class CopyClassVisitor {
1055 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001056 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001057 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001058 size_t new_length,
1059 size_t copy_bytes,
1060 ImTable* imt,
1061 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001062 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001063 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001064 }
1065
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001066 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001067 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001068 StackHandleScope<1> hs(self_);
1069 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001070 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
1071 Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001072 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1073 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001074 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001075 // Visit all of the references to make sure there is no from space references in the native
1076 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001077 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001078 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001079 }
1080
1081 private:
1082 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001083 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001084 const size_t new_length_;
1085 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001086 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001087 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001088 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1089};
1090
Andreas Gampe542451c2016-07-26 09:02:02 -07001091Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001092 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1093 // We may get copied by a compacting GC.
1094 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001095 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001096 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001097 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1098 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001099 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001100 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001101 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1102 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001103 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001104 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001105 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001106 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001107 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001108}
1109
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001110bool Class::ProxyDescriptorEquals(const char* match) {
1111 DCHECK(IsProxyClass());
1112 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001113}
1114
Mathieu Chartiere401d142015-04-22 13:56:20 -07001115// TODO: Move this to java_lang_Class.cc?
1116ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001117 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001118 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001119 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001120 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001121 continue;
1122 }
1123 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001124 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001125 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001126 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001127 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001128 return nullptr;
1129 }
1130 }
1131 return nullptr;
1132}
1133
Mathieu Chartiere401d142015-04-22 13:56:20 -07001134uint32_t Class::Depth() {
1135 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001136 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001137 depth++;
1138 }
1139 return depth;
1140}
1141
Andreas Gampea5b09a62016-11-17 15:21:22 -08001142dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001143 std::string temp;
1144 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampea5b09a62016-11-17 15:21:22 -08001145 return (type_id == nullptr)
1146 ? dex::TypeIndex(DexFile::kDexNoIndex)
1147 : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001148}
1149
Andreas Gampe542451c2016-07-26 09:02:02 -07001150template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001151ObjPtr<Method> Class::GetDeclaredMethodInternal(
1152 Thread* self,
1153 ObjPtr<Class> klass,
1154 ObjPtr<String> name,
1155 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001156 // Covariant return types permit the class to define multiple
1157 // methods with the same name and parameter types. Prefer to
1158 // return a non-synthetic method in such situations. We may
1159 // still return a synthetic method to handle situations like
1160 // escalated visibility. We never return miranda methods that
1161 // were synthesized by the runtime.
1162 constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
1163 StackHandleScope<3> hs(self);
1164 auto h_method_name = hs.NewHandle(name);
1165 if (UNLIKELY(h_method_name.Get() == nullptr)) {
1166 ThrowNullPointerException("name == null");
1167 return nullptr;
1168 }
1169 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001170 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001171 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001172 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1173 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001174 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001175 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001176 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1177 if (UNLIKELY(self->IsExceptionPending())) {
1178 return nullptr;
1179 }
1180 continue;
1181 }
1182 auto modifiers = m.GetAccessFlags();
1183 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001184 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001185 }
1186 if ((modifiers & kAccMiranda) == 0) {
1187 result = &m; // Remember as potential result if it's not a miranda method.
1188 }
1189 }
1190 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001191 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001192 auto modifiers = m.GetAccessFlags();
1193 if ((modifiers & kAccConstructor) != 0) {
1194 continue;
1195 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001196 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001197 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001198 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001199 if (np_name == nullptr) {
1200 self->AssertPendingException();
1201 return nullptr;
1202 }
1203 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1204 if (UNLIKELY(self->IsExceptionPending())) {
1205 return nullptr;
1206 }
1207 continue;
1208 }
1209 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001210 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001211 }
1212 // Direct methods cannot be miranda methods, so this potential result must be synthetic.
1213 result = &m;
1214 }
1215 }
1216 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001217 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001218 : nullptr;
1219}
1220
1221template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001222ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001223 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001224 ObjPtr<Class> klass,
1225 ObjPtr<String> name,
1226 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001227template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001228ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001229 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001230 ObjPtr<Class> klass,
1231 ObjPtr<String> name,
1232 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001233template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001234ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001235 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001236 ObjPtr<Class> klass,
1237 ObjPtr<String> name,
1238 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001239template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001240ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001241 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001242 ObjPtr<Class> klass,
1243 ObjPtr<String> name,
1244 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001245
Andreas Gampe542451c2016-07-26 09:02:02 -07001246template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001247ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001248 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001249 ObjPtr<Class> klass,
1250 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001251 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001252 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001253 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001254 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001255 : nullptr;
1256}
1257
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001258// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001259
Andreas Gampe542451c2016-07-26 09:02:02 -07001260template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001261ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001262 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001263 ObjPtr<Class> klass,
1264 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001265template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001266ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001267 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001268 ObjPtr<Class> klass,
1269 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001270template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001271ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001272 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001273 ObjPtr<Class> klass,
1274 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001275template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001276ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001277 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001278 ObjPtr<Class> klass,
1279 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001280
Andreas Gampe715fdc22016-04-18 17:07:30 -07001281int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1282 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1283 return default_value;
1284 }
1285 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001286 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001287 return default_value;
1288 }
1289 return flags;
1290}
1291
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001292void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1293 if (Runtime::Current()->IsActiveTransaction()) {
1294 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1295 } else {
1296 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1297 }
1298}
1299
David Sehr709b0702016-10-13 09:12:37 -07001300std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1301 if (klass == nullptr) {
1302 return "null";
1303 }
1304 return klass->PrettyDescriptor();
1305}
1306
1307std::string Class::PrettyDescriptor() {
1308 std::string temp;
1309 return art::PrettyDescriptor(GetDescriptor(&temp));
1310}
1311
1312std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1313 if (c == nullptr) {
1314 return "null";
1315 }
1316 return c->PrettyClass();
1317}
1318
1319std::string Class::PrettyClass() {
1320 std::string result;
1321 result += "java.lang.Class<";
1322 result += PrettyDescriptor();
1323 result += ">";
1324 return result;
1325}
1326
1327std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1328 if (c == nullptr) {
1329 return "null";
1330 }
1331 return c->PrettyClassAndClassLoader();
1332}
1333
1334std::string Class::PrettyClassAndClassLoader() {
1335 std::string result;
1336 result += "java.lang.Class<";
1337 result += PrettyDescriptor();
1338 result += ",";
1339 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1340 // TODO: add an identifying hash value for the loader
1341 result += ">";
1342 return result;
1343}
1344
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001345} // namespace mirror
1346} // namespace art