blob: 9fa60736985a8326031a2222c7df9b92fb406cde [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "class_linker.h"
22#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"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "object_array-inl.h"
29#include "object-inl.h"
30#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "thread.h"
32#include "throwable.h"
33#include "utils.h"
34#include "well_known_classes.h"
35
36namespace art {
37namespace mirror {
38
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070039GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
41void Class::SetClassClass(Class* java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070042 CHECK(java_lang_Class_.IsNull())
43 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070044 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070045 CHECK(java_lang_Class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070046 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047}
48
49void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070050 CHECK(!java_lang_Class_.IsNull());
51 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080054void Class::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080055 java_lang_Class_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080056}
57
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070058void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
59 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070062 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070063 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
64 new_status != kStatusRetired)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070065 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
66 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
69 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070070 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070072 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
Ian Rogers98379392014-02-24 16:53:16 -080075 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070076 CHECK_NE(h_this->GetStatus(), kStatusError)
77 << "Attempt to set as erroneous an already erroneous class "
78 << PrettyClass(h_this.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079
Ian Rogers62d6c772013-02-27 08:32:07 -080080 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000081 StackHandleScope<1> hs(self);
82 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070084 Class* eiie_class;
85 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
86 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070087 bool throw_eiie = (old_exception.Get() == nullptr);
88 if (!throw_eiie) {
89 std::string temp;
90 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
91 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
92 }
93 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070094 // Clear exception to call FindSystemClass.
95 self->ClearException();
96 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
97 self, "Ljava/lang/ExceptionInInitializerError;");
98 CHECK(!self->IsExceptionPending());
99 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700100 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
101 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700102 Class* exception_class = old_exception->GetClass();
103 if (!eiie_class->IsAssignableFrom(exception_class)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700104 h_this->SetVerifyErrorClass(exception_class);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700105 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 }
107
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000109 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800111 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100112 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700113 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700115 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700117
118 if (!class_linker_initialized) {
119 // When the class linker is being initialized its single threaded and by definition there can be
120 // no waiters. During initialization classes may appear temporary but won't be retired as their
121 // size was statically computed.
122 } else {
123 // Classes that are being resolved or initialized need to notify waiters that the class status
124 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700125 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
127 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700128 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700129 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700130 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700131 }
132 } else {
133 CHECK_NE(new_status, kStatusRetired);
134 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700135 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700136 }
137 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700138 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139}
140
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700142 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800143 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144}
145
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700147 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
148 DumpClass(LOG(ERROR), kDumpClassFullDetail);
149 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
150 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100151 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700152 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153}
154
155// Return the class' name. The exact format is bizarre, but it's the specified behavior for
156// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
157// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
158// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700159String* Class::ComputeName(Handle<Class> h_this) {
160 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800161 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 return name;
163 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700164 std::string temp;
165 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800166 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
168 // The descriptor indicates that this is the class for
169 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700170 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 switch (descriptor[0]) {
172 case 'Z': c_name = "boolean"; break;
173 case 'B': c_name = "byte"; break;
174 case 'C': c_name = "char"; break;
175 case 'S': c_name = "short"; break;
176 case 'I': c_name = "int"; break;
177 case 'J': c_name = "long"; break;
178 case 'F': c_name = "float"; break;
179 case 'D': c_name = "double"; break;
180 case 'V': c_name = "void"; break;
181 default:
182 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
183 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800184 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 } else {
186 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
187 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700190 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 return name;
192}
193
Ian Rogersef7d42f2014-01-06 12:55:46 -0800194void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 if ((flags & kDumpClassFullDetail) == 0) {
196 os << PrettyClass(this);
197 if ((flags & kDumpClassClassLoader) != 0) {
198 os << ' ' << GetClassLoader();
199 }
200 if ((flags & kDumpClassInitialized) != 0) {
201 os << ' ' << GetStatus();
202 }
203 os << "\n";
204 return;
205 }
206
Mathieu Chartierf8322842014-05-16 10:59:25 -0700207 Thread* self = Thread::Current();
208 StackHandleScope<2> hs(self);
209 Handle<mirror::Class> h_this(hs.NewHandle(this));
210 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
211
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700216 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 os << StringPrintf(" access=0x%04x.%04x\n",
218 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700219 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700220 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
221 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 }
223 if (IsArrayClass()) {
224 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
225 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700226 const size_t num_direct_interfaces = NumDirectInterfaces();
227 if (num_direct_interfaces > 0) {
228 os << " interfaces (" << num_direct_interfaces << "):\n";
229 for (size_t i = 0; i < num_direct_interfaces; ++i) {
230 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 const ClassLoader* cl = interface->GetClassLoader();
232 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
233 }
234 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700235 if (!IsLoaded()) {
236 os << " class not yet loaded";
237 } else {
238 // After this point, this may have moved due to GetDirectInterface.
239 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
240 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
241 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
242 os << StringPrintf(" %2zd: %s\n", i,
243 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800244 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700245 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
246 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
247 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
248 }
249 if (h_this->NumStaticFields() > 0) {
250 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
251 if (h_this->IsResolved() || h_this->IsErroneous()) {
252 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
253 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
254 }
255 } else {
256 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800257 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700258 }
259 if (h_this->NumInstanceFields() > 0) {
260 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
261 if (h_this->IsResolved() || h_this->IsErroneous()) {
262 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
263 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
264 }
265 } else {
266 os << " <not yet available>";
267 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268 }
269 }
270}
271
272void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700273 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 // Sanity check that the number of bits set in the reference offset bitmap
275 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700276 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700277 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 count += c->NumReferenceInstanceFieldsDuringLinking();
279 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700280 // +1 for the Class in Object.
281 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100283 // Not called within a transaction.
284 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700285 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286}
287
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
289 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700290 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
291 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800292 ++i;
293 }
294 if (descriptor1.find('/', i) != StringPiece::npos ||
295 descriptor2.find('/', i) != StringPiece::npos) {
296 return false;
297 } else {
298 return true;
299 }
300}
301
Ian Rogersef7d42f2014-01-06 12:55:46 -0800302bool Class::IsInSamePackage(Class* that) {
303 Class* klass1 = this;
304 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 if (klass1 == klass2) {
306 return true;
307 }
308 // Class loaders must match.
309 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
310 return false;
311 }
312 // Arrays are in the same package when their element classes are.
313 while (klass1->IsArrayClass()) {
314 klass1 = klass1->GetComponentType();
315 }
316 while (klass2->IsArrayClass()) {
317 klass2 = klass2->GetComponentType();
318 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700319 // trivial check again for array types
320 if (klass1 == klass2) {
321 return true;
322 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700324 std::string temp1, temp2;
325 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326}
327
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328bool Class::IsStringClass() const {
329 return this == String::GetJavaLangString();
330}
331
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
334}
335
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100337 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700338 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100339 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700340 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342}
343
Brian Carlstrom004644f2014-06-18 08:34:01 -0700344ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700346 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700347 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 return method;
349 }
350
351 int32_t iftable_count = GetIfTableCount();
352 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700353 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700355 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 return method;
357 }
358 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700359 return nullptr;
360}
361
362ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
363 // Check the current class before checking the interfaces.
364 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
365 if (method != nullptr) {
366 return method;
367 }
368
369 int32_t iftable_count = GetIfTableCount();
370 IfTable* iftable = GetIfTable();
371 for (int32_t i = 0; i < iftable_count; ++i) {
372 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
373 if (method != nullptr) {
374 return method;
375 }
376 }
377 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378}
379
Ian Rogersef7d42f2014-01-06 12:55:46 -0800380ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700382 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700383 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384 return method;
385 }
386
387 int32_t iftable_count = GetIfTableCount();
388 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700389 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700391 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 return method;
393 }
394 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700395 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396}
397
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700401 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700402 return method;
403 }
404 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700405 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700406}
407
Ian Rogersef7d42f2014-01-06 12:55:46 -0800408ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700409 for (size_t i = 0; i < NumDirectMethods(); ++i) {
410 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700411 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 return method;
413 }
414 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700415 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416}
417
Ian Rogersef7d42f2014-01-06 12:55:46 -0800418ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419 if (GetDexCache() == dex_cache) {
420 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700421 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 if (method->GetDexMethodIndex() == dex_method_idx) {
423 return method;
424 }
425 }
426 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700427 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428}
429
Ian Rogersef7d42f2014-01-06 12:55:46 -0800430ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700431 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700432 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700433 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 return method;
435 }
436 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700437 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438}
439
Ian Rogersef7d42f2014-01-06 12:55:46 -0800440ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700441 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700442 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700443 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700444 return method;
445 }
446 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700448}
449
Ian Rogersef7d42f2014-01-06 12:55:46 -0800450ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700452 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700453 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 return method;
455 }
456 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800458}
459
Ian Rogersef7d42f2014-01-06 12:55:46 -0800460ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700461 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
462 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700463 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700464 return method;
465 }
466 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700467 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700468}
469
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700470ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700472 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700473 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800474 return method;
475 }
476 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700477 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478}
479
Ian Rogersef7d42f2014-01-06 12:55:46 -0800480ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 if (GetDexCache() == dex_cache) {
482 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700483 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstromf322c4c2014-10-31 00:01:54 -0700484 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe2b0fa5b2014-10-31 18:12:30 -0700485 // A miranda method may have a different DexCache and is always created by linking,
486 // never *declared* in the class.
487 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 return method;
489 }
490 }
491 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700492 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493}
494
Ian Rogersef7d42f2014-01-06 12:55:46 -0800495ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700496 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700497 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700498 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 return method;
500 }
501 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700502 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800503}
504
Ian Rogersef7d42f2014-01-06 12:55:46 -0800505ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700506 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700507 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700508 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700509 return method;
510 }
511 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700512 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700513}
514
Ian Rogersef7d42f2014-01-06 12:55:46 -0800515ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700516 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700517 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700518 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800519 return method;
520 }
521 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700522 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523}
524
Ian Rogersef7d42f2014-01-06 12:55:46 -0800525ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700526 for (size_t i = 0; i < NumDirectMethods(); ++i) {
527 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700528 if (method->IsClassInitializer()) {
529 DCHECK_STREQ(method->GetName(), "<clinit>");
530 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700531 return method;
532 }
533 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700534 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700535}
536
Brian Carlstromea46f952013-07-30 01:26:50 -0700537ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800538 // Is the field in this class?
539 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700541 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700542 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800543 return f;
544 }
545 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700546 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547}
548
Brian Carlstromea46f952013-07-30 01:26:50 -0700549ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550 if (GetDexCache() == dex_cache) {
551 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700552 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800553 if (f->GetDexFieldIndex() == dex_field_idx) {
554 return f;
555 }
556 }
557 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700558 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800559}
560
Brian Carlstromea46f952013-07-30 01:26:50 -0700561ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 // Is the field in this class, or any of its superclasses?
563 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700564 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700565 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700566 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567 return f;
568 }
569 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700570 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571}
572
Brian Carlstromea46f952013-07-30 01:26:50 -0700573ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 // Is the field in this class, or any of its superclasses?
575 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700576 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700578 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 return f;
580 }
581 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700582 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583}
584
Brian Carlstromea46f952013-07-30 01:26:50 -0700585ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700586 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800587 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700588 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700589 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800590 return f;
591 }
592 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700593 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594}
595
Brian Carlstromea46f952013-07-30 01:26:50 -0700596ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597 if (dex_cache == GetDexCache()) {
598 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600 if (f->GetDexFieldIndex() == dex_field_idx) {
601 return f;
602 }
603 }
604 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700605 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800606}
607
Mathieu Chartierf8322842014-05-16 10:59:25 -0700608ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
609 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610 // Is the field in this class (or its interfaces), or any of its
611 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700612 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800613 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700615 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800616 return f;
617 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700618 // Wrap k incase it moves during GetDirectInterface.
619 StackHandleScope<1> hs(self);
620 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700622 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800623 StackHandleScope<1> hs2(self);
624 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700625 f = FindStaticField(self, interface, name, type);
626 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 return f;
628 }
629 }
630 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700631 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632}
633
Mathieu Chartierf8322842014-05-16 10:59:25 -0700634ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
635 uint32_t dex_field_idx) {
636 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800637 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700638 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700639 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800640 return f;
641 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700642 // Wrap k incase it moves during GetDirectInterface.
643 StackHandleScope<1> hs(self);
644 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800645 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800647 StackHandleScope<1> hs2(self);
648 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700649 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
650 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651 return f;
652 }
653 }
654 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700655 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800656}
657
Mathieu Chartierf8322842014-05-16 10:59:25 -0700658ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
659 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700661 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800662 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700663 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700664 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800665 return f;
666 }
667 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700668 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800669 return f;
670 }
671 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700672 StackHandleScope<1> hs(self);
673 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
674 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800675 StackHandleScope<1> hs2(self);
676 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700677 f = interface->FindStaticField(self, interface, name, type);
678 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800679 return f;
680 }
681 }
682 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700683 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800684}
685
Brian Carlstromea46f952013-07-30 01:26:50 -0700686static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200687 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700688 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200689 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700690 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700691 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700692 if (!method->IsNative() && !method->IsAbstract()) {
693 method->SetPreverified();
694 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200695 }
696 }
697}
698
699void Class::SetPreverifiedFlagOnAllMethods() {
700 DCHECK(IsVerified());
701 SetPreverifiedFlagOnMethods(GetDirectMethods());
702 SetPreverifiedFlagOnMethods(GetVirtualMethods());
703}
704
Ian Rogers1ff3c982014-08-12 02:30:58 -0700705const char* Class::GetDescriptor(std::string* storage) {
706 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700707 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700708 } else if (IsArrayClass()) {
709 return GetArrayDescriptor(storage);
710 } else if (IsProxyClass()) {
711 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
712 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700713 } else {
714 const DexFile& dex_file = GetDexFile();
715 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
716 return dex_file.GetTypeDescriptor(type_id);
717 }
718}
719
Ian Rogers1ff3c982014-08-12 02:30:58 -0700720const char* Class::GetArrayDescriptor(std::string* storage) {
721 std::string temp;
722 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
723 *storage = "[";
724 *storage += elem_desc;
725 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700726}
727
728const DexFile::ClassDef* Class::GetClassDef() {
729 uint16_t class_def_idx = GetDexClassDefIndex();
730 if (class_def_idx == DexFile::kDexNoIndex16) {
731 return nullptr;
732 }
733 return &GetDexFile().GetClassDef(class_def_idx);
734}
735
Mathieu Chartierf8322842014-05-16 10:59:25 -0700736uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
737 DCHECK(!IsPrimitive());
738 DCHECK(!IsArrayClass());
739 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
740}
741
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700742mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700743 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700744 DCHECK(klass.Get() != nullptr);
745 DCHECK(!klass->IsPrimitive());
746 if (klass->IsArrayClass()) {
747 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
748 if (idx == 0) {
749 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
750 } else {
751 DCHECK_EQ(1U, idx);
752 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
753 }
754 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700755 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700756 DCHECK(interfaces != nullptr);
757 return interfaces->Get(idx);
758 } else {
759 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
760 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
761 if (interface == nullptr) {
762 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
763 klass.Get());
764 CHECK(interface != nullptr || self->IsExceptionPending());
765 }
766 return interface;
767 }
768}
769
770const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700771 const DexFile& dex_file = GetDexFile();
772 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200773 if (dex_class_def == nullptr) {
774 // Generated classes have no class def.
775 return nullptr;
776 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700777 return dex_file.GetSourceFile(*dex_class_def);
778}
779
780std::string Class::GetLocation() {
781 mirror::DexCache* dex_cache = GetDexCache();
782 if (dex_cache != nullptr && !IsProxyClass()) {
783 return dex_cache->GetLocation()->ToModifiedUtf8();
784 }
785 // Arrays and proxies are generated and have no corresponding dex file location.
786 return "generated class";
787}
788
789const DexFile::TypeList* Class::GetInterfaceTypeList() {
790 const DexFile::ClassDef* class_def = GetClassDef();
791 if (class_def == nullptr) {
792 return nullptr;
793 }
794 return GetDexFile().GetInterfacesList(*class_def);
795}
796
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700797void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
798 for (uint32_t i = 0; i < kImtSize; i++) {
799 // Replace null with conflict.
800 mirror::Object* obj = imt_handle_scope->GetReference(i);
801 DCHECK(obj != nullptr);
802 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700803 }
804
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700805 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700806 CHECK(table != nullptr) << PrettyClass(this);
807 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700808 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700809 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700810 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700811
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700812 // Keep java.lang.Object class's vtable around for since it's easier
813 // to be reused by array classes during their linking.
814 if (!IsObjectClass()) {
815 SetVTable(nullptr);
816 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700817}
818
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700819// The pre-fence visitor for Class::CopyOf().
820class CopyClassVisitor {
821 public:
822 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700823 size_t new_length, size_t copy_bytes,
824 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700825 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700826 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700827 }
828
829 void operator()(Object* obj, size_t usable_size) const
830 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
831 UNUSED(usable_size);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700832 StackHandleScope<1> hs(self_);
833 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
834 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
835 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
836 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
837 h_new_class_obj->SetClassSize(new_length_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700838 }
839
840 private:
841 Thread* const self_;
842 Handle<mirror::Class>* const orig_;
843 const size_t new_length_;
844 const size_t copy_bytes_;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700845 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700846 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
847};
848
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700849Class* Class::CopyOf(Thread* self, int32_t new_length,
850 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700851 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
852 // We may get copied by a compacting GC.
853 StackHandleScope<1> hs(self);
854 Handle<mirror::Class> h_this(hs.NewHandle(this));
855 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700856 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
857 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700858 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700859 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700860 kMovingClasses
861 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
862 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700863 if (UNLIKELY(new_class == nullptr)) {
864 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700865 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700866 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700867 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700868}
869
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800870} // namespace mirror
871} // namespace art