blob: a218b1cf5fd0c8f63b4d7ad502bf7041d27dce3d [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
Brian Carlstrom004644f2014-06-18 08:34:01 -070039Class* Class::java_lang_Class_ = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
41void Class::SetClassClass(Class* java_lang_Class) {
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070042 CHECK(java_lang_Class_ == nullptr)
43 << ReadBarrier::BarrierForRoot<mirror::Class, kWithReadBarrier>(&java_lang_Class_)
44 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070045 CHECK(java_lang_Class != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080046 java_lang_Class_ = java_lang_Class;
47}
48
49void Class::ResetClass() {
Brian Carlstrom004644f2014-06-18 08:34:01 -070050 CHECK(java_lang_Class_ != nullptr);
51 java_lang_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 Chartierc528dba2013-11-26 12:00:11 -080055 if (java_lang_Class_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -080056 callback(reinterpret_cast<mirror::Object**>(&java_lang_Class_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057 }
58}
59
Ian Rogers7dfb28c2013-08-22 08:18:36 -070060void Class::SetStatus(Status new_status, Thread* self) {
61 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070062 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
63 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070064 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070065 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
66 new_status != kStatusRetired)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070069 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070070 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
71 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070072 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 << "Attempt to change status of class while not holding its lock: "
74 << PrettyClass(this) << " " << old_status << " -> " << new_status;
75 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 }
Ian Rogers98379392014-02-24 16:53:16 -080077 if (UNLIKELY(new_status == kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070078 CHECK_NE(GetStatus(), kStatusError)
79 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Ian Rogers62d6c772013-02-27 08:32:07 -080081 // Stash current exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 StackHandleScope<3> hs(self);
83 ThrowLocation old_throw_location;
84 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location)));
85 CHECK(old_exception.Get() != nullptr);
86 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis()));
87 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod()));
88 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +020089 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070090 Class* eiie_class;
91 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
92 // allocations and may cause infinite loops.
93 if (old_exception.Get() == nullptr ||
94 old_exception->GetClass()->GetDescriptor() != "Ljava/lang/OutOfMemoryError;") {
95 // 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.
101 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
102 Class* exception_class = old_exception->GetClass();
103 if (!eiie_class->IsAssignableFrom(exception_class)) {
104 SetVerifyErrorClass(exception_class);
105 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 }
107
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 // Restore exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700109 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800110 old_throw_dex_pc);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700111 self->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200112 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 }
Ian Rogers03dbc042014-06-02 14:24:56 -0700114 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700116 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700118 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100119 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700120
121 if (!class_linker_initialized) {
122 // When the class linker is being initialized its single threaded and by definition there can be
123 // no waiters. During initialization classes may appear temporary but won't be retired as their
124 // size was statically computed.
125 } else {
126 // Classes that are being resolved or initialized need to notify waiters that the class status
127 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
128 if (IsTemp()) {
129 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
130 // so that they can grab the new version of the class from the class linker's table.
131 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this);
132 if (new_status == kStatusRetired || new_status == kStatusError) {
133 NotifyAll(self);
134 }
135 } else {
136 CHECK_NE(new_status, kStatusRetired);
137 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
138 NotifyAll(self);
139 }
140 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700141 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142}
143
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700145 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146}
147
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700149 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
150 DumpClass(LOG(ERROR), kDumpClassFullDetail);
151 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
152 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700154 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155}
156
157// Return the class' name. The exact format is bizarre, but it's the specified behavior for
158// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
159// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
160// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161String* Class::ComputeName(Handle<Class> h_this) {
162 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800163 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 return name;
165 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700166 std::string descriptor(h_this->GetDescriptor());
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.
189 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
190 descriptor.erase(0, 1);
191 descriptor.erase(descriptor.size() - 1);
192 }
193 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800194 name = String::AllocFromModifiedUtf8(self, descriptor.c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700196 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 return name;
198}
199
Ian Rogersef7d42f2014-01-06 12:55:46 -0800200void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800201 if ((flags & kDumpClassFullDetail) == 0) {
202 os << PrettyClass(this);
203 if ((flags & kDumpClassClassLoader) != 0) {
204 os << ' ' << GetClassLoader();
205 }
206 if ((flags & kDumpClassInitialized) != 0) {
207 os << ' ' << GetStatus();
208 }
209 os << "\n";
210 return;
211 }
212
Mathieu Chartierf8322842014-05-16 10:59:25 -0700213 Thread* self = Thread::Current();
214 StackHandleScope<2> hs(self);
215 Handle<mirror::Class> h_this(hs.NewHandle(this));
216 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
217
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Mathieu Chartierf8322842014-05-16 10:59:25 -0700219 << "'" << GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700221 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 os << StringPrintf(" access=0x%04x.%04x\n",
223 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700224 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700225 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
226 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 }
228 if (IsArrayClass()) {
229 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
230 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700231 const size_t num_direct_interfaces = NumDirectInterfaces();
232 if (num_direct_interfaces > 0) {
233 os << " interfaces (" << num_direct_interfaces << "):\n";
234 for (size_t i = 0; i < num_direct_interfaces; ++i) {
235 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 const ClassLoader* cl = interface->GetClassLoader();
237 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
238 }
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) {
278 if (new_reference_offsets != CLASS_WALK_SUPER) {
279 // Sanity check that the number of bits set in the reference offset bitmap
280 // agrees with the number of references
281 size_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 }
Vladimir Marko81949632014-05-02 11:53:22 +0100285 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100287 // Not called within a transaction.
288 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700289 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290}
291
292void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
293 if (new_reference_offsets != CLASS_WALK_SUPER) {
294 // Sanity check that the number of bits set in the reference offset bitmap
295 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100296 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 NumReferenceStaticFieldsDuringLinking());
298 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100299 // Not called within a transaction.
300 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700301 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302}
303
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
305 size_t i = 0;
306 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
307 ++i;
308 }
309 if (descriptor1.find('/', i) != StringPiece::npos ||
310 descriptor2.find('/', i) != StringPiece::npos) {
311 return false;
312 } else {
313 return true;
314 }
315}
316
Ian Rogersef7d42f2014-01-06 12:55:46 -0800317bool Class::IsInSamePackage(Class* that) {
318 Class* klass1 = this;
319 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 if (klass1 == klass2) {
321 return true;
322 }
323 // Class loaders must match.
324 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
325 return false;
326 }
327 // Arrays are in the same package when their element classes are.
328 while (klass1->IsArrayClass()) {
329 klass1 = klass1->GetComponentType();
330 }
331 while (klass2->IsArrayClass()) {
332 klass2 = klass2->GetComponentType();
333 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700334 // trivial check again for array types
335 if (klass1 == klass2) {
336 return true;
337 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 // Compare the package part of the descriptor string.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700339 return IsInSamePackage(klass1->GetDescriptor().c_str(), klass2->GetDescriptor().c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340}
341
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342bool Class::IsStringClass() const {
343 return this == String::GetJavaLangString();
344}
345
Ian Rogersef7d42f2014-01-06 12:55:46 -0800346bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
348}
349
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100351 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700352 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100353 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700354 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100355 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356}
357
Brian Carlstrom004644f2014-06-18 08:34:01 -0700358ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700360 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700361 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 return method;
363 }
364
365 int32_t iftable_count = GetIfTableCount();
366 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700367 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700369 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800370 return method;
371 }
372 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700373 return nullptr;
374}
375
376ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
377 // Check the current class before checking the interfaces.
378 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
379 if (method != nullptr) {
380 return method;
381 }
382
383 int32_t iftable_count = GetIfTableCount();
384 IfTable* iftable = GetIfTable();
385 for (int32_t i = 0; i < iftable_count; ++i) {
386 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
387 if (method != nullptr) {
388 return method;
389 }
390 }
391 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392}
393
Ian Rogersef7d42f2014-01-06 12:55:46 -0800394ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700396 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700397 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 return method;
399 }
400
401 int32_t iftable_count = GetIfTableCount();
402 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700403 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700405 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 return method;
407 }
408 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700409 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410}
411
Ian Rogersef7d42f2014-01-06 12:55:46 -0800412ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700414 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700415 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700416 return method;
417 }
418 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700419 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700420}
421
Ian Rogersef7d42f2014-01-06 12:55:46 -0800422ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700423 for (size_t i = 0; i < NumDirectMethods(); ++i) {
424 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700425 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 return method;
427 }
428 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700429 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430}
431
Ian Rogersef7d42f2014-01-06 12:55:46 -0800432ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433 if (GetDexCache() == dex_cache) {
434 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700435 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436 if (method->GetDexMethodIndex() == dex_method_idx) {
437 return method;
438 }
439 }
440 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700441 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442}
443
Ian Rogersef7d42f2014-01-06 12:55:46 -0800444ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700445 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700446 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448 return method;
449 }
450 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800452}
453
Ian Rogersef7d42f2014-01-06 12:55:46 -0800454ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700455 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700456 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700458 return method;
459 }
460 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700461 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700462}
463
Ian Rogersef7d42f2014-01-06 12:55:46 -0800464ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700465 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700466 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700467 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468 return method;
469 }
470 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700471 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472}
473
Ian Rogersef7d42f2014-01-06 12:55:46 -0800474ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700475 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
476 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700477 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700478 return method;
479 }
480 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700481 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700482}
483
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700484ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700486 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700487 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 return method;
489 }
490 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700491 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492}
493
Ian Rogersef7d42f2014-01-06 12:55:46 -0800494ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 if (GetDexCache() == dex_cache) {
496 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700497 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498 if (method->GetDexMethodIndex() == dex_method_idx) {
499 return method;
500 }
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 StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700507 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700508 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700509 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800510 return method;
511 }
512 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514}
515
Ian Rogersef7d42f2014-01-06 12:55:46 -0800516ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700518 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700519 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700520 return method;
521 }
522 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700523 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700524}
525
Ian Rogersef7d42f2014-01-06 12:55:46 -0800526ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700527 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700528 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700529 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 return method;
531 }
532 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700533 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534}
535
Ian Rogersef7d42f2014-01-06 12:55:46 -0800536ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700537 for (size_t i = 0; i < NumDirectMethods(); ++i) {
538 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700539 if (method->IsClassInitializer()) {
540 DCHECK_STREQ(method->GetName(), "<clinit>");
541 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700542 return method;
543 }
544 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700545 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700546}
547
Brian Carlstromea46f952013-07-30 01:26:50 -0700548ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549 // Is the field in this class?
550 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700552 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700553 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 return f;
555 }
556 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700557 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558}
559
Brian Carlstromea46f952013-07-30 01:26:50 -0700560ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 if (GetDexCache() == dex_cache) {
562 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700563 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 if (f->GetDexFieldIndex() == dex_field_idx) {
565 return f;
566 }
567 }
568 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700569 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570}
571
Brian Carlstromea46f952013-07-30 01:26:50 -0700572ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573 // Is the field in this class, or any of its superclasses?
574 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700575 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700577 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 return f;
579 }
580 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700581 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800582}
583
Brian Carlstromea46f952013-07-30 01:26:50 -0700584ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800585 // Is the field in this class, or any of its superclasses?
586 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700587 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700588 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700589 if (f != nullptr) {
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 StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700597 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700600 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 return f;
602 }
603 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700604 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605}
606
Brian Carlstromea46f952013-07-30 01:26:50 -0700607ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608 if (dex_cache == GetDexCache()) {
609 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700610 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 if (f->GetDexFieldIndex() == dex_field_idx) {
612 return f;
613 }
614 }
615 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700616 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617}
618
Mathieu Chartierf8322842014-05-16 10:59:25 -0700619ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
620 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 // Is the field in this class (or its interfaces), or any of its
622 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700623 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700625 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700626 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 return f;
628 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700629 // Wrap k incase it moves during GetDirectInterface.
630 StackHandleScope<1> hs(self);
631 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700633 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
634 StackHandleScope<1> hs(self);
635 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
636 f = FindStaticField(self, interface, name, type);
637 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800638 return f;
639 }
640 }
641 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700642 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800643}
644
Mathieu Chartierf8322842014-05-16 10:59:25 -0700645ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
646 uint32_t dex_field_idx) {
647 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800648 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700649 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700650 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651 return f;
652 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700653 // Wrap k incase it moves during GetDirectInterface.
654 StackHandleScope<1> hs(self);
655 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800656 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700657 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
658 StackHandleScope<1> hs(self);
659 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
660 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
661 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800662 return f;
663 }
664 }
665 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700666 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800667}
668
Mathieu Chartierf8322842014-05-16 10:59:25 -0700669ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
670 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700672 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800673 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700674 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700675 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800676 return f;
677 }
678 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700679 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800680 return f;
681 }
682 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700683 StackHandleScope<1> hs(self);
684 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
685 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
686 StackHandleScope<1> hs(self);
687 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
688 f = interface->FindStaticField(self, interface, name, type);
689 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800690 return f;
691 }
692 }
693 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700694 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800695}
696
Brian Carlstromea46f952013-07-30 01:26:50 -0700697static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200698 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700699 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200700 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700701 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700702 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700703 if (!method->IsNative() && !method->IsAbstract()) {
704 method->SetPreverified();
705 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200706 }
707 }
708}
709
710void Class::SetPreverifiedFlagOnAllMethods() {
711 DCHECK(IsVerified());
712 SetPreverifiedFlagOnMethods(GetDirectMethods());
713 SetPreverifiedFlagOnMethods(GetVirtualMethods());
714}
715
Mathieu Chartierf8322842014-05-16 10:59:25 -0700716std::string Class::GetDescriptor() {
717 if (UNLIKELY(IsArrayClass())) {
718 return GetArrayDescriptor();
719 } else if (UNLIKELY(IsPrimitive())) {
720 return Primitive::Descriptor(GetPrimitiveType());
721 } else if (UNLIKELY(IsProxyClass())) {
722 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
723 } else {
724 const DexFile& dex_file = GetDexFile();
725 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
726 return dex_file.GetTypeDescriptor(type_id);
727 }
728}
729
730std::string Class::GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
731 return "[" + GetComponentType()->GetDescriptor();
732}
733
734const DexFile::ClassDef* Class::GetClassDef() {
735 uint16_t class_def_idx = GetDexClassDefIndex();
736 if (class_def_idx == DexFile::kDexNoIndex16) {
737 return nullptr;
738 }
739 return &GetDexFile().GetClassDef(class_def_idx);
740}
741
742uint32_t Class::NumDirectInterfaces() {
743 if (IsPrimitive()) {
744 return 0;
745 } else if (IsArrayClass()) {
746 return 2;
747 } else if (IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700748 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700749 return interfaces != nullptr ? interfaces->GetLength() : 0;
750 } else {
751 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
752 if (interfaces == nullptr) {
753 return 0;
754 } else {
755 return interfaces->Size();
756 }
757 }
758}
759
760uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
761 DCHECK(!IsPrimitive());
762 DCHECK(!IsArrayClass());
763 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
764}
765
766mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) {
767 DCHECK(klass.Get() != nullptr);
768 DCHECK(!klass->IsPrimitive());
769 if (klass->IsArrayClass()) {
770 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
771 if (idx == 0) {
772 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
773 } else {
774 DCHECK_EQ(1U, idx);
775 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
776 }
777 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700778 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700779 DCHECK(interfaces != nullptr);
780 return interfaces->Get(idx);
781 } else {
782 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
783 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
784 if (interface == nullptr) {
785 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
786 klass.Get());
787 CHECK(interface != nullptr || self->IsExceptionPending());
788 }
789 return interface;
790 }
791}
792
793const char* Class::GetSourceFile() {
794 std::string descriptor(GetDescriptor());
795 const DexFile& dex_file = GetDexFile();
796 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200797 if (dex_class_def == nullptr) {
798 // Generated classes have no class def.
799 return nullptr;
800 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700801 return dex_file.GetSourceFile(*dex_class_def);
802}
803
804std::string Class::GetLocation() {
805 mirror::DexCache* dex_cache = GetDexCache();
806 if (dex_cache != nullptr && !IsProxyClass()) {
807 return dex_cache->GetLocation()->ToModifiedUtf8();
808 }
809 // Arrays and proxies are generated and have no corresponding dex file location.
810 return "generated class";
811}
812
813const DexFile::TypeList* Class::GetInterfaceTypeList() {
814 const DexFile::ClassDef* class_def = GetClassDef();
815 if (class_def == nullptr) {
816 return nullptr;
817 }
818 return GetDexFile().GetInterfacesList(*class_def);
819}
820
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700821void Class::PopulateEmbeddedImtAndVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
822 ObjectArray<ArtMethod>* table = GetImTable();
823 if (table != nullptr) {
824 for (uint32_t i = 0; i < kImtSize; i++) {
825 SetEmbeddedImTableEntry(i, table->Get(i));
826 }
827 }
828
829 table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700830 CHECK(table != nullptr) << PrettyClass(this);
831 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700832 for (int32_t i = 0; i < table->GetLength(); i++) {
833 SetEmbeddedVTableEntry(i, table->Get(i));
834 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700835
836 SetImTable(nullptr);
837 // Keep java.lang.Object class's vtable around for since it's easier
838 // to be reused by array classes during their linking.
839 if (!IsObjectClass()) {
840 SetVTable(nullptr);
841 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700842}
843
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700844// The pre-fence visitor for Class::CopyOf().
845class CopyClassVisitor {
846 public:
847 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
848 size_t new_length, size_t copy_bytes)
849 : self_(self), orig_(orig), new_length_(new_length),
850 copy_bytes_(copy_bytes) {
851 }
852
853 void operator()(Object* obj, size_t usable_size) const
854 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
855 UNUSED(usable_size);
856 mirror::Class* new_class_obj = obj->AsClass();
857 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_);
858 new_class_obj->SetStatus(Class::kStatusResolving, self_);
859 new_class_obj->PopulateEmbeddedImtAndVTable();
860 new_class_obj->SetClassSize(new_length_);
861 }
862
863 private:
864 Thread* const self_;
865 Handle<mirror::Class>* const orig_;
866 const size_t new_length_;
867 const size_t copy_bytes_;
868 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
869};
870
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700871Class* Class::CopyOf(Thread* self, int32_t new_length) {
872 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
873 // We may get copied by a compacting GC.
874 StackHandleScope<1> hs(self);
875 Handle<mirror::Class> h_this(hs.NewHandle(this));
876 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700877 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
878 // to skip copying the tail part that we will overwrite here.
879 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700880
881 mirror::Object* new_class =
882 kMovingClasses ? heap->AllocObject<true>(self, java_lang_Class_, new_length, visitor)
883 : heap->AllocNonMovableObject<true>(self, java_lang_Class_, new_length, visitor);
884 if (UNLIKELY(new_class == nullptr)) {
885 CHECK(self->IsExceptionPending()); // Expect an OOME.
886 return NULL;
887 }
888
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700889 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700890}
891
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800892} // namespace mirror
893} // namespace art