blob: 328c67deb111f55302833b22dd950dc6fffb9035 [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) {
122 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
123 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
124}
125
126// Return the class' name. The exact format is bizarre, but it's the specified behavior for
127// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
128// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
129// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
130String* Class::ComputeName() {
131 String* name = GetName();
132 if (name != NULL) {
133 return name;
134 }
135 std::string descriptor(ClassHelper(this).GetDescriptor());
136 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
137 // The descriptor indicates that this is the class for
138 // a primitive type; special-case the return value.
139 const char* c_name = NULL;
140 switch (descriptor[0]) {
141 case 'Z': c_name = "boolean"; break;
142 case 'B': c_name = "byte"; break;
143 case 'C': c_name = "char"; break;
144 case 'S': c_name = "short"; break;
145 case 'I': c_name = "int"; break;
146 case 'J': c_name = "long"; break;
147 case 'F': c_name = "float"; break;
148 case 'D': c_name = "double"; break;
149 case 'V': c_name = "void"; break;
150 default:
151 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
152 }
153 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
154 } else {
155 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
156 // components.
157 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
158 descriptor.erase(0, 1);
159 descriptor.erase(descriptor.size() - 1);
160 }
161 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
162 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
163 }
164 SetName(name);
165 return name;
166}
167
168void Class::DumpClass(std::ostream& os, int flags) const {
169 if ((flags & kDumpClassFullDetail) == 0) {
170 os << PrettyClass(this);
171 if ((flags & kDumpClassClassLoader) != 0) {
172 os << ' ' << GetClassLoader();
173 }
174 if ((flags & kDumpClassInitialized) != 0) {
175 os << ' ' << GetStatus();
176 }
177 os << "\n";
178 return;
179 }
180
181 Class* super = GetSuperClass();
182 ClassHelper kh(this);
183 os << "----- " << (IsInterface() ? "interface" : "class") << " "
184 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
185 os << " objectSize=" << SizeOf() << " "
186 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
187 os << StringPrintf(" access=0x%04x.%04x\n",
188 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
189 if (super != NULL) {
190 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
191 }
192 if (IsArrayClass()) {
193 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
194 }
195 if (kh.NumDirectInterfaces() > 0) {
196 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
197 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
198 Class* interface = kh.GetDirectInterface(i);
199 const ClassLoader* cl = interface->GetClassLoader();
200 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
201 }
202 }
203 os << " vtable (" << NumVirtualMethods() << " entries, "
204 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
205 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
206 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
207 }
208 os << " direct methods (" << NumDirectMethods() << " entries):\n";
209 for (size_t i = 0; i < NumDirectMethods(); ++i) {
210 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
211 }
212 if (NumStaticFields() > 0) {
213 os << " static fields (" << NumStaticFields() << " entries):\n";
214 if (IsResolved() || IsErroneous()) {
215 for (size_t i = 0; i < NumStaticFields(); ++i) {
216 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
217 }
218 } else {
219 os << " <not yet available>";
220 }
221 }
222 if (NumInstanceFields() > 0) {
223 os << " instance fields (" << NumInstanceFields() << " entries):\n";
224 if (IsResolved() || IsErroneous()) {
225 for (size_t i = 0; i < NumInstanceFields(); ++i) {
226 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
227 }
228 } else {
229 os << " <not yet available>";
230 }
231 }
232}
233
234void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
235 if (new_reference_offsets != CLASS_WALK_SUPER) {
236 // Sanity check that the number of bits set in the reference offset bitmap
237 // agrees with the number of references
238 size_t count = 0;
239 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
240 count += c->NumReferenceInstanceFieldsDuringLinking();
241 }
242 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
243 }
244 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
245 new_reference_offsets, false);
246}
247
248void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
249 if (new_reference_offsets != CLASS_WALK_SUPER) {
250 // Sanity check that the number of bits set in the reference offset bitmap
251 // agrees with the number of references
252 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
253 NumReferenceStaticFieldsDuringLinking());
254 }
255 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
256 new_reference_offsets, false);
257}
258
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
260 size_t i = 0;
261 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
262 ++i;
263 }
264 if (descriptor1.find('/', i) != StringPiece::npos ||
265 descriptor2.find('/', i) != StringPiece::npos) {
266 return false;
267 } else {
268 return true;
269 }
270}
271
272bool Class::IsInSamePackage(const Class* that) const {
273 const Class* klass1 = this;
274 const Class* klass2 = that;
275 if (klass1 == klass2) {
276 return true;
277 }
278 // Class loaders must match.
279 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
280 return false;
281 }
282 // Arrays are in the same package when their element classes are.
283 while (klass1->IsArrayClass()) {
284 klass1 = klass1->GetComponentType();
285 }
286 while (klass2->IsArrayClass()) {
287 klass2 = klass2->GetComponentType();
288 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700289 // trivial check again for array types
290 if (klass1 == klass2) {
291 return true;
292 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 // Compare the package part of the descriptor string.
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700294 if (LIKELY(!klass1->IsProxyClass() && !klass2->IsProxyClass())) {
295 ClassHelper kh(klass1);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700296 const DexFile* dex_file1 = &kh.GetDexFile();
297 const DexFile::TypeId* type_id1 = &dex_file1->GetTypeId(klass1->GetDexTypeIndex());
298 const char* descriptor1 = dex_file1->GetTypeDescriptor(*type_id1);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700299 kh.ChangeClass(klass2);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700300 const DexFile* dex_file2 = &kh.GetDexFile();
301 const DexFile::TypeId* type_id2 = &dex_file2->GetTypeId(klass2->GetDexTypeIndex());
302 const char* descriptor2 = dex_file2->GetTypeDescriptor(*type_id2);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700303 return IsInSamePackage(descriptor1, descriptor2);
304 }
Brian Carlstrom857fe962013-03-26 22:46:15 -0700305 ClassHelper kh(klass1);
306 std::string descriptor1(kh.GetDescriptor());
307 kh.ChangeClass(klass2);
308 std::string descriptor2(kh.GetDescriptor());
309 return IsInSamePackage(descriptor1, descriptor2);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310}
311
312bool Class::IsClassClass() const {
313 Class* java_lang_Class = GetClass()->GetClass();
314 return this == java_lang_Class;
315}
316
317bool Class::IsStringClass() const {
318 return this == String::GetJavaLangString();
319}
320
321bool Class::IsThrowableClass() const {
322 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
323}
324
Brian Carlstromea46f952013-07-30 01:26:50 -0700325bool Class::IsArtFieldClass() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700327 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
328 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329}
330
Brian Carlstromea46f952013-07-30 01:26:50 -0700331bool Class::IsArtMethodClass() const {
332 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333}
334
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335void Class::SetClassLoader(ClassLoader* new_class_loader) {
336 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
337}
338
Brian Carlstromea46f952013-07-30 01:26:50 -0700339ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700341 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 if (method != NULL) {
343 return method;
344 }
345
346 int32_t iftable_count = GetIfTableCount();
347 IfTable* iftable = GetIfTable();
348 for (int32_t i = 0; i < iftable_count; i++) {
349 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
350 if (method != NULL) {
351 return method;
352 }
353 }
354 return NULL;
355}
356
Brian Carlstromea46f952013-07-30 01:26:50 -0700357ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700359 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 if (method != NULL) {
361 return method;
362 }
363
364 int32_t iftable_count = GetIfTableCount();
365 IfTable* iftable = GetIfTable();
366 for (int32_t i = 0; i < iftable_count; i++) {
367 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
368 if (method != NULL) {
369 return method;
370 }
371 }
372 return NULL;
373}
374
375
Brian Carlstromea46f952013-07-30 01:26:50 -0700376ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 MethodHelper mh;
378 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700379 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 mh.ChangeMethod(method);
381 if (name == mh.GetName() && signature == mh.GetSignature()) {
382 return method;
383 }
384 }
385 return NULL;
386}
387
Brian Carlstromea46f952013-07-30 01:26:50 -0700388ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 if (GetDexCache() == dex_cache) {
390 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700391 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 if (method->GetDexMethodIndex() == dex_method_idx) {
393 return method;
394 }
395 }
396 }
397 return NULL;
398}
399
Brian Carlstromea46f952013-07-30 01:26:50 -0700400ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700402 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 if (method != NULL) {
404 return method;
405 }
406 }
407 return NULL;
408}
409
Brian Carlstromea46f952013-07-30 01:26:50 -0700410ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700412 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 if (method != NULL) {
414 return method;
415 }
416 }
417 return NULL;
418}
419
Brian Carlstromea46f952013-07-30 01:26:50 -0700420ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 const StringPiece& signature) const {
422 MethodHelper mh;
423 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700424 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425 mh.ChangeMethod(method);
426 if (name == mh.GetName() && signature == mh.GetSignature()) {
427 return method;
428 }
429 }
430 return NULL;
431}
432
Brian Carlstromea46f952013-07-30 01:26:50 -0700433ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 if (GetDexCache() == dex_cache) {
435 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437 if (method->GetDexMethodIndex() == dex_method_idx) {
438 return method;
439 }
440 }
441 }
442 return NULL;
443}
444
Brian Carlstromea46f952013-07-30 01:26:50 -0700445ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700447 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448 if (method != NULL) {
449 return method;
450 }
451 }
452 return NULL;
453}
454
Brian Carlstromea46f952013-07-30 01:26:50 -0700455ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700457 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800458 if (method != NULL) {
459 return method;
460 }
461 }
462 return NULL;
463}
464
Brian Carlstromea46f952013-07-30 01:26:50 -0700465ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466 // Is the field in this class?
467 // Interfaces are not relevant because they can't contain instance fields.
468 FieldHelper fh;
469 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700470 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 fh.ChangeField(f);
472 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
473 return f;
474 }
475 }
476 return NULL;
477}
478
Brian Carlstromea46f952013-07-30 01:26:50 -0700479ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480 if (GetDexCache() == dex_cache) {
481 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700482 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483 if (f->GetDexFieldIndex() == dex_field_idx) {
484 return f;
485 }
486 }
487 }
488 return NULL;
489}
490
Brian Carlstromea46f952013-07-30 01:26:50 -0700491ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492 // Is the field in this class, or any of its superclasses?
493 // Interfaces are not relevant because they can't contain instance fields.
494 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700495 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496 if (f != NULL) {
497 return f;
498 }
499 }
500 return NULL;
501}
502
Brian Carlstromea46f952013-07-30 01:26:50 -0700503ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800504 // Is the field in this class, or any of its superclasses?
505 // Interfaces are not relevant because they can't contain instance fields.
506 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700507 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508 if (f != NULL) {
509 return f;
510 }
511 }
512 return NULL;
513}
514
Brian Carlstromea46f952013-07-30 01:26:50 -0700515ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516 DCHECK(type != NULL);
517 FieldHelper fh;
518 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700519 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 fh.ChangeField(f);
521 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
522 return f;
523 }
524 }
525 return NULL;
526}
527
Brian Carlstromea46f952013-07-30 01:26:50 -0700528ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800529 if (dex_cache == GetDexCache()) {
530 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700531 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800532 if (f->GetDexFieldIndex() == dex_field_idx) {
533 return f;
534 }
535 }
536 }
537 return NULL;
538}
539
Brian Carlstromea46f952013-07-30 01:26:50 -0700540ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 // Is the field in this class (or its interfaces), or any of its
542 // superclasses (or their interfaces)?
543 ClassHelper kh;
544 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
545 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700546 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 if (f != NULL) {
548 return f;
549 }
550 // Is this field in any of this class' interfaces?
551 kh.ChangeClass(k);
552 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
553 Class* interface = kh.GetDirectInterface(i);
554 f = interface->FindStaticField(name, type);
555 if (f != NULL) {
556 return f;
557 }
558 }
559 }
560 return NULL;
561}
562
Brian Carlstromea46f952013-07-30 01:26:50 -0700563ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 ClassHelper kh;
565 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
566 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700567 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568 if (f != NULL) {
569 return f;
570 }
571 // Is this field in any of this class' interfaces?
572 kh.ChangeClass(k);
573 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
574 Class* interface = kh.GetDirectInterface(i);
575 f = interface->FindStaticField(dex_cache, dex_field_idx);
576 if (f != NULL) {
577 return f;
578 }
579 }
580 }
581 return NULL;
582}
583
Brian Carlstromea46f952013-07-30 01:26:50 -0700584ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800585 // Find a field using the JLS field resolution order
586 ClassHelper kh;
587 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
588 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700589 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800590 if (f != NULL) {
591 return f;
592 }
593 f = k->FindDeclaredStaticField(name, type);
594 if (f != NULL) {
595 return f;
596 }
597 // Is this field in any of this class' interfaces?
598 kh.ChangeClass(k);
599 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
600 Class* interface = kh.GetDirectInterface(i);
601 f = interface->FindStaticField(name, type);
602 if (f != NULL) {
603 return f;
604 }
605 }
606 }
607 return NULL;
608}
609
Brian Carlstromea46f952013-07-30 01:26:50 -0700610static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200611 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
612 if (methods != NULL) {
613 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200615 DCHECK(method != NULL);
616 method->SetPreverified();
617 }
618 }
619}
620
621void Class::SetPreverifiedFlagOnAllMethods() {
622 DCHECK(IsVerified());
623 SetPreverifiedFlagOnMethods(GetDirectMethods());
624 SetPreverifiedFlagOnMethods(GetVirtualMethods());
625}
626
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627} // namespace mirror
628} // namespace art