blob: 6a357b3595f9b34ccafcf4bff29241efa1a5eb69 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "class.h"
18
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_field-inl.h"
20#include "art_method-inl.h"
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "class_loader.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070023#include "class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
David Sehr9323e6e2016-09-13 08:58:35 -070026#include "dex_file_annotations.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070027#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070028#include "handle_scope-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070029#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070030#include "object_array-inl.h"
31#include "object-inl.h"
32#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "thread.h"
34#include "throwable.h"
35#include "utils.h"
36#include "well_known_classes.h"
37
38namespace art {
39namespace mirror {
40
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070041GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070043void Class::SetClassClass(ObjPtr<Class> java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070044 CHECK(java_lang_Class_.IsNull())
45 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070046 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070047 CHECK(java_lang_Class != nullptr);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070048 java_lang_Class->SetClassFlags(kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070049 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080050}
51
52void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070053 CHECK(!java_lang_Class_.IsNull());
54 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080055}
56
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070057void Class::VisitRoots(RootVisitor* visitor) {
58 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080059}
60
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070061inline void Class::SetVerifyError(ObjPtr<Object> error) {
David Sehr709b0702016-10-13 09:12:37 -070062 CHECK(error != nullptr) << PrettyClass();
Andreas Gampe99babb62015-11-02 16:20:00 -080063 if (Runtime::Current()->IsActiveTransaction()) {
64 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error);
65 } else {
66 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error);
67 }
68}
69
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070070void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
71 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
73 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070074 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070075 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
76 new_status != kStatusRetired)) {
David Sehr709b0702016-10-13 09:12:37 -070077 LOG(FATAL) << "Unexpected change back of class status for " << h_this->PrettyClass()
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070078 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070079 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070080 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
81 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070082 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070083 << "Attempt to change status of class while not holding its lock: "
David Sehr709b0702016-10-13 09:12:37 -070084 << h_this->PrettyClass() << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070085 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 }
Ian Rogers98379392014-02-24 16:53:16 -080087 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070088 CHECK_NE(h_this->GetStatus(), kStatusError)
89 << "Attempt to set as erroneous an already erroneous class "
David Sehr709b0702016-10-13 09:12:37 -070090 << h_this->PrettyClass();
Andreas Gampe31decb12015-08-24 21:09:05 -070091 if (VLOG_IS_ON(class_linker)) {
David Sehr709b0702016-10-13 09:12:37 -070092 LOG(ERROR) << "Setting " << h_this->PrettyDescriptor() << " to erroneous.";
Andreas Gampe31decb12015-08-24 21:09:05 -070093 if (self->IsExceptionPending()) {
94 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
95 }
96 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097
Andreas Gampecb086952015-11-02 16:20:00 -080098 // Remember the current exception.
99 CHECK(self->GetException() != nullptr);
100 h_this->SetVerifyError(self->GetException());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800102 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100103 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700104 h_this->SetField32Volatile<true>(StatusOffset(), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100105 } else {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700106 h_this->SetField32Volatile<false>(StatusOffset(), new_status);
107 }
108
109 // Setting the object size alloc fast path needs to be after the status write so that if the
110 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
111 // load-acquire/fake dependency.
112 if (new_status == kStatusInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700113 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
114 // Finalizable objects must always go slow path.
115 if (!h_this->IsFinalizable()) {
116 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700117 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700119
120 if (!class_linker_initialized) {
121 // When the class linker is being initialized its single threaded and by definition there can be
122 // no waiters. During initialization classes may appear temporary but won't be retired as their
123 // size was statically computed.
124 } else {
125 // Classes that are being resolved or initialized need to notify waiters that the class status
126 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700127 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700128 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
129 // so that they can grab the new version of the class from the class linker's table.
David Sehr709b0702016-10-13 09:12:37 -0700130 CHECK_LT(new_status, kStatusResolved) << h_this->PrettyDescriptor();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700131 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700132 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700133 }
134 } else {
135 CHECK_NE(new_status, kStatusRetired);
136 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700137 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700138 }
139 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700140 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141}
142
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700143void Class::SetDexCache(ObjPtr<DexCache> new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700144 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800145 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146}
147
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149 if (kIsDebugBuild && new_class_size < GetClassSize()) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700150 DumpClass(LOG_STREAM(FATAL_WITHOUT_ABORT), kDumpClassFullDetail);
151 LOG(FATAL_WITHOUT_ABORT) << new_class_size << " vs " << GetClassSize();
David Sehr709b0702016-10-13 09:12:37 -0700152 LOG(FATAL) << "class=" << PrettyTypeOf();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700153 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700155 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156}
157
158// Return the class' name. The exact format is bizarre, but it's the specified behavior for
159// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
160// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
161// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700162String* Class::ComputeName(Handle<Class> h_this) {
163 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800164 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 return name;
166 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700167 std::string temp;
168 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800169 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
171 // The descriptor indicates that this is the class for
172 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700173 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 switch (descriptor[0]) {
175 case 'Z': c_name = "boolean"; break;
176 case 'B': c_name = "byte"; break;
177 case 'C': c_name = "char"; break;
178 case 'S': c_name = "short"; break;
179 case 'I': c_name = "int"; break;
180 case 'J': c_name = "long"; break;
181 case 'F': c_name = "float"; break;
182 case 'D': c_name = "double"; break;
183 case 'V': c_name = "void"; break;
184 default:
185 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
186 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800187 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 } else {
189 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
190 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700191 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700193 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 return name;
195}
196
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 if ((flags & kDumpClassFullDetail) == 0) {
David Sehr709b0702016-10-13 09:12:37 -0700199 os << PrettyClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 if ((flags & kDumpClassClassLoader) != 0) {
201 os << ' ' << GetClassLoader();
202 }
203 if ((flags & kDumpClassInitialized) != 0) {
204 os << ' ' << GetStatus();
205 }
206 os << "\n";
207 return;
208 }
209
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700211 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700212 Handle<Class> h_this(hs.NewHandle(this));
213 Handle<Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700215
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700218 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700220 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 os << StringPrintf(" access=0x%04x.%04x\n",
222 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700223 if (h_super.Get() != nullptr) {
David Sehr709b0702016-10-13 09:12:37 -0700224 os << " super='" << h_super->PrettyClass() << "' (cl=" << h_super->GetClassLoader()
Mathieu Chartierf8322842014-05-16 10:59:25 -0700225 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 }
227 if (IsArrayClass()) {
228 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
229 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700230 const size_t num_direct_interfaces = NumDirectInterfaces();
231 if (num_direct_interfaces > 0) {
232 os << " interfaces (" << num_direct_interfaces << "):\n";
233 for (size_t i = 0; i < num_direct_interfaces; ++i) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700234 ObjPtr<Class> interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700235 if (interface == nullptr) {
236 os << StringPrintf(" %2zd: nullptr!\n", i);
237 } else {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700238 ObjPtr<ClassLoader> cl = interface->GetClassLoader();
239 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl.Ptr());
Andreas Gampe16f149c2015-03-23 10:10:20 -0700240 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 }
242 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700243 if (!IsLoaded()) {
244 os << " class not yet loaded";
245 } else {
246 // After this point, this may have moved due to GetDirectInterface.
247 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
248 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
249 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700250 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700251 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
254 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700255 os << StringPrintf(" %2zd: %s\n", i, ArtMethod::PrettyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700257 }
258 if (h_this->NumStaticFields() > 0) {
259 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
260 if (h_this->IsResolved() || h_this->IsErroneous()) {
261 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700262 os << StringPrintf(" %2zd: %s\n", i,
263 ArtField::PrettyField(h_this->GetStaticField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700264 }
265 } else {
266 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700268 }
269 if (h_this->NumInstanceFields() > 0) {
270 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
271 if (h_this->IsResolved() || h_this->IsErroneous()) {
272 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
David Sehr709b0702016-10-13 09:12:37 -0700273 os << StringPrintf(" %2zd: %s\n", i,
274 ArtField::PrettyField(h_this->GetInstanceField(i)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700275 }
276 } else {
277 os << " <not yet available>";
278 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 }
280 }
281}
282
283void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 // Sanity check that the number of bits set in the reference offset bitmap
286 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700287 uint32_t count = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700288 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 count += c->NumReferenceInstanceFieldsDuringLinking();
290 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700291 // +1 for the Class in Object.
292 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100294 // Not called within a transaction.
295 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700296 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297}
298
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
300 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700301 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
302 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 ++i;
304 }
305 if (descriptor1.find('/', i) != StringPiece::npos ||
306 descriptor2.find('/', i) != StringPiece::npos) {
307 return false;
308 } else {
309 return true;
310 }
311}
312
Mathieu Chartier3398c782016-09-30 10:27:43 -0700313bool Class::IsInSamePackage(ObjPtr<Class> that) {
314 ObjPtr<Class> klass1 = this;
315 ObjPtr<Class> klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 if (klass1 == klass2) {
317 return true;
318 }
319 // Class loaders must match.
320 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
321 return false;
322 }
323 // Arrays are in the same package when their element classes are.
324 while (klass1->IsArrayClass()) {
325 klass1 = klass1->GetComponentType();
326 }
327 while (klass2->IsArrayClass()) {
328 klass2 = klass2->GetComponentType();
329 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700330 // trivial check again for array types
331 if (klass1 == klass2) {
332 return true;
333 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700335 std::string temp1, temp2;
336 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337}
338
Ian Rogersef7d42f2014-01-06 12:55:46 -0800339bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
341}
342
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700343void Class::SetClassLoader(ObjPtr<ClassLoader> new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100344 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700345 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700347 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349}
350
Andreas Gampe542451c2016-07-26 09:02:02 -0700351ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
352 const StringPiece& signature,
353 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700356 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 return method;
358 }
359
360 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700361 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700362 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700363 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700364 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 return method;
366 }
367 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700368 return nullptr;
369}
370
Andreas Gampe542451c2016-07-26 09:02:02 -0700371ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
372 const Signature& signature,
373 PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700374 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700375 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700376 if (method != nullptr) {
377 return method;
378 }
379
380 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700381 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700382 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700383 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700384 if (method != nullptr) {
385 return method;
386 }
387 }
388 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389}
390
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700391ArtMethod* Class::FindInterfaceMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700392 uint32_t dex_method_idx,
393 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700396 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 return method;
398 }
399
400 int32_t iftable_count = GetIfTableCount();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700401 ObjPtr<IfTable> iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700402 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
404 dex_cache, dex_method_idx, 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 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700409 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410}
411
Andreas Gampe542451c2016-07-26 09:02:02 -0700412ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
413 const StringPiece& signature,
414 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 for (auto& method : GetDirectMethods(pointer_size)) {
416 if (name == method.GetName() && method.GetSignature() == signature) {
417 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700418 }
419 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700420 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700421}
422
Andreas Gampe542451c2016-07-26 09:02:02 -0700423ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
424 const Signature& signature,
425 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426 for (auto& method : GetDirectMethods(pointer_size)) {
427 if (name == method.GetName() && signature == method.GetSignature()) {
428 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 }
430 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700431 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432}
433
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700434ArtMethod* Class::FindDeclaredDirectMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700435 uint32_t dex_method_idx,
436 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438 for (auto& method : GetDirectMethods(pointer_size)) {
439 if (method.GetDexMethodIndex() == dex_method_idx) {
440 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 }
442 }
443 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700444 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445}
446
Andreas Gampe542451c2016-07-26 09:02:02 -0700447ArtMethod* Class::FindDirectMethod(const StringPiece& name,
448 const StringPiece& signature,
449 PointerSize pointer_size) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700450 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 return method;
454 }
455 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700456 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457}
458
Andreas Gampe542451c2016-07-26 09:02:02 -0700459ArtMethod* Class::FindDirectMethod(const StringPiece& name,
460 const Signature& signature,
461 PointerSize pointer_size) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700462 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700465 return method;
466 }
467 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700469}
470
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700471ArtMethod* Class::FindDirectMethod(ObjPtr<DexCache> dex_cache,
472 uint32_t dex_method_idx,
473 PointerSize pointer_size) {
474 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700475 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700476 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477 return method;
478 }
479 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700480 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481}
482
Andreas Gampe542451c2016-07-26 09:02:02 -0700483ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
484 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000485 for (auto& method : GetDirectMethods(pointer_size)) {
486 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
487 if (name == np_method->GetName()) {
488 return &method;
489 }
490 }
491 return nullptr;
492}
493
Alex Lighte64300b2015-12-15 15:02:47 -0800494// TODO These should maybe be changed to be named FindOwnedVirtualMethod or something similar
495// because they do not only find 'declared' methods and will return copied methods. This behavior is
496// desired and correct but the naming can lead to confusion because in the java language declared
497// excludes interface methods which might be found by this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700498ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
499 const StringPiece& signature,
500 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700501 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700502 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
503 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700504 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700505 }
506 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700507 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700508}
509
Andreas Gampe542451c2016-07-26 09:02:02 -0700510ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
511 const Signature& signature,
512 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700513 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700514 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
515 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800517 }
518 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700519 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520}
521
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700522ArtMethod* Class::FindDeclaredVirtualMethod(ObjPtr<DexCache> dex_cache,
Andreas Gampe542451c2016-07-26 09:02:02 -0700523 uint32_t dex_method_idx,
524 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525 if (GetDexCache() == dex_cache) {
Alex Lighte64300b2015-12-15 15:02:47 -0800526 for (auto& method : GetDeclaredVirtualMethods(pointer_size)) {
527 if (method.GetDexMethodIndex() == dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700528 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800529 }
530 }
531 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700532 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533}
534
Andreas Gampe542451c2016-07-26 09:02:02 -0700535ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
536 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000537 for (auto& method : GetVirtualMethods(pointer_size)) {
538 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
539 if (name == np_method->GetName()) {
540 return &method;
541 }
542 }
543 return nullptr;
544}
545
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700546ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
547 const StringPiece& signature,
548 PointerSize pointer_size) {
549 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700550 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700551 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 return method;
553 }
554 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700555 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800556}
557
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700558ArtMethod* Class::FindVirtualMethod(const StringPiece& name,
559 const Signature& signature,
560 PointerSize pointer_size) {
561 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700562 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700563 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700564 return method;
565 }
566 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700567 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700568}
569
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700570ArtMethod* Class::FindVirtualMethod(ObjPtr<DexCache> dex_cache,
571 uint32_t dex_method_idx,
572 PointerSize pointer_size) {
573 for (ObjPtr<Class> klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700574 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700575 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576 return method;
577 }
578 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700579 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800580}
581
Andreas Gampe542451c2016-07-26 09:02:02 -0700582ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700583 DCHECK(method->GetDeclaringClass()->IsInterface());
584 DCHECK(IsInterface()) << "Should only be called on a interface class";
585 // Check if we have one defined on this interface first. This includes searching copied ones to
586 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
587 // don't do any indirect method checks here.
588 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
589 if (method->HasSameNameAndSignature(&iface_method)) {
590 return &iface_method;
591 }
592 }
593
594 std::vector<ArtMethod*> abstract_methods;
595 // Search through the IFTable for a working version. We don't need to check for conflicts
596 // because if there was one it would appear in this classes virtual_methods_ above.
597
598 Thread* self = Thread::Current();
599 StackHandleScope<2> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700600 MutableHandle<IfTable> iftable(hs.NewHandle(GetIfTable()));
601 MutableHandle<Class> iface(hs.NewHandle<Class>(nullptr));
Alex Light705ad492015-09-21 11:36:30 -0700602 size_t iftable_count = GetIfTableCount();
603 // Find the method. We don't need to check for conflicts because they would have been in the
604 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
605 // subtypiest interfaces get visited first.
606 for (size_t k = iftable_count; k != 0;) {
607 k--;
608 DCHECK_LT(k, iftable->Count());
609 iface.Assign(iftable->GetInterface(k));
610 // Iterate through every declared method on this interface. Each direct method's name/signature
611 // is unique so the order of the inner loop doesn't matter.
612 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
613 ArtMethod* current_method = &method_iter;
614 if (current_method->HasSameNameAndSignature(method)) {
615 if (current_method->IsDefault()) {
616 // Handle JLS soft errors, a default method from another superinterface tree can
617 // "override" an abstract method(s) from another superinterface tree(s). To do this,
618 // ignore any [default] method which are dominated by the abstract methods we've seen so
619 // far. Check if overridden by any in abstract_methods. We do not need to check for
620 // default_conflicts because we would hit those before we get to this loop.
621 bool overridden = false;
622 for (ArtMethod* possible_override : abstract_methods) {
623 DCHECK(possible_override->HasSameNameAndSignature(current_method));
624 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
625 overridden = true;
626 break;
627 }
628 }
629 if (!overridden) {
630 return current_method;
631 }
632 } else {
633 // Is not default.
634 // This might override another default method. Just stash it for now.
635 abstract_methods.push_back(current_method);
636 }
637 }
638 }
639 }
640 // If we reach here we either never found any declaration of the method (in which case
641 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
642 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
643 // of these arbitrarily.
644 return abstract_methods.empty() ? nullptr : abstract_methods[0];
645}
646
Andreas Gampe542451c2016-07-26 09:02:02 -0700647ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700648 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
649 if (method.IsClassInitializer()) {
650 DCHECK_STREQ(method.GetName(), "<clinit>");
651 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
652 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700653 }
654 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700655 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700656}
657
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700658// Custom binary search to avoid double comparisons from std::binary_search.
659static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
660 const StringPiece& name,
661 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700662 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700663 if (fields == nullptr) {
664 return nullptr;
665 }
666 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100667 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700668 ArtField* ret = nullptr;
669 while (low < high) {
670 size_t mid = (low + high) / 2;
671 ArtField& field = fields->At(mid);
672 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
673 // verifier. There can be multiple fields with the same in the same class name due to proguard.
674 int result = StringPiece(field.GetName()).Compare(name);
675 if (result == 0) {
676 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
677 }
678 if (result < 0) {
679 low = mid + 1;
680 } else if (result > 0) {
681 high = mid;
682 } else {
683 ret = &field;
684 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800685 }
686 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700687 if (kIsDebugBuild) {
688 ArtField* found = nullptr;
689 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
690 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
691 found = &field;
692 break;
693 }
694 }
David Sehr709b0702016-10-13 09:12:37 -0700695 CHECK_EQ(found, ret) << "Found " << found->PrettyField() << " vs " << ret->PrettyField();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700696 }
697 return ret;
698}
699
700ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
701 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
702 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800703}
704
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700705ArtField* Class::FindDeclaredInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800706 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700707 for (ArtField& field : GetIFields()) {
708 if (field.GetDexFieldIndex() == dex_field_idx) {
709 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800710 }
711 }
712 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700713 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800714}
715
Brian Carlstromea46f952013-07-30 01:26:50 -0700716ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800717 // Is the field in this class, or any of its superclasses?
718 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700719 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700720 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700721 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800722 return f;
723 }
724 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700725 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800726}
727
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700728ArtField* Class::FindInstanceField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800729 // Is the field in this class, or any of its superclasses?
730 // Interfaces are not relevant because they can't contain instance fields.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700731 for (ObjPtr<Class> c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700732 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700733 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800734 return f;
735 }
736 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700737 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800738}
739
Brian Carlstromea46f952013-07-30 01:26:50 -0700740ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700741 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700742 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800743}
744
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700745ArtField* Class::FindDeclaredStaticField(ObjPtr<DexCache> dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800746 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700747 for (ArtField& field : GetSFields()) {
748 if (field.GetDexFieldIndex() == dex_field_idx) {
749 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800750 }
751 }
752 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700753 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800754}
755
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700756ArtField* Class::FindStaticField(Thread* self,
757 Handle<Class> klass,
758 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700759 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800760 // Is the field in this class (or its interfaces), or any of its
761 // superclasses (or their interfaces)?
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700762 for (ObjPtr<Class> k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800763 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700764 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700765 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800766 return f;
767 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700768 // Wrap k incase it moves during GetDirectInterface.
769 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700770 HandleWrapperObjPtr<Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800771 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700772 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800773 StackHandleScope<1> hs2(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700774 Handle<Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700775 f = FindStaticField(self, interface, name, type);
776 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800777 return f;
778 }
779 }
780 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700781 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800782}
783
Vladimir Markobb268b12016-06-30 15:52:56 +0100784ArtField* Class::FindStaticField(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700785 ObjPtr<Class> klass,
786 ObjPtr<DexCache> dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700787 uint32_t dex_field_idx) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700788 for (ObjPtr<Class> k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800789 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700790 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700791 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800792 return f;
793 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100794 // Though GetDirectInterface() should not cause thread suspension when called
795 // from here, it takes a Handle as an argument, so we need to wrap `k`.
Mathieu Chartier268764d2016-09-13 12:09:38 -0700796 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700797 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700798 Handle<Class> h_k(hs.NewHandle(k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800799 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700800 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700801 ObjPtr<Class> interface = GetDirectInterface(self, h_k, i);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700802 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
803 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800804 return f;
805 }
806 }
807 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700808 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800809}
810
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700811ArtField* Class::FindField(Thread* self,
812 Handle<Class> klass,
813 const StringPiece& name,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700814 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800815 // Find a field using the JLS field resolution order
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700816 for (ObjPtr<Class> k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800817 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700818 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700819 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800820 return f;
821 }
822 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700823 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800824 return f;
825 }
826 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700827 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700828 HandleWrapperObjPtr<Class> h_k(hs.NewHandleWrapper(&k));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700829 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800830 StackHandleScope<1> hs2(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700831 Handle<Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700832 f = interface->FindStaticField(self, interface, name, type);
833 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800834 return f;
835 }
836 }
837 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700838 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800839}
840
Andreas Gampe542451c2016-07-26 09:02:02 -0700841void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700842 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800843 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700844 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800845 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700846 }
847 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200848}
849
Ian Rogers1ff3c982014-08-12 02:30:58 -0700850const char* Class::GetDescriptor(std::string* storage) {
851 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700852 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700853 } else if (IsArrayClass()) {
854 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000855 } else if (IsProxyClass()) {
856 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700857 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700858 } else {
859 const DexFile& dex_file = GetDexFile();
860 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
861 return dex_file.GetTypeDescriptor(type_id);
862 }
863}
864
Ian Rogers1ff3c982014-08-12 02:30:58 -0700865const char* Class::GetArrayDescriptor(std::string* storage) {
866 std::string temp;
867 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
868 *storage = "[";
869 *storage += elem_desc;
870 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700871}
872
873const DexFile::ClassDef* Class::GetClassDef() {
874 uint16_t class_def_idx = GetDexClassDefIndex();
875 if (class_def_idx == DexFile::kDexNoIndex16) {
876 return nullptr;
877 }
878 return &GetDexFile().GetClassDef(class_def_idx);
879}
880
Mathieu Chartierf8322842014-05-16 10:59:25 -0700881uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
882 DCHECK(!IsPrimitive());
883 DCHECK(!IsArrayClass());
884 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
885}
886
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700887ObjPtr<Class> Class::GetDirectInterface(Thread* self,
888 Handle<Class> klass,
889 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700890 DCHECK(klass.Get() != nullptr);
891 DCHECK(!klass->IsPrimitive());
892 if (klass->IsArrayClass()) {
893 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
894 if (idx == 0) {
895 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
896 } else {
897 DCHECK_EQ(1U, idx);
898 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
899 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000900 } else if (klass->IsProxyClass()) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700901 ObjPtr<ObjectArray<Class>> interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700902 DCHECK(interfaces != nullptr);
903 return interfaces->Get(idx);
904 } else {
905 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700906 ObjPtr<Class> interface = klass->GetDexCache()->GetResolvedType(type_idx);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700907 if (interface == nullptr) {
908 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
909 klass.Get());
910 CHECK(interface != nullptr || self->IsExceptionPending());
911 }
912 return interface;
913 }
914}
915
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700916ObjPtr<Class> Class::GetCommonSuperClass(Handle<Class> klass) {
Calin Juravle52503d82015-11-11 16:58:31 +0000917 DCHECK(klass.Get() != nullptr);
918 DCHECK(!klass->IsInterface());
919 DCHECK(!IsInterface());
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700920 ObjPtr<Class> common_super_class = this;
Calin Juravle52503d82015-11-11 16:58:31 +0000921 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700922 ObjPtr<Class> old_common = common_super_class;
Aart Bik22deed02016-04-04 14:19:01 -0700923 common_super_class = old_common->GetSuperClass();
David Sehr709b0702016-10-13 09:12:37 -0700924 DCHECK(common_super_class != nullptr) << old_common->PrettyClass();
Calin Juravle52503d82015-11-11 16:58:31 +0000925 }
Calin Juravle52503d82015-11-11 16:58:31 +0000926 return common_super_class;
927}
928
Mathieu Chartierf8322842014-05-16 10:59:25 -0700929const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700930 const DexFile& dex_file = GetDexFile();
931 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200932 if (dex_class_def == nullptr) {
933 // Generated classes have no class def.
934 return nullptr;
935 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700936 return dex_file.GetSourceFile(*dex_class_def);
937}
938
939std::string Class::GetLocation() {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700940 ObjPtr<DexCache> dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000941 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700942 return dex_cache->GetLocation()->ToModifiedUtf8();
943 }
944 // Arrays and proxies are generated and have no corresponding dex file location.
945 return "generated class";
946}
947
948const DexFile::TypeList* Class::GetInterfaceTypeList() {
949 const DexFile::ClassDef* class_def = GetClassDef();
950 if (class_def == nullptr) {
951 return nullptr;
952 }
953 return GetDexFile().GetInterfacesList(*class_def);
954}
955
Andreas Gampe542451c2016-07-26 09:02:02 -0700956void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700957 PointerArray* table = GetVTableDuringLinking();
David Sehr709b0702016-10-13 09:12:37 -0700958 CHECK(table != nullptr) << PrettyClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700959 const size_t table_length = table->GetLength();
960 SetEmbeddedVTableLength(table_length);
961 for (size_t i = 0; i < table_length; i++) {
962 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700963 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700964 // Keep java.lang.Object class's vtable around for since it's easier
965 // to be reused by array classes during their linking.
966 if (!IsObjectClass()) {
967 SetVTable(nullptr);
968 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700969}
970
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700971class ReadBarrierOnNativeRootsVisitor {
972 public:
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700973 void operator()(ObjPtr<Object> obj ATTRIBUTE_UNUSED,
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700974 MemberOffset offset ATTRIBUTE_UNUSED,
975 bool is_static ATTRIBUTE_UNUSED) const {}
976
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700977 void VisitRootIfNonNull(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700978 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700979 if (!root->IsNull()) {
980 VisitRoot(root);
981 }
982 }
983
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700984 void VisitRoot(CompressedReference<Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700985 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700986 ObjPtr<Object> old_ref = root->AsMirrorPtr();
987 ObjPtr<Object> new_ref = ReadBarrier::BarrierForRoot(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700988 if (old_ref != new_ref) {
989 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
990 auto* atomic_root =
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700991 reinterpret_cast<Atomic<CompressedReference<Object>>*>(root);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700992 atomic_root->CompareExchangeStrongSequentiallyConsistent(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700993 CompressedReference<Object>::FromMirrorPtr(old_ref.Ptr()),
994 CompressedReference<Object>::FromMirrorPtr(new_ref.Ptr()));
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700995 }
996 }
997};
998
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700999// The pre-fence visitor for Class::CopyOf().
1000class CopyClassVisitor {
1001 public:
Andreas Gampe542451c2016-07-26 09:02:02 -07001002 CopyClassVisitor(Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001003 Handle<Class>* orig,
Andreas Gampe542451c2016-07-26 09:02:02 -07001004 size_t new_length,
1005 size_t copy_bytes,
1006 ImTable* imt,
1007 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001008 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -07001009 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001010 }
1011
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001012 void operator()(ObjPtr<Object> obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001013 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001014 StackHandleScope<1> hs(self_);
1015 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001016 Object::CopyObject(h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
1017 Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001018 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1019 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001020 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001021 // Visit all of the references to make sure there is no from space references in the native
1022 // roots.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001023 ObjPtr<Object>(h_new_class_obj.Get())->VisitReferences(
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001024 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001025 }
1026
1027 private:
1028 Thread* const self_;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001029 Handle<Class>* const orig_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001030 const size_t new_length_;
1031 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001032 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001033 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001034 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1035};
1036
Andreas Gampe542451c2016-07-26 09:02:02 -07001037Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001038 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1039 // We may get copied by a compacting GC.
1040 StackHandleScope<1> hs(self);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001041 Handle<Class> h_this(hs.NewHandle(this));
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001042 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001043 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1044 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001045 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001046 ObjPtr<Object> new_class = kMovingClasses ?
Mathieu Chartiere401d142015-04-22 13:56:20 -07001047 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1048 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001049 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001050 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001051 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001052 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001053 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001054}
1055
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001056bool Class::ProxyDescriptorEquals(const char* match) {
1057 DCHECK(IsProxyClass());
1058 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001059}
1060
Mathieu Chartiere401d142015-04-22 13:56:20 -07001061// TODO: Move this to java_lang_Class.cc?
1062ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001063 Thread* self, Handle<ObjectArray<Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001064 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001065 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001066 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001067 continue;
1068 }
1069 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001070 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001071 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001072 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001073 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001074 return nullptr;
1075 }
1076 }
1077 return nullptr;
1078}
1079
Mathieu Chartiere401d142015-04-22 13:56:20 -07001080uint32_t Class::Depth() {
1081 uint32_t depth = 0;
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001082 for (ObjPtr<Class> klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001083 depth++;
1084 }
1085 return depth;
1086}
1087
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001088uint32_t Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
1089 std::string temp;
1090 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
1091 return (type_id == nullptr) ? DexFile::kDexNoIndex : dex_file.GetIndexForTypeId(*type_id);
1092}
1093
Andreas Gampe542451c2016-07-26 09:02:02 -07001094template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001095ObjPtr<Method> Class::GetDeclaredMethodInternal(
1096 Thread* self,
1097 ObjPtr<Class> klass,
1098 ObjPtr<String> name,
1099 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001100 // Covariant return types permit the class to define multiple
1101 // methods with the same name and parameter types. Prefer to
1102 // return a non-synthetic method in such situations. We may
1103 // still return a synthetic method to handle situations like
1104 // escalated visibility. We never return miranda methods that
1105 // were synthesized by the runtime.
1106 constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
1107 StackHandleScope<3> hs(self);
1108 auto h_method_name = hs.NewHandle(name);
1109 if (UNLIKELY(h_method_name.Get() == nullptr)) {
1110 ThrowNullPointerException("name == null");
1111 return nullptr;
1112 }
1113 auto h_args = hs.NewHandle(args);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001114 Handle<Class> h_klass = hs.NewHandle(klass);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001115 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001116 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1117 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001118 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001119 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001120 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1121 if (UNLIKELY(self->IsExceptionPending())) {
1122 return nullptr;
1123 }
1124 continue;
1125 }
1126 auto modifiers = m.GetAccessFlags();
1127 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001128 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001129 }
1130 if ((modifiers & kAccMiranda) == 0) {
1131 result = &m; // Remember as potential result if it's not a miranda method.
1132 }
1133 }
1134 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001135 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001136 auto modifiers = m.GetAccessFlags();
1137 if ((modifiers & kAccConstructor) != 0) {
1138 continue;
1139 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001140 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001141 // May cause thread suspension.
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001142 ObjPtr<String> np_name = np_method->GetNameAsString(self);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001143 if (np_name == nullptr) {
1144 self->AssertPendingException();
1145 return nullptr;
1146 }
1147 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1148 if (UNLIKELY(self->IsExceptionPending())) {
1149 return nullptr;
1150 }
1151 continue;
1152 }
1153 if ((modifiers & kSkipModifiers) == 0) {
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001154 return Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001155 }
1156 // Direct methods cannot be miranda methods, so this potential result must be synthetic.
1157 result = &m;
1158 }
1159 }
1160 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001161 ? Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001162 : nullptr;
1163}
1164
1165template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001166ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001167 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001168 ObjPtr<Class> klass,
1169 ObjPtr<String> name,
1170 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001171template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001172ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001173 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001174 ObjPtr<Class> klass,
1175 ObjPtr<String> name,
1176 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001177template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001178ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001179 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001180 ObjPtr<Class> klass,
1181 ObjPtr<String> name,
1182 ObjPtr<ObjectArray<Class>> args);
Andreas Gampee01e3642016-07-25 13:06:04 -07001183template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001184ObjPtr<Method> Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001185 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001186 ObjPtr<Class> klass,
1187 ObjPtr<String> name,
1188 ObjPtr<ObjectArray<Class>> args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001189
Andreas Gampe542451c2016-07-26 09:02:02 -07001190template <PointerSize kPointerSize, bool kTransactionActive>
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001191ObjPtr<Constructor> Class::GetDeclaredConstructorInternal(
Andreas Gampe6039e562016-04-05 18:18:43 -07001192 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001193 ObjPtr<Class> klass,
1194 ObjPtr<ObjectArray<Class>> args) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001195 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001196 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001197 return result != nullptr
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001198 ? Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001199 : nullptr;
1200}
1201
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001202// Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001203
Andreas Gampe542451c2016-07-26 09:02:02 -07001204template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001205ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001206 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001207 ObjPtr<Class> klass,
1208 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001209template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001210ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001211 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001212 ObjPtr<Class> klass,
1213 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001214template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001215ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001216 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001217 ObjPtr<Class> klass,
1218 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001219template
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001220ObjPtr<Constructor> Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001221 Thread* self,
Mathieu Chartier28bd2e42016-10-04 13:54:57 -07001222 ObjPtr<Class> klass,
1223 ObjPtr<ObjectArray<Class>> args);
Andreas Gampe6039e562016-04-05 18:18:43 -07001224
Andreas Gampe715fdc22016-04-18 17:07:30 -07001225int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1226 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1227 return default_value;
1228 }
1229 uint32_t flags;
David Sehr9323e6e2016-09-13 08:58:35 -07001230 if (!annotations::GetInnerClassFlags(h_this, &flags)) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001231 return default_value;
1232 }
1233 return flags;
1234}
1235
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001236void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1237 if (Runtime::Current()->IsActiveTransaction()) {
1238 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1239 } else {
1240 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1241 }
1242}
1243
David Sehr709b0702016-10-13 09:12:37 -07001244std::string Class::PrettyDescriptor(ObjPtr<mirror::Class> klass) {
1245 if (klass == nullptr) {
1246 return "null";
1247 }
1248 return klass->PrettyDescriptor();
1249}
1250
1251std::string Class::PrettyDescriptor() {
1252 std::string temp;
1253 return art::PrettyDescriptor(GetDescriptor(&temp));
1254}
1255
1256std::string Class::PrettyClass(ObjPtr<mirror::Class> c) {
1257 if (c == nullptr) {
1258 return "null";
1259 }
1260 return c->PrettyClass();
1261}
1262
1263std::string Class::PrettyClass() {
1264 std::string result;
1265 result += "java.lang.Class<";
1266 result += PrettyDescriptor();
1267 result += ">";
1268 return result;
1269}
1270
1271std::string Class::PrettyClassAndClassLoader(ObjPtr<mirror::Class> c) {
1272 if (c == nullptr) {
1273 return "null";
1274 }
1275 return c->PrettyClassAndClassLoader();
1276}
1277
1278std::string Class::PrettyClassAndClassLoader() {
1279 std::string result;
1280 result += "java.lang.Class<";
1281 result += PrettyDescriptor();
1282 result += ",";
1283 result += mirror::Object::PrettyTypeOf(GetClassLoader());
1284 // TODO: add an identifying hash value for the loader
1285 result += ">";
1286 return result;
1287}
1288
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001289} // namespace mirror
1290} // namespace art