blob: 56c586a94d3401aacd30fb0d6538382e6a13eca5 [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);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070047 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048}
49
50void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070051 CHECK(!java_lang_Class_.IsNull());
52 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053}
54
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070055void Class::VisitRoots(RootVisitor* visitor) {
56 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057}
58
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070059void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
60 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070063 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070064 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
65 new_status != kStatusRetired)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070066 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
67 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070068 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070069 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
70 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070071 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070072 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070073 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070074 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 }
Ian Rogers98379392014-02-24 16:53:16 -080076 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070077 CHECK_NE(h_this->GetStatus(), kStatusError)
78 << "Attempt to set as erroneous an already erroneous class "
79 << PrettyClass(h_this.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Ian Rogers62d6c772013-02-27 08:32:07 -080081 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000082 StackHandleScope<1> hs(self);
83 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070085 Class* eiie_class;
86 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
87 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070088 bool throw_eiie = (old_exception.Get() == nullptr);
89 if (!throw_eiie) {
90 std::string temp;
91 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
92 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
93 }
94 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070095 // Clear exception to call FindSystemClass.
96 self->ClearException();
97 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
98 self, "Ljava/lang/ExceptionInInitializerError;");
99 CHECK(!self->IsExceptionPending());
100 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700101 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
102 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700103 Class* exception_class = old_exception->GetClass();
104 if (!eiie_class->IsAssignableFrom(exception_class)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700105 h_this->SetVerifyErrorClass(exception_class);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700106 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
108
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000110 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800112 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700114 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700116 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
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) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700148 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
149 DumpClass(LOG(ERROR), kDumpClassFullDetail);
150 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
151 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100152 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700153 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154}
155
156// Return the class' name. The exact format is bizarre, but it's the specified behavior for
157// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
158// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
159// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700160String* Class::ComputeName(Handle<Class> h_this) {
161 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800162 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 return name;
164 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700165 std::string temp;
166 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800167 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
169 // The descriptor indicates that this is the class for
170 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700171 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 switch (descriptor[0]) {
173 case 'Z': c_name = "boolean"; break;
174 case 'B': c_name = "byte"; break;
175 case 'C': c_name = "char"; break;
176 case 'S': c_name = "short"; break;
177 case 'I': c_name = "int"; break;
178 case 'J': c_name = "long"; break;
179 case 'F': c_name = "float"; break;
180 case 'D': c_name = "double"; break;
181 case 'V': c_name = "void"; break;
182 default:
183 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
184 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800185 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 } else {
187 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
188 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700189 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700191 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 return name;
193}
194
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 if ((flags & kDumpClassFullDetail) == 0) {
197 os << PrettyClass(this);
198 if ((flags & kDumpClassClassLoader) != 0) {
199 os << ' ' << GetClassLoader();
200 }
201 if ((flags & kDumpClassInitialized) != 0) {
202 os << ' ' << GetStatus();
203 }
204 os << "\n";
205 return;
206 }
207
Mathieu Chartierf8322842014-05-16 10:59:25 -0700208 Thread* self = Thread::Current();
209 StackHandleScope<2> hs(self);
210 Handle<mirror::Class> h_this(hs.NewHandle(this));
211 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
212
Ian Rogers1ff3c982014-08-12 02:30:58 -0700213 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700217 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 os << StringPrintf(" access=0x%04x.%04x\n",
219 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700220 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700221 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
222 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 }
224 if (IsArrayClass()) {
225 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
226 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700227 const size_t num_direct_interfaces = NumDirectInterfaces();
228 if (num_direct_interfaces > 0) {
229 os << " interfaces (" << num_direct_interfaces << "):\n";
230 for (size_t i = 0; i < num_direct_interfaces; ++i) {
231 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700232 if (interface == nullptr) {
233 os << StringPrintf(" %2zd: nullptr!\n", i);
234 } else {
235 const ClassLoader* cl = interface->GetClassLoader();
236 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
237 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 }
239 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700240 if (!IsLoaded()) {
241 os << " class not yet loaded";
242 } else {
243 // After this point, this may have moved due to GetDirectInterface.
244 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
245 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
246 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
247 os << StringPrintf(" %2zd: %s\n", i,
248 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700250 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
251 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
252 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
253 }
254 if (h_this->NumStaticFields() > 0) {
255 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
256 if (h_this->IsResolved() || h_this->IsErroneous()) {
257 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
258 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
259 }
260 } else {
261 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700263 }
264 if (h_this->NumInstanceFields() > 0) {
265 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
266 if (h_this->IsResolved() || h_this->IsErroneous()) {
267 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
268 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
269 }
270 } else {
271 os << " <not yet available>";
272 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 }
274 }
275}
276
277void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700278 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 // Sanity check that the number of bits set in the reference offset bitmap
280 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700281 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700282 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 count += c->NumReferenceInstanceFieldsDuringLinking();
284 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700285 // +1 for the Class in Object.
286 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288 // Not called within a transaction.
289 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700290 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291}
292
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
294 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700295 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
296 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 ++i;
298 }
299 if (descriptor1.find('/', i) != StringPiece::npos ||
300 descriptor2.find('/', i) != StringPiece::npos) {
301 return false;
302 } else {
303 return true;
304 }
305}
306
Ian Rogersef7d42f2014-01-06 12:55:46 -0800307bool Class::IsInSamePackage(Class* that) {
308 Class* klass1 = this;
309 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 if (klass1 == klass2) {
311 return true;
312 }
313 // Class loaders must match.
314 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
315 return false;
316 }
317 // Arrays are in the same package when their element classes are.
318 while (klass1->IsArrayClass()) {
319 klass1 = klass1->GetComponentType();
320 }
321 while (klass2->IsArrayClass()) {
322 klass2 = klass2->GetComponentType();
323 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700324 // trivial check again for array types
325 if (klass1 == klass2) {
326 return true;
327 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700329 std::string temp1, temp2;
330 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331}
332
Ian Rogersef7d42f2014-01-06 12:55:46 -0800333bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
335}
336
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100338 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700339 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100340 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700341 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100342 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343}
344
Brian Carlstrom004644f2014-06-18 08:34:01 -0700345ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700347 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700348 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 return method;
350 }
351
352 int32_t iftable_count = GetIfTableCount();
353 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700354 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700356 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 return method;
358 }
359 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700360 return nullptr;
361}
362
363ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
364 // Check the current class before checking the interfaces.
365 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
366 if (method != nullptr) {
367 return method;
368 }
369
370 int32_t iftable_count = GetIfTableCount();
371 IfTable* iftable = GetIfTable();
372 for (int32_t i = 0; i < iftable_count; ++i) {
373 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
374 if (method != nullptr) {
375 return method;
376 }
377 }
378 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800379}
380
Ian Rogersef7d42f2014-01-06 12:55:46 -0800381ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700383 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700384 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 return method;
386 }
387
388 int32_t iftable_count = GetIfTableCount();
389 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700390 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700392 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 return method;
394 }
395 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700396 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397}
398
Ian Rogersef7d42f2014-01-06 12:55:46 -0800399ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700401 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700402 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700403 return method;
404 }
405 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700406 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700407}
408
Ian Rogersef7d42f2014-01-06 12:55:46 -0800409ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700410 for (size_t i = 0; i < NumDirectMethods(); ++i) {
411 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700412 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 return method;
414 }
415 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700416 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417}
418
Ian Rogersef7d42f2014-01-06 12:55:46 -0800419ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 if (GetDexCache() == dex_cache) {
421 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700422 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 if (method->GetDexMethodIndex() == dex_method_idx) {
424 return method;
425 }
426 }
427 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700428 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429}
430
Ian Rogersef7d42f2014-01-06 12:55:46 -0800431ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700432 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700433 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700434 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435 return method;
436 }
437 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700438 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439}
440
Ian Rogersef7d42f2014-01-06 12:55:46 -0800441ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700443 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700444 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700445 return method;
446 }
447 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700448 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700449}
450
Ian Rogersef7d42f2014-01-06 12:55:46 -0800451ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700453 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455 return method;
456 }
457 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459}
460
Ian Rogersef7d42f2014-01-06 12:55:46 -0800461ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700462 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
463 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700464 if (name == method->GetName() && method->GetSignature() == signature) {
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 Chartierbfd9a432014-05-21 17:43:44 -0700471ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700473 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700474 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800475 return method;
476 }
477 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700478 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800479}
480
Ian Rogersef7d42f2014-01-06 12:55:46 -0800481ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 if (GetDexCache() == dex_cache) {
483 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700484 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstromf322c4c2014-10-31 00:01:54 -0700485 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe2b0fa5b2014-10-31 18:12:30 -0700486 // A miranda method may have a different DexCache and is always created by linking,
487 // never *declared* in the class.
488 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 return method;
490 }
491 }
492 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700493 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494}
495
Ian Rogersef7d42f2014-01-06 12:55:46 -0800496ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700497 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700498 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700499 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800500 return method;
501 }
502 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700503 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800504}
505
Ian Rogersef7d42f2014-01-06 12:55:46 -0800506ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700507 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700508 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700509 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700510 return method;
511 }
512 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700514}
515
Ian Rogersef7d42f2014-01-06 12:55:46 -0800516ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700518 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700519 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 return method;
521 }
522 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700523 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800524}
525
Ian Rogersef7d42f2014-01-06 12:55:46 -0800526ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700527 for (size_t i = 0; i < NumDirectMethods(); ++i) {
528 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700529 if (method->IsClassInitializer()) {
530 DCHECK_STREQ(method->GetName(), "<clinit>");
531 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700532 return method;
533 }
534 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700535 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700536}
537
Brian Carlstromea46f952013-07-30 01:26:50 -0700538ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539 // Is the field in this class?
540 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700542 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700543 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544 return f;
545 }
546 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700547 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548}
549
Brian Carlstromea46f952013-07-30 01:26:50 -0700550ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 if (GetDexCache() == dex_cache) {
552 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700553 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 if (f->GetDexFieldIndex() == dex_field_idx) {
555 return f;
556 }
557 }
558 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700559 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560}
561
Brian Carlstromea46f952013-07-30 01:26:50 -0700562ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800563 // Is the field in this class, or any of its superclasses?
564 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700565 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700566 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700567 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568 return f;
569 }
570 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700571 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800572}
573
Brian Carlstromea46f952013-07-30 01:26:50 -0700574ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575 // Is the field in this class, or any of its superclasses?
576 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700577 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700578 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700579 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800580 return f;
581 }
582 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700583 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584}
585
Brian Carlstromea46f952013-07-30 01:26:50 -0700586ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700587 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700589 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700590 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 return f;
592 }
593 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700594 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595}
596
Brian Carlstromea46f952013-07-30 01:26:50 -0700597ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598 if (dex_cache == GetDexCache()) {
599 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700600 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 if (f->GetDexFieldIndex() == dex_field_idx) {
602 return f;
603 }
604 }
605 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700606 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800607}
608
Mathieu Chartierf8322842014-05-16 10:59:25 -0700609ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
610 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 // Is the field in this class (or its interfaces), or any of its
612 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700613 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700615 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700616 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 return f;
618 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700619 // Wrap k incase it moves during GetDirectInterface.
620 StackHandleScope<1> hs(self);
621 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800622 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700623 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800624 StackHandleScope<1> hs2(self);
625 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700626 f = FindStaticField(self, interface, name, type);
627 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 return f;
629 }
630 }
631 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700632 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800633}
634
Mathieu Chartierf8322842014-05-16 10:59:25 -0700635ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
636 uint32_t dex_field_idx) {
637 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800638 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700639 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700640 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 return f;
642 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700643 // Wrap k incase it moves during GetDirectInterface.
644 StackHandleScope<1> hs(self);
645 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800646 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700647 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800648 StackHandleScope<1> hs2(self);
649 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700650 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
651 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 return f;
653 }
654 }
655 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700656 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657}
658
Mathieu Chartierf8322842014-05-16 10:59:25 -0700659ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
660 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800661 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700662 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800663 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700664 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700665 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800666 return f;
667 }
668 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700669 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800670 return f;
671 }
672 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700673 StackHandleScope<1> hs(self);
674 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
675 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800676 StackHandleScope<1> hs2(self);
677 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700678 f = interface->FindStaticField(self, interface, name, type);
679 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800680 return f;
681 }
682 }
683 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700684 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800685}
686
Brian Carlstromea46f952013-07-30 01:26:50 -0700687static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200688 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700689 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200690 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700691 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700692 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700693 if (!method->IsNative() && !method->IsAbstract()) {
694 method->SetPreverified();
695 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200696 }
697 }
698}
699
700void Class::SetPreverifiedFlagOnAllMethods() {
701 DCHECK(IsVerified());
702 SetPreverifiedFlagOnMethods(GetDirectMethods());
703 SetPreverifiedFlagOnMethods(GetVirtualMethods());
704}
705
Ian Rogers1ff3c982014-08-12 02:30:58 -0700706const char* Class::GetDescriptor(std::string* storage) {
707 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700708 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700709 } else if (IsArrayClass()) {
710 return GetArrayDescriptor(storage);
711 } else if (IsProxyClass()) {
712 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
713 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700714 } else {
715 const DexFile& dex_file = GetDexFile();
716 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
717 return dex_file.GetTypeDescriptor(type_id);
718 }
719}
720
Ian Rogers1ff3c982014-08-12 02:30:58 -0700721const char* Class::GetArrayDescriptor(std::string* storage) {
722 std::string temp;
723 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
724 *storage = "[";
725 *storage += elem_desc;
726 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700727}
728
729const DexFile::ClassDef* Class::GetClassDef() {
730 uint16_t class_def_idx = GetDexClassDefIndex();
731 if (class_def_idx == DexFile::kDexNoIndex16) {
732 return nullptr;
733 }
734 return &GetDexFile().GetClassDef(class_def_idx);
735}
736
Mathieu Chartierf8322842014-05-16 10:59:25 -0700737uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
738 DCHECK(!IsPrimitive());
739 DCHECK(!IsArrayClass());
740 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
741}
742
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700743mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700744 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700745 DCHECK(klass.Get() != nullptr);
746 DCHECK(!klass->IsPrimitive());
747 if (klass->IsArrayClass()) {
748 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
749 if (idx == 0) {
750 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
751 } else {
752 DCHECK_EQ(1U, idx);
753 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
754 }
755 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700756 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700757 DCHECK(interfaces != nullptr);
758 return interfaces->Get(idx);
759 } else {
760 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
761 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
762 if (interface == nullptr) {
763 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
764 klass.Get());
765 CHECK(interface != nullptr || self->IsExceptionPending());
766 }
767 return interface;
768 }
769}
770
771const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700772 const DexFile& dex_file = GetDexFile();
773 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200774 if (dex_class_def == nullptr) {
775 // Generated classes have no class def.
776 return nullptr;
777 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700778 return dex_file.GetSourceFile(*dex_class_def);
779}
780
781std::string Class::GetLocation() {
782 mirror::DexCache* dex_cache = GetDexCache();
783 if (dex_cache != nullptr && !IsProxyClass()) {
784 return dex_cache->GetLocation()->ToModifiedUtf8();
785 }
786 // Arrays and proxies are generated and have no corresponding dex file location.
787 return "generated class";
788}
789
790const DexFile::TypeList* Class::GetInterfaceTypeList() {
791 const DexFile::ClassDef* class_def = GetClassDef();
792 if (class_def == nullptr) {
793 return nullptr;
794 }
795 return GetDexFile().GetInterfacesList(*class_def);
796}
797
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700798void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
799 for (uint32_t i = 0; i < kImtSize; i++) {
800 // Replace null with conflict.
801 mirror::Object* obj = imt_handle_scope->GetReference(i);
802 DCHECK(obj != nullptr);
803 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700804 }
805
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700806 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700807 CHECK(table != nullptr) << PrettyClass(this);
808 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700809 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700810 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700811 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700812
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700813 // Keep java.lang.Object class's vtable around for since it's easier
814 // to be reused by array classes during their linking.
815 if (!IsObjectClass()) {
816 SetVTable(nullptr);
817 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700818}
819
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700820// The pre-fence visitor for Class::CopyOf().
821class CopyClassVisitor {
822 public:
823 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700824 size_t new_length, size_t copy_bytes,
825 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700826 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700827 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700828 }
829
830 void operator()(Object* obj, size_t usable_size) const
831 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
832 UNUSED(usable_size);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700833 StackHandleScope<1> hs(self_);
834 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
835 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
836 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
837 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
838 h_new_class_obj->SetClassSize(new_length_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700839 }
840
841 private:
842 Thread* const self_;
843 Handle<mirror::Class>* const orig_;
844 const size_t new_length_;
845 const size_t copy_bytes_;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700846 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700847 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
848};
849
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700850Class* Class::CopyOf(Thread* self, int32_t new_length,
851 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700852 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
853 // We may get copied by a compacting GC.
854 StackHandleScope<1> hs(self);
855 Handle<mirror::Class> h_this(hs.NewHandle(this));
856 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700857 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
858 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700859 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700860 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700861 kMovingClasses
862 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
863 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700864 if (UNLIKELY(new_class == nullptr)) {
865 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700866 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700867 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700868 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700869}
870
Vladimir Marko3481ba22015-04-13 12:22:36 +0100871bool Class::ProxyDescriptorEquals(const char* match) {
872 DCHECK(IsProxyClass());
873 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
874}
875
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700876mirror::ArtMethod* Class::GetDeclaredConstructor(
877 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
878 auto* direct_methods = GetDirectMethods();
879 size_t count = direct_methods != nullptr ? direct_methods->GetLength() : 0u;
880 for (size_t i = 0; i < count; ++i) {
881 auto* m = direct_methods->GetWithoutChecks(i);
882 // Skip <clinit> which is a static constructor, as well as non constructors.
883 if (m->IsStatic() || !m->IsConstructor()) {
884 continue;
885 }
886 // May cause thread suspension and exceptions.
887 if (m->EqualParameters(args)) {
888 return m;
889 }
890 if (self->IsExceptionPending()) {
891 return nullptr;
892 }
893 }
894 return nullptr;
895}
896
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800897} // namespace mirror
898} // namespace art