blob: 6f70b19aef95a19ff38222f5f635909b967f65db [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"
Andreas Gampec6ea7d02017-02-01 16:46:28 -080035#include "object-refvisitor-inl.h"
Alex Lightd6251582016-10-31 11:12:30 -070036#include "object_lock.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070037#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "thread.h"
39#include "throwable.h"
40#include "utils.h"
41#include "well_known_classes.h"
42
43namespace art {
44namespace mirror {
45
Andreas Gampe46ee31b2016-12-14 10:11:49 -080046using android::base::StringPrintf;
47
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070048GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070050void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070051 CHECK(java_lang_Class_.IsNull())
52 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070053 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070054 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070055 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070056 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080057}
58
59void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070060 CHECK(!java_lang_Class_.IsNull());
61 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062}
63
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070064void Class::VisitRoots(RootVisitor* visitor) {
65 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080066}
67
Alex Light0273ad12016-11-02 11:19:31 -070068ClassExt* Class::EnsureExtDataPresent(Thread* self) {
69 ObjPtr<ClassExt> existing(GetExtData());
70 if (!existing.IsNull()) {
71 return existing.Ptr();
72 }
73 StackHandleScope<3> hs(self);
74 // Handlerize 'this' since we are allocating here.
75 Handle<Class> h_this(hs.NewHandle(this));
76 // Clear exception so we can allocate.
77 Handle<Throwable> throwable(hs.NewHandle(self->GetException()));
78 self->ClearException();
79 // Allocate the ClassExt
80 Handle<ClassExt> new_ext(hs.NewHandle(ClassExt::Alloc(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -080081 if (new_ext == nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -070082 // OOM allocating the classExt.
83 // TODO Should we restore the suppressed exception?
84 self->AssertPendingOOMException();
85 return nullptr;
Andreas Gampe99babb62015-11-02 16:20:00 -080086 } else {
Alex Light0273ad12016-11-02 11:19:31 -070087 MemberOffset ext_offset(OFFSET_OF_OBJECT_MEMBER(Class, ext_data_));
88 bool set;
89 // Set the ext_data_ field using CAS semantics.
90 if (Runtime::Current()->IsActiveTransaction()) {
91 set = h_this->CasFieldStrongSequentiallyConsistentObject<true>(ext_offset,
92 ObjPtr<ClassExt>(nullptr),
93 new_ext.Get());
94 } else {
95 set = h_this->CasFieldStrongSequentiallyConsistentObject<false>(ext_offset,
96 ObjPtr<ClassExt>(nullptr),
97 new_ext.Get());
98 }
99 ObjPtr<ClassExt> ret(set ? new_ext.Get() : h_this->GetExtData());
100 DCHECK(!set || h_this->GetExtData() == new_ext.Get());
101 CHECK(!ret.IsNull());
102 // Restore the exception if there was one.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800103 if (throwable != nullptr) {
Alex Light0273ad12016-11-02 11:19:31 -0700104 self->SetException(throwable.Get());
105 }
106 return ret.Ptr();
Andreas Gampe99babb62015-11-02 16:20:00 -0800107 }
108}
109
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700110void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
111 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700112 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
113 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700114 if (LIKELY(class_linker_initialized)) {
Vladimir Marko72ab6842017-01-20 19:32:50 +0000115 if (UNLIKELY(new_status <= old_status &&
116 new_status != kStatusErrorUnresolved &&
117 new_status != kStatusErrorResolved &&
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118 new_status != kStatusRetired)) {
David Sehr709b0702016-10-13 09:12:37 -0700119 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700120 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700121 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700122 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
123 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700124 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700125 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -0700126 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700127 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128 }
Vladimir Marko72ab6842017-01-20 19:32:50 +0000129 if (UNLIKELY(IsErroneous(new_status))) {
130 CHECK(!h_this->IsErroneous())
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700131 << "Attempt to set as erroneous an already erroneous class "
Vladimir Marko72ab6842017-01-20 19:32:50 +0000132 << h_this->PrettyClass()
133 << " old_status: " << old_status << " new_status: " << new_status;
134 CHECK_EQ(new_status == kStatusErrorResolved, old_status >= kStatusResolved);
Andreas Gampe31decb12015-08-24 21:09:05 -0700135 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -0700136 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -0700137 if (self->IsExceptionPending()) {
138 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
139 }
140 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141
Alex Light0273ad12016-11-02 11:19:31 -0700142 ObjPtr<ClassExt> ext(h_this->EnsureExtDataPresent(self));
143 if (!ext.IsNull()) {
144 self->AssertPendingException();
145 ext->SetVerifyError(self->GetException());
146 } else {
147 self->AssertPendingOOMException();
Alex Lightd6251582016-10-31 11:12:30 -0700148 }
149 self->AssertPendingException();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150 }
Alex Light0273ad12016-11-02 11:19:31 -0700151
Andreas Gampe575e78c2014-11-03 23:41:03 -0800152 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700154 h_this->SetField32Volatile<true>(StatusOffset(), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100155 } else {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700156 h_this->SetField32Volatile<false>(StatusOffset(), new_status);
157 }
158
159 // Setting the object size alloc fast path needs to be after the status write so that if the
160 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
161 // load-acquire/fake dependency.
162 if (new_status == kStatusInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700163 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
164 // Finalizable objects must always go slow path.
165 if (!h_this->IsFinalizable()) {
166 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700167 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100168 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700169
170 if (!class_linker_initialized) {
171 // When the class linker is being initialized its single threaded and by definition there can be
172 // no waiters. During initialization classes may appear temporary but won't be retired as their
173 // size was statically computed.
174 } else {
175 // Classes that are being resolved or initialized need to notify waiters that the class status
176 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700177 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700178 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
179 // so that they can grab the new version of the class from the class linker's table.
David Sehr709b0702016-10-13 09:12:37 -0700180 CHECK_LT(new_status, kStatusResolved) << h_this->PrettyDescriptor();
Vladimir Marko72ab6842017-01-20 19:32:50 +0000181 if (new_status == kStatusRetired || new_status == kStatusErrorUnresolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700182 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700183 }
184 } else {
185 CHECK_NE(new_status, kStatusRetired);
186 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700187 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700188 }
189 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700190 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191}
192
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700193void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Chang Xing6d3e7682017-07-11 10:31:29 -0700194 SetFieldObjectTransaction(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
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 }
Chang Xing6d3e7682017-07-11 10:31:29 -0700203 SetField32Transaction(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204}
205
206// Return the class' name. The exact format is bizarre, but it's the specified behavior for
207// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
208// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
209// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700210String* Class::ComputeName(Handle<Class> h_this) {
211 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800212 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 return name;
214 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 std::string temp;
216 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800217 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
219 // The descriptor indicates that this is the class for
220 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700221 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 switch (descriptor[0]) {
223 case 'Z': c_name = "boolean"; break;
224 case 'B': c_name = "byte"; break;
225 case 'C': c_name = "char"; break;
226 case 'S': c_name = "short"; break;
227 case 'I': c_name = "int"; break;
228 case 'J': c_name = "long"; break;
229 case 'F': c_name = "float"; break;
230 case 'D': c_name = "double"; break;
231 case 'V': c_name = "void"; break;
232 default:
233 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
234 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800235 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 } else {
237 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
238 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700239 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700241 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 return name;
243}
244
Ian Rogersef7d42f2014-01-06 12:55:46 -0800245void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700247 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 if ((flags & kDumpClassClassLoader) != 0) {
249 os << ' ' << GetClassLoader();
250 }
251 if ((flags & kDumpClassInitialized) != 0) {
252 os << ' ' << GetStatus();
253 }
254 os << "\n";
255 return;
256 }
257
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700259 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700260 Handle<Class> h_this(hs.NewHandle(this));
261 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700263
Ian Rogers1ff3c982014-08-12 02:30:58 -0700264 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700266 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 os << " objectSize=" << SizeOf() << " "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800268 << "(" << (h_super != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 os << StringPrintf(" access=0x%04x.%04x\n",
270 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Andreas Gampefa4333d2017-02-14 11:10:34 -0800271 if (h_super != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700272 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700273 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 }
275 if (IsArrayClass()) {
276 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
277 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700278 const size_t num_direct_interfaces = NumDirectInterfaces();
279 if (num_direct_interfaces > 0) {
280 os << " interfaces (" << num_direct_interfaces << "):\n";
281 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000282 ObjPtr<Class> interface = GetDirectInterface(self, h_this.Get(), i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700283 if (interface == nullptr) {
284 os << StringPrintf(" %2zd: nullptr!\n", i);
285 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700286 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
287 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700288 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 }
290 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700291 if (!IsLoaded()) {
292 os << " class not yet loaded";
293 } else {
294 // After this point, this may have moved due to GetDirectInterface.
295 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Andreas Gampefa4333d2017-02-14 11:10:34 -0800296 << (h_super != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700297 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700298 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700301 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
302 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700303 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700305 }
306 if (h_this->NumStaticFields() > 0) {
307 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000308 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700309 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700310 os << StringPrintf(" %2zd: %s\n", i,
311 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700312 }
313 } else {
314 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700316 }
317 if (h_this->NumInstanceFields() > 0) {
318 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
Vladimir Marko72ab6842017-01-20 19:32:50 +0000319 if (h_this->IsResolved()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700320 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700321 os << StringPrintf(" %2zd: %s\n", i,
322 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700323 }
324 } else {
325 os << " <not yet available>";
326 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 }
328 }
329}
330
331void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333 // Sanity check that the number of bits set in the reference offset bitmap
334 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700335 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700336 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 count += c->NumReferenceInstanceFieldsDuringLinking();
338 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700339 // +1 for the Class in Object.
340 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100342 // Not called within a transaction.
343 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700344 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345}
346
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
348 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700349 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
350 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 ++i;
352 }
353 if (descriptor1.find('/', i) != StringPiece::npos ||
354 descriptor2.find('/', i) != StringPiece::npos) {
355 return false;
356 } else {
357 return true;
358 }
359}
360
Mathieu Chartier3398c782016-09-30 10:27:43 -0700361bool Class::IsInSamePackage(ObjPtr<Class> that) {
362 ObjPtr<Class> klass1 = this;
363 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800364 if (klass1 == klass2) {
365 return true;
366 }
367 // Class loaders must match.
368 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
369 return false;
370 }
371 // Arrays are in the same package when their element classes are.
372 while (klass1->IsArrayClass()) {
373 klass1 = klass1->GetComponentType();
374 }
375 while (klass2->IsArrayClass()) {
376 klass2 = klass2->GetComponentType();
377 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700378 // trivial check again for array types
379 if (klass1 == klass2) {
380 return true;
381 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700383 std::string temp1, temp2;
384 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385}
386
Ian Rogersef7d42f2014-01-06 12:55:46 -0800387bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
389}
390
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700391void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100392 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700393 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100394 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700395 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100396 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397}
398
Vladimir Markoba118822017-06-12 15:41:56 +0100399template <typename SignatureType>
400static inline ArtMethod* FindInterfaceMethodWithSignature(ObjPtr<Class> klass,
401 const StringPiece& name,
402 const SignatureType& signature,
403 PointerSize pointer_size)
404 REQUIRES_SHARED(Locks::mutator_lock_) {
405 // If the current class is not an interface, skip the search of its declared methods;
406 // such lookup is used only to distinguish between IncompatibleClassChangeError and
407 // NoSuchMethodError and the caller has already tried to search methods in the class.
408 if (LIKELY(klass->IsInterface())) {
409 // Search declared methods, both direct and virtual.
410 // (This lookup is used also for invoke-static on interface classes.)
411 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
412 if (method.GetName() == name && method.GetSignature() == signature) {
413 return &method;
414 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415 }
416 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700417
Vladimir Markoba118822017-06-12 15:41:56 +0100418 // TODO: If there is a unique maximally-specific non-abstract superinterface method,
419 // we should return it, otherwise an arbitrary one can be returned.
420 ObjPtr<IfTable> iftable = klass->GetIfTable();
421 for (int32_t i = 0, iftable_count = iftable->Count(); i < iftable_count; ++i) {
422 ObjPtr<Class> iface = iftable->GetInterface(i);
423 for (ArtMethod& method : iface->GetVirtualMethodsSlice(pointer_size)) {
424 if (method.GetName() == name && method.GetSignature() == signature) {
425 return &method;
426 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700427 }
428 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429
Vladimir Markoba118822017-06-12 15:41:56 +0100430 // Then search for public non-static methods in the java.lang.Object.
431 if (LIKELY(klass->IsInterface())) {
432 ObjPtr<Class> object_class = klass->GetSuperClass();
433 DCHECK(object_class->IsObjectClass());
434 for (ArtMethod& method : object_class->GetDeclaredMethodsSlice(pointer_size)) {
435 if (method.IsPublic() && !method.IsStatic() &&
436 method.GetName() == name && method.GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700437 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438 }
439 }
440 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700441 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442}
443
Vladimir Markoba118822017-06-12 15:41:56 +0100444ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
445 const StringPiece& signature,
446 PointerSize pointer_size) {
447 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448}
449
Vladimir Markoba118822017-06-12 15:41:56 +0100450ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
451 const Signature& signature,
452 PointerSize pointer_size) {
453 return FindInterfaceMethodWithSignature(this, name, signature, pointer_size);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700454}
455
Vladimir Markoba118822017-06-12 15:41:56 +0100456ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
457 uint32_t dex_method_idx,
458 PointerSize pointer_size) {
459 // We always search by name and signature, ignoring the type index in the MethodId.
460 const DexFile& dex_file = *dex_cache->GetDexFile();
461 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
462 StringPiece name = dex_file.StringDataByIdx(method_id.name_idx_);
463 const Signature signature = dex_file.GetMethodSignature(method_id);
464 return FindInterfaceMethod(name, signature, pointer_size);
465}
466
467static inline bool IsInheritedMethod(ObjPtr<mirror::Class> klass,
468 ObjPtr<mirror::Class> declaring_class,
469 ArtMethod& method)
470 REQUIRES_SHARED(Locks::mutator_lock_) {
471 DCHECK_EQ(declaring_class, method.GetDeclaringClass());
472 DCHECK_NE(klass, declaring_class);
473 DCHECK(klass->IsArrayClass() ? declaring_class->IsObjectClass()
474 : klass->IsSubClass(declaring_class));
475 uint32_t access_flags = method.GetAccessFlags();
476 if ((access_flags & (kAccPublic | kAccProtected)) != 0) {
477 return true;
478 }
479 if ((access_flags & kAccPrivate) != 0) {
480 return false;
481 }
482 for (; klass != declaring_class; klass = klass->GetSuperClass()) {
483 if (!klass->IsInSamePackage(declaring_class)) {
484 return false;
485 }
486 }
487 return true;
488}
489
490template <typename SignatureType>
491static inline ArtMethod* FindClassMethodWithSignature(ObjPtr<Class> this_klass,
492 const StringPiece& name,
493 const SignatureType& signature,
494 PointerSize pointer_size)
495 REQUIRES_SHARED(Locks::mutator_lock_) {
496 // Search declared methods first.
497 for (ArtMethod& method : this_klass->GetDeclaredMethodsSlice(pointer_size)) {
498 ArtMethod* np_method = method.GetInterfaceMethodIfProxy(pointer_size);
499 if (np_method->GetName() == name && np_method->GetSignature() == signature) {
500 return &method;
501 }
502 }
503
504 // Then search the superclass chain. If we find an inherited method, return it.
505 // If we find a method that's not inherited because of access restrictions,
506 // try to find a method inherited from an interface in copied methods.
507 ObjPtr<Class> klass = this_klass->GetSuperClass();
508 ArtMethod* uninherited_method = nullptr;
509 for (; klass != nullptr; klass = klass->GetSuperClass()) {
510 DCHECK(!klass->IsProxyClass());
511 for (ArtMethod& method : klass->GetDeclaredMethodsSlice(pointer_size)) {
512 if (method.GetName() == name && method.GetSignature() == signature) {
513 if (IsInheritedMethod(this_klass, klass, method)) {
514 return &method;
515 }
516 uninherited_method = &method;
517 break;
518 }
519 }
520 if (uninherited_method != nullptr) {
521 break;
522 }
523 }
524
525 // Then search copied methods.
526 // If we found a method that's not inherited, stop the search in its declaring class.
527 ObjPtr<Class> end_klass = klass;
528 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
529 klass = this_klass;
530 if (UNLIKELY(klass->IsProxyClass())) {
531 DCHECK(klass->GetCopiedMethodsSlice(pointer_size).empty());
532 klass = klass->GetSuperClass();
533 }
534 for (; klass != end_klass; klass = klass->GetSuperClass()) {
535 DCHECK(!klass->IsProxyClass());
536 for (ArtMethod& method : klass->GetCopiedMethodsSlice(pointer_size)) {
537 if (method.GetName() == name && method.GetSignature() == signature) {
538 return &method; // No further check needed, copied methods are inherited by definition.
539 }
540 }
541 }
542 return uninherited_method; // Return the `uninherited_method` if any.
543}
544
545
546ArtMethod* Class::FindClassMethod(const StringPiece& name,
547 const StringPiece& signature,
548 PointerSize pointer_size) {
549 return FindClassMethodWithSignature(this, name, signature, pointer_size);
550}
551
552ArtMethod* Class::FindClassMethod(const StringPiece& name,
553 const Signature& signature,
554 PointerSize pointer_size) {
555 return FindClassMethodWithSignature(this, name, signature, pointer_size);
556}
557
558ArtMethod* Class::FindClassMethod(ObjPtr<DexCache> dex_cache,
559 uint32_t dex_method_idx,
560 PointerSize pointer_size) {
561 // FIXME: Hijacking a proxy class by a custom class loader can break this assumption.
562 DCHECK(!IsProxyClass());
563
564 // First try to find a declared method by dex_method_idx if we have a dex_cache match.
565 ObjPtr<DexCache> this_dex_cache = GetDexCache();
566 if (this_dex_cache == dex_cache) {
567 // Lookup is always performed in the class referenced by the MethodId.
568 DCHECK_EQ(dex_type_idx_, GetDexFile().GetMethodId(dex_method_idx).class_idx_.index_);
569 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
570 if (method.GetDexMethodIndex() == dex_method_idx) {
571 return &method;
572 }
573 }
574 }
575 // If not found, we need to search by name and signature.
576 const DexFile& dex_file = *dex_cache->GetDexFile();
577 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
578 const Signature signature = dex_file.GetMethodSignature(method_id);
579 StringPiece name; // Delay strlen() until actually needed.
580 // If we do not have a dex_cache match, try to find the declared method in this class now.
581 if (this_dex_cache != dex_cache && !GetDeclaredMethodsSlice(pointer_size).empty()) {
582 DCHECK(name.empty());
583 name = dex_file.StringDataByIdx(method_id.name_idx_);
584 for (ArtMethod& method : GetDeclaredMethodsSlice(pointer_size)) {
585 if (method.GetName() == name && method.GetSignature() == signature) {
586 return &method;
587 }
588 }
589 }
590
591 // Then search the superclass chain. If we find an inherited method, return it.
592 // If we find a method that's not inherited because of access restrictions,
593 // try to find a method inherited from an interface in copied methods.
594 ArtMethod* uninherited_method = nullptr;
595 ObjPtr<Class> klass = GetSuperClass();
596 for (; klass != nullptr; klass = klass->GetSuperClass()) {
597 ArtMethod* candidate_method = nullptr;
598 ArraySlice<ArtMethod> declared_methods = klass->GetDeclaredMethodsSlice(pointer_size);
599 if (klass->GetDexCache() == dex_cache) {
600 // Matching dex_cache. We cannot compare the `dex_method_idx` anymore because
601 // the type index differs, so compare the name index and proto index.
602 for (ArtMethod& method : declared_methods) {
603 const DexFile::MethodId& cmp_method_id = dex_file.GetMethodId(method.GetDexMethodIndex());
604 if (cmp_method_id.name_idx_ == method_id.name_idx_ &&
605 cmp_method_id.proto_idx_ == method_id.proto_idx_) {
606 candidate_method = &method;
607 break;
608 }
609 }
610 } else {
611 if (!declared_methods.empty() && name.empty()) {
612 name = dex_file.StringDataByIdx(method_id.name_idx_);
613 }
614 for (ArtMethod& method : declared_methods) {
615 if (method.GetName() == name && method.GetSignature() == signature) {
616 candidate_method = &method;
617 break;
618 }
619 }
620 }
621 if (candidate_method != nullptr) {
622 if (IsInheritedMethod(this, klass, *candidate_method)) {
623 return candidate_method;
624 } else {
625 uninherited_method = candidate_method;
626 break;
627 }
628 }
629 }
630
631 // Then search copied methods.
632 // If we found a method that's not inherited, stop the search in its declaring class.
633 ObjPtr<Class> end_klass = klass;
634 DCHECK_EQ(uninherited_method != nullptr, end_klass != nullptr);
635 // After we have searched the declared methods of the super-class chain,
636 // search copied methods which can contain methods from interfaces.
637 for (klass = this; klass != end_klass; klass = klass->GetSuperClass()) {
638 ArraySlice<ArtMethod> copied_methods = klass->GetCopiedMethodsSlice(pointer_size);
639 if (!copied_methods.empty() && name.empty()) {
640 name = dex_file.StringDataByIdx(method_id.name_idx_);
641 }
642 for (ArtMethod& method : copied_methods) {
643 if (method.GetName() == name && method.GetSignature() == signature) {
644 return &method; // No further check needed, copied methods are inherited by definition.
645 }
646 }
647 }
648 return uninherited_method; // Return the `uninherited_method` if any.
649}
650
651ArtMethod* Class::FindConstructor(const StringPiece& signature, PointerSize pointer_size) {
652 // Internal helper, never called on proxy classes. We can skip GetInterfaceMethodIfProxy().
653 DCHECK(!IsProxyClass());
654 StringPiece name("<init>");
655 for (ArtMethod& method : GetDirectMethodsSliceUnchecked(pointer_size)) {
656 if (method.GetName() == name && method.GetSignature() == signature) {
657 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800658 }
659 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700660 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800661}
662
Andreas Gampe542451c2016-07-26 09:02:02 -0700663ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
664 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000665 for (auto& method : GetDirectMethods(pointer_size)) {
666 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
667 if (name == np_method->GetName()) {
668 return &method;
669 }
670 }
671 return nullptr;
672}
673
Andreas Gampe542451c2016-07-26 09:02:02 -0700674ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
675 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000676 for (auto& method : GetVirtualMethods(pointer_size)) {
677 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
678 if (name == np_method->GetName()) {
679 return &method;
680 }
681 }
682 return nullptr;
683}
684
Andreas Gampe542451c2016-07-26 09:02:02 -0700685ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700686 DCHECK(method->GetDeclaringClass()->IsInterface());
687 DCHECK(IsInterface()) << "Should only be called on a interface class";
688 // Check if we have one defined on this interface first. This includes searching copied ones to
689 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
690 // don't do any indirect method checks here.
691 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
692 if (method->HasSameNameAndSignature(&iface_method)) {
693 return &iface_method;
694 }
695 }
696
697 std::vector<ArtMethod*> abstract_methods;
698 // Search through the IFTable for a working version. We don't need to check for conflicts
699 // because if there was one it would appear in this classes virtual_methods_ above.
700
701 Thread* self = Thread::Current();
702 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700703 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
704 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700705 size_t iftable_count = GetIfTableCount();
706 // Find the method. We don't need to check for conflicts because they would have been in the
707 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
708 // subtypiest interfaces get visited first.
709 for (size_t k = iftable_count; k != 0;) {
710 k--;
711 DCHECK_LT(k, iftable->Count());
712 iface.Assign(iftable->GetInterface(k));
713 // Iterate through every declared method on this interface. Each direct method's name/signature
714 // is unique so the order of the inner loop doesn't matter.
715 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
716 ArtMethod* current_method = &method_iter;
717 if (current_method->HasSameNameAndSignature(method)) {
718 if (current_method->IsDefault()) {
719 // Handle JLS soft errors, a default method from another superinterface tree can
720 // "override" an abstract method(s) from another superinterface tree(s). To do this,
721 // ignore any [default] method which are dominated by the abstract methods we've seen so
722 // far. Check if overridden by any in abstract_methods. We do not need to check for
723 // default_conflicts because we would hit those before we get to this loop.
724 bool overridden = false;
725 for (ArtMethod* possible_override : abstract_methods) {
726 DCHECK(possible_override->HasSameNameAndSignature(current_method));
727 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
728 overridden = true;
729 break;
730 }
731 }
732 if (!overridden) {
733 return current_method;
734 }
735 } else {
736 // Is not default.
737 // This might override another default method. Just stash it for now.
738 abstract_methods.push_back(current_method);
739 }
740 }
741 }
742 }
743 // If we reach here we either never found any declaration of the method (in which case
744 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
745 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
746 // of these arbitrarily.
747 return abstract_methods.empty() ? nullptr : abstract_methods[0];
748}
749
Andreas Gampe542451c2016-07-26 09:02:02 -0700750ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700751 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
752 if (method.IsClassInitializer()) {
753 DCHECK_STREQ(method.GetName(), "<clinit>");
754 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
755 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700756 }
757 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700758 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700759}
760
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700761// Custom binary search to avoid double comparisons from std::binary_search.
762static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
763 const StringPiece& name,
764 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700765 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700766 if (fields == nullptr) {
767 return nullptr;
768 }
769 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100770 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700771 ArtField* ret = nullptr;
772 while (low < high) {
773 size_t mid = (low + high) / 2;
774 ArtField& field = fields->At(mid);
775 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
776 // verifier. There can be multiple fields with the same in the same class name due to proguard.
777 int result = StringPiece(field.GetName()).Compare(name);
778 if (result == 0) {
779 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
780 }
781 if (result < 0) {
782 low = mid + 1;
783 } else if (result > 0) {
784 high = mid;
785 } else {
786 ret = &field;
787 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800788 }
789 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700790 if (kIsDebugBuild) {
791 ArtField* found = nullptr;
792 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
793 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
794 found = &field;
795 break;
796 }
797 }
David Sehr709b0702016-10-13 09:12:37 -0700798 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700799 }
800 return ret;
801}
802
803ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
804 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
805 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800806}
807
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700808ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800809 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700810 for (ArtField& field : GetIFields()) {
811 if (field.GetDexFieldIndex() == dex_field_idx) {
812 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800813 }
814 }
815 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700816 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800817}
818
Brian Carlstromea46f952013-07-30 01:26:50 -0700819ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800820 // Is the field in this class, or any of its superclasses?
821 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700822 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700823 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700824 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800825 return f;
826 }
827 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700828 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800829}
830
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700831ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800832 // Is the field in this class, or any of its superclasses?
833 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700834 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700835 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700836 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800837 return f;
838 }
839 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700840 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800841}
842
Brian Carlstromea46f952013-07-30 01:26:50 -0700843ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700844 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700845 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800846}
847
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700848ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800849 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700850 for (ArtField& field : GetSFields()) {
851 if (field.GetDexFieldIndex() == dex_field_idx) {
852 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800853 }
854 }
855 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700856 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800857}
858
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700859ArtField* Class::FindStaticField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000860 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700861 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700862 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800863 // Is the field in this class (or its interfaces), or any of its
864 // superclasses (or their interfaces)?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000865 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800866 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700867 ArtField* 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);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700875 f = FindStaticField(self, interface, name, type);
876 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
Vladimir Markobb268b12016-06-30 15:52:56 +0100884ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700885 ObjPtr<Class> klass,
886 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700887 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700888 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800889 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700890 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700891 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800892 return f;
893 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100894 // Though GetDirectInterface() should not cause thread suspension when called
895 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700896 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800897 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000898 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
899 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
900 DCHECK(interface != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700901 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
902 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800903 return f;
904 }
905 }
906 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700907 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800908}
909
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700910ArtField* Class::FindField(Thread* self,
Vladimir Marko19a4d372016-12-08 14:41:46 +0000911 ObjPtr<Class> klass,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700912 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700913 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800914 // Find a field using the JLS field resolution order
Vladimir Marko19a4d372016-12-08 14:41:46 +0000915 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800916 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700917 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700918 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800919 return f;
920 }
921 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700922 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800923 return f;
924 }
925 // Is this field in any of this class' interfaces?
Vladimir Marko19a4d372016-12-08 14:41:46 +0000926 for (uint32_t i = 0, num_interfaces = k->NumDirectInterfaces(); i != num_interfaces; ++i) {
927 ObjPtr<Class> interface = GetDirectInterface(self, k, i);
928 DCHECK(interface != nullptr);
929 f = FindStaticField(self, interface, name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700930 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800931 return f;
932 }
933 }
934 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700935 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800936}
937
Andreas Gampe542451c2016-07-26 09:02:02 -0700938void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700939 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800940 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700941 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800942 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700943 }
944 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200945}
946
Ian Rogers1ff3c982014-08-12 02:30:58 -0700947const char* Class::GetDescriptor(std::string* storage) {
948 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700949 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700950 } else if (IsArrayClass()) {
951 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000952 } else if (IsProxyClass()) {
953 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700954 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700955 } else {
956 const DexFile& dex_file = GetDexFile();
957 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
958 return dex_file.GetTypeDescriptor(type_id);
959 }
960}
961
Ian Rogers1ff3c982014-08-12 02:30:58 -0700962const char* Class::GetArrayDescriptor(std::string* storage) {
963 std::string temp;
964 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
965 *storage = "[";
966 *storage += elem_desc;
967 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700968}
969
970const DexFile::ClassDef* Class::GetClassDef() {
971 uint16_t class_def_idx = GetDexClassDefIndex();
972 if (class_def_idx == DexFile::kDexNoIndex16) {
973 return nullptr;
974 }
975 return &GetDexFile().GetClassDef(class_def_idx);
976}
977
Andreas Gampea5b09a62016-11-17 15:21:22 -0800978dex::TypeIndex Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700979 DCHECK(!IsPrimitive());
980 DCHECK(!IsArrayClass());
981 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
982}
983
Vladimir Marko19a4d372016-12-08 14:41:46 +0000984ObjPtr<Class> Class::GetDirectInterface(Thread* self, ObjPtr<Class> klass, uint32_t idx) {
985 DCHECK(klass != nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700986 DCHECK(!klass->IsPrimitive());
987 if (klass->IsArrayClass()) {
988 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Vladimir Marko19a4d372016-12-08 14:41:46 +0000989 // Use ClassLinker::LookupClass(); avoid poisoning ObjPtr<>s by ClassLinker::FindSystemClass().
990 ObjPtr<Class> interface;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700991 if (idx == 0) {
Vladimir Marko19a4d372016-12-08 14:41:46 +0000992 interface = class_linker->LookupClass(self, "Ljava/lang/Cloneable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700993 } else {
994 DCHECK_EQ(1U, idx);
Vladimir Marko19a4d372016-12-08 14:41:46 +0000995 interface = class_linker->LookupClass(self, "Ljava/io/Serializable;", nullptr);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700996 }
Vladimir Marko19a4d372016-12-08 14:41:46 +0000997 DCHECK(interface != nullptr);
998 return interface;
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000999 } else if (klass->IsProxyClass()) {
Narayan Kamath6b2dc312017-03-14 13:26:12 +00001000 ObjPtr<ObjectArray<Class>> interfaces = klass->GetProxyInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -07001001 DCHECK(interfaces != nullptr);
1002 return interfaces->Get(idx);
1003 } else {
Andreas Gampea5b09a62016-11-17 15:21:22 -08001004 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Vladimir Marko8d6768d2017-03-14 10:13:21 +00001005 ObjPtr<Class> interface = ClassLinker::LookupResolvedType(
1006 type_idx, klass->GetDexCache(), klass->GetClassLoader());
Mathieu Chartierf8322842014-05-16 10:59:25 -07001007 return interface;
1008 }
1009}
1010
Vladimir Marko19a4d372016-12-08 14:41:46 +00001011ObjPtr<Class> Class::ResolveDirectInterface(Thread* self, Handle<Class> klass, uint32_t idx) {
1012 ObjPtr<Class> interface = GetDirectInterface(self, klass.Get(), idx);
1013 if (interface == nullptr) {
1014 DCHECK(!klass->IsArrayClass());
1015 DCHECK(!klass->IsProxyClass());
1016 dex::TypeIndex type_idx = klass->GetDirectInterfaceTypeIdx(idx);
1017 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(),
1018 type_idx,
1019 klass.Get());
1020 CHECK(interface != nullptr || self->IsExceptionPending());
1021 }
1022 return interface;
1023}
1024
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001025ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Andreas Gampefa4333d2017-02-14 11:10:34 -08001026 DCHECK(klass != nullptr);
Calin Juravle52503d82015-11-11 16:58:31 +00001027 DCHECK(!klass->IsInterface());
1028 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001029 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +00001030 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001031 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -07001032 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -07001033 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +00001034 }
Calin Juravle52503d82015-11-11 16:58:31 +00001035 return common_super_class;
1036}
1037
Mathieu Chartierf8322842014-05-16 10:59:25 -07001038const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001039 const DexFile& dex_file = GetDexFile();
1040 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +02001041 if (dex_class_def == nullptr) {
1042 // Generated classes have no class def.
1043 return nullptr;
1044 }
Mathieu Chartierf8322842014-05-16 10:59:25 -07001045 return dex_file.GetSourceFile(*dex_class_def);
1046}
1047
1048std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001049 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001050 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -07001051 return dex_cache->GetLocation()->ToModifiedUtf8();
1052 }
1053 // Arrays and proxies are generated and have no corresponding dex file location.
1054 return "generated class";
1055}
1056
1057const DexFile::TypeList* Class::GetInterfaceTypeList() {
1058 const DexFile::ClassDef* class_def = GetClassDef();
1059 if (class_def == nullptr) {
1060 return nullptr;
1061 }
1062 return GetDexFile().GetInterfacesList(*class_def);
1063}
1064
Andreas Gampe542451c2016-07-26 09:02:02 -07001065void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001066 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -07001067 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001068 const size_t table_length = table->GetLength();
1069 SetEmbeddedVTableLength(table_length);
1070 for (size_t i = 0; i < table_length; i++) {
1071 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001072 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07001073 // Keep java.lang.Object class's vtable around for since it's easier
1074 // to be reused by array classes during their linking.
1075 if (!IsObjectClass()) {
1076 SetVTable(nullptr);
1077 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001078}
1079
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001080class ReadBarrierOnNativeRootsVisitor {
1081 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001082 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001083 MemberOffset offset ATTRIBUTE_UNUSED,
1084 bool is_static ATTRIBUTE_UNUSED) const {}
1085
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001086 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001087 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001088 if (!root->IsNull()) {
1089 VisitRoot(root);
1090 }
1091 }
1092
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001093 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001094 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001095 ObjPtr<Object> old_ref = root->AsMirrorPtr();
1096 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001097 if (old_ref != new_ref) {
1098 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
1099 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001100 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001101 atomic_root->CompareExchangeStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001102 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
1103 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001104 }
1105 }
1106};
1107
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001108// The pre-fence visitor for Class::CopyOf().
1109class CopyClassVisitor {
1110 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001111 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001112 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001113 size_t new_length,
1114 size_t copy_bytes,
1115 ImTable* imt,
1116 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001117 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001118 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001119 }
1120
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001121 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001122 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001123 StackHandleScope<1> hs(self_);
1124 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001125 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
1126 Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001127 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1128 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001129 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001130 // Visit all of the references to make sure there is no from space references in the native
1131 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001132 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001133 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001134 }
1135
1136 private:
1137 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001138 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001139 const size_t new_length_;
1140 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001141 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001142 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001143 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1144};
1145
Andreas Gampe542451c2016-07-26 09:02:02 -07001146Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001147 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1148 // We may get copied by a compacting GC.
1149 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001150 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001151 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001152 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1153 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001154 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001155 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001156 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1157 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001158 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001159 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001160 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001161 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001162 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001163}
1164
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001165bool Class::ProxyDescriptorEquals(const char* match) {
1166 DCHECK(IsProxyClass());
1167 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001168}
1169
Mathieu Chartiere401d142015-04-22 13:56:20 -07001170// TODO: Move this to java_lang_Class.cc?
1171ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001172 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001173 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001174 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001175 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001176 continue;
1177 }
1178 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001179 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001180 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001181 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001182 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001183 return nullptr;
1184 }
1185 }
1186 return nullptr;
1187}
1188
Mathieu Chartiere401d142015-04-22 13:56:20 -07001189uint32_t Class::Depth() {
1190 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001191 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001192 depth++;
1193 }
1194 return depth;
1195}
1196
Andreas Gampea5b09a62016-11-17 15:21:22 -08001197dex::TypeIndex Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001198 std::string temp;
1199 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
Andreas Gampe2722f382017-06-08 18:03:25 -07001200 return (type_id == nullptr) ? dex::TypeIndex() : dex_file.GetIndexForTypeId(*type_id);
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001201}
1202
Andreas Gampe542451c2016-07-26 09:02:02 -07001203template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001204ObjPtr<Method> Class::GetDeclaredMethodInternal(
1205 Thread* self,
1206 ObjPtr<Class> klass,
1207 ObjPtr<String> name,
1208 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001209 // Covariant return types permit the class to define multiple
1210 // methods with the same name and parameter types. Prefer to
1211 // return a non-synthetic method in such situations. We may
1212 // still return a synthetic method to handle situations like
1213 // escalated visibility. We never return miranda methods that
1214 // were synthesized by the runtime.
1215 constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
1216 StackHandleScope<3> hs(self);
1217 auto h_method_name = hs.NewHandle(name);
Andreas Gampefa4333d2017-02-14 11:10:34 -08001218 if (UNLIKELY(h_method_name == nullptr)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001219 ThrowNullPointerException("name == null");
1220 return nullptr;
1221 }
1222 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001223 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001224 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001225 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1226 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001227 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001228 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001229 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1230 if (UNLIKELY(self->IsExceptionPending())) {
1231 return nullptr;
1232 }
1233 continue;
1234 }
1235 auto modifiers = m.GetAccessFlags();
1236 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001237 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001238 }
1239 if ((modifiers & kAccMiranda) == 0) {
1240 result = &m; // Remember as potential result if it's not a miranda method.
1241 }
1242 }
1243 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001244 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001245 auto modifiers = m.GetAccessFlags();
1246 if ((modifiers & kAccConstructor) != 0) {
1247 continue;
1248 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001249 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001250 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001251 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001252 if (np_name == nullptr) {
1253 self->AssertPendingException();
1254 return nullptr;
1255 }
1256 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1257 if (UNLIKELY(self->IsExceptionPending())) {
1258 return nullptr;
1259 }
1260 continue;
1261 }
1262 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001263 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001264 }
1265 // Direct methods cannot be miranda methods, so this potential result must be synthetic.
1266 result = &m;
1267 }
1268 }
1269 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001270 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001271 : nullptr;
1272}
1273
1274template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001275ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001276 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001277 ObjPtr<Class> klass,
1278 ObjPtr<String> name,
1279 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001280template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001281ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001282 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001283 ObjPtr<Class> klass,
1284 ObjPtr<String> name,
1285 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001286template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001287ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001288 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001289 ObjPtr<Class> klass,
1290 ObjPtr<String> name,
1291 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001292template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001293ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001294 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001295 ObjPtr<Class> klass,
1296 ObjPtr<String> name,
1297 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001298
Andreas Gampe542451c2016-07-26 09:02:02 -07001299template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001300ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001301 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001302 ObjPtr<Class> klass,
1303 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001304 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001305 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001306 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001307 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001308 : nullptr;
1309}
1310
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001311// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001312
Andreas Gampe542451c2016-07-26 09:02:02 -07001313template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001314ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001315 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001316 ObjPtr<Class> klass,
1317 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001318template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001319ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001320 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001321 ObjPtr<Class> klass,
1322 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001323template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001324ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001325 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001326 ObjPtr<Class> klass,
1327 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001328template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001329ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001330 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001331 ObjPtr<Class> klass,
1332 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001333
Andreas Gampe715fdc22016-04-18 17:07:30 -07001334int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1335 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1336 return default_value;
1337 }
1338 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001339 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001340 return default_value;
1341 }
1342 return flags;
1343}
1344
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001345void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1346 if (Runtime::Current()->IsActiveTransaction()) {
1347 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1348 } else {
1349 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1350 }
1351}
1352
David Sehr709b0702016-10-13 09:12:37 -07001353std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1354 if (klass == nullptr) {
1355 return "null";
1356 }
1357 return klass->PrettyDescriptor();
1358}
1359
1360std::string Class::PrettyDescriptor() {
1361 std::string temp;
1362 return art::PrettyDescriptor(GetDescriptor(&temp));
1363}
1364
1365std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1366 if (c == nullptr) {
1367 return "null";
1368 }
1369 return c->PrettyClass();
1370}
1371
1372std::string Class::PrettyClass() {
1373 std::string result;
1374 result += "java.lang.Class<";
1375 result += PrettyDescriptor();
1376 result += ">";
1377 return result;
1378}
1379
1380std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1381 if (c == nullptr) {
1382 return "null";
1383 }
1384 return c->PrettyClassAndClassLoader();
1385}
1386
1387std::string Class::PrettyClassAndClassLoader() {
1388 std::string result;
1389 result += "java.lang.Class<";
1390 result += PrettyDescriptor();
1391 result += ",";
1392 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1393 // TODO: add an identifying hash value for the loader
1394 result += ">";
1395 return result;
1396}
1397
Andreas Gampe90b936d2017-01-31 08:58:55 -08001398template<VerifyObjectFlags kVerifyFlags> void Class::GetAccessFlagsDCheck() {
1399 // Check class is loaded/retired or this is java.lang.String that has a
1400 // circularity issue during loading the names of its members
1401 DCHECK(IsIdxLoaded<kVerifyFlags>() || IsRetired<kVerifyFlags>() ||
1402 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>() ||
1403 this == String::GetJavaLangString())
1404 << "IsIdxLoaded=" << IsIdxLoaded<kVerifyFlags>()
1405 << " IsRetired=" << IsRetired<kVerifyFlags>()
1406 << " IsErroneous=" <<
1407 IsErroneous<static_cast<VerifyObjectFlags>(kVerifyFlags & ~kVerifyThis)>()
1408 << " IsString=" << (this == String::GetJavaLangString())
1409 << " status= " << GetStatus<kVerifyFlags>()
1410 << " descriptor=" << PrettyDescriptor();
1411}
1412// Instantiate the common cases.
1413template void Class::GetAccessFlagsDCheck<kVerifyNone>();
1414template void Class::GetAccessFlagsDCheck<kVerifyThis>();
1415template void Class::GetAccessFlagsDCheck<kVerifyReads>();
1416template void Class::GetAccessFlagsDCheck<kVerifyWrites>();
1417template void Class::GetAccessFlagsDCheck<kVerifyAll>();
1418
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001419} // namespace mirror
1420} // namespace art