blob: 7f52d176160857a7f02119dcd5e9d6a89155ea6d [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"
24#include "field-inl.h"
25#include "gc/card_table-inl.h"
26#include "object-inl.h"
27#include "object_array-inl.h"
28#include "object_utils.h"
29#include "runtime.h"
30#include "sirt_ref.h"
31#include "thread.h"
32#include "throwable.h"
33#include "utils.h"
34#include "well_known_classes.h"
35
36namespace art {
37namespace mirror {
38
39Class* Class::java_lang_Class_ = NULL;
40
41void Class::SetClassClass(Class* java_lang_Class) {
42 CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class;
43 CHECK(java_lang_Class != NULL);
44 java_lang_Class_ = java_lang_Class;
45}
46
47void Class::ResetClass() {
48 CHECK(java_lang_Class_ != NULL);
49 java_lang_Class_ = NULL;
50}
51
52void Class::SetStatus(Status new_status) {
53 CHECK(new_status > GetStatus() || new_status == kStatusError || !Runtime::Current()->IsStarted())
54 << PrettyClass(this) << " " << GetStatus() << " -> " << new_status;
55 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
56 if (new_status > kStatusResolved) {
57 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId()) << PrettyClass(this);
58 }
59 if (new_status == kStatusError) {
60 CHECK_NE(GetStatus(), kStatusError) << PrettyClass(this);
61
62 // stash current exception
63 Thread* self = Thread::Current();
64 SirtRef<Throwable> exception(self, self->GetException());
65 CHECK(exception.get() != NULL);
66
67 // clear exception to call FindSystemClass
68 self->ClearException();
69 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
70 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
71 CHECK(!self->IsExceptionPending());
72
73 // only verification errors, not initialization problems, should set a verify error.
74 // this is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
75 Class* exception_class = exception->GetClass();
76 if (!eiie_class->IsAssignableFrom(exception_class)) {
77 SetVerifyErrorClass(exception_class);
78 }
79
80 // restore exception
81 self->SetException(exception.get());
82 }
83 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
84}
85
86DexCache* Class::GetDexCache() const {
87 return GetFieldObject<DexCache*>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), false);
88}
89
90void Class::SetDexCache(DexCache* new_dex_cache) {
91 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
92}
93
94Object* Class::AllocObject(Thread* self) {
95 DCHECK(!IsArrayClass()) << PrettyClass(this);
96 DCHECK(IsInstantiable()) << PrettyClass(this);
97 // TODO: decide whether we want this check. It currently fails during bootstrap.
98 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
99 DCHECK_GE(this->object_size_, sizeof(Object));
100 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
101}
102
103void Class::SetClassSize(size_t new_class_size) {
104 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
105 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
106}
107
108// Return the class' name. The exact format is bizarre, but it's the specified behavior for
109// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
110// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
111// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
112String* Class::ComputeName() {
113 String* name = GetName();
114 if (name != NULL) {
115 return name;
116 }
117 std::string descriptor(ClassHelper(this).GetDescriptor());
118 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
119 // The descriptor indicates that this is the class for
120 // a primitive type; special-case the return value.
121 const char* c_name = NULL;
122 switch (descriptor[0]) {
123 case 'Z': c_name = "boolean"; break;
124 case 'B': c_name = "byte"; break;
125 case 'C': c_name = "char"; break;
126 case 'S': c_name = "short"; break;
127 case 'I': c_name = "int"; break;
128 case 'J': c_name = "long"; break;
129 case 'F': c_name = "float"; break;
130 case 'D': c_name = "double"; break;
131 case 'V': c_name = "void"; break;
132 default:
133 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
134 }
135 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
136 } else {
137 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
138 // components.
139 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
140 descriptor.erase(0, 1);
141 descriptor.erase(descriptor.size() - 1);
142 }
143 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
144 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
145 }
146 SetName(name);
147 return name;
148}
149
150void Class::DumpClass(std::ostream& os, int flags) const {
151 if ((flags & kDumpClassFullDetail) == 0) {
152 os << PrettyClass(this);
153 if ((flags & kDumpClassClassLoader) != 0) {
154 os << ' ' << GetClassLoader();
155 }
156 if ((flags & kDumpClassInitialized) != 0) {
157 os << ' ' << GetStatus();
158 }
159 os << "\n";
160 return;
161 }
162
163 Class* super = GetSuperClass();
164 ClassHelper kh(this);
165 os << "----- " << (IsInterface() ? "interface" : "class") << " "
166 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
167 os << " objectSize=" << SizeOf() << " "
168 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
169 os << StringPrintf(" access=0x%04x.%04x\n",
170 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
171 if (super != NULL) {
172 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
173 }
174 if (IsArrayClass()) {
175 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
176 }
177 if (kh.NumDirectInterfaces() > 0) {
178 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
179 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
180 Class* interface = kh.GetDirectInterface(i);
181 const ClassLoader* cl = interface->GetClassLoader();
182 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
183 }
184 }
185 os << " vtable (" << NumVirtualMethods() << " entries, "
186 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
187 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
188 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
189 }
190 os << " direct methods (" << NumDirectMethods() << " entries):\n";
191 for (size_t i = 0; i < NumDirectMethods(); ++i) {
192 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
193 }
194 if (NumStaticFields() > 0) {
195 os << " static fields (" << NumStaticFields() << " entries):\n";
196 if (IsResolved() || IsErroneous()) {
197 for (size_t i = 0; i < NumStaticFields(); ++i) {
198 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
199 }
200 } else {
201 os << " <not yet available>";
202 }
203 }
204 if (NumInstanceFields() > 0) {
205 os << " instance fields (" << NumInstanceFields() << " entries):\n";
206 if (IsResolved() || IsErroneous()) {
207 for (size_t i = 0; i < NumInstanceFields(); ++i) {
208 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
209 }
210 } else {
211 os << " <not yet available>";
212 }
213 }
214}
215
216void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
217 if (new_reference_offsets != CLASS_WALK_SUPER) {
218 // Sanity check that the number of bits set in the reference offset bitmap
219 // agrees with the number of references
220 size_t count = 0;
221 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
222 count += c->NumReferenceInstanceFieldsDuringLinking();
223 }
224 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
225 }
226 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
227 new_reference_offsets, false);
228}
229
230void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
231 if (new_reference_offsets != CLASS_WALK_SUPER) {
232 // Sanity check that the number of bits set in the reference offset bitmap
233 // agrees with the number of references
234 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
235 NumReferenceStaticFieldsDuringLinking());
236 }
237 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
238 new_reference_offsets, false);
239}
240
241bool Class::Implements(const Class* klass) const {
242 DCHECK(klass != NULL);
243 DCHECK(klass->IsInterface()) << PrettyClass(this);
244 // All interfaces implemented directly and by our superclass, and
245 // recursively all super-interfaces of those interfaces, are listed
246 // in iftable_, so we can just do a linear scan through that.
247 int32_t iftable_count = GetIfTableCount();
248 IfTable* iftable = GetIfTable();
249 for (int32_t i = 0; i < iftable_count; i++) {
250 if (iftable->GetInterface(i) == klass) {
251 return true;
252 }
253 }
254 return false;
255}
256
257// Determine whether "this" is assignable from "src", where both of these
258// are array classes.
259//
260// Consider an array class, e.g. Y[][], where Y is a subclass of X.
261// Y[][] = Y[][] --> true (identity)
262// X[][] = Y[][] --> true (element superclass)
263// Y = Y[][] --> false
264// Y[] = Y[][] --> false
265// Object = Y[][] --> true (everything is an object)
266// Object[] = Y[][] --> true
267// Object[][] = Y[][] --> true
268// Object[][][] = Y[][] --> false (too many []s)
269// Serializable = Y[][] --> true (all arrays are Serializable)
270// Serializable[] = Y[][] --> true
271// Serializable[][] = Y[][] --> false (unless Y is Serializable)
272//
273// Don't forget about primitive types.
274// Object[] = int[] --> false
275//
276bool Class::IsArrayAssignableFromArray(const Class* src) const {
277 DCHECK(IsArrayClass()) << PrettyClass(this);
278 DCHECK(src->IsArrayClass()) << PrettyClass(src);
279 return GetComponentType()->IsAssignableFrom(src->GetComponentType());
280}
281
282bool Class::IsAssignableFromArray(const Class* src) const {
283 DCHECK(!IsInterface()) << PrettyClass(this); // handled first in IsAssignableFrom
284 DCHECK(src->IsArrayClass()) << PrettyClass(src);
285 if (!IsArrayClass()) {
286 // If "this" is not also an array, it must be Object.
287 // src's super should be java_lang_Object, since it is an array.
288 Class* java_lang_Object = src->GetSuperClass();
289 DCHECK(java_lang_Object != NULL) << PrettyClass(src);
290 DCHECK(java_lang_Object->GetSuperClass() == NULL) << PrettyClass(src);
291 return this == java_lang_Object;
292 }
293 return IsArrayAssignableFromArray(src);
294}
295
296bool Class::IsSubClass(const Class* klass) const {
297 DCHECK(!IsInterface()) << PrettyClass(this);
298 DCHECK(!IsArrayClass()) << PrettyClass(this);
299 const Class* current = this;
300 do {
301 if (current == klass) {
302 return true;
303 }
304 current = current->GetSuperClass();
305 } while (current != NULL);
306 return false;
307}
308
309bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
310 size_t i = 0;
311 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
312 ++i;
313 }
314 if (descriptor1.find('/', i) != StringPiece::npos ||
315 descriptor2.find('/', i) != StringPiece::npos) {
316 return false;
317 } else {
318 return true;
319 }
320}
321
322bool Class::IsInSamePackage(const Class* that) const {
323 const Class* klass1 = this;
324 const Class* klass2 = that;
325 if (klass1 == klass2) {
326 return true;
327 }
328 // Class loaders must match.
329 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
330 return false;
331 }
332 // Arrays are in the same package when their element classes are.
333 while (klass1->IsArrayClass()) {
334 klass1 = klass1->GetComponentType();
335 }
336 while (klass2->IsArrayClass()) {
337 klass2 = klass2->GetComponentType();
338 }
339 // Compare the package part of the descriptor string.
340 ClassHelper kh(klass1);
341 std::string descriptor1(kh.GetDescriptor());
342 kh.ChangeClass(klass2);
343 std::string descriptor2(kh.GetDescriptor());
344 return IsInSamePackage(descriptor1, descriptor2);
345}
346
347bool Class::IsClassClass() const {
348 Class* java_lang_Class = GetClass()->GetClass();
349 return this == java_lang_Class;
350}
351
352bool Class::IsStringClass() const {
353 return this == String::GetJavaLangString();
354}
355
356bool Class::IsThrowableClass() const {
357 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
358}
359
360bool Class::IsFieldClass() const {
361 Class* java_lang_Class = GetClass();
362 Class* java_lang_reflect_Field = java_lang_Class->GetInstanceField(0)->GetClass();
363 return this == java_lang_reflect_Field;
364
365}
366
367bool Class::IsMethodClass() const {
368 return (this == AbstractMethod::GetMethodClass()) ||
369 (this == AbstractMethod::GetConstructorClass());
370
371}
372
373ClassLoader* Class::GetClassLoader() const {
374 return GetFieldObject<ClassLoader*>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), false);
375}
376
377void Class::SetClassLoader(ClassLoader* new_class_loader) {
378 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
379}
380
381AbstractMethod* Class::FindVirtualMethodForInterface(AbstractMethod* method) const {
382 Class* declaring_class = method->GetDeclaringClass();
383 DCHECK(declaring_class != NULL) << PrettyClass(this);
384 DCHECK(declaring_class->IsInterface()) << PrettyMethod(method);
385 // TODO cache to improve lookup speed
386 int32_t iftable_count = GetIfTableCount();
387 IfTable* iftable = GetIfTable();
388 for (int32_t i = 0; i < iftable_count; i++) {
389 if (iftable->GetInterface(i) == declaring_class) {
390 return iftable->GetMethodArray(i)->Get(method->GetMethodIndex());
391 }
392 }
393 return NULL;
394}
395
396AbstractMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
397 // Check the current class before checking the interfaces.
398 AbstractMethod* method = FindDeclaredVirtualMethod(name, signature);
399 if (method != NULL) {
400 return method;
401 }
402
403 int32_t iftable_count = GetIfTableCount();
404 IfTable* iftable = GetIfTable();
405 for (int32_t i = 0; i < iftable_count; i++) {
406 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
407 if (method != NULL) {
408 return method;
409 }
410 }
411 return NULL;
412}
413
414AbstractMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
415 // Check the current class before checking the interfaces.
416 AbstractMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
417 if (method != NULL) {
418 return method;
419 }
420
421 int32_t iftable_count = GetIfTableCount();
422 IfTable* iftable = GetIfTable();
423 for (int32_t i = 0; i < iftable_count; i++) {
424 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
425 if (method != NULL) {
426 return method;
427 }
428 }
429 return NULL;
430}
431
432
433AbstractMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
434 MethodHelper mh;
435 for (size_t i = 0; i < NumDirectMethods(); ++i) {
436 AbstractMethod* method = GetDirectMethod(i);
437 mh.ChangeMethod(method);
438 if (name == mh.GetName() && signature == mh.GetSignature()) {
439 return method;
440 }
441 }
442 return NULL;
443}
444
445AbstractMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
446 if (GetDexCache() == dex_cache) {
447 for (size_t i = 0; i < NumDirectMethods(); ++i) {
448 AbstractMethod* method = GetDirectMethod(i);
449 if (method->GetDexMethodIndex() == dex_method_idx) {
450 return method;
451 }
452 }
453 }
454 return NULL;
455}
456
457AbstractMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
458 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
459 AbstractMethod* method = klass->FindDeclaredDirectMethod(name, signature);
460 if (method != NULL) {
461 return method;
462 }
463 }
464 return NULL;
465}
466
467AbstractMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
468 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
469 AbstractMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
470 if (method != NULL) {
471 return method;
472 }
473 }
474 return NULL;
475}
476
477AbstractMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
478 const StringPiece& signature) const {
479 MethodHelper mh;
480 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
481 AbstractMethod* method = GetVirtualMethod(i);
482 mh.ChangeMethod(method);
483 if (name == mh.GetName() && signature == mh.GetSignature()) {
484 return method;
485 }
486 }
487 return NULL;
488}
489
490AbstractMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
491 if (GetDexCache() == dex_cache) {
492 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
493 AbstractMethod* method = GetVirtualMethod(i);
494 if (method->GetDexMethodIndex() == dex_method_idx) {
495 return method;
496 }
497 }
498 }
499 return NULL;
500}
501
502AbstractMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
503 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
504 AbstractMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
505 if (method != NULL) {
506 return method;
507 }
508 }
509 return NULL;
510}
511
512AbstractMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
513 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
514 AbstractMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
515 if (method != NULL) {
516 return method;
517 }
518 }
519 return NULL;
520}
521
522Field* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
523 // 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) {
527 Field* f = GetInstanceField(i);
528 fh.ChangeField(f);
529 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
530 return f;
531 }
532 }
533 return NULL;
534}
535
536Field* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
537 if (GetDexCache() == dex_cache) {
538 for (size_t i = 0; i < NumInstanceFields(); ++i) {
539 Field* f = GetInstanceField(i);
540 if (f->GetDexFieldIndex() == dex_field_idx) {
541 return f;
542 }
543 }
544 }
545 return NULL;
546}
547
548Field* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
549 // 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()) {
552 Field* f = c->FindDeclaredInstanceField(name, type);
553 if (f != NULL) {
554 return f;
555 }
556 }
557 return NULL;
558}
559
560Field* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
561 // 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()) {
564 Field* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
565 if (f != NULL) {
566 return f;
567 }
568 }
569 return NULL;
570}
571
572Field* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
573 DCHECK(type != NULL);
574 FieldHelper fh;
575 for (size_t i = 0; i < NumStaticFields(); ++i) {
576 Field* f = GetStaticField(i);
577 fh.ChangeField(f);
578 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
579 return f;
580 }
581 }
582 return NULL;
583}
584
585Field* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
586 if (dex_cache == GetDexCache()) {
587 for (size_t i = 0; i < NumStaticFields(); ++i) {
588 Field* f = GetStaticField(i);
589 if (f->GetDexFieldIndex() == dex_field_idx) {
590 return f;
591 }
592 }
593 }
594 return NULL;
595}
596
597Field* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
598 // Is the field in this class (or its interfaces), or any of its
599 // superclasses (or their interfaces)?
600 ClassHelper kh;
601 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
602 // Is the field in this class?
603 Field* f = k->FindDeclaredStaticField(name, type);
604 if (f != NULL) {
605 return f;
606 }
607 // Is this field in any of this class' interfaces?
608 kh.ChangeClass(k);
609 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
610 Class* interface = kh.GetDirectInterface(i);
611 f = interface->FindStaticField(name, type);
612 if (f != NULL) {
613 return f;
614 }
615 }
616 }
617 return NULL;
618}
619
620Field* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
621 ClassHelper kh;
622 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
623 // Is the field in this class?
624 Field* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
625 if (f != NULL) {
626 return f;
627 }
628 // Is this field in any of this class' interfaces?
629 kh.ChangeClass(k);
630 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
631 Class* interface = kh.GetDirectInterface(i);
632 f = interface->FindStaticField(dex_cache, dex_field_idx);
633 if (f != NULL) {
634 return f;
635 }
636 }
637 }
638 return NULL;
639}
640
641Field* Class::FindField(const StringPiece& name, const StringPiece& type) {
642 // Find a field using the JLS field resolution order
643 ClassHelper kh;
644 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
645 // Is the field in this class?
646 Field* f = k->FindDeclaredInstanceField(name, type);
647 if (f != NULL) {
648 return f;
649 }
650 f = k->FindDeclaredStaticField(name, type);
651 if (f != NULL) {
652 return f;
653 }
654 // Is this field in any of this class' interfaces?
655 kh.ChangeClass(k);
656 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
657 Class* interface = kh.GetDirectInterface(i);
658 f = interface->FindStaticField(name, type);
659 if (f != NULL) {
660 return f;
661 }
662 }
663 }
664 return NULL;
665}
666
667} // namespace mirror
668} // namespace art