blob: 287e8b0959fb72e7b5a6ecef5192b718933afb03 [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
Ian Rogers7dfb28c2013-08-22 08:18:36 -070053void 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)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070058 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070059 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070060 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070061 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
62 // When classes are being resolved the resolution code should hold the lock.
63 CHECK_EQ(GetThinLockId(), self->GetThinLockId())
64 << "Attempt to change status of class while not holding its lock: "
65 << PrettyClass(this) << " " << old_status << " -> " << new_status;
66 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 }
68 if (new_status == kStatusError) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070069 CHECK_NE(GetStatus(), kStatusError)
70 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071
Ian Rogers62d6c772013-02-27 08:32:07 -080072 // Stash current exception.
Ian Rogers62d6c772013-02-27 08:32:07 -080073 SirtRef<mirror::Object> old_throw_this_object(self, NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -070074 SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -080075 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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087
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
Ian Rogers62d6c772013-02-27 08:32:07 -080094 // 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();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 if (!eiie_class->IsAssignableFrom(exception_class)) {
98 SetVerifyErrorClass(exception_class);
99 }
100
Ian Rogers62d6c772013-02-27 08:32:07 -0800101 // 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());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700107 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700108 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 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115}
116
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117void Class::SetDexCache(DexCache* new_dex_cache) {
118 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
119}
120
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121void Class::SetClassSize(size_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700122 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
123 DumpClass(LOG(ERROR), kDumpClassFullDetail);
124 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
125 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126 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 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700138 std::string descriptor(ClassHelper(this).GetDescriptorAsStringPiece().as_string());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262bool 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 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700292 // trivial check again for array types
293 if (klass1 == klass2) {
294 return true;
295 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 // Compare the package part of the descriptor string.
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700297 return IsInSamePackage(ClassHelper(klass1).GetDescriptorAsStringPiece(),
298 ClassHelper(klass2).GetDescriptorAsStringPiece());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299}
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
Brian Carlstromea46f952013-07-30 01:26:50 -0700314bool Class::IsArtFieldClass() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700316 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
317 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318}
319
Brian Carlstromea46f952013-07-30 01:26:50 -0700320bool Class::IsArtMethodClass() const {
321 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322}
323
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800324void Class::SetClassLoader(ClassLoader* new_class_loader) {
325 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
326}
327
Brian Carlstromea46f952013-07-30 01:26:50 -0700328ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700330 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700346ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700348 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 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
364
Brian Carlstromea46f952013-07-30 01:26:50 -0700365ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 MethodHelper mh;
367 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 mh.ChangeMethod(method);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700370 if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 return method;
372 }
373 }
374 return NULL;
375}
376
Brian Carlstromea46f952013-07-30 01:26:50 -0700377ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 if (GetDexCache() == dex_cache) {
379 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700380 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800381 if (method->GetDexMethodIndex() == dex_method_idx) {
382 return method;
383 }
384 }
385 }
386 return NULL;
387}
388
Brian Carlstromea46f952013-07-30 01:26:50 -0700389ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700391 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 if (method != NULL) {
393 return method;
394 }
395 }
396 return NULL;
397}
398
Brian Carlstromea46f952013-07-30 01:26:50 -0700399ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700401 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 if (method != NULL) {
403 return method;
404 }
405 }
406 return NULL;
407}
408
Brian Carlstromea46f952013-07-30 01:26:50 -0700409ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 const StringPiece& signature) const {
411 MethodHelper mh;
412 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700413 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414 mh.ChangeMethod(method);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700415 if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 return method;
417 }
418 }
419 return NULL;
420}
421
Brian Carlstromea46f952013-07-30 01:26:50 -0700422ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 if (GetDexCache() == dex_cache) {
424 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700425 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 if (method->GetDexMethodIndex() == dex_method_idx) {
427 return method;
428 }
429 }
430 }
431 return NULL;
432}
433
Brian Carlstromea46f952013-07-30 01:26:50 -0700434ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437 if (method != NULL) {
438 return method;
439 }
440 }
441 return NULL;
442}
443
Brian Carlstromea46f952013-07-30 01:26:50 -0700444ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700446 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447 if (method != NULL) {
448 return method;
449 }
450 }
451 return NULL;
452}
453
Brian Carlstromea46f952013-07-30 01:26:50 -0700454ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455 // Is the field in this class?
456 // Interfaces are not relevant because they can't contain instance fields.
457 FieldHelper fh;
458 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700459 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800460 fh.ChangeField(f);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700461 if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800462 return f;
463 }
464 }
465 return NULL;
466}
467
Brian Carlstromea46f952013-07-30 01:26:50 -0700468ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469 if (GetDexCache() == dex_cache) {
470 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700471 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 if (f->GetDexFieldIndex() == dex_field_idx) {
473 return f;
474 }
475 }
476 }
477 return NULL;
478}
479
Brian Carlstromea46f952013-07-30 01:26:50 -0700480ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 // Is the field in this class, or any of its superclasses?
482 // Interfaces are not relevant because they can't contain instance fields.
483 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700484 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 if (f != NULL) {
486 return f;
487 }
488 }
489 return NULL;
490}
491
Brian Carlstromea46f952013-07-30 01:26:50 -0700492ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493 // Is the field in this class, or any of its superclasses?
494 // Interfaces are not relevant because they can't contain instance fields.
495 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700496 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800497 if (f != NULL) {
498 return f;
499 }
500 }
501 return NULL;
502}
503
Brian Carlstromea46f952013-07-30 01:26:50 -0700504ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505 DCHECK(type != NULL);
506 FieldHelper fh;
507 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700508 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800509 fh.ChangeField(f);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700510 if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511 return f;
512 }
513 }
514 return NULL;
515}
516
Brian Carlstromea46f952013-07-30 01:26:50 -0700517ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518 if (dex_cache == GetDexCache()) {
519 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700520 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800521 if (f->GetDexFieldIndex() == dex_field_idx) {
522 return f;
523 }
524 }
525 }
526 return NULL;
527}
528
Brian Carlstromea46f952013-07-30 01:26:50 -0700529ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 // Is the field in this class (or its interfaces), or any of its
531 // superclasses (or their interfaces)?
532 ClassHelper kh;
533 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
534 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700535 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536 if (f != NULL) {
537 return f;
538 }
539 // Is this field in any of this class' interfaces?
540 kh.ChangeClass(k);
541 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
542 Class* interface = kh.GetDirectInterface(i);
543 f = interface->FindStaticField(name, type);
544 if (f != NULL) {
545 return f;
546 }
547 }
548 }
549 return NULL;
550}
551
Brian Carlstromea46f952013-07-30 01:26:50 -0700552ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800553 ClassHelper kh;
554 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
555 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 if (f != NULL) {
558 return f;
559 }
560 // Is this field in any of this class' interfaces?
561 kh.ChangeClass(k);
562 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
563 Class* interface = kh.GetDirectInterface(i);
564 f = interface->FindStaticField(dex_cache, dex_field_idx);
565 if (f != NULL) {
566 return f;
567 }
568 }
569 }
570 return NULL;
571}
572
Brian Carlstromea46f952013-07-30 01:26:50 -0700573ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 // Find a field using the JLS field resolution order
575 ClassHelper kh;
576 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
577 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700578 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 if (f != NULL) {
580 return f;
581 }
582 f = k->FindDeclaredStaticField(name, type);
583 if (f != NULL) {
584 return f;
585 }
586 // Is this field in any of this class' interfaces?
587 kh.ChangeClass(k);
588 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
589 Class* interface = kh.GetDirectInterface(i);
590 f = interface->FindStaticField(name, type);
591 if (f != NULL) {
592 return f;
593 }
594 }
595 }
596 return NULL;
597}
598
Brian Carlstromea46f952013-07-30 01:26:50 -0700599static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200600 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
601 if (methods != NULL) {
602 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700603 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200604 DCHECK(method != NULL);
605 method->SetPreverified();
606 }
607 }
608}
609
610void Class::SetPreverifiedFlagOnAllMethods() {
611 DCHECK(IsVerified());
612 SetPreverifiedFlagOnMethods(GetDirectMethods());
613 SetPreverifiedFlagOnMethods(GetVirtualMethods());
614}
615
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800616} // namespace mirror
617} // namespace art