blob: b16c2f71bb9be8c9d82c521317d88c2bd18db7bb [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.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070063 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070064 << "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
Ian Rogersd91d6d62013-09-25 20:26:14 -0700328ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700364ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 MethodHelper mh;
366 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700367 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 mh.ChangeMethod(method);
Ian Rogersd91d6d62013-09-25 20:26:14 -0700369 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);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700381 if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 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
Ian Rogersd91d6d62013-09-25 20:26:14 -0700410ArtMethod* 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700420ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700422 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 if (method != NULL) {
424 return method;
425 }
426 }
427 return NULL;
428}
429
Ian Rogersd91d6d62013-09-25 20:26:14 -0700430ArtMethod* 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700442ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700443 const Signature& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 MethodHelper mh;
445 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700446 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447 mh.ChangeMethod(method);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700448 if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449 return method;
450 }
451 }
452 return NULL;
453}
454
Brian Carlstromea46f952013-07-30 01:26:50 -0700455ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 if (GetDexCache() == dex_cache) {
457 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700458 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459 if (method->GetDexMethodIndex() == dex_method_idx) {
460 return method;
461 }
462 }
463 }
464 return NULL;
465}
466
Brian Carlstromea46f952013-07-30 01:26:50 -0700467ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700469 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470 if (method != NULL) {
471 return method;
472 }
473 }
474 return NULL;
475}
476
Ian Rogersd91d6d62013-09-25 20:26:14 -0700477ArtMethod* 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
Brian Carlstromea46f952013-07-30 01:26:50 -0700487ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700489 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 if (method != NULL) {
491 return method;
492 }
493 }
494 return NULL;
495}
496
Ian Rogersd91d6d62013-09-25 20:26:14 -0700497ArtMethod* 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_STREQ(mh.GetName(), "<clinit>");
504 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
505 }
506 return method;
507 }
508 }
509 return NULL;
510}
511
Brian Carlstromea46f952013-07-30 01:26:50 -0700512ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 // Is the field in this class?
514 // Interfaces are not relevant because they can't contain instance fields.
515 FieldHelper fh;
516 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700517 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518 fh.ChangeField(f);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700519 if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 return f;
521 }
522 }
523 return NULL;
524}
525
Brian Carlstromea46f952013-07-30 01:26:50 -0700526ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527 if (GetDexCache() == dex_cache) {
528 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700529 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 if (f->GetDexFieldIndex() == dex_field_idx) {
531 return f;
532 }
533 }
534 }
535 return NULL;
536}
537
Brian Carlstromea46f952013-07-30 01:26:50 -0700538ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539 // Is the field in this class, or any of its superclasses?
540 // Interfaces are not relevant because they can't contain instance fields.
541 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700542 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800543 if (f != NULL) {
544 return f;
545 }
546 }
547 return NULL;
548}
549
Brian Carlstromea46f952013-07-30 01:26:50 -0700550ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 // Is the field in this class, or any of its superclasses?
552 // Interfaces are not relevant because they can't contain instance fields.
553 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700554 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 if (f != NULL) {
556 return f;
557 }
558 }
559 return NULL;
560}
561
Brian Carlstromea46f952013-07-30 01:26:50 -0700562ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800563 DCHECK(type != NULL);
564 FieldHelper fh;
565 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700566 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567 fh.ChangeField(f);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700568 if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 return f;
570 }
571 }
572 return NULL;
573}
574
Brian Carlstromea46f952013-07-30 01:26:50 -0700575ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576 if (dex_cache == GetDexCache()) {
577 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700578 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 if (f->GetDexFieldIndex() == dex_field_idx) {
580 return f;
581 }
582 }
583 }
584 return NULL;
585}
586
Brian Carlstromea46f952013-07-30 01:26:50 -0700587ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588 // Is the field in this class (or its interfaces), or any of its
589 // superclasses (or their interfaces)?
590 ClassHelper kh;
591 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
592 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 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 -0700610ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 ClassHelper kh;
612 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
613 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615 if (f != NULL) {
616 return f;
617 }
618 // Is this field in any of this class' interfaces?
619 kh.ChangeClass(k);
620 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
621 Class* interface = kh.GetDirectInterface(i);
622 f = interface->FindStaticField(dex_cache, dex_field_idx);
623 if (f != NULL) {
624 return f;
625 }
626 }
627 }
628 return NULL;
629}
630
Brian Carlstromea46f952013-07-30 01:26:50 -0700631ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 // Find a field using the JLS field resolution order
633 ClassHelper kh;
634 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
635 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700636 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800637 if (f != NULL) {
638 return f;
639 }
640 f = k->FindDeclaredStaticField(name, type);
641 if (f != NULL) {
642 return f;
643 }
644 // Is this field in any of this class' interfaces?
645 kh.ChangeClass(k);
646 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
647 Class* interface = kh.GetDirectInterface(i);
648 f = interface->FindStaticField(name, type);
649 if (f != NULL) {
650 return f;
651 }
652 }
653 }
654 return NULL;
655}
656
Brian Carlstromea46f952013-07-30 01:26:50 -0700657static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200658 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
659 if (methods != NULL) {
660 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700661 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200662 DCHECK(method != NULL);
663 method->SetPreverified();
664 }
665 }
666}
667
668void Class::SetPreverifiedFlagOnAllMethods() {
669 DCHECK(IsVerified());
670 SetPreverifiedFlagOnMethods(GetDirectMethods());
671 SetPreverifiedFlagOnMethods(GetVirtualMethods());
672}
673
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674} // namespace mirror
675} // namespace art