blob: 2746e1e91afb653f210f02aab2fb0584c1594c38 [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
Mathieu Chartierc528dba2013-11-26 12:00:11 -080053void Class::VisitRoots(RootVisitor* visitor, void* arg) {
54 if (java_lang_Class_ != nullptr) {
55 java_lang_Class_ = down_cast<Class*>(visitor(java_lang_Class_, arg));
56 }
57}
58
Ian Rogers7dfb28c2013-08-22 08:18:36 -070059void Class::SetStatus(Status new_status, Thread* self) {
60 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070063 if (LIKELY(class_linker_initialized)) {
64 if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070065 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070066 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
69 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070070 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071 << "Attempt to change status of class while not holding its lock: "
72 << PrettyClass(this) << " " << old_status << " -> " << new_status;
73 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
75 if (new_status == kStatusError) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070076 CHECK_NE(GetStatus(), kStatusError)
77 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078
Ian Rogers62d6c772013-02-27 08:32:07 -080079 // Stash current exception.
Ian Rogers62d6c772013-02-27 08:32:07 -080080 SirtRef<mirror::Object> old_throw_this_object(self, NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -070081 SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -080082 SirtRef<mirror::Throwable> old_exception(self, NULL);
83 uint32_t old_throw_dex_pc;
84 {
85 ThrowLocation old_throw_location;
86 mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location);
87 old_throw_this_object.reset(old_throw_location.GetThis());
88 old_throw_method.reset(old_throw_location.GetMethod());
89 old_exception.reset(old_exception_obj);
90 old_throw_dex_pc = old_throw_location.GetDexPc();
91 self->ClearException();
92 }
93 CHECK(old_exception.get() != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080094
95 // clear exception to call FindSystemClass
96 self->ClearException();
97 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
98 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
99 CHECK(!self->IsExceptionPending());
100
Ian Rogers62d6c772013-02-27 08:32:07 -0800101 // Only verification errors, not initialization problems, should set a verify error.
102 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
103 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 if (!eiie_class->IsAssignableFrom(exception_class)) {
105 SetVerifyErrorClass(exception_class);
106 }
107
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 // Restore exception.
109 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
110 old_throw_dex_pc);
111
112 self->SetException(gc_safe_throw_location, old_exception.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800113 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700114 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700115 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
116 // Classes that are being resolved or initialized need to notify waiters that the class status
117 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
118 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
119 class_linker_initialized) {
120 NotifyAll(self);
121 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122}
123
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124void Class::SetDexCache(DexCache* new_dex_cache) {
125 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
126}
127
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128void Class::SetClassSize(size_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700129 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
130 DumpClass(LOG(ERROR), kDumpClassFullDetail);
131 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
132 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
134}
135
136// Return the class' name. The exact format is bizarre, but it's the specified behavior for
137// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
138// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
139// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
140String* Class::ComputeName() {
141 String* name = GetName();
142 if (name != NULL) {
143 return name;
144 }
Ian Rogersdfb325e2013-10-30 01:00:44 -0700145 std::string descriptor(ClassHelper(this).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
147 // The descriptor indicates that this is the class for
148 // a primitive type; special-case the return value.
149 const char* c_name = NULL;
150 switch (descriptor[0]) {
151 case 'Z': c_name = "boolean"; break;
152 case 'B': c_name = "byte"; break;
153 case 'C': c_name = "char"; break;
154 case 'S': c_name = "short"; break;
155 case 'I': c_name = "int"; break;
156 case 'J': c_name = "long"; break;
157 case 'F': c_name = "float"; break;
158 case 'D': c_name = "double"; break;
159 case 'V': c_name = "void"; break;
160 default:
161 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
162 }
163 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
164 } else {
165 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
166 // components.
167 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
168 descriptor.erase(0, 1);
169 descriptor.erase(descriptor.size() - 1);
170 }
171 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
172 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
173 }
174 SetName(name);
175 return name;
176}
177
178void Class::DumpClass(std::ostream& os, int flags) const {
179 if ((flags & kDumpClassFullDetail) == 0) {
180 os << PrettyClass(this);
181 if ((flags & kDumpClassClassLoader) != 0) {
182 os << ' ' << GetClassLoader();
183 }
184 if ((flags & kDumpClassInitialized) != 0) {
185 os << ' ' << GetStatus();
186 }
187 os << "\n";
188 return;
189 }
190
191 Class* super = GetSuperClass();
192 ClassHelper kh(this);
193 os << "----- " << (IsInterface() ? "interface" : "class") << " "
194 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
195 os << " objectSize=" << SizeOf() << " "
196 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
197 os << StringPrintf(" access=0x%04x.%04x\n",
198 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
199 if (super != NULL) {
200 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
201 }
202 if (IsArrayClass()) {
203 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
204 }
205 if (kh.NumDirectInterfaces() > 0) {
206 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
207 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
208 Class* interface = kh.GetDirectInterface(i);
209 const ClassLoader* cl = interface->GetClassLoader();
210 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
211 }
212 }
213 os << " vtable (" << NumVirtualMethods() << " entries, "
214 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
215 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
216 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
217 }
218 os << " direct methods (" << NumDirectMethods() << " entries):\n";
219 for (size_t i = 0; i < NumDirectMethods(); ++i) {
220 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
221 }
222 if (NumStaticFields() > 0) {
223 os << " static fields (" << NumStaticFields() << " entries):\n";
224 if (IsResolved() || IsErroneous()) {
225 for (size_t i = 0; i < NumStaticFields(); ++i) {
226 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
227 }
228 } else {
229 os << " <not yet available>";
230 }
231 }
232 if (NumInstanceFields() > 0) {
233 os << " instance fields (" << NumInstanceFields() << " entries):\n";
234 if (IsResolved() || IsErroneous()) {
235 for (size_t i = 0; i < NumInstanceFields(); ++i) {
236 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
237 }
238 } else {
239 os << " <not yet available>";
240 }
241 }
242}
243
244void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
245 if (new_reference_offsets != CLASS_WALK_SUPER) {
246 // Sanity check that the number of bits set in the reference offset bitmap
247 // agrees with the number of references
248 size_t count = 0;
249 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
250 count += c->NumReferenceInstanceFieldsDuringLinking();
251 }
252 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
253 }
254 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
255 new_reference_offsets, false);
256}
257
258void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
259 if (new_reference_offsets != CLASS_WALK_SUPER) {
260 // Sanity check that the number of bits set in the reference offset bitmap
261 // agrees with the number of references
262 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
263 NumReferenceStaticFieldsDuringLinking());
264 }
265 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
266 new_reference_offsets, false);
267}
268
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
270 size_t i = 0;
271 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
272 ++i;
273 }
274 if (descriptor1.find('/', i) != StringPiece::npos ||
275 descriptor2.find('/', i) != StringPiece::npos) {
276 return false;
277 } else {
278 return true;
279 }
280}
281
282bool Class::IsInSamePackage(const Class* that) const {
283 const Class* klass1 = this;
284 const Class* klass2 = that;
285 if (klass1 == klass2) {
286 return true;
287 }
288 // Class loaders must match.
289 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
290 return false;
291 }
292 // Arrays are in the same package when their element classes are.
293 while (klass1->IsArrayClass()) {
294 klass1 = klass1->GetComponentType();
295 }
296 while (klass2->IsArrayClass()) {
297 klass2 = klass2->GetComponentType();
298 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700299 // trivial check again for array types
300 if (klass1 == klass2) {
301 return true;
302 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 // Compare the package part of the descriptor string.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700304 return IsInSamePackage(ClassHelper(klass1).GetDescriptor(),
305 ClassHelper(klass2).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306}
307
308bool Class::IsClassClass() const {
309 Class* java_lang_Class = GetClass()->GetClass();
310 return this == java_lang_Class;
311}
312
313bool Class::IsStringClass() const {
314 return this == String::GetJavaLangString();
315}
316
317bool Class::IsThrowableClass() const {
318 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
319}
320
Brian Carlstromea46f952013-07-30 01:26:50 -0700321bool Class::IsArtFieldClass() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700323 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
324 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325}
326
Brian Carlstromea46f952013-07-30 01:26:50 -0700327bool Class::IsArtMethodClass() const {
328 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329}
330
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331void Class::SetClassLoader(ClassLoader* new_class_loader) {
332 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
333}
334
Ian Rogersd91d6d62013-09-25 20:26:14 -0700335ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700337 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 if (method != NULL) {
339 return method;
340 }
341
342 int32_t iftable_count = GetIfTableCount();
343 IfTable* iftable = GetIfTable();
344 for (int32_t i = 0; i < iftable_count; i++) {
345 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
346 if (method != NULL) {
347 return method;
348 }
349 }
350 return NULL;
351}
352
Brian Carlstromea46f952013-07-30 01:26:50 -0700353ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700355 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 if (method != NULL) {
357 return method;
358 }
359
360 int32_t iftable_count = GetIfTableCount();
361 IfTable* iftable = GetIfTable();
362 for (int32_t i = 0; i < iftable_count; i++) {
363 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
364 if (method != NULL) {
365 return method;
366 }
367 }
368 return NULL;
369}
370
Brian Carlstromea46f952013-07-30 01:26:50 -0700371ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 MethodHelper mh;
373 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700374 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700376 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700377 return method;
378 }
379 }
380 return NULL;
381}
382
383ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const {
384 MethodHelper mh;
385 for (size_t i = 0; i < NumDirectMethods(); ++i) {
386 ArtMethod* method = GetDirectMethod(i);
387 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700388 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 return method;
390 }
391 }
392 return NULL;
393}
394
Brian Carlstromea46f952013-07-30 01:26:50 -0700395ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 if (GetDexCache() == dex_cache) {
397 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700398 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 if (method->GetDexMethodIndex() == dex_method_idx) {
400 return method;
401 }
402 }
403 }
404 return NULL;
405}
406
Brian Carlstromea46f952013-07-30 01:26:50 -0700407ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700409 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 if (method != NULL) {
411 return method;
412 }
413 }
414 return NULL;
415}
416
Ian Rogersd91d6d62013-09-25 20:26:14 -0700417ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) const {
418 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
419 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
420 if (method != NULL) {
421 return method;
422 }
423 }
424 return NULL;
425}
426
Brian Carlstromea46f952013-07-30 01:26:50 -0700427ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700429 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 if (method != NULL) {
431 return method;
432 }
433 }
434 return NULL;
435}
436
Ian Rogersd91d6d62013-09-25 20:26:14 -0700437ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
438 MethodHelper mh;
439 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
440 ArtMethod* method = GetVirtualMethod(i);
441 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700442 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700443 return method;
444 }
445 }
446 return NULL;
447}
448
Brian Carlstromea46f952013-07-30 01:26:50 -0700449ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700450 const Signature& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800451 MethodHelper mh;
452 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700453 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700455 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 return method;
457 }
458 }
459 return NULL;
460}
461
Brian Carlstromea46f952013-07-30 01:26:50 -0700462ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800463 if (GetDexCache() == dex_cache) {
464 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700465 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466 if (method->GetDexMethodIndex() == dex_method_idx) {
467 return method;
468 }
469 }
470 }
471 return NULL;
472}
473
Brian Carlstromea46f952013-07-30 01:26:50 -0700474ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800475 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700476 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477 if (method != NULL) {
478 return method;
479 }
480 }
481 return NULL;
482}
483
Ian Rogersd91d6d62013-09-25 20:26:14 -0700484ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) const {
485 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
486 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
487 if (method != NULL) {
488 return method;
489 }
490 }
491 return NULL;
492}
493
Brian Carlstromea46f952013-07-30 01:26:50 -0700494ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700496 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800497 if (method != NULL) {
498 return method;
499 }
500 }
501 return NULL;
502}
503
Ian Rogersd91d6d62013-09-25 20:26:14 -0700504ArtMethod* Class::FindClassInitializer() const {
505 for (size_t i = 0; i < NumDirectMethods(); ++i) {
506 ArtMethod* method = GetDirectMethod(i);
507 if (method->IsConstructor() && method->IsStatic()) {
508 if (kIsDebugBuild) {
509 MethodHelper mh(method);
Ian Rogers241b5de2013-10-09 17:58:57 -0700510 CHECK(mh.IsClassInitializer());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700511 CHECK_STREQ(mh.GetName(), "<clinit>");
512 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
513 }
514 return method;
515 }
516 }
517 return NULL;
518}
519
Brian Carlstromea46f952013-07-30 01:26:50 -0700520ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800521 // Is the field in this class?
522 // Interfaces are not relevant because they can't contain instance fields.
523 FieldHelper fh;
524 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700525 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800526 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700527 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800528 return f;
529 }
530 }
531 return NULL;
532}
533
Brian Carlstromea46f952013-07-30 01:26:50 -0700534ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800535 if (GetDexCache() == dex_cache) {
536 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700537 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800538 if (f->GetDexFieldIndex() == dex_field_idx) {
539 return f;
540 }
541 }
542 }
543 return NULL;
544}
545
Brian Carlstromea46f952013-07-30 01:26:50 -0700546ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 // Is the field in this class, or any of its superclasses?
548 // Interfaces are not relevant because they can't contain instance fields.
549 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700550 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 if (f != NULL) {
552 return f;
553 }
554 }
555 return NULL;
556}
557
Brian Carlstromea46f952013-07-30 01:26:50 -0700558ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800559 // Is the field in this class, or any of its superclasses?
560 // Interfaces are not relevant because they can't contain instance fields.
561 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700562 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800563 if (f != NULL) {
564 return f;
565 }
566 }
567 return NULL;
568}
569
Brian Carlstromea46f952013-07-30 01:26:50 -0700570ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 DCHECK(type != NULL);
572 FieldHelper fh;
573 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700574 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700576 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 return f;
578 }
579 }
580 return NULL;
581}
582
Brian Carlstromea46f952013-07-30 01:26:50 -0700583ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584 if (dex_cache == GetDexCache()) {
585 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800587 if (f->GetDexFieldIndex() == dex_field_idx) {
588 return f;
589 }
590 }
591 }
592 return NULL;
593}
594
Brian Carlstromea46f952013-07-30 01:26:50 -0700595ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596 // Is the field in this class (or its interfaces), or any of its
597 // superclasses (or their interfaces)?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
599 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700600 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 if (f != NULL) {
602 return f;
603 }
604 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700605 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800606 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
607 Class* interface = kh.GetDirectInterface(i);
608 f = interface->FindStaticField(name, type);
609 if (f != NULL) {
610 return f;
611 }
612 }
613 }
614 return NULL;
615}
616
Brian Carlstromea46f952013-07-30 01:26:50 -0700617ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
619 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700620 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 if (f != NULL) {
622 return f;
623 }
624 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700625 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800626 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
627 Class* interface = kh.GetDirectInterface(i);
628 f = interface->FindStaticField(dex_cache, dex_field_idx);
629 if (f != NULL) {
630 return f;
631 }
632 }
633 }
634 return NULL;
635}
636
Brian Carlstromea46f952013-07-30 01:26:50 -0700637ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800638 // Find a field using the JLS field resolution order
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
640 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700641 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800642 if (f != NULL) {
643 return f;
644 }
645 f = k->FindDeclaredStaticField(name, type);
646 if (f != NULL) {
647 return f;
648 }
649 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700650 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
652 Class* interface = kh.GetDirectInterface(i);
653 f = interface->FindStaticField(name, type);
654 if (f != NULL) {
655 return f;
656 }
657 }
658 }
659 return NULL;
660}
661
Brian Carlstromea46f952013-07-30 01:26:50 -0700662static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200663 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
664 if (methods != NULL) {
665 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700666 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200667 DCHECK(method != NULL);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700668 if (!method->IsNative() && !method->IsAbstract()) {
669 method->SetPreverified();
670 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200671 }
672 }
673}
674
675void Class::SetPreverifiedFlagOnAllMethods() {
676 DCHECK(IsVerified());
677 SetPreverifiedFlagOnMethods(GetDirectMethods());
678 SetPreverifiedFlagOnMethods(GetVirtualMethods());
679}
680
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800681} // namespace mirror
682} // namespace art