blob: bd965fa4628d530b9af5a303347631d381e9250e [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-inl.h"
22#include "class_linker.h"
23#include "class_loader.h"
24#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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "object-inl.h"
28#include "object_array-inl.h"
29#include "object_utils.h"
30#include "runtime.h"
31#include "sirt_ref.h"
32#include "thread.h"
33#include "throwable.h"
34#include "utils.h"
35#include "well_known_classes.h"
36
37namespace art {
38namespace mirror {
39
40Class* Class::java_lang_Class_ = NULL;
41
42void Class::SetClassClass(Class* java_lang_Class) {
43 CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class;
44 CHECK(java_lang_Class != NULL);
45 java_lang_Class_ = java_lang_Class;
46}
47
48void Class::ResetClass() {
49 CHECK(java_lang_Class_ != NULL);
50 java_lang_Class_ = NULL;
51}
52
Mathieu Chartierc528dba2013-11-26 12:00:11 -080053void Class::VisitRoots(RootVisitor* visitor, void* arg) {
54 if (java_lang_Class_ != nullptr) {
55 java_lang_Class_ = down_cast<Class*>(visitor(java_lang_Class_, arg));
56 }
57}
58
Ian Rogers7dfb28c2013-08-22 08:18:36 -070059void Class::SetStatus(Status new_status, Thread* self) {
60 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070063 if (LIKELY(class_linker_initialized)) {
64 if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070065 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070066 << 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.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070070 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071 << "Attempt to change status of class while not holding its lock: "
72 << PrettyClass(this) << " " << old_status << " -> " << new_status;
73 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
75 if (new_status == kStatusError) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070076 CHECK_NE(GetStatus(), kStatusError)
77 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078
Ian Rogers62d6c772013-02-27 08:32:07 -080079 // Stash current exception.
Ian Rogers62d6c772013-02-27 08:32:07 -080080 SirtRef<mirror::Object> old_throw_this_object(self, NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -070081 SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -080082 SirtRef<mirror::Throwable> old_exception(self, NULL);
83 uint32_t old_throw_dex_pc;
84 {
85 ThrowLocation old_throw_location;
86 mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location);
87 old_throw_this_object.reset(old_throw_location.GetThis());
88 old_throw_method.reset(old_throw_location.GetMethod());
89 old_exception.reset(old_exception_obj);
90 old_throw_dex_pc = old_throw_location.GetDexPc();
91 self->ClearException();
92 }
93 CHECK(old_exception.get() != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
95 // clear exception to call FindSystemClass
96 self->ClearException();
97 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
98 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
99 CHECK(!self->IsExceptionPending());
100
Ian Rogers62d6c772013-02-27 08:32:07 -0800101 // Only verification errors, not initialization problems, should set a verify error.
102 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
103 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 if (!eiie_class->IsAssignableFrom(exception_class)) {
105 SetVerifyErrorClass(exception_class);
106 }
107
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 // Restore exception.
109 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
110 old_throw_dex_pc);
111
112 self->SetException(gc_safe_throw_location, old_exception.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700114 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700115 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
116 // Classes that are being resolved or initialized need to notify waiters that the class status
117 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
118 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
119 class_linker_initialized) {
120 NotifyAll(self);
121 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122}
123
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124void Class::SetDexCache(DexCache* new_dex_cache) {
125 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
126}
127
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128void Class::SetClassSize(size_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700129 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
130 DumpClass(LOG(ERROR), kDumpClassFullDetail);
131 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
132 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
134}
135
136// Return the class' name. The exact format is bizarre, but it's the specified behavior for
137// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
138// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
139// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
140String* Class::ComputeName() {
141 String* name = GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800142 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143 return name;
144 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800145 Thread* self = Thread::Current();
146 SirtRef<mirror::Class> sirt_c(self, this);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700147 std::string descriptor(ClassHelper(this).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
149 // The descriptor indicates that this is the class for
150 // a primitive type; special-case the return value.
151 const char* c_name = NULL;
152 switch (descriptor[0]) {
153 case 'Z': c_name = "boolean"; break;
154 case 'B': c_name = "byte"; break;
155 case 'C': c_name = "char"; break;
156 case 'S': c_name = "short"; break;
157 case 'I': c_name = "int"; break;
158 case 'J': c_name = "long"; break;
159 case 'F': c_name = "float"; break;
160 case 'D': c_name = "double"; break;
161 case 'V': c_name = "void"; break;
162 default:
163 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
164 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800165 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 } else {
167 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
168 // components.
169 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
170 descriptor.erase(0, 1);
171 descriptor.erase(descriptor.size() - 1);
172 }
173 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800174 name = String::AllocFromModifiedUtf8(self, descriptor.c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800176 sirt_c->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 return name;
178}
179
180void Class::DumpClass(std::ostream& os, int flags) const {
181 if ((flags & kDumpClassFullDetail) == 0) {
182 os << PrettyClass(this);
183 if ((flags & kDumpClassClassLoader) != 0) {
184 os << ' ' << GetClassLoader();
185 }
186 if ((flags & kDumpClassInitialized) != 0) {
187 os << ' ' << GetStatus();
188 }
189 os << "\n";
190 return;
191 }
192
193 Class* super = GetSuperClass();
194 ClassHelper kh(this);
195 os << "----- " << (IsInterface() ? "interface" : "class") << " "
196 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
197 os << " objectSize=" << SizeOf() << " "
198 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
199 os << StringPrintf(" access=0x%04x.%04x\n",
200 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
201 if (super != NULL) {
202 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
203 }
204 if (IsArrayClass()) {
205 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
206 }
207 if (kh.NumDirectInterfaces() > 0) {
208 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
209 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
210 Class* interface = kh.GetDirectInterface(i);
211 const ClassLoader* cl = interface->GetClassLoader();
212 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
213 }
214 }
215 os << " vtable (" << NumVirtualMethods() << " entries, "
216 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
217 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
218 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
219 }
220 os << " direct methods (" << NumDirectMethods() << " entries):\n";
221 for (size_t i = 0; i < NumDirectMethods(); ++i) {
222 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
223 }
224 if (NumStaticFields() > 0) {
225 os << " static fields (" << NumStaticFields() << " entries):\n";
226 if (IsResolved() || IsErroneous()) {
227 for (size_t i = 0; i < NumStaticFields(); ++i) {
228 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
229 }
230 } else {
231 os << " <not yet available>";
232 }
233 }
234 if (NumInstanceFields() > 0) {
235 os << " instance fields (" << NumInstanceFields() << " entries):\n";
236 if (IsResolved() || IsErroneous()) {
237 for (size_t i = 0; i < NumInstanceFields(); ++i) {
238 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
239 }
240 } else {
241 os << " <not yet available>";
242 }
243 }
244}
245
246void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
247 if (new_reference_offsets != CLASS_WALK_SUPER) {
248 // Sanity check that the number of bits set in the reference offset bitmap
249 // agrees with the number of references
250 size_t count = 0;
251 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
252 count += c->NumReferenceInstanceFieldsDuringLinking();
253 }
254 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
255 }
256 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
257 new_reference_offsets, false);
258}
259
260void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
261 if (new_reference_offsets != CLASS_WALK_SUPER) {
262 // Sanity check that the number of bits set in the reference offset bitmap
263 // agrees with the number of references
264 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
265 NumReferenceStaticFieldsDuringLinking());
266 }
267 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
268 new_reference_offsets, false);
269}
270
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
272 size_t i = 0;
273 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
274 ++i;
275 }
276 if (descriptor1.find('/', i) != StringPiece::npos ||
277 descriptor2.find('/', i) != StringPiece::npos) {
278 return false;
279 } else {
280 return true;
281 }
282}
283
284bool Class::IsInSamePackage(const Class* that) const {
285 const Class* klass1 = this;
286 const Class* klass2 = that;
287 if (klass1 == klass2) {
288 return true;
289 }
290 // Class loaders must match.
291 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
292 return false;
293 }
294 // Arrays are in the same package when their element classes are.
295 while (klass1->IsArrayClass()) {
296 klass1 = klass1->GetComponentType();
297 }
298 while (klass2->IsArrayClass()) {
299 klass2 = klass2->GetComponentType();
300 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700301 // trivial check again for array types
302 if (klass1 == klass2) {
303 return true;
304 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305 // Compare the package part of the descriptor string.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700306 return IsInSamePackage(ClassHelper(klass1).GetDescriptor(),
307 ClassHelper(klass2).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308}
309
310bool Class::IsClassClass() const {
311 Class* java_lang_Class = GetClass()->GetClass();
312 return this == java_lang_Class;
313}
314
315bool Class::IsStringClass() const {
316 return this == String::GetJavaLangString();
317}
318
319bool Class::IsThrowableClass() const {
320 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
321}
322
Brian Carlstromea46f952013-07-30 01:26:50 -0700323bool Class::IsArtFieldClass() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700325 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
326 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327}
328
Brian Carlstromea46f952013-07-30 01:26:50 -0700329bool Class::IsArtMethodClass() const {
330 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331}
332
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333void Class::SetClassLoader(ClassLoader* new_class_loader) {
334 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
335}
336
Ian Rogersd91d6d62013-09-25 20:26:14 -0700337ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700339 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 if (method != NULL) {
341 return method;
342 }
343
344 int32_t iftable_count = GetIfTableCount();
345 IfTable* iftable = GetIfTable();
346 for (int32_t i = 0; i < iftable_count; i++) {
347 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
348 if (method != NULL) {
349 return method;
350 }
351 }
352 return NULL;
353}
354
Brian Carlstromea46f952013-07-30 01:26:50 -0700355ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700357 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 if (method != NULL) {
359 return method;
360 }
361
362 int32_t iftable_count = GetIfTableCount();
363 IfTable* iftable = GetIfTable();
364 for (int32_t i = 0; i < iftable_count; i++) {
365 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
366 if (method != NULL) {
367 return method;
368 }
369 }
370 return NULL;
371}
372
Brian Carlstromea46f952013-07-30 01:26:50 -0700373ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 MethodHelper mh;
375 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700376 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700378 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700379 return method;
380 }
381 }
382 return NULL;
383}
384
385ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const {
386 MethodHelper mh;
387 for (size_t i = 0; i < NumDirectMethods(); ++i) {
388 ArtMethod* method = GetDirectMethod(i);
389 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700390 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 return method;
392 }
393 }
394 return NULL;
395}
396
Brian Carlstromea46f952013-07-30 01:26:50 -0700397ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 if (GetDexCache() == dex_cache) {
399 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 if (method->GetDexMethodIndex() == dex_method_idx) {
402 return method;
403 }
404 }
405 }
406 return NULL;
407}
408
Brian Carlstromea46f952013-07-30 01:26:50 -0700409ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700411 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 if (method != NULL) {
413 return method;
414 }
415 }
416 return NULL;
417}
418
Ian Rogersd91d6d62013-09-25 20:26:14 -0700419ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) const {
420 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
421 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
422 if (method != NULL) {
423 return method;
424 }
425 }
426 return NULL;
427}
428
Brian Carlstromea46f952013-07-30 01:26:50 -0700429ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700431 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 if (method != NULL) {
433 return method;
434 }
435 }
436 return NULL;
437}
438
Ian Rogersd91d6d62013-09-25 20:26:14 -0700439ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
440 MethodHelper mh;
441 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
442 ArtMethod* method = GetVirtualMethod(i);
443 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700444 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700445 return method;
446 }
447 }
448 return NULL;
449}
450
Brian Carlstromea46f952013-07-30 01:26:50 -0700451ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700452 const Signature& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 MethodHelper mh;
454 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700455 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700457 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800458 return method;
459 }
460 }
461 return NULL;
462}
463
Brian Carlstromea46f952013-07-30 01:26:50 -0700464ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 if (GetDexCache() == dex_cache) {
466 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700467 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468 if (method->GetDexMethodIndex() == dex_method_idx) {
469 return method;
470 }
471 }
472 }
473 return NULL;
474}
475
Brian Carlstromea46f952013-07-30 01:26:50 -0700476ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700478 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800479 if (method != NULL) {
480 return method;
481 }
482 }
483 return NULL;
484}
485
Ian Rogersd91d6d62013-09-25 20:26:14 -0700486ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) const {
487 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
488 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
489 if (method != NULL) {
490 return method;
491 }
492 }
493 return NULL;
494}
495
Brian Carlstromea46f952013-07-30 01:26:50 -0700496ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800497 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700498 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 if (method != NULL) {
500 return method;
501 }
502 }
503 return NULL;
504}
505
Ian Rogersd91d6d62013-09-25 20:26:14 -0700506ArtMethod* Class::FindClassInitializer() const {
507 for (size_t i = 0; i < NumDirectMethods(); ++i) {
508 ArtMethod* method = GetDirectMethod(i);
509 if (method->IsConstructor() && method->IsStatic()) {
510 if (kIsDebugBuild) {
511 MethodHelper mh(method);
Ian Rogers241b5de2013-10-09 17:58:57 -0700512 CHECK(mh.IsClassInitializer());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700513 CHECK_STREQ(mh.GetName(), "<clinit>");
514 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
515 }
516 return method;
517 }
518 }
519 return NULL;
520}
521
Brian Carlstromea46f952013-07-30 01:26:50 -0700522ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523 // Is the field in this class?
524 // Interfaces are not relevant because they can't contain instance fields.
525 FieldHelper fh;
526 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700527 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800528 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700529 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 return f;
531 }
532 }
533 return NULL;
534}
535
Brian Carlstromea46f952013-07-30 01:26:50 -0700536ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 if (GetDexCache() == dex_cache) {
538 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700539 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540 if (f->GetDexFieldIndex() == dex_field_idx) {
541 return f;
542 }
543 }
544 }
545 return NULL;
546}
547
Brian Carlstromea46f952013-07-30 01:26:50 -0700548ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549 // Is the field in this class, or any of its superclasses?
550 // Interfaces are not relevant because they can't contain instance fields.
551 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700552 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800553 if (f != NULL) {
554 return f;
555 }
556 }
557 return NULL;
558}
559
Brian Carlstromea46f952013-07-30 01:26:50 -0700560ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 // Is the field in this class, or any of its superclasses?
562 // Interfaces are not relevant because they can't contain instance fields.
563 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 if (f != NULL) {
566 return f;
567 }
568 }
569 return NULL;
570}
571
Brian Carlstromea46f952013-07-30 01:26:50 -0700572ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573 DCHECK(type != NULL);
574 FieldHelper fh;
575 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700578 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 return f;
580 }
581 }
582 return NULL;
583}
584
Brian Carlstromea46f952013-07-30 01:26:50 -0700585ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 if (dex_cache == GetDexCache()) {
587 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700588 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800589 if (f->GetDexFieldIndex() == dex_field_idx) {
590 return f;
591 }
592 }
593 }
594 return NULL;
595}
596
Brian Carlstromea46f952013-07-30 01:26:50 -0700597ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598 // Is the field in this class (or its interfaces), or any of its
599 // superclasses (or their interfaces)?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
601 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700602 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800603 if (f != NULL) {
604 return f;
605 }
606 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700607 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
609 Class* interface = kh.GetDirectInterface(i);
610 f = interface->FindStaticField(name, type);
611 if (f != NULL) {
612 return f;
613 }
614 }
615 }
616 return NULL;
617}
618
Brian Carlstromea46f952013-07-30 01:26:50 -0700619ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
621 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700622 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800623 if (f != NULL) {
624 return f;
625 }
626 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700627 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
629 Class* interface = kh.GetDirectInterface(i);
630 f = interface->FindStaticField(dex_cache, dex_field_idx);
631 if (f != NULL) {
632 return f;
633 }
634 }
635 }
636 return NULL;
637}
638
Brian Carlstromea46f952013-07-30 01:26:50 -0700639ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800640 // Find a field using the JLS field resolution order
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
642 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700643 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 if (f != NULL) {
645 return f;
646 }
647 f = k->FindDeclaredStaticField(name, type);
648 if (f != NULL) {
649 return f;
650 }
651 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700652 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800653 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
654 Class* interface = kh.GetDirectInterface(i);
655 f = interface->FindStaticField(name, type);
656 if (f != NULL) {
657 return f;
658 }
659 }
660 }
661 return NULL;
662}
663
Brian Carlstromea46f952013-07-30 01:26:50 -0700664static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200665 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
666 if (methods != NULL) {
667 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700668 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200669 DCHECK(method != NULL);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700670 if (!method->IsNative() && !method->IsAbstract()) {
671 method->SetPreverified();
672 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200673 }
674 }
675}
676
677void Class::SetPreverifiedFlagOnAllMethods() {
678 DCHECK(IsVerified());
679 SetPreverifiedFlagOnMethods(GetDirectMethods());
680 SetPreverifiedFlagOnMethods(GetVirtualMethods());
681}
682
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683} // namespace mirror
684} // namespace art