class.cc revision 1eb512d33f94d1dd7ea38263307ba0f7a0dfa653
1/*
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 "art_field-inl.h"
20#include "art_method-inl.h"
21#include "class-inl.h"
22#include "class_linker.h"
23#include "class_loader.h"
24#include "dex_cache.h"
25#include "dex_file-inl.h"
26#include "gc/accounting/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, Thread* self) {
54  Status old_status = GetStatus();
55  bool class_linker_initialized = Runtime::Current()->GetClassLinker() != nullptr;
56  if (LIKELY(class_linker_initialized)) {
57    if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) {
58      LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
59          << old_status << " -> " << new_status;
60    }
61    if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
62      // When classes are being resolved the resolution code should hold the lock.
63      CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
64            << "Attempt to change status of class while not holding its lock: "
65            << PrettyClass(this) << " " << old_status << " -> " << new_status;
66    }
67  }
68  if (new_status == kStatusError) {
69    CHECK_NE(GetStatus(), kStatusError)
70        << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
71
72    // Stash current exception.
73    SirtRef<mirror::Object> old_throw_this_object(self, NULL);
74    SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
75    SirtRef<mirror::Throwable> old_exception(self, NULL);
76    uint32_t old_throw_dex_pc;
77    {
78      ThrowLocation old_throw_location;
79      mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location);
80      old_throw_this_object.reset(old_throw_location.GetThis());
81      old_throw_method.reset(old_throw_location.GetMethod());
82      old_exception.reset(old_exception_obj);
83      old_throw_dex_pc = old_throw_location.GetDexPc();
84      self->ClearException();
85    }
86    CHECK(old_exception.get() != NULL);
87
88    // clear exception to call FindSystemClass
89    self->ClearException();
90    ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
91    Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
92    CHECK(!self->IsExceptionPending());
93
94    // Only verification errors, not initialization problems, should set a verify error.
95    // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
96    Class* exception_class = old_exception->GetClass();
97    if (!eiie_class->IsAssignableFrom(exception_class)) {
98      SetVerifyErrorClass(exception_class);
99    }
100
101    // Restore exception.
102    ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
103                                         old_throw_dex_pc);
104
105    self->SetException(gc_safe_throw_location, old_exception.get());
106  }
107  CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
108  SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
109  // Classes that are being resolved or initialized need to notify waiters that the class status
110  // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
111  if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
112      class_linker_initialized) {
113    NotifyAll(self);
114  }
115}
116
117void Class::SetDexCache(DexCache* new_dex_cache) {
118  SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
119}
120
121void Class::SetClassSize(size_t new_class_size) {
122  if (kIsDebugBuild && (new_class_size < GetClassSize())) {
123    DumpClass(LOG(ERROR), kDumpClassFullDetail);
124    CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
125  }
126  SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
127}
128
129// Return the class' name. The exact format is bizarre, but it's the specified behavior for
130// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
131// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
132// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
133String* Class::ComputeName() {
134  String* name = GetName();
135  if (name != NULL) {
136    return name;
137  }
138  std::string descriptor(ClassHelper(this).GetDescriptorAsStringPiece().as_string());
139  if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
140    // The descriptor indicates that this is the class for
141    // a primitive type; special-case the return value.
142    const char* c_name = NULL;
143    switch (descriptor[0]) {
144    case 'Z': c_name = "boolean"; break;
145    case 'B': c_name = "byte";    break;
146    case 'C': c_name = "char";    break;
147    case 'S': c_name = "short";   break;
148    case 'I': c_name = "int";     break;
149    case 'J': c_name = "long";    break;
150    case 'F': c_name = "float";   break;
151    case 'D': c_name = "double";  break;
152    case 'V': c_name = "void";    break;
153    default:
154      LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
155    }
156    name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
157  } else {
158    // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
159    // components.
160    if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
161      descriptor.erase(0, 1);
162      descriptor.erase(descriptor.size() - 1);
163    }
164    std::replace(descriptor.begin(), descriptor.end(), '/', '.');
165    name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
166  }
167  SetName(name);
168  return name;
169}
170
171void Class::DumpClass(std::ostream& os, int flags) const {
172  if ((flags & kDumpClassFullDetail) == 0) {
173    os << PrettyClass(this);
174    if ((flags & kDumpClassClassLoader) != 0) {
175      os << ' ' << GetClassLoader();
176    }
177    if ((flags & kDumpClassInitialized) != 0) {
178      os << ' ' << GetStatus();
179    }
180    os << "\n";
181    return;
182  }
183
184  Class* super = GetSuperClass();
185  ClassHelper kh(this);
186  os << "----- " << (IsInterface() ? "interface" : "class") << " "
187     << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
188  os << "  objectSize=" << SizeOf() << " "
189     << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
190  os << StringPrintf("  access=0x%04x.%04x\n",
191      GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
192  if (super != NULL) {
193    os << "  super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
194  }
195  if (IsArrayClass()) {
196    os << "  componentType=" << PrettyClass(GetComponentType()) << "\n";
197  }
198  if (kh.NumDirectInterfaces() > 0) {
199    os << "  interfaces (" << kh.NumDirectInterfaces() << "):\n";
200    for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
201      Class* interface = kh.GetDirectInterface(i);
202      const ClassLoader* cl = interface->GetClassLoader();
203      os << StringPrintf("    %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
204    }
205  }
206  os << "  vtable (" << NumVirtualMethods() << " entries, "
207     << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
208  for (size_t i = 0; i < NumVirtualMethods(); ++i) {
209    os << StringPrintf("    %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
210  }
211  os << "  direct methods (" << NumDirectMethods() << " entries):\n";
212  for (size_t i = 0; i < NumDirectMethods(); ++i) {
213    os << StringPrintf("    %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
214  }
215  if (NumStaticFields() > 0) {
216    os << "  static fields (" << NumStaticFields() << " entries):\n";
217    if (IsResolved() || IsErroneous()) {
218      for (size_t i = 0; i < NumStaticFields(); ++i) {
219        os << StringPrintf("    %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
220      }
221    } else {
222      os << "    <not yet available>";
223    }
224  }
225  if (NumInstanceFields() > 0) {
226    os << "  instance fields (" << NumInstanceFields() << " entries):\n";
227    if (IsResolved() || IsErroneous()) {
228      for (size_t i = 0; i < NumInstanceFields(); ++i) {
229        os << StringPrintf("    %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
230      }
231    } else {
232      os << "    <not yet available>";
233    }
234  }
235}
236
237void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
238  if (new_reference_offsets != CLASS_WALK_SUPER) {
239    // Sanity check that the number of bits set in the reference offset bitmap
240    // agrees with the number of references
241    size_t count = 0;
242    for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
243      count += c->NumReferenceInstanceFieldsDuringLinking();
244    }
245    CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
246  }
247  SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
248             new_reference_offsets, false);
249}
250
251void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
252  if (new_reference_offsets != CLASS_WALK_SUPER) {
253    // Sanity check that the number of bits set in the reference offset bitmap
254    // agrees with the number of references
255    CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
256             NumReferenceStaticFieldsDuringLinking());
257  }
258  SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
259             new_reference_offsets, false);
260}
261
262bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
263  size_t i = 0;
264  while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
265    ++i;
266  }
267  if (descriptor1.find('/', i) != StringPiece::npos ||
268      descriptor2.find('/', i) != StringPiece::npos) {
269    return false;
270  } else {
271    return true;
272  }
273}
274
275bool Class::IsInSamePackage(const Class* that) const {
276  const Class* klass1 = this;
277  const Class* klass2 = that;
278  if (klass1 == klass2) {
279    return true;
280  }
281  // Class loaders must match.
282  if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
283    return false;
284  }
285  // Arrays are in the same package when their element classes are.
286  while (klass1->IsArrayClass()) {
287    klass1 = klass1->GetComponentType();
288  }
289  while (klass2->IsArrayClass()) {
290    klass2 = klass2->GetComponentType();
291  }
292  // trivial check again for array types
293  if (klass1 == klass2) {
294    return true;
295  }
296  // Compare the package part of the descriptor string.
297  return IsInSamePackage(ClassHelper(klass1).GetDescriptorAsStringPiece(),
298                         ClassHelper(klass2).GetDescriptorAsStringPiece());
299}
300
301bool Class::IsClassClass() const {
302  Class* java_lang_Class = GetClass()->GetClass();
303  return this == java_lang_Class;
304}
305
306bool Class::IsStringClass() const {
307  return this == String::GetJavaLangString();
308}
309
310bool Class::IsThrowableClass() const {
311  return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
312}
313
314bool Class::IsArtFieldClass() const {
315  Class* java_lang_Class = GetClass();
316  Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
317  return this == java_lang_reflect_ArtField;
318}
319
320bool Class::IsArtMethodClass() const {
321  return this == ArtMethod::GetJavaLangReflectArtMethod();
322}
323
324void Class::SetClassLoader(ClassLoader* new_class_loader) {
325  SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
326}
327
328ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) const {
329  // Check the current class before checking the interfaces.
330  ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
331  if (method != NULL) {
332    return method;
333  }
334
335  int32_t iftable_count = GetIfTableCount();
336  IfTable* iftable = GetIfTable();
337  for (int32_t i = 0; i < iftable_count; i++) {
338    method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
339    if (method != NULL) {
340      return method;
341    }
342  }
343  return NULL;
344}
345
346ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
347  // Check the current class before checking the interfaces.
348  ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
349  if (method != NULL) {
350    return method;
351  }
352
353  int32_t iftable_count = GetIfTableCount();
354  IfTable* iftable = GetIfTable();
355  for (int32_t i = 0; i < iftable_count; i++) {
356    method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
357    if (method != NULL) {
358      return method;
359    }
360  }
361  return NULL;
362}
363
364ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
365  MethodHelper mh;
366  for (size_t i = 0; i < NumDirectMethods(); ++i) {
367    ArtMethod* method = GetDirectMethod(i);
368    mh.ChangeMethod(method);
369    if (name == mh.GetNameAsStringPiece() && mh.GetSignature() == signature) {
370      return method;
371    }
372  }
373  return NULL;
374}
375
376ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const {
377  MethodHelper mh;
378  for (size_t i = 0; i < NumDirectMethods(); ++i) {
379    ArtMethod* method = GetDirectMethod(i);
380    mh.ChangeMethod(method);
381    if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) {
382      return method;
383    }
384  }
385  return NULL;
386}
387
388ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
389  if (GetDexCache() == dex_cache) {
390    for (size_t i = 0; i < NumDirectMethods(); ++i) {
391      ArtMethod* method = GetDirectMethod(i);
392      if (method->GetDexMethodIndex() == dex_method_idx) {
393        return method;
394      }
395    }
396  }
397  return NULL;
398}
399
400ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
401  for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
402    ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
403    if (method != NULL) {
404      return method;
405    }
406  }
407  return NULL;
408}
409
410ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) const {
411  for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
412    ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
413    if (method != NULL) {
414      return method;
415    }
416  }
417  return NULL;
418}
419
420ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
421  for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
422    ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
423    if (method != NULL) {
424      return method;
425    }
426  }
427  return NULL;
428}
429
430ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
431  MethodHelper mh;
432  for (size_t i = 0; i < NumVirtualMethods(); ++i) {
433    ArtMethod* method = GetVirtualMethod(i);
434    mh.ChangeMethod(method);
435    if (name == mh.GetNameAsStringPiece() && mh.GetSignature() == signature) {
436      return method;
437    }
438  }
439  return NULL;
440}
441
442ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
443                                            const Signature& signature) const {
444  MethodHelper mh;
445  for (size_t i = 0; i < NumVirtualMethods(); ++i) {
446    ArtMethod* method = GetVirtualMethod(i);
447    mh.ChangeMethod(method);
448    if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) {
449      return method;
450    }
451  }
452  return NULL;
453}
454
455ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
456  if (GetDexCache() == dex_cache) {
457    for (size_t i = 0; i < NumVirtualMethods(); ++i) {
458      ArtMethod* method = GetVirtualMethod(i);
459      if (method->GetDexMethodIndex() == dex_method_idx) {
460        return method;
461      }
462    }
463  }
464  return NULL;
465}
466
467ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
468  for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
469    ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
470    if (method != NULL) {
471      return method;
472    }
473  }
474  return NULL;
475}
476
477ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) const {
478  for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
479    ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
480    if (method != NULL) {
481      return method;
482    }
483  }
484  return NULL;
485}
486
487ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
488  for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
489    ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
490    if (method != NULL) {
491      return method;
492    }
493  }
494  return NULL;
495}
496
497ArtMethod* Class::FindClassInitializer() const {
498  for (size_t i = 0; i < NumDirectMethods(); ++i) {
499    ArtMethod* method = GetDirectMethod(i);
500    if (method->IsConstructor() && method->IsStatic()) {
501      if (kIsDebugBuild) {
502        MethodHelper mh(method);
503        CHECK(mh.IsClassInitializer());
504        CHECK_STREQ(mh.GetName(), "<clinit>");
505        CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
506      }
507      return method;
508    }
509  }
510  return NULL;
511}
512
513ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
514  // Is the field in this class?
515  // Interfaces are not relevant because they can't contain instance fields.
516  FieldHelper fh;
517  for (size_t i = 0; i < NumInstanceFields(); ++i) {
518    ArtField* f = GetInstanceField(i);
519    fh.ChangeField(f);
520    if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) {
521      return f;
522    }
523  }
524  return NULL;
525}
526
527ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
528  if (GetDexCache() == dex_cache) {
529    for (size_t i = 0; i < NumInstanceFields(); ++i) {
530      ArtField* f = GetInstanceField(i);
531      if (f->GetDexFieldIndex() == dex_field_idx) {
532        return f;
533      }
534    }
535  }
536  return NULL;
537}
538
539ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
540  // Is the field in this class, or any of its superclasses?
541  // Interfaces are not relevant because they can't contain instance fields.
542  for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
543    ArtField* f = c->FindDeclaredInstanceField(name, type);
544    if (f != NULL) {
545      return f;
546    }
547  }
548  return NULL;
549}
550
551ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
552  // Is the field in this class, or any of its superclasses?
553  // Interfaces are not relevant because they can't contain instance fields.
554  for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
555    ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
556    if (f != NULL) {
557      return f;
558    }
559  }
560  return NULL;
561}
562
563ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
564  DCHECK(type != NULL);
565  FieldHelper fh;
566  for (size_t i = 0; i < NumStaticFields(); ++i) {
567    ArtField* f = GetStaticField(i);
568    fh.ChangeField(f);
569    if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) {
570      return f;
571    }
572  }
573  return NULL;
574}
575
576ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
577  if (dex_cache == GetDexCache()) {
578    for (size_t i = 0; i < NumStaticFields(); ++i) {
579      ArtField* f = GetStaticField(i);
580      if (f->GetDexFieldIndex() == dex_field_idx) {
581        return f;
582      }
583    }
584  }
585  return NULL;
586}
587
588ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
589  // Is the field in this class (or its interfaces), or any of its
590  // superclasses (or their interfaces)?
591  ClassHelper kh;
592  for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
593    // Is the field in this class?
594    ArtField* f = k->FindDeclaredStaticField(name, type);
595    if (f != NULL) {
596      return f;
597    }
598    // Is this field in any of this class' interfaces?
599    kh.ChangeClass(k);
600    for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
601      Class* interface = kh.GetDirectInterface(i);
602      f = interface->FindStaticField(name, type);
603      if (f != NULL) {
604        return f;
605      }
606    }
607  }
608  return NULL;
609}
610
611ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
612  ClassHelper kh;
613  for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
614    // Is the field in this class?
615    ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
616    if (f != NULL) {
617      return f;
618    }
619    // Is this field in any of this class' interfaces?
620    kh.ChangeClass(k);
621    for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
622      Class* interface = kh.GetDirectInterface(i);
623      f = interface->FindStaticField(dex_cache, dex_field_idx);
624      if (f != NULL) {
625        return f;
626      }
627    }
628  }
629  return NULL;
630}
631
632ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
633  // Find a field using the JLS field resolution order
634  ClassHelper kh;
635  for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
636    // Is the field in this class?
637    ArtField* f = k->FindDeclaredInstanceField(name, type);
638    if (f != NULL) {
639      return f;
640    }
641    f = k->FindDeclaredStaticField(name, type);
642    if (f != NULL) {
643      return f;
644    }
645    // Is this field in any of this class' interfaces?
646    kh.ChangeClass(k);
647    for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
648      Class* interface = kh.GetDirectInterface(i);
649      f = interface->FindStaticField(name, type);
650      if (f != NULL) {
651        return f;
652      }
653    }
654  }
655  return NULL;
656}
657
658static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
659    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
660  if (methods != NULL) {
661    for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
662      mirror::ArtMethod* method = methods->GetWithoutChecks(index);
663      DCHECK(method != NULL);
664      if (!method->IsNative() && !method->IsAbstract()) {
665        method->SetPreverified();
666      }
667    }
668  }
669}
670
671void Class::SetPreverifiedFlagOnAllMethods() {
672  DCHECK(IsVerified());
673  SetPreverifiedFlagOnMethods(GetDirectMethods());
674  SetPreverifiedFlagOnMethods(GetVirtualMethods());
675}
676
677}  // namespace mirror
678}  // namespace art
679