blob: 15129ab6dcdb072bd6fa97cecf428563f7abbf75 [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
19#include "abstract_method-inl.h"
20#include "class-inl.h"
21#include "class_linker.h"
22#include "class_loader.h"
23#include "dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "field-inl.h"
26#include "gc/card_table-inl.h"
27#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
53void Class::SetStatus(Status new_status) {
54 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
55 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
56 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
57 if (new_status > kStatusResolved) {
58 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
59 }
60 if (new_status == kStatusError) {
61 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
62
Ian Rogers62d6c772013-02-27 08:32:07 -080063 // Stash current exception.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -080065 SirtRef<mirror::Object> old_throw_this_object(self, NULL);
66 SirtRef<mirror::AbstractMethod> old_throw_method(self, NULL);
67 SirtRef<mirror::Throwable> old_exception(self, NULL);
68 uint32_t old_throw_dex_pc;
69 {
70 ThrowLocation old_throw_location;
71 mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location);
72 old_throw_this_object.reset(old_throw_location.GetThis());
73 old_throw_method.reset(old_throw_location.GetMethod());
74 old_exception.reset(old_exception_obj);
75 old_throw_dex_pc = old_throw_location.GetDexPc();
76 self->ClearException();
77 }
78 CHECK(old_exception.get() != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079
80 // clear exception to call FindSystemClass
81 self->ClearException();
82 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
83 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
84 CHECK(!self->IsExceptionPending());
85
Ian Rogers62d6c772013-02-27 08:32:07 -080086 // Only verification errors, not initialization problems, should set a verify error.
87 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
88 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080089 if (!eiie_class->IsAssignableFrom(exception_class)) {
90 SetVerifyErrorClass(exception_class);
91 }
92
Ian Rogers62d6c772013-02-27 08:32:07 -080093 // Restore exception.
94 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
95 old_throw_dex_pc);
96
97 self->SetException(gc_safe_throw_location, old_exception.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 }
99 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
100}
101
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102void Class::SetDexCache(DexCache* new_dex_cache) {
103 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
104}
105
106Object* Class::AllocObject(Thread* self) {
107 DCHECK(!IsArrayClass()) << PrettyClass(this);
108 DCHECK(IsInstantiable()) << PrettyClass(this);
109 // TODO: decide whether we want this check. It currently fails during bootstrap.
110 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
111 DCHECK_GE(this->object_size_, sizeof(Object));
112 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
113}
114
115void Class::SetClassSize(size_t new_class_size) {
116 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
117 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
118}
119
120// Return the class' name. The exact format is bizarre, but it's the specified behavior for
121// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
122// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
123// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
124String* Class::ComputeName() {
125 String* name = GetName();
126 if (name != NULL) {
127 return name;
128 }
129 std::string descriptor(ClassHelper(this).GetDescriptor());
130 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
131 // The descriptor indicates that this is the class for
132 // a primitive type; special-case the return value.
133 const char* c_name = NULL;
134 switch (descriptor[0]) {
135 case 'Z': c_name = "boolean"; break;
136 case 'B': c_name = "byte"; break;
137 case 'C': c_name = "char"; break;
138 case 'S': c_name = "short"; break;
139 case 'I': c_name = "int"; break;
140 case 'J': c_name = "long"; break;
141 case 'F': c_name = "float"; break;
142 case 'D': c_name = "double"; break;
143 case 'V': c_name = "void"; break;
144 default:
145 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
146 }
147 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
148 } else {
149 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
150 // components.
151 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
152 descriptor.erase(0, 1);
153 descriptor.erase(descriptor.size() - 1);
154 }
155 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
156 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
157 }
158 SetName(name);
159 return name;
160}
161
162void Class::DumpClass(std::ostream& os, int flags) const {
163 if ((flags & kDumpClassFullDetail) == 0) {
164 os << PrettyClass(this);
165 if ((flags & kDumpClassClassLoader) != 0) {
166 os << ' ' << GetClassLoader();
167 }
168 if ((flags & kDumpClassInitialized) != 0) {
169 os << ' ' << GetStatus();
170 }
171 os << "\n";
172 return;
173 }
174
175 Class* super = GetSuperClass();
176 ClassHelper kh(this);
177 os << "----- " << (IsInterface() ? "interface" : "class") << " "
178 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
179 os << " objectSize=" << SizeOf() << " "
180 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
181 os << StringPrintf(" access=0x%04x.%04x\n",
182 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
183 if (super != NULL) {
184 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
185 }
186 if (IsArrayClass()) {
187 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
188 }
189 if (kh.NumDirectInterfaces() > 0) {
190 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
191 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
192 Class* interface = kh.GetDirectInterface(i);
193 const ClassLoader* cl = interface->GetClassLoader();
194 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
195 }
196 }
197 os << " vtable (" << NumVirtualMethods() << " entries, "
198 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
199 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
200 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
201 }
202 os << " direct methods (" << NumDirectMethods() << " entries):\n";
203 for (size_t i = 0; i < NumDirectMethods(); ++i) {
204 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
205 }
206 if (NumStaticFields() > 0) {
207 os << " static fields (" << NumStaticFields() << " entries):\n";
208 if (IsResolved() || IsErroneous()) {
209 for (size_t i = 0; i < NumStaticFields(); ++i) {
210 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
211 }
212 } else {
213 os << " <not yet available>";
214 }
215 }
216 if (NumInstanceFields() > 0) {
217 os << " instance fields (" << NumInstanceFields() << " entries):\n";
218 if (IsResolved() || IsErroneous()) {
219 for (size_t i = 0; i < NumInstanceFields(); ++i) {
220 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
221 }
222 } else {
223 os << " <not yet available>";
224 }
225 }
226}
227
228void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
229 if (new_reference_offsets != CLASS_WALK_SUPER) {
230 // Sanity check that the number of bits set in the reference offset bitmap
231 // agrees with the number of references
232 size_t count = 0;
233 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
234 count += c->NumReferenceInstanceFieldsDuringLinking();
235 }
236 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
237 }
238 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
239 new_reference_offsets, false);
240}
241
242void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
243 if (new_reference_offsets != CLASS_WALK_SUPER) {
244 // Sanity check that the number of bits set in the reference offset bitmap
245 // agrees with the number of references
246 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
247 NumReferenceStaticFieldsDuringLinking());
248 }
249 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
250 new_reference_offsets, false);
251}
252
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
254 size_t i = 0;
255 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
256 ++i;
257 }
258 if (descriptor1.find('/', i) != StringPiece::npos ||
259 descriptor2.find('/', i) != StringPiece::npos) {
260 return false;
261 } else {
262 return true;
263 }
264}
265
266bool Class::IsInSamePackage(const Class* that) const {
267 const Class* klass1 = this;
268 const Class* klass2 = that;
269 if (klass1 == klass2) {
270 return true;
271 }
272 // Class loaders must match.
273 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
274 return false;
275 }
276 // Arrays are in the same package when their element classes are.
277 while (klass1->IsArrayClass()) {
278 klass1 = klass1->GetComponentType();
279 }
280 while (klass2->IsArrayClass()) {
281 klass2 = klass2->GetComponentType();
282 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700283 // trivial check again for array types
284 if (klass1 == klass2) {
285 return true;
286 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 // Compare the package part of the descriptor string.
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700288 if (LIKELY(!klass1->IsProxyClass() && !klass2->IsProxyClass())) {
289 ClassHelper kh(klass1);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700290 const DexFile* dex_file1 = &kh.GetDexFile();
291 const DexFile::TypeId* type_id1 = &dex_file1->GetTypeId(klass1->GetDexTypeIndex());
292 const char* descriptor1 = dex_file1->GetTypeDescriptor(*type_id1);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700293 kh.ChangeClass(klass2);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700294 const DexFile* dex_file2 = &kh.GetDexFile();
295 const DexFile::TypeId* type_id2 = &dex_file2->GetTypeId(klass2->GetDexTypeIndex());
296 const char* descriptor2 = dex_file2->GetTypeDescriptor(*type_id2);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700297 return IsInSamePackage(descriptor1, descriptor2);
298 }
Brian Carlstrom857fe962013-03-26 22:46:15 -0700299 ClassHelper kh(klass1);
300 std::string descriptor1(kh.GetDescriptor());
301 kh.ChangeClass(klass2);
302 std::string descriptor2(kh.GetDescriptor());
303 return IsInSamePackage(descriptor1, descriptor2);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304}
305
306bool Class::IsClassClass() const {
307 Class* java_lang_Class = GetClass()->GetClass();
308 return this == java_lang_Class;
309}
310
311bool Class::IsStringClass() const {
312 return this == String::GetJavaLangString();
313}
314
315bool Class::IsThrowableClass() const {
316 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
317}
318
319bool Class::IsFieldClass() const {
320 Class* java_lang_Class = GetClass();
321 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
322 return this == java_lang_reflect_Field;
323
324}
325
326bool Class::IsMethodClass() const {
327 return (this == AbstractMethod::GetMethodClass()) ||
328 (this == AbstractMethod::GetConstructorClass());
329
330}
331
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332void Class::SetClassLoader(ClassLoader* new_class_loader) {
333 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
334}
335
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
337 // Check the current class before checking the interfaces.
338 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
339 if (method != NULL) {
340 return method;
341 }
342
343 int32_t iftable_count = GetIfTableCount();
344 IfTable* iftable = GetIfTable();
345 for (int32_t i = 0; i < iftable_count; i++) {
346 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
347 if (method != NULL) {
348 return method;
349 }
350 }
351 return NULL;
352}
353
354AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
355 // Check the current class before checking the interfaces.
356 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
357 if (method != NULL) {
358 return method;
359 }
360
361 int32_t iftable_count = GetIfTableCount();
362 IfTable* iftable = GetIfTable();
363 for (int32_t i = 0; i < iftable_count; i++) {
364 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
365 if (method != NULL) {
366 return method;
367 }
368 }
369 return NULL;
370}
371
372
373AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
374 MethodHelper mh;
375 for (size_t i = 0; i < NumDirectMethods(); ++i) {
376 AbstractMethod* method = GetDirectMethod(i);
377 mh.ChangeMethod(method);
378 if (name == mh.GetName() && signature == mh.GetSignature()) {
379 return method;
380 }
381 }
382 return NULL;
383}
384
385AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
386 if (GetDexCache() == dex_cache) {
387 for (size_t i = 0; i < NumDirectMethods(); ++i) {
388 AbstractMethod* method = GetDirectMethod(i);
389 if (method->GetDexMethodIndex() == dex_method_idx) {
390 return method;
391 }
392 }
393 }
394 return NULL;
395}
396
397AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
398 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
399 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
400 if (method != NULL) {
401 return method;
402 }
403 }
404 return NULL;
405}
406
407AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
408 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
409 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
410 if (method != NULL) {
411 return method;
412 }
413 }
414 return NULL;
415}
416
417AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
418 const StringPiece& signature) const {
419 MethodHelper mh;
420 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
421 AbstractMethod* method = GetVirtualMethod(i);
422 mh.ChangeMethod(method);
423 if (name == mh.GetName() && signature == mh.GetSignature()) {
424 return method;
425 }
426 }
427 return NULL;
428}
429
430AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
431 if (GetDexCache() == dex_cache) {
432 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
433 AbstractMethod* method = GetVirtualMethod(i);
434 if (method->GetDexMethodIndex() == dex_method_idx) {
435 return method;
436 }
437 }
438 }
439 return NULL;
440}
441
442AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
443 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
444 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
445 if (method != NULL) {
446 return method;
447 }
448 }
449 return NULL;
450}
451
452AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
453 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
454 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
455 if (method != NULL) {
456 return method;
457 }
458 }
459 return NULL;
460}
461
462Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
463 // Is the field in this class?
464 // Interfaces are not relevant because they can't contain instance fields.
465 FieldHelper fh;
466 for (size_t i = 0; i < NumInstanceFields(); ++i) {
467 Field* f = GetInstanceField(i);
468 fh.ChangeField(f);
469 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
470 return f;
471 }
472 }
473 return NULL;
474}
475
476Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
477 if (GetDexCache() == dex_cache) {
478 for (size_t i = 0; i < NumInstanceFields(); ++i) {
479 Field* f = GetInstanceField(i);
480 if (f->GetDexFieldIndex() == dex_field_idx) {
481 return f;
482 }
483 }
484 }
485 return NULL;
486}
487
488Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
489 // Is the field in this class, or any of its superclasses?
490 // Interfaces are not relevant because they can't contain instance fields.
491 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
492 Field* f = c->FindDeclaredInstanceField(name, type);
493 if (f != NULL) {
494 return f;
495 }
496 }
497 return NULL;
498}
499
500Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
501 // Is the field in this class, or any of its superclasses?
502 // Interfaces are not relevant because they can't contain instance fields.
503 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
504 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
505 if (f != NULL) {
506 return f;
507 }
508 }
509 return NULL;
510}
511
512Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
513 DCHECK(type != NULL);
514 FieldHelper fh;
515 for (size_t i = 0; i < NumStaticFields(); ++i) {
516 Field* f = GetStaticField(i);
517 fh.ChangeField(f);
518 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
519 return f;
520 }
521 }
522 return NULL;
523}
524
525Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
526 if (dex_cache == GetDexCache()) {
527 for (size_t i = 0; i < NumStaticFields(); ++i) {
528 Field* f = GetStaticField(i);
529 if (f->GetDexFieldIndex() == dex_field_idx) {
530 return f;
531 }
532 }
533 }
534 return NULL;
535}
536
537Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
538 // Is the field in this class (or its interfaces), or any of its
539 // superclasses (or their interfaces)?
540 ClassHelper kh;
541 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
542 // Is the field in this class?
543 Field* f = k->FindDeclaredStaticField(name, type);
544 if (f != NULL) {
545 return f;
546 }
547 // Is this field in any of this class' interfaces?
548 kh.ChangeClass(k);
549 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
550 Class* interface = kh.GetDirectInterface(i);
551 f = interface->FindStaticField(name, type);
552 if (f != NULL) {
553 return f;
554 }
555 }
556 }
557 return NULL;
558}
559
560Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
561 ClassHelper kh;
562 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
563 // Is the field in this class?
564 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
565 if (f != NULL) {
566 return f;
567 }
568 // Is this field in any of this class' interfaces?
569 kh.ChangeClass(k);
570 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
571 Class* interface = kh.GetDirectInterface(i);
572 f = interface->FindStaticField(dex_cache, dex_field_idx);
573 if (f != NULL) {
574 return f;
575 }
576 }
577 }
578 return NULL;
579}
580
581Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
582 // Find a field using the JLS field resolution order
583 ClassHelper kh;
584 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
585 // Is the field in this class?
586 Field* f = k->FindDeclaredInstanceField(name, type);
587 if (f != NULL) {
588 return f;
589 }
590 f = k->FindDeclaredStaticField(name, type);
591 if (f != NULL) {
592 return f;
593 }
594 // Is this field in any of this class' interfaces?
595 kh.ChangeClass(k);
596 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
597 Class* interface = kh.GetDirectInterface(i);
598 f = interface->FindStaticField(name, type);
599 if (f != NULL) {
600 return f;
601 }
602 }
603 }
604 return NULL;
605}
606
607} // namespace mirror
608} // namespace art