blob: 19e134fdfe3e6daf35059da5a6f4cd1f90ce03e7 [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
53void Class::SetStatus(Status new_status) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070054 if (UNLIKELY(new_status <= GetStatus() && new_status != kStatusError)) {
55 bool class_linker_initialized = Runtime::Current()->GetClassLinker() != nullptr;
56 if (class_linker_initialized) {
57 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
58 << GetStatus() << " -> " << new_status;
59 }
60 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080061 if (new_status > kStatusResolved) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070062 CHECK_EQ(GetThinLockId(), Thread::Current()->GetThinLockId())
63 << "Attempt to change status of class while not holding its lock " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080064 }
65 if (new_status == kStatusError) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070066 CHECK_NE(GetStatus(), kStatusError)
67 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068
Ian Rogers62d6c772013-02-27 08:32:07 -080069 // Stash current exception.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080070 Thread* self = Thread::Current();
Ian Rogers62d6c772013-02-27 08:32:07 -080071 SirtRef<mirror::Object> old_throw_this_object(self, NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -070072 SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -080073 SirtRef<mirror::Throwable> old_exception(self, NULL);
74 uint32_t old_throw_dex_pc;
75 {
76 ThrowLocation old_throw_location;
77 mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location);
78 old_throw_this_object.reset(old_throw_location.GetThis());
79 old_throw_method.reset(old_throw_location.GetMethod());
80 old_exception.reset(old_exception_obj);
81 old_throw_dex_pc = old_throw_location.GetDexPc();
82 self->ClearException();
83 }
84 CHECK(old_exception.get() != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085
86 // clear exception to call FindSystemClass
87 self->ClearException();
88 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
89 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
90 CHECK(!self->IsExceptionPending());
91
Ian Rogers62d6c772013-02-27 08:32:07 -080092 // Only verification errors, not initialization problems, should set a verify error.
93 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
94 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 if (!eiie_class->IsAssignableFrom(exception_class)) {
96 SetVerifyErrorClass(exception_class);
97 }
98
Ian Rogers62d6c772013-02-27 08:32:07 -080099 // Restore exception.
100 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
101 old_throw_dex_pc);
102
103 self->SetException(gc_safe_throw_location, old_exception.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700105 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 return SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
107}
108
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109void Class::SetDexCache(DexCache* new_dex_cache) {
110 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
111}
112
113Object* Class::AllocObject(Thread* self) {
114 DCHECK(!IsArrayClass()) << PrettyClass(this);
115 DCHECK(IsInstantiable()) << PrettyClass(this);
116 // TODO: decide whether we want this check. It currently fails during bootstrap.
117 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
118 DCHECK_GE(this->object_size_, sizeof(Object));
119 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
120}
121
122void Class::SetClassSize(size_t new_class_size) {
123 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
124 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
125}
126
127// Return the class' name. The exact format is bizarre, but it's the specified behavior for
128// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
129// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
130// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
131String* Class::ComputeName() {
132 String* name = GetName();
133 if (name != NULL) {
134 return name;
135 }
136 std::string descriptor(ClassHelper(this).GetDescriptor());
137 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
138 // The descriptor indicates that this is the class for
139 // a primitive type; special-case the return value.
140 const char* c_name = NULL;
141 switch (descriptor[0]) {
142 case 'Z': c_name = "boolean"; break;
143 case 'B': c_name = "byte"; break;
144 case 'C': c_name = "char"; break;
145 case 'S': c_name = "short"; break;
146 case 'I': c_name = "int"; break;
147 case 'J': c_name = "long"; break;
148 case 'F': c_name = "float"; break;
149 case 'D': c_name = "double"; break;
150 case 'V': c_name = "void"; break;
151 default:
152 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
153 }
154 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
155 } else {
156 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
157 // components.
158 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
159 descriptor.erase(0, 1);
160 descriptor.erase(descriptor.size() - 1);
161 }
162 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
163 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
164 }
165 SetName(name);
166 return name;
167}
168
169void Class::DumpClass(std::ostream& os, int flags) const {
170 if ((flags & kDumpClassFullDetail) == 0) {
171 os << PrettyClass(this);
172 if ((flags & kDumpClassClassLoader) != 0) {
173 os << ' ' << GetClassLoader();
174 }
175 if ((flags & kDumpClassInitialized) != 0) {
176 os << ' ' << GetStatus();
177 }
178 os << "\n";
179 return;
180 }
181
182 Class* super = GetSuperClass();
183 ClassHelper kh(this);
184 os << "----- " << (IsInterface() ? "interface" : "class") << " "
185 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
186 os << " objectSize=" << SizeOf() << " "
187 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
188 os << StringPrintf(" access=0x%04x.%04x\n",
189 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
190 if (super != NULL) {
191 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
192 }
193 if (IsArrayClass()) {
194 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
195 }
196 if (kh.NumDirectInterfaces() > 0) {
197 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
198 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
199 Class* interface = kh.GetDirectInterface(i);
200 const ClassLoader* cl = interface->GetClassLoader();
201 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
202 }
203 }
204 os << " vtable (" << NumVirtualMethods() << " entries, "
205 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
206 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
207 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
208 }
209 os << " direct methods (" << NumDirectMethods() << " entries):\n";
210 for (size_t i = 0; i < NumDirectMethods(); ++i) {
211 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
212 }
213 if (NumStaticFields() > 0) {
214 os << " static fields (" << NumStaticFields() << " entries):\n";
215 if (IsResolved() || IsErroneous()) {
216 for (size_t i = 0; i < NumStaticFields(); ++i) {
217 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
218 }
219 } else {
220 os << " <not yet available>";
221 }
222 }
223 if (NumInstanceFields() > 0) {
224 os << " instance fields (" << NumInstanceFields() << " entries):\n";
225 if (IsResolved() || IsErroneous()) {
226 for (size_t i = 0; i < NumInstanceFields(); ++i) {
227 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
228 }
229 } else {
230 os << " <not yet available>";
231 }
232 }
233}
234
235void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
236 if (new_reference_offsets != CLASS_WALK_SUPER) {
237 // Sanity check that the number of bits set in the reference offset bitmap
238 // agrees with the number of references
239 size_t count = 0;
240 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
241 count += c->NumReferenceInstanceFieldsDuringLinking();
242 }
243 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
244 }
245 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
246 new_reference_offsets, false);
247}
248
249void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
250 if (new_reference_offsets != CLASS_WALK_SUPER) {
251 // Sanity check that the number of bits set in the reference offset bitmap
252 // agrees with the number of references
253 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
254 NumReferenceStaticFieldsDuringLinking());
255 }
256 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
257 new_reference_offsets, false);
258}
259
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800260bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
261 size_t i = 0;
262 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
263 ++i;
264 }
265 if (descriptor1.find('/', i) != StringPiece::npos ||
266 descriptor2.find('/', i) != StringPiece::npos) {
267 return false;
268 } else {
269 return true;
270 }
271}
272
273bool Class::IsInSamePackage(const Class* that) const {
274 const Class* klass1 = this;
275 const Class* klass2 = that;
276 if (klass1 == klass2) {
277 return true;
278 }
279 // Class loaders must match.
280 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
281 return false;
282 }
283 // Arrays are in the same package when their element classes are.
284 while (klass1->IsArrayClass()) {
285 klass1 = klass1->GetComponentType();
286 }
287 while (klass2->IsArrayClass()) {
288 klass2 = klass2->GetComponentType();
289 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700290 // trivial check again for array types
291 if (klass1 == klass2) {
292 return true;
293 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 // Compare the package part of the descriptor string.
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700295 if (LIKELY(!klass1->IsProxyClass() && !klass2->IsProxyClass())) {
296 ClassHelper kh(klass1);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700297 const DexFile* dex_file1 = &kh.GetDexFile();
298 const DexFile::TypeId* type_id1 = &dex_file1->GetTypeId(klass1->GetDexTypeIndex());
299 const char* descriptor1 = dex_file1->GetTypeDescriptor(*type_id1);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700300 kh.ChangeClass(klass2);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700301 const DexFile* dex_file2 = &kh.GetDexFile();
302 const DexFile::TypeId* type_id2 = &dex_file2->GetTypeId(klass2->GetDexTypeIndex());
303 const char* descriptor2 = dex_file2->GetTypeDescriptor(*type_id2);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700304 return IsInSamePackage(descriptor1, descriptor2);
305 }
Brian Carlstrom857fe962013-03-26 22:46:15 -0700306 ClassHelper kh(klass1);
307 std::string descriptor1(kh.GetDescriptor());
308 kh.ChangeClass(klass2);
309 std::string descriptor2(kh.GetDescriptor());
310 return IsInSamePackage(descriptor1, descriptor2);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800311}
312
313bool Class::IsClassClass() const {
314 Class* java_lang_Class = GetClass()->GetClass();
315 return this == java_lang_Class;
316}
317
318bool Class::IsStringClass() const {
319 return this == String::GetJavaLangString();
320}
321
322bool Class::IsThrowableClass() const {
323 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
324}
325
Brian Carlstromea46f952013-07-30 01:26:50 -0700326bool Class::IsArtFieldClass() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700328 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
329 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330}
331
Brian Carlstromea46f952013-07-30 01:26:50 -0700332bool Class::IsArtMethodClass() const {
333 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334}
335
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336void Class::SetClassLoader(ClassLoader* new_class_loader) {
337 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
338}
339
Brian Carlstromea46f952013-07-30 01:26:50 -0700340ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700342 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343 if (method != NULL) {
344 return method;
345 }
346
347 int32_t iftable_count = GetIfTableCount();
348 IfTable* iftable = GetIfTable();
349 for (int32_t i = 0; i < iftable_count; i++) {
350 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
351 if (method != NULL) {
352 return method;
353 }
354 }
355 return NULL;
356}
357
Brian Carlstromea46f952013-07-30 01:26:50 -0700358ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700360 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 if (method != NULL) {
362 return method;
363 }
364
365 int32_t iftable_count = GetIfTableCount();
366 IfTable* iftable = GetIfTable();
367 for (int32_t i = 0; i < iftable_count; i++) {
368 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
369 if (method != NULL) {
370 return method;
371 }
372 }
373 return NULL;
374}
375
376
Brian Carlstromea46f952013-07-30 01:26:50 -0700377ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800378 MethodHelper mh;
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 mh.ChangeMethod(method);
382 if (name == mh.GetName() && signature == mh.GetSignature()) {
383 return method;
384 }
385 }
386 return NULL;
387}
388
Brian Carlstromea46f952013-07-30 01:26:50 -0700389ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 if (GetDexCache() == dex_cache) {
391 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700392 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 if (method->GetDexMethodIndex() == dex_method_idx) {
394 return method;
395 }
396 }
397 }
398 return NULL;
399}
400
Brian Carlstromea46f952013-07-30 01:26:50 -0700401ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700403 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404 if (method != NULL) {
405 return method;
406 }
407 }
408 return NULL;
409}
410
Brian Carlstromea46f952013-07-30 01:26:50 -0700411ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700413 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414 if (method != NULL) {
415 return method;
416 }
417 }
418 return NULL;
419}
420
Brian Carlstromea46f952013-07-30 01:26:50 -0700421ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 const StringPiece& signature) const {
423 MethodHelper mh;
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 mh.ChangeMethod(method);
427 if (name == mh.GetName() && signature == mh.GetSignature()) {
428 return method;
429 }
430 }
431 return NULL;
432}
433
Brian Carlstromea46f952013-07-30 01:26:50 -0700434ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435 if (GetDexCache() == dex_cache) {
436 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700437 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438 if (method->GetDexMethodIndex() == dex_method_idx) {
439 return method;
440 }
441 }
442 }
443 return NULL;
444}
445
Brian Carlstromea46f952013-07-30 01:26:50 -0700446ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700448 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449 if (method != NULL) {
450 return method;
451 }
452 }
453 return NULL;
454}
455
Brian Carlstromea46f952013-07-30 01:26:50 -0700456ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700458 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459 if (method != NULL) {
460 return method;
461 }
462 }
463 return NULL;
464}
465
Brian Carlstromea46f952013-07-30 01:26:50 -0700466ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 // Is the field in this class?
468 // Interfaces are not relevant because they can't contain instance fields.
469 FieldHelper fh;
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 fh.ChangeField(f);
473 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
474 return f;
475 }
476 }
477 return NULL;
478}
479
Brian Carlstromea46f952013-07-30 01:26:50 -0700480ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 if (GetDexCache() == dex_cache) {
482 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700483 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800484 if (f->GetDexFieldIndex() == dex_field_idx) {
485 return f;
486 }
487 }
488 }
489 return NULL;
490}
491
Brian Carlstromea46f952013-07-30 01:26:50 -0700492ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
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(name, type);
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::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505 // Is the field in this class, or any of its superclasses?
506 // Interfaces are not relevant because they can't contain instance fields.
507 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700508 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800509 if (f != NULL) {
510 return f;
511 }
512 }
513 return NULL;
514}
515
Brian Carlstromea46f952013-07-30 01:26:50 -0700516ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800517 DCHECK(type != NULL);
518 FieldHelper fh;
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 fh.ChangeField(f);
522 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
523 return f;
524 }
525 }
526 return NULL;
527}
528
Brian Carlstromea46f952013-07-30 01:26:50 -0700529ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 if (dex_cache == GetDexCache()) {
531 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700532 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 if (f->GetDexFieldIndex() == dex_field_idx) {
534 return f;
535 }
536 }
537 }
538 return NULL;
539}
540
Brian Carlstromea46f952013-07-30 01:26:50 -0700541ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800542 // Is the field in this class (or its interfaces), or any of its
543 // superclasses (or their interfaces)?
544 ClassHelper kh;
545 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
546 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700547 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548 if (f != NULL) {
549 return f;
550 }
551 // Is this field in any of this class' interfaces?
552 kh.ChangeClass(k);
553 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
554 Class* interface = kh.GetDirectInterface(i);
555 f = interface->FindStaticField(name, type);
556 if (f != NULL) {
557 return f;
558 }
559 }
560 }
561 return NULL;
562}
563
Brian Carlstromea46f952013-07-30 01:26:50 -0700564ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 ClassHelper kh;
566 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
567 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700568 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 if (f != NULL) {
570 return f;
571 }
572 // Is this field in any of this class' interfaces?
573 kh.ChangeClass(k);
574 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
575 Class* interface = kh.GetDirectInterface(i);
576 f = interface->FindStaticField(dex_cache, dex_field_idx);
577 if (f != NULL) {
578 return f;
579 }
580 }
581 }
582 return NULL;
583}
584
Brian Carlstromea46f952013-07-30 01:26:50 -0700585ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 // Find a field using the JLS field resolution order
587 ClassHelper kh;
588 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
589 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700590 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 if (f != NULL) {
592 return f;
593 }
594 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700611static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200612 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
613 if (methods != NULL) {
614 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700615 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200616 DCHECK(method != NULL);
617 method->SetPreverified();
618 }
619 }
620}
621
622void Class::SetPreverifiedFlagOnAllMethods() {
623 DCHECK(IsVerified());
624 SetPreverifiedFlagOnMethods(GetDirectMethods());
625 SetPreverifiedFlagOnMethods(GetVirtualMethods());
626}
627
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628} // namespace mirror
629} // namespace art