blob: c979c281383c63aa70ec1e6fac33fdbcffc37648 [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"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/accounting/card_table-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027#include "handle_scope-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070028#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070029#include "object_array-inl.h"
30#include "object-inl.h"
31#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "thread.h"
33#include "throwable.h"
34#include "utils.h"
35#include "well_known_classes.h"
36
37namespace art {
38namespace mirror {
39
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070040GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
42void Class::SetClassClass(Class* java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070043 CHECK(java_lang_Class_.IsNull())
44 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070045 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070046 CHECK(java_lang_Class != nullptr);
Mathieu Chartier66c2d2d2015-08-25 14:32:32 -070047 java_lang_Class->SetClassFlags(mirror::kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070048 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049}
50
51void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070052 CHECK(!java_lang_Class_.IsNull());
53 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054}
55
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070056void Class::VisitRoots(RootVisitor* visitor) {
57 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080058}
59
Andreas Gampe99babb62015-11-02 16:20:00 -080060inline void Class::SetVerifyError(mirror::Object* error) {
61 CHECK(error != nullptr) << PrettyClass(this);
62 if (Runtime::Current()->IsActiveTransaction()) {
63 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error);
64 } else {
65 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error);
66 }
67}
68
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070069void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
70 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
72 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070074 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
75 new_status != kStatusRetired)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070076 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
77 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070078 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070079 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
80 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070081 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070082 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070083 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070084 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 }
Ian Rogers98379392014-02-24 16:53:16 -080086 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070087 CHECK_NE(h_this->GetStatus(), kStatusError)
88 << "Attempt to set as erroneous an already erroneous class "
89 << PrettyClass(h_this.Get());
Andreas Gampe31decb12015-08-24 21:09:05 -070090 if (VLOG_IS_ON(class_linker)) {
91 LOG(ERROR) << "Setting " << PrettyDescriptor(h_this.Get()) << " to erroneous.";
92 if (self->IsExceptionPending()) {
93 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
94 }
95 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096
Andreas Gampecb086952015-11-02 16:20:00 -080097 // Remember the current exception.
98 CHECK(self->GetException() != nullptr);
99 h_this->SetVerifyError(self->GetException());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800101 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100102 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700103 h_this->SetField32Volatile<true>(StatusOffset(), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104 } else {
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700105 h_this->SetField32Volatile<false>(StatusOffset(), new_status);
106 }
107
108 // Setting the object size alloc fast path needs to be after the status write so that if the
109 // alloc path sees a valid object size, we would know that it's initialized as long as it has a
110 // load-acquire/fake dependency.
111 if (new_status == kStatusInitialized && !h_this->IsVariableSize()) {
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700112 DCHECK_EQ(h_this->GetObjectSizeAllocFastPath(), std::numeric_limits<uint32_t>::max());
113 // Finalizable objects must always go slow path.
114 if (!h_this->IsFinalizable()) {
115 h_this->SetObjectSizeAllocFastPath(RoundUp(h_this->GetObjectSize(), kObjectAlignment));
Mathieu Chartier93bbee02016-08-31 09:38:40 -0700116 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118
119 if (!class_linker_initialized) {
120 // When the class linker is being initialized its single threaded and by definition there can be
121 // no waiters. During initialization classes may appear temporary but won't be retired as their
122 // size was statically computed.
123 } else {
124 // Classes that are being resolved or initialized need to notify waiters that the class status
125 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700126 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
128 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700129 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700130 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700131 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700132 }
133 } else {
134 CHECK_NE(new_status, kStatusRetired);
135 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700136 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 }
138 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700139 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140}
141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700143 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800144 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145}
146
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 if (kIsDebugBuild && new_class_size < GetClassSize()) {
149 DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail);
Mathieu Chartier5bb4b0b2016-08-17 22:43:52 +0000150 LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize();
Mathieu Chartier161db1d2016-09-01 14:06:54 -0700151 LOG(FATAL) << "class=" << PrettyTypeOf(this);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700152 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700154 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155}
156
157// Return the class' name. The exact format is bizarre, but it's the specified behavior for
158// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
159// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
160// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161String* Class::ComputeName(Handle<Class> h_this) {
162 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800163 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 return name;
165 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700166 std::string temp;
167 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800168 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
170 // The descriptor indicates that this is the class for
171 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700172 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 switch (descriptor[0]) {
174 case 'Z': c_name = "boolean"; break;
175 case 'B': c_name = "byte"; break;
176 case 'C': c_name = "char"; break;
177 case 'S': c_name = "short"; break;
178 case 'I': c_name = "int"; break;
179 case 'J': c_name = "long"; break;
180 case 'F': c_name = "float"; break;
181 case 'D': c_name = "double"; break;
182 case 'V': c_name = "void"; break;
183 default:
184 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
185 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800186 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 } else {
188 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
189 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700190 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700192 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 return name;
194}
195
Ian Rogersef7d42f2014-01-06 12:55:46 -0800196void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 if ((flags & kDumpClassFullDetail) == 0) {
198 os << PrettyClass(this);
199 if ((flags & kDumpClassClassLoader) != 0) {
200 os << ' ' << GetClassLoader();
201 }
202 if ((flags & kDumpClassInitialized) != 0) {
203 os << ' ' << GetStatus();
204 }
205 os << "\n";
206 return;
207 }
208
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700210 StackHandleScope<2> hs(self);
211 Handle<mirror::Class> h_this(hs.NewHandle(this));
212 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700219 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 os << StringPrintf(" access=0x%04x.%04x\n",
221 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700222 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700223 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
224 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 }
226 if (IsArrayClass()) {
227 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
228 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 const size_t num_direct_interfaces = NumDirectInterfaces();
230 if (num_direct_interfaces > 0) {
231 os << " interfaces (" << num_direct_interfaces << "):\n";
232 for (size_t i = 0; i < num_direct_interfaces; ++i) {
233 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700234 if (interface == nullptr) {
235 os << StringPrintf(" %2zd: nullptr!\n", i);
236 } else {
237 const ClassLoader* cl = interface->GetClassLoader();
238 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
239 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 }
241 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 if (!IsLoaded()) {
243 os << " class not yet loaded";
244 } else {
245 // After this point, this may have moved due to GetDirectInterface.
246 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
247 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
248 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
250 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
253 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
255 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256 }
257 if (h_this->NumStaticFields() > 0) {
258 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
259 if (h_this->IsResolved() || h_this->IsErroneous()) {
260 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
261 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
262 }
263 } else {
264 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700266 }
267 if (h_this->NumInstanceFields() > 0) {
268 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
269 if (h_this->IsResolved() || h_this->IsErroneous()) {
270 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
271 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
272 }
273 } else {
274 os << " <not yet available>";
275 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 }
277 }
278}
279
280void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 // Sanity check that the number of bits set in the reference offset bitmap
283 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700284 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700285 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 count += c->NumReferenceInstanceFieldsDuringLinking();
287 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700288 // +1 for the Class in Object.
289 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100291 // Not called within a transaction.
292 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700293 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294}
295
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
297 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700298 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
299 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 ++i;
301 }
302 if (descriptor1.find('/', i) != StringPiece::npos ||
303 descriptor2.find('/', i) != StringPiece::npos) {
304 return false;
305 } else {
306 return true;
307 }
308}
309
Ian Rogersef7d42f2014-01-06 12:55:46 -0800310bool Class::IsInSamePackage(Class* that) {
311 Class* klass1 = this;
312 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 if (klass1 == klass2) {
314 return true;
315 }
316 // Class loaders must match.
317 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
318 return false;
319 }
320 // Arrays are in the same package when their element classes are.
321 while (klass1->IsArrayClass()) {
322 klass1 = klass1->GetComponentType();
323 }
324 while (klass2->IsArrayClass()) {
325 klass2 = klass2->GetComponentType();
326 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700327 // trivial check again for array types
328 if (klass1 == klass2) {
329 return true;
330 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700332 std::string temp1, temp2;
333 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334}
335
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
338}
339
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700342 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700344 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100345 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346}
347
Andreas Gampe542451c2016-07-26 09:02:02 -0700348ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
349 const StringPiece& signature,
350 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700353 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 return method;
355 }
356
357 int32_t iftable_count = GetIfTableCount();
358 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700359 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700361 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 return method;
363 }
364 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700365 return nullptr;
366}
367
Andreas Gampe542451c2016-07-26 09:02:02 -0700368ArtMethod* Class::FindInterfaceMethod(const StringPiece& name,
369 const Signature& signature,
370 PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700371 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700373 if (method != nullptr) {
374 return method;
375 }
376
377 int32_t iftable_count = GetIfTableCount();
378 IfTable* iftable = GetIfTable();
379 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700380 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700381 if (method != nullptr) {
382 return method;
383 }
384 }
385 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386}
387
Andreas Gampe542451c2016-07-26 09:02:02 -0700388ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache,
389 uint32_t dex_method_idx,
390 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700393 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 return method;
395 }
396
397 int32_t iftable_count = GetIfTableCount();
398 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700399 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
401 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700402 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 return method;
404 }
405 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700406 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407}
408
Andreas Gampe542451c2016-07-26 09:02:02 -0700409ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
410 const StringPiece& signature,
411 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700412 for (auto& method : GetDirectMethods(pointer_size)) {
413 if (name == method.GetName() && method.GetSignature() == signature) {
414 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700415 }
416 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700417 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700418}
419
Andreas Gampe542451c2016-07-26 09:02:02 -0700420ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name,
421 const Signature& signature,
422 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423 for (auto& method : GetDirectMethods(pointer_size)) {
424 if (name == method.GetName() && signature == method.GetSignature()) {
425 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 }
427 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700428 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429}
430
Andreas Gampe542451c2016-07-26 09:02:02 -0700431ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache,
432 uint32_t dex_method_idx,
433 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700435 for (auto& method : GetDirectMethods(pointer_size)) {
436 if (method.GetDexMethodIndex() == dex_method_idx) {
437 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
Andreas Gampe542451c2016-07-26 09:02:02 -0700444ArtMethod* Class::FindDirectMethod(const StringPiece& name,
445 const StringPiece& signature,
446 PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700449 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 return method;
451 }
452 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700453 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454}
455
Andreas Gampe542451c2016-07-26 09:02:02 -0700456ArtMethod* Class::FindDirectMethod(const StringPiece& name,
457 const Signature& signature,
458 PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700459 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700461 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700462 return method;
463 }
464 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700465 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700466}
467
Mathieu Chartiere401d142015-04-22 13:56:20 -0700468ArtMethod* Class::FindDirectMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -0700469 const DexCache* dex_cache, uint32_t dex_method_idx, PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700470 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700472 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800473 return method;
474 }
475 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700476 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477}
478
Andreas Gampe542451c2016-07-26 09:02:02 -0700479ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name,
480 PointerSize pointer_size) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +0000481 for (auto& method : GetDirectMethods(pointer_size)) {
482 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
483 if (name == np_method->GetName()) {
484 return &method;
485 }
486 }
487 return nullptr;
488}
489
Alex Lighte64300b2015-12-15 15:02:47 -0800490// TODO These should maybe be changed to be named FindOwnedVirtualMethod or something similar
491// because they do not only find 'declared' methods and will return copied methods. This behavior is
492// desired and correct but the naming can lead to confusion because in the java language declared
493// excludes interface methods which might be found by this.
Andreas Gampe542451c2016-07-26 09:02:02 -0700494ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
495 const StringPiece& signature,
496 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700498 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
499 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700500 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700501 }
502 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700503 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700504}
505
Andreas Gampe542451c2016-07-26 09:02:02 -0700506ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
507 const Signature& signature,
508 PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700509 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700510 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
511 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700512 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 }
514 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700515 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516}
517
Andreas Gampe542451c2016-07-26 09:02:02 -0700518ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache,
519 uint32_t dex_method_idx,
520 PointerSize pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800521 if (GetDexCache() == dex_cache) {
Alex Lighte64300b2015-12-15 15:02:47 -0800522 for (auto& method : GetDeclaredVirtualMethods(pointer_size)) {
523 if (method.GetDexMethodIndex() == dex_method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700524 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525 }
526 }
527 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700528 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800529}
530
Andreas Gampe542451c2016-07-26 09:02:02 -0700531ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name,
532 PointerSize pointer_size) {
Jeff Hao13e748b2015-08-25 20:44:19 +0000533 for (auto& method : GetVirtualMethods(pointer_size)) {
534 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
535 if (name == np_method->GetName()) {
536 return &method;
537 }
538 }
539 return nullptr;
540}
541
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542ArtMethod* Class::FindVirtualMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -0700543 const StringPiece& name, const StringPiece& signature, PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700544 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700545 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700546 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 return method;
548 }
549 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700550 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551}
552
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553ArtMethod* Class::FindVirtualMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -0700554 const StringPiece& name, const Signature& signature, PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700555 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700556 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700557 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700558 return method;
559 }
560 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700561 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700562}
563
Mathieu Chartiere401d142015-04-22 13:56:20 -0700564ArtMethod* Class::FindVirtualMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -0700565 const DexCache* dex_cache, uint32_t dex_method_idx, PointerSize pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700566 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700567 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700568 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 return method;
570 }
571 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700572 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573}
574
Andreas Gampe542451c2016-07-26 09:02:02 -0700575ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, PointerSize pointer_size) {
Alex Light705ad492015-09-21 11:36:30 -0700576 DCHECK(method->GetDeclaringClass()->IsInterface());
577 DCHECK(IsInterface()) << "Should only be called on a interface class";
578 // Check if we have one defined on this interface first. This includes searching copied ones to
579 // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We
580 // don't do any indirect method checks here.
581 for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) {
582 if (method->HasSameNameAndSignature(&iface_method)) {
583 return &iface_method;
584 }
585 }
586
587 std::vector<ArtMethod*> abstract_methods;
588 // Search through the IFTable for a working version. We don't need to check for conflicts
589 // because if there was one it would appear in this classes virtual_methods_ above.
590
591 Thread* self = Thread::Current();
592 StackHandleScope<2> hs(self);
593 MutableHandle<mirror::IfTable> iftable(hs.NewHandle(GetIfTable()));
594 MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr));
595 size_t iftable_count = GetIfTableCount();
596 // Find the method. We don't need to check for conflicts because they would have been in the
597 // copied virtuals of this interface. Order matters, traverse in reverse topological order; most
598 // subtypiest interfaces get visited first.
599 for (size_t k = iftable_count; k != 0;) {
600 k--;
601 DCHECK_LT(k, iftable->Count());
602 iface.Assign(iftable->GetInterface(k));
603 // Iterate through every declared method on this interface. Each direct method's name/signature
604 // is unique so the order of the inner loop doesn't matter.
605 for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) {
606 ArtMethod* current_method = &method_iter;
607 if (current_method->HasSameNameAndSignature(method)) {
608 if (current_method->IsDefault()) {
609 // Handle JLS soft errors, a default method from another superinterface tree can
610 // "override" an abstract method(s) from another superinterface tree(s). To do this,
611 // ignore any [default] method which are dominated by the abstract methods we've seen so
612 // far. Check if overridden by any in abstract_methods. We do not need to check for
613 // default_conflicts because we would hit those before we get to this loop.
614 bool overridden = false;
615 for (ArtMethod* possible_override : abstract_methods) {
616 DCHECK(possible_override->HasSameNameAndSignature(current_method));
617 if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) {
618 overridden = true;
619 break;
620 }
621 }
622 if (!overridden) {
623 return current_method;
624 }
625 } else {
626 // Is not default.
627 // This might override another default method. Just stash it for now.
628 abstract_methods.push_back(current_method);
629 }
630 }
631 }
632 }
633 // If we reach here we either never found any declaration of the method (in which case
634 // 'abstract_methods' is empty or we found no non-overriden default methods in which case
635 // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one
636 // of these arbitrarily.
637 return abstract_methods.empty() ? nullptr : abstract_methods[0];
638}
639
Andreas Gampe542451c2016-07-26 09:02:02 -0700640ArtMethod* Class::FindClassInitializer(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
642 if (method.IsClassInitializer()) {
643 DCHECK_STREQ(method.GetName(), "<clinit>");
644 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
645 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700646 }
647 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700648 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700649}
650
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700651// Custom binary search to avoid double comparisons from std::binary_search.
652static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
653 const StringPiece& name,
654 const StringPiece& type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700655 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700656 if (fields == nullptr) {
657 return nullptr;
658 }
659 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100660 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700661 ArtField* ret = nullptr;
662 while (low < high) {
663 size_t mid = (low + high) / 2;
664 ArtField& field = fields->At(mid);
665 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
666 // verifier. There can be multiple fields with the same in the same class name due to proguard.
667 int result = StringPiece(field.GetName()).Compare(name);
668 if (result == 0) {
669 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
670 }
671 if (result < 0) {
672 low = mid + 1;
673 } else if (result > 0) {
674 high = mid;
675 } else {
676 ret = &field;
677 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800678 }
679 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700680 if (kIsDebugBuild) {
681 ArtField* found = nullptr;
682 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
683 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
684 found = &field;
685 break;
686 }
687 }
688 CHECK_EQ(found, ret) << "Found " << PrettyField(found) << " vs " << PrettyField(ret);
689 }
690 return ret;
691}
692
693ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
694 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
695 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800696}
697
Brian Carlstromea46f952013-07-30 01:26:50 -0700698ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800699 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700700 for (ArtField& field : GetIFields()) {
701 if (field.GetDexFieldIndex() == dex_field_idx) {
702 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800703 }
704 }
705 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700706 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800707}
708
Brian Carlstromea46f952013-07-30 01:26:50 -0700709ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800710 // Is the field in this class, or any of its superclasses?
711 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700712 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700713 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700714 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800715 return f;
716 }
717 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700718 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800719}
720
Brian Carlstromea46f952013-07-30 01:26:50 -0700721ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800722 // Is the field in this class, or any of its superclasses?
723 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700724 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700725 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700726 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800727 return f;
728 }
729 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700730 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800731}
732
Brian Carlstromea46f952013-07-30 01:26:50 -0700733ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700734 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700735 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800736}
737
Brian Carlstromea46f952013-07-30 01:26:50 -0700738ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800739 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700740 for (ArtField& field : GetSFields()) {
741 if (field.GetDexFieldIndex() == dex_field_idx) {
742 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800743 }
744 }
745 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700746 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800747}
748
Mathieu Chartierf8322842014-05-16 10:59:25 -0700749ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
750 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800751 // Is the field in this class (or its interfaces), or any of its
752 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700753 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800754 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700755 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700756 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800757 return f;
758 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700759 // Wrap k incase it moves during GetDirectInterface.
760 StackHandleScope<1> hs(self);
761 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800762 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700763 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800764 StackHandleScope<1> hs2(self);
765 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700766 f = FindStaticField(self, interface, name, type);
767 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800768 return f;
769 }
770 }
771 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700772 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800773}
774
Vladimir Markobb268b12016-06-30 15:52:56 +0100775ArtField* Class::FindStaticField(Thread* self,
776 Class* klass,
777 const DexCache* dex_cache,
Mathieu Chartierf8322842014-05-16 10:59:25 -0700778 uint32_t dex_field_idx) {
Vladimir Markobb268b12016-06-30 15:52:56 +0100779 for (Class* k = klass; k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800780 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700781 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700782 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800783 return f;
784 }
Vladimir Markobb268b12016-06-30 15:52:56 +0100785 // Though GetDirectInterface() should not cause thread suspension when called
786 // from here, it takes a Handle as an argument, so we need to wrap `k`.
787 ScopedAssertNoThreadSuspension ants(self, __FUNCTION__);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700788 StackHandleScope<1> hs(self);
Vladimir Markobb268b12016-06-30 15:52:56 +0100789 Handle<mirror::Class> h_k(hs.NewHandle(k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800790 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700791 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Vladimir Markobb268b12016-06-30 15:52:56 +0100792 mirror::Class* interface = GetDirectInterface(self, h_k, i);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700793 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
794 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800795 return f;
796 }
797 }
798 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700799 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800800}
801
Mathieu Chartierf8322842014-05-16 10:59:25 -0700802ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
803 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800804 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700805 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800806 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700807 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700808 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800809 return f;
810 }
811 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700812 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800813 return f;
814 }
815 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700816 StackHandleScope<1> hs(self);
817 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
818 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800819 StackHandleScope<1> hs2(self);
820 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700821 f = interface->FindStaticField(self, interface, name, type);
822 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800823 return f;
824 }
825 }
826 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700827 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800828}
829
Andreas Gampe542451c2016-07-26 09:02:02 -0700830void Class::SetSkipAccessChecksFlagOnAllMethods(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700831 DCHECK(IsVerified());
Alex Lighte64300b2015-12-15 15:02:47 -0800832 for (auto& m : GetMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700833 if (!m.IsNative() && m.IsInvokable()) {
Igor Murashkindf707e42016-02-02 16:56:50 -0800834 m.SetSkipAccessChecks();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700835 }
836 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200837}
838
Ian Rogers1ff3c982014-08-12 02:30:58 -0700839const char* Class::GetDescriptor(std::string* storage) {
840 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700841 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700842 } else if (IsArrayClass()) {
843 return GetArrayDescriptor(storage);
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000844 } else if (IsProxyClass()) {
845 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700846 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700847 } else {
848 const DexFile& dex_file = GetDexFile();
849 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
850 return dex_file.GetTypeDescriptor(type_id);
851 }
852}
853
Ian Rogers1ff3c982014-08-12 02:30:58 -0700854const char* Class::GetArrayDescriptor(std::string* storage) {
855 std::string temp;
856 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
857 *storage = "[";
858 *storage += elem_desc;
859 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700860}
861
862const DexFile::ClassDef* Class::GetClassDef() {
863 uint16_t class_def_idx = GetDexClassDefIndex();
864 if (class_def_idx == DexFile::kDexNoIndex16) {
865 return nullptr;
866 }
867 return &GetDexFile().GetClassDef(class_def_idx);
868}
869
Mathieu Chartierf8322842014-05-16 10:59:25 -0700870uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
871 DCHECK(!IsPrimitive());
872 DCHECK(!IsArrayClass());
873 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
874}
875
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700876mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700877 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700878 DCHECK(klass.Get() != nullptr);
879 DCHECK(!klass->IsPrimitive());
880 if (klass->IsArrayClass()) {
881 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
882 if (idx == 0) {
883 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
884 } else {
885 DCHECK_EQ(1U, idx);
886 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
887 }
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000888 } else if (klass->IsProxyClass()) {
889 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700890 DCHECK(interfaces != nullptr);
891 return interfaces->Get(idx);
892 } else {
893 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
894 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
895 if (interface == nullptr) {
896 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
897 klass.Get());
898 CHECK(interface != nullptr || self->IsExceptionPending());
899 }
900 return interface;
901 }
902}
903
Calin Juravle52503d82015-11-11 16:58:31 +0000904mirror::Class* Class::GetCommonSuperClass(Handle<Class> klass) {
905 DCHECK(klass.Get() != nullptr);
906 DCHECK(!klass->IsInterface());
907 DCHECK(!IsInterface());
908 mirror::Class* common_super_class = this;
909 while (!common_super_class->IsAssignableFrom(klass.Get())) {
Aart Bik22deed02016-04-04 14:19:01 -0700910 mirror::Class* old_common = common_super_class;
911 common_super_class = old_common->GetSuperClass();
912 DCHECK(common_super_class != nullptr) << PrettyClass(old_common);
Calin Juravle52503d82015-11-11 16:58:31 +0000913 }
Calin Juravle52503d82015-11-11 16:58:31 +0000914 return common_super_class;
915}
916
Mathieu Chartierf8322842014-05-16 10:59:25 -0700917const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700918 const DexFile& dex_file = GetDexFile();
919 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200920 if (dex_class_def == nullptr) {
921 // Generated classes have no class def.
922 return nullptr;
923 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700924 return dex_file.GetSourceFile(*dex_class_def);
925}
926
927std::string Class::GetLocation() {
928 mirror::DexCache* dex_cache = GetDexCache();
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000929 if (dex_cache != nullptr && !IsProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700930 return dex_cache->GetLocation()->ToModifiedUtf8();
931 }
932 // Arrays and proxies are generated and have no corresponding dex file location.
933 return "generated class";
934}
935
936const DexFile::TypeList* Class::GetInterfaceTypeList() {
937 const DexFile::ClassDef* class_def = GetClassDef();
938 if (class_def == nullptr) {
939 return nullptr;
940 }
941 return GetDexFile().GetInterfacesList(*class_def);
942}
943
Andreas Gampe542451c2016-07-26 09:02:02 -0700944void Class::PopulateEmbeddedVTable(PointerSize pointer_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700945 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700946 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700947 const size_t table_length = table->GetLength();
948 SetEmbeddedVTableLength(table_length);
949 for (size_t i = 0; i < table_length; i++) {
950 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700951 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700952 // Keep java.lang.Object class's vtable around for since it's easier
953 // to be reused by array classes during their linking.
954 if (!IsObjectClass()) {
955 SetVTable(nullptr);
956 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700957}
958
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700959class ReadBarrierOnNativeRootsVisitor {
960 public:
961 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
962 MemberOffset offset ATTRIBUTE_UNUSED,
963 bool is_static ATTRIBUTE_UNUSED) const {}
964
965 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700966 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700967 if (!root->IsNull()) {
968 VisitRoot(root);
969 }
970 }
971
972 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700973 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700974 mirror::Object* old_ref = root->AsMirrorPtr();
975 mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root);
976 if (old_ref != new_ref) {
977 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
978 auto* atomic_root =
979 reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
980 atomic_root->CompareExchangeStrongSequentiallyConsistent(
981 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref),
982 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref));
983 }
984 }
985};
986
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700987// The pre-fence visitor for Class::CopyOf().
988class CopyClassVisitor {
989 public:
Andreas Gampe542451c2016-07-26 09:02:02 -0700990 CopyClassVisitor(Thread* self,
991 Handle<mirror::Class>* orig,
992 size_t new_length,
993 size_t copy_bytes,
994 ImTable* imt,
995 PointerSize pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700996 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700997 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700998 }
999
Mathieu Chartiere401d142015-04-22 13:56:20 -07001000 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001001 REQUIRES_SHARED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001002 StackHandleScope<1> hs(self_);
1003 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
1004 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
1005 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001006 h_new_class_obj->PopulateEmbeddedVTable(pointer_size_);
1007 h_new_class_obj->SetImt(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -07001008 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -07001009 // Visit all of the references to make sure there is no from space references in the native
1010 // roots.
Mathieu Chartier059ef3d2015-08-18 13:54:21 -07001011 static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences(
1012 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001013 }
1014
1015 private:
1016 Thread* const self_;
1017 Handle<mirror::Class>* const orig_;
1018 const size_t new_length_;
1019 const size_t copy_bytes_;
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00001020 ImTable* imt_;
Andreas Gampe542451c2016-07-26 09:02:02 -07001021 const PointerSize pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001022 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
1023};
1024
Andreas Gampe542451c2016-07-26 09:02:02 -07001025Class* Class::CopyOf(Thread* self, int32_t new_length, ImTable* imt, PointerSize pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001026 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
1027 // We may get copied by a compacting GC.
1028 StackHandleScope<1> hs(self);
1029 Handle<mirror::Class> h_this(hs.NewHandle(this));
1030 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001031 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
1032 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001033 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
1034 mirror::Object* new_class = kMovingClasses ?
1035 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
1036 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001037 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001038 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -07001039 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001040 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -07001041 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -07001042}
1043
Nicolas Geoffray3a090922015-11-24 09:17:30 +00001044bool Class::ProxyDescriptorEquals(const char* match) {
1045 DCHECK(IsProxyClass());
1046 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +01001047}
1048
Mathieu Chartiere401d142015-04-22 13:56:20 -07001049// TODO: Move this to java_lang_Class.cc?
1050ArtMethod* Class::GetDeclaredConstructor(
Andreas Gampe542451c2016-07-26 09:02:02 -07001051 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args, PointerSize pointer_size) {
Andreas Gampe6039e562016-04-05 18:18:43 -07001052 for (auto& m : GetDirectMethods(pointer_size)) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001053 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001054 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001055 continue;
1056 }
1057 // May cause thread suspension and exceptions.
Andreas Gampe542451c2016-07-26 09:02:02 -07001058 if (m.GetInterfaceMethodIfProxy(kRuntimePointerSize)->EqualParameters(args)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001059 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001060 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07001061 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -07001062 return nullptr;
1063 }
1064 }
1065 return nullptr;
1066}
1067
Mathieu Chartiere401d142015-04-22 13:56:20 -07001068uint32_t Class::Depth() {
1069 uint32_t depth = 0;
1070 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
1071 depth++;
1072 }
1073 return depth;
1074}
1075
Nicolas Geoffraye4084a52016-02-18 14:43:42 +00001076uint32_t Class::FindTypeIndexInOtherDexFile(const DexFile& dex_file) {
1077 std::string temp;
1078 const DexFile::TypeId* type_id = dex_file.FindTypeId(GetDescriptor(&temp));
1079 return (type_id == nullptr) ? DexFile::kDexNoIndex : dex_file.GetIndexForTypeId(*type_id);
1080}
1081
Andreas Gampe542451c2016-07-26 09:02:02 -07001082template <PointerSize kPointerSize, bool kTransactionActive>
Andreas Gampebc4d2182016-02-22 10:03:12 -08001083mirror::Method* Class::GetDeclaredMethodInternal(Thread* self,
1084 mirror::Class* klass,
1085 mirror::String* name,
1086 mirror::ObjectArray<mirror::Class>* args) {
1087 // Covariant return types permit the class to define multiple
1088 // methods with the same name and parameter types. Prefer to
1089 // return a non-synthetic method in such situations. We may
1090 // still return a synthetic method to handle situations like
1091 // escalated visibility. We never return miranda methods that
1092 // were synthesized by the runtime.
1093 constexpr uint32_t kSkipModifiers = kAccMiranda | kAccSynthetic;
1094 StackHandleScope<3> hs(self);
1095 auto h_method_name = hs.NewHandle(name);
1096 if (UNLIKELY(h_method_name.Get() == nullptr)) {
1097 ThrowNullPointerException("name == null");
1098 return nullptr;
1099 }
1100 auto h_args = hs.NewHandle(args);
1101 Handle<mirror::Class> h_klass = hs.NewHandle(klass);
1102 ArtMethod* result = nullptr;
Andreas Gampee01e3642016-07-25 13:06:04 -07001103 for (auto& m : h_klass->GetDeclaredVirtualMethods(kPointerSize)) {
1104 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001105 // May cause thread suspension.
1106 mirror::String* np_name = np_method->GetNameAsString(self);
1107 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1108 if (UNLIKELY(self->IsExceptionPending())) {
1109 return nullptr;
1110 }
1111 continue;
1112 }
1113 auto modifiers = m.GetAccessFlags();
1114 if ((modifiers & kSkipModifiers) == 0) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001115 return mirror::Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001116 }
1117 if ((modifiers & kAccMiranda) == 0) {
1118 result = &m; // Remember as potential result if it's not a miranda method.
1119 }
1120 }
1121 if (result == nullptr) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001122 for (auto& m : h_klass->GetDirectMethods(kPointerSize)) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001123 auto modifiers = m.GetAccessFlags();
1124 if ((modifiers & kAccConstructor) != 0) {
1125 continue;
1126 }
Andreas Gampee01e3642016-07-25 13:06:04 -07001127 auto* np_method = m.GetInterfaceMethodIfProxy(kPointerSize);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001128 // May cause thread suspension.
1129 mirror::String* np_name = np_method->GetNameAsString(self);
1130 if (np_name == nullptr) {
1131 self->AssertPendingException();
1132 return nullptr;
1133 }
1134 if (!np_name->Equals(h_method_name.Get()) || !np_method->EqualParameters(h_args)) {
1135 if (UNLIKELY(self->IsExceptionPending())) {
1136 return nullptr;
1137 }
1138 continue;
1139 }
1140 if ((modifiers & kSkipModifiers) == 0) {
Andreas Gampee01e3642016-07-25 13:06:04 -07001141 return mirror::Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, &m);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001142 }
1143 // Direct methods cannot be miranda methods, so this potential result must be synthetic.
1144 result = &m;
1145 }
1146 }
1147 return result != nullptr
Andreas Gampee01e3642016-07-25 13:06:04 -07001148 ? mirror::Method::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampebc4d2182016-02-22 10:03:12 -08001149 : nullptr;
1150}
1151
1152template
Andreas Gampe542451c2016-07-26 09:02:02 -07001153mirror::Method* Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001154 Thread* self,
1155 mirror::Class* klass,
1156 mirror::String* name,
1157 mirror::ObjectArray<mirror::Class>* args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001158template
Andreas Gampe542451c2016-07-26 09:02:02 -07001159mirror::Method* Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001160 Thread* self,
1161 mirror::Class* klass,
1162 mirror::String* name,
1163 mirror::ObjectArray<mirror::Class>* args);
1164template
Andreas Gampe542451c2016-07-26 09:02:02 -07001165mirror::Method* Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001166 Thread* self,
1167 mirror::Class* klass,
1168 mirror::String* name,
1169 mirror::ObjectArray<mirror::Class>* args);
1170template
Andreas Gampe542451c2016-07-26 09:02:02 -07001171mirror::Method* Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001172 Thread* self,
1173 mirror::Class* klass,
1174 mirror::String* name,
1175 mirror::ObjectArray<mirror::Class>* args);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001176
Andreas Gampe542451c2016-07-26 09:02:02 -07001177template <PointerSize kPointerSize, bool kTransactionActive>
Andreas Gampe6039e562016-04-05 18:18:43 -07001178mirror::Constructor* Class::GetDeclaredConstructorInternal(
1179 Thread* self,
1180 mirror::Class* klass,
1181 mirror::ObjectArray<mirror::Class>* args) {
1182 StackHandleScope<1> hs(self);
Andreas Gampee01e3642016-07-25 13:06:04 -07001183 ArtMethod* result = klass->GetDeclaredConstructor(self, hs.NewHandle(args), kPointerSize);
Andreas Gampe6039e562016-04-05 18:18:43 -07001184 return result != nullptr
Andreas Gampee01e3642016-07-25 13:06:04 -07001185 ? mirror::Constructor::CreateFromArtMethod<kPointerSize, kTransactionActive>(self, result)
Andreas Gampe6039e562016-04-05 18:18:43 -07001186 : nullptr;
1187}
1188
1189// mirror::Constructor::CreateFromArtMethod<kTransactionActive>(self, result)
1190
Andreas Gampe542451c2016-07-26 09:02:02 -07001191template
1192mirror::Constructor* Class::GetDeclaredConstructorInternal<PointerSize::k32, false>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001193 Thread* self,
1194 mirror::Class* klass,
1195 mirror::ObjectArray<mirror::Class>* args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001196template
1197mirror::Constructor* Class::GetDeclaredConstructorInternal<PointerSize::k32, true>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001198 Thread* self,
1199 mirror::Class* klass,
1200 mirror::ObjectArray<mirror::Class>* args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001201template
1202mirror::Constructor* Class::GetDeclaredConstructorInternal<PointerSize::k64, false>(
Andreas Gampee01e3642016-07-25 13:06:04 -07001203 Thread* self,
1204 mirror::Class* klass,
1205 mirror::ObjectArray<mirror::Class>* args);
Andreas Gampe542451c2016-07-26 09:02:02 -07001206template
1207mirror::Constructor* Class::GetDeclaredConstructorInternal<PointerSize::k64, true>(
Andreas Gampe6039e562016-04-05 18:18:43 -07001208 Thread* self,
1209 mirror::Class* klass,
1210 mirror::ObjectArray<mirror::Class>* args);
1211
Andreas Gampe715fdc22016-04-18 17:07:30 -07001212int32_t Class::GetInnerClassFlags(Handle<Class> h_this, int32_t default_value) {
1213 if (h_this->IsProxyClass() || h_this->GetDexCache() == nullptr) {
1214 return default_value;
1215 }
1216 uint32_t flags;
1217 if (!h_this->GetDexFile().GetInnerClassFlags(h_this, &flags)) {
1218 return default_value;
1219 }
1220 return flags;
1221}
1222
Mathieu Chartier93bbee02016-08-31 09:38:40 -07001223void Class::SetObjectSizeAllocFastPath(uint32_t new_object_size) {
1224 if (Runtime::Current()->IsActiveTransaction()) {
1225 SetField32Volatile<true>(ObjectSizeAllocFastPathOffset(), new_object_size);
1226 } else {
1227 SetField32Volatile<false>(ObjectSizeAllocFastPathOffset(), new_object_size);
1228 }
1229}
1230
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001231} // namespace mirror
1232} // namespace art