blob: cdc5ab2b3995fc9dd49b3a240d5db56f5e4189d8 [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();
Mathieu Chartier590fee92013-09-13 13:46:47 -070055 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
56 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070057 if (LIKELY(class_linker_initialized)) {
58 if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070059 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070060 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070061 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070062 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
63 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070064 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070065 << "Attempt to change status of class while not holding its lock: "
66 << PrettyClass(this) << " " << old_status << " -> " << new_status;
67 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080068 }
69 if (new_status == kStatusError) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070070 CHECK_NE(GetStatus(), kStatusError)
71 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072
Ian Rogers62d6c772013-02-27 08:32:07 -080073 // Stash current exception.
Ian Rogers62d6c772013-02-27 08:32:07 -080074 SirtRef<mirror::Object> old_throw_this_object(self, NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -070075 SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -080076 SirtRef<mirror::Throwable> old_exception(self, NULL);
77 uint32_t old_throw_dex_pc;
78 {
79 ThrowLocation old_throw_location;
80 mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location);
81 old_throw_this_object.reset(old_throw_location.GetThis());
82 old_throw_method.reset(old_throw_location.GetMethod());
83 old_exception.reset(old_exception_obj);
84 old_throw_dex_pc = old_throw_location.GetDexPc();
85 self->ClearException();
86 }
87 CHECK(old_exception.get() != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080088
89 // clear exception to call FindSystemClass
90 self->ClearException();
91 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
92 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
93 CHECK(!self->IsExceptionPending());
94
Ian Rogers62d6c772013-02-27 08:32:07 -080095 // Only verification errors, not initialization problems, should set a verify error.
96 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
97 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 if (!eiie_class->IsAssignableFrom(exception_class)) {
99 SetVerifyErrorClass(exception_class);
100 }
101
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 // Restore exception.
103 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
104 old_throw_dex_pc);
105
106 self->SetException(gc_safe_throw_location, old_exception.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700108 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700109 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
110 // Classes that are being resolved or initialized need to notify waiters that the class status
111 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
112 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
113 class_linker_initialized) {
114 NotifyAll(self);
115 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116}
117
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118void Class::SetDexCache(DexCache* new_dex_cache) {
119 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
120}
121
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122void Class::SetClassSize(size_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700123 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
124 DumpClass(LOG(ERROR), kDumpClassFullDetail);
125 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
126 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
128}
129
130// Return the class' name. The exact format is bizarre, but it's the specified behavior for
131// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
132// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
133// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
134String* Class::ComputeName() {
135 String* name = GetName();
136 if (name != NULL) {
137 return name;
138 }
Ian Rogersdfb325e2013-10-30 01:00:44 -0700139 std::string descriptor(ClassHelper(this).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
141 // The descriptor indicates that this is the class for
142 // a primitive type; special-case the return value.
143 const char* c_name = NULL;
144 switch (descriptor[0]) {
145 case 'Z': c_name = "boolean"; break;
146 case 'B': c_name = "byte"; break;
147 case 'C': c_name = "char"; break;
148 case 'S': c_name = "short"; break;
149 case 'I': c_name = "int"; break;
150 case 'J': c_name = "long"; break;
151 case 'F': c_name = "float"; break;
152 case 'D': c_name = "double"; break;
153 case 'V': c_name = "void"; break;
154 default:
155 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
156 }
157 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
158 } else {
159 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
160 // components.
161 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
162 descriptor.erase(0, 1);
163 descriptor.erase(descriptor.size() - 1);
164 }
165 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
166 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
167 }
168 SetName(name);
169 return name;
170}
171
172void Class::DumpClass(std::ostream& os, int flags) const {
173 if ((flags & kDumpClassFullDetail) == 0) {
174 os << PrettyClass(this);
175 if ((flags & kDumpClassClassLoader) != 0) {
176 os << ' ' << GetClassLoader();
177 }
178 if ((flags & kDumpClassInitialized) != 0) {
179 os << ' ' << GetStatus();
180 }
181 os << "\n";
182 return;
183 }
184
185 Class* super = GetSuperClass();
186 ClassHelper kh(this);
187 os << "----- " << (IsInterface() ? "interface" : "class") << " "
188 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
189 os << " objectSize=" << SizeOf() << " "
190 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
191 os << StringPrintf(" access=0x%04x.%04x\n",
192 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
193 if (super != NULL) {
194 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
195 }
196 if (IsArrayClass()) {
197 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
198 }
199 if (kh.NumDirectInterfaces() > 0) {
200 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
201 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
202 Class* interface = kh.GetDirectInterface(i);
203 const ClassLoader* cl = interface->GetClassLoader();
204 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
205 }
206 }
207 os << " vtable (" << NumVirtualMethods() << " entries, "
208 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
209 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
210 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
211 }
212 os << " direct methods (" << NumDirectMethods() << " entries):\n";
213 for (size_t i = 0; i < NumDirectMethods(); ++i) {
214 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
215 }
216 if (NumStaticFields() > 0) {
217 os << " static fields (" << NumStaticFields() << " entries):\n";
218 if (IsResolved() || IsErroneous()) {
219 for (size_t i = 0; i < NumStaticFields(); ++i) {
220 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
221 }
222 } else {
223 os << " <not yet available>";
224 }
225 }
226 if (NumInstanceFields() > 0) {
227 os << " instance fields (" << NumInstanceFields() << " entries):\n";
228 if (IsResolved() || IsErroneous()) {
229 for (size_t i = 0; i < NumInstanceFields(); ++i) {
230 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
231 }
232 } else {
233 os << " <not yet available>";
234 }
235 }
236}
237
238void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
239 if (new_reference_offsets != CLASS_WALK_SUPER) {
240 // Sanity check that the number of bits set in the reference offset bitmap
241 // agrees with the number of references
242 size_t count = 0;
243 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
244 count += c->NumReferenceInstanceFieldsDuringLinking();
245 }
246 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
247 }
248 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
249 new_reference_offsets, false);
250}
251
252void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
253 if (new_reference_offsets != CLASS_WALK_SUPER) {
254 // Sanity check that the number of bits set in the reference offset bitmap
255 // agrees with the number of references
256 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
257 NumReferenceStaticFieldsDuringLinking());
258 }
259 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
260 new_reference_offsets, false);
261}
262
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
264 size_t i = 0;
265 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
266 ++i;
267 }
268 if (descriptor1.find('/', i) != StringPiece::npos ||
269 descriptor2.find('/', i) != StringPiece::npos) {
270 return false;
271 } else {
272 return true;
273 }
274}
275
276bool Class::IsInSamePackage(const Class* that) const {
277 const Class* klass1 = this;
278 const Class* klass2 = that;
279 if (klass1 == klass2) {
280 return true;
281 }
282 // Class loaders must match.
283 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
284 return false;
285 }
286 // Arrays are in the same package when their element classes are.
287 while (klass1->IsArrayClass()) {
288 klass1 = klass1->GetComponentType();
289 }
290 while (klass2->IsArrayClass()) {
291 klass2 = klass2->GetComponentType();
292 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700293 // trivial check again for array types
294 if (klass1 == klass2) {
295 return true;
296 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 // Compare the package part of the descriptor string.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700298 return IsInSamePackage(ClassHelper(klass1).GetDescriptor(),
299 ClassHelper(klass2).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300}
301
302bool Class::IsClassClass() const {
303 Class* java_lang_Class = GetClass()->GetClass();
304 return this == java_lang_Class;
305}
306
307bool Class::IsStringClass() const {
308 return this == String::GetJavaLangString();
309}
310
311bool Class::IsThrowableClass() const {
312 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
313}
314
Brian Carlstromea46f952013-07-30 01:26:50 -0700315bool Class::IsArtFieldClass() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700317 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
318 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319}
320
Brian Carlstromea46f952013-07-30 01:26:50 -0700321bool Class::IsArtMethodClass() const {
322 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323}
324
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325void Class::SetClassLoader(ClassLoader* new_class_loader) {
326 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
327}
328
Ian Rogersd91d6d62013-09-25 20:26:14 -0700329ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700331 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 if (method != NULL) {
333 return method;
334 }
335
336 int32_t iftable_count = GetIfTableCount();
337 IfTable* iftable = GetIfTable();
338 for (int32_t i = 0; i < iftable_count; i++) {
339 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
340 if (method != NULL) {
341 return method;
342 }
343 }
344 return NULL;
345}
346
Brian Carlstromea46f952013-07-30 01:26:50 -0700347ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700349 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 if (method != NULL) {
351 return method;
352 }
353
354 int32_t iftable_count = GetIfTableCount();
355 IfTable* iftable = GetIfTable();
356 for (int32_t i = 0; i < iftable_count; i++) {
357 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
358 if (method != NULL) {
359 return method;
360 }
361 }
362 return NULL;
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 Rogersdfb325e2013-10-30 01:00:44 -0700370 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700371 return method;
372 }
373 }
374 return NULL;
375}
376
377ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const {
378 MethodHelper mh;
379 for (size_t i = 0; i < NumDirectMethods(); ++i) {
380 ArtMethod* method = GetDirectMethod(i);
381 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700382 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 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
Ian Rogersd91d6d62013-09-25 20:26:14 -0700411ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) const {
412 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
413 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
414 if (method != NULL) {
415 return method;
416 }
417 }
418 return NULL;
419}
420
Brian Carlstromea46f952013-07-30 01:26:50 -0700421ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700423 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424 if (method != NULL) {
425 return method;
426 }
427 }
428 return NULL;
429}
430
Ian Rogersd91d6d62013-09-25 20:26:14 -0700431ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
432 MethodHelper mh;
433 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
434 ArtMethod* method = GetVirtualMethod(i);
435 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700436 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700437 return method;
438 }
439 }
440 return NULL;
441}
442
Brian Carlstromea46f952013-07-30 01:26:50 -0700443ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700444 const Signature& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445 MethodHelper mh;
446 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700447 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700449 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 return method;
451 }
452 }
453 return NULL;
454}
455
Brian Carlstromea46f952013-07-30 01:26:50 -0700456ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457 if (GetDexCache() == dex_cache) {
458 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700459 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800460 if (method->GetDexMethodIndex() == dex_method_idx) {
461 return method;
462 }
463 }
464 }
465 return NULL;
466}
467
Brian Carlstromea46f952013-07-30 01:26:50 -0700468ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700470 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 if (method != NULL) {
472 return method;
473 }
474 }
475 return NULL;
476}
477
Ian Rogersd91d6d62013-09-25 20:26:14 -0700478ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) const {
479 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
480 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
481 if (method != NULL) {
482 return method;
483 }
484 }
485 return NULL;
486}
487
Brian Carlstromea46f952013-07-30 01:26:50 -0700488ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700490 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491 if (method != NULL) {
492 return method;
493 }
494 }
495 return NULL;
496}
497
Ian Rogersd91d6d62013-09-25 20:26:14 -0700498ArtMethod* Class::FindClassInitializer() const {
499 for (size_t i = 0; i < NumDirectMethods(); ++i) {
500 ArtMethod* method = GetDirectMethod(i);
501 if (method->IsConstructor() && method->IsStatic()) {
502 if (kIsDebugBuild) {
503 MethodHelper mh(method);
Ian Rogers241b5de2013-10-09 17:58:57 -0700504 CHECK(mh.IsClassInitializer());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700505 CHECK_STREQ(mh.GetName(), "<clinit>");
506 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
507 }
508 return method;
509 }
510 }
511 return NULL;
512}
513
Brian Carlstromea46f952013-07-30 01:26:50 -0700514ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515 // Is the field in this class?
516 // Interfaces are not relevant because they can't contain instance fields.
517 FieldHelper fh;
518 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700519 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700521 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522 return f;
523 }
524 }
525 return NULL;
526}
527
Brian Carlstromea46f952013-07-30 01:26:50 -0700528ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800529 if (GetDexCache() == dex_cache) {
530 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700531 ArtField* f = GetInstanceField(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::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 // Is the field in this class, or any of its superclasses?
542 // Interfaces are not relevant because they can't contain instance fields.
543 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700544 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800545 if (f != NULL) {
546 return f;
547 }
548 }
549 return NULL;
550}
551
Brian Carlstromea46f952013-07-30 01:26:50 -0700552ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800553 // Is the field in this class, or any of its superclasses?
554 // Interfaces are not relevant because they can't contain instance fields.
555 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 if (f != NULL) {
558 return f;
559 }
560 }
561 return NULL;
562}
563
Brian Carlstromea46f952013-07-30 01:26:50 -0700564ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 DCHECK(type != NULL);
566 FieldHelper fh;
567 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700568 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700570 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 return f;
572 }
573 }
574 return NULL;
575}
576
Brian Carlstromea46f952013-07-30 01:26:50 -0700577ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 if (dex_cache == GetDexCache()) {
579 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700580 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581 if (f->GetDexFieldIndex() == dex_field_idx) {
582 return f;
583 }
584 }
585 }
586 return NULL;
587}
588
Brian Carlstromea46f952013-07-30 01:26:50 -0700589ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800590 // Is the field in this class (or its interfaces), or any of its
591 // superclasses (or their interfaces)?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800592 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
593 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595 if (f != NULL) {
596 return f;
597 }
598 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700599 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600 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 -0700611ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800612 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?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700619 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800633 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
634 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700635 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636 if (f != NULL) {
637 return f;
638 }
639 f = k->FindDeclaredStaticField(name, type);
640 if (f != NULL) {
641 return f;
642 }
643 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700644 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800645 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
646 Class* interface = kh.GetDirectInterface(i);
647 f = interface->FindStaticField(name, type);
648 if (f != NULL) {
649 return f;
650 }
651 }
652 }
653 return NULL;
654}
655
Brian Carlstromea46f952013-07-30 01:26:50 -0700656static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200657 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
658 if (methods != NULL) {
659 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700660 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200661 DCHECK(method != NULL);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700662 if (!method->IsNative() && !method->IsAbstract()) {
663 method->SetPreverified();
664 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200665 }
666 }
667}
668
669void Class::SetPreverifiedFlagOnAllMethods() {
670 DCHECK(IsVerified());
671 SetPreverifiedFlagOnMethods(GetDirectMethods());
672 SetPreverifiedFlagOnMethods(GetVirtualMethods());
673}
674
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800675} // namespace mirror
676} // namespace art