blob: 5e8b82793af5ddaf3f241c729a9715a282de0084 [file] [log] [blame]
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "class.h"
18
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "art_field-inl.h"
20#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "class-inl.h"
22#include "class_linker.h"
23#include "class_loader.h"
24#include "dex_cache.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070026#include "gc/accounting/card_table-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "object-inl.h"
28#include "object_array-inl.h"
29#include "object_utils.h"
30#include "runtime.h"
31#include "sirt_ref.h"
32#include "thread.h"
33#include "throwable.h"
34#include "utils.h"
35#include "well_known_classes.h"
36
37namespace art {
38namespace mirror {
39
40Class* Class::java_lang_Class_ = NULL;
41
42void Class::SetClassClass(Class* java_lang_Class) {
43 CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class;
44 CHECK(java_lang_Class != NULL);
45 java_lang_Class_ = java_lang_Class;
46}
47
48void Class::ResetClass() {
49 CHECK(java_lang_Class_ != NULL);
50 java_lang_Class_ = NULL;
51}
52
Ian Rogers7dfb28c2013-08-22 08:18:36 -070053void Class::SetStatus(Status new_status, Thread* self) {
54 Status old_status = GetStatus();
55 bool class_linker_initialized = Runtime::Current()->GetClassLinker() != nullptr;
56 if (LIKELY(class_linker_initialized)) {
57 if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070058 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070059 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070060 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070061 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
62 // When classes are being resolved the resolution code should hold the lock.
63 CHECK_EQ(GetThinLockId(), self->GetThinLockId())
64 << "Attempt to change status of class while not holding its lock: "
65 << PrettyClass(this) << " " << old_status << " -> " << new_status;
66 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080067 }
68 if (new_status == kStatusError) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070069 CHECK_NE(GetStatus(), kStatusError)
70 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080071
Ian Rogers62d6c772013-02-27 08:32:07 -080072 // Stash current exception.
Ian Rogers62d6c772013-02-27 08:32:07 -080073 SirtRef<mirror::Object> old_throw_this_object(self, NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -070074 SirtRef<mirror::ArtMethod> old_throw_method(self, NULL);
Ian Rogers62d6c772013-02-27 08:32:07 -080075 SirtRef<mirror::Throwable> old_exception(self, NULL);
76 uint32_t old_throw_dex_pc;
77 {
78 ThrowLocation old_throw_location;
79 mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location);
80 old_throw_this_object.reset(old_throw_location.GetThis());
81 old_throw_method.reset(old_throw_location.GetMethod());
82 old_exception.reset(old_exception_obj);
83 old_throw_dex_pc = old_throw_location.GetDexPc();
84 self->ClearException();
85 }
86 CHECK(old_exception.get() != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087
88 // clear exception to call FindSystemClass
89 self->ClearException();
90 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
91 Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;");
92 CHECK(!self->IsExceptionPending());
93
Ian Rogers62d6c772013-02-27 08:32:07 -080094 // Only verification errors, not initialization problems, should set a verify error.
95 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
96 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080097 if (!eiie_class->IsAssignableFrom(exception_class)) {
98 SetVerifyErrorClass(exception_class);
99 }
100
Ian Rogers62d6c772013-02-27 08:32:07 -0800101 // Restore exception.
102 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
103 old_throw_dex_pc);
104
105 self->SetException(gc_safe_throw_location, old_exception.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700107 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700108 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
109 // Classes that are being resolved or initialized need to notify waiters that the class status
110 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
111 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
112 class_linker_initialized) {
113 NotifyAll(self);
114 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115}
116
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117void Class::SetDexCache(DexCache* new_dex_cache) {
118 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
119}
120
121Object* Class::AllocObject(Thread* self) {
122 DCHECK(!IsArrayClass()) << PrettyClass(this);
123 DCHECK(IsInstantiable()) << PrettyClass(this);
124 // TODO: decide whether we want this check. It currently fails during bootstrap.
125 // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this);
126 DCHECK_GE(this->object_size_, sizeof(Object));
127 return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_);
128}
129
130void Class::SetClassSize(size_t new_class_size) {
131 DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
132 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
133}
134
135// Return the class' name. The exact format is bizarre, but it's the specified behavior for
136// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
137// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
138// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
139String* Class::ComputeName() {
140 String* name = GetName();
141 if (name != NULL) {
142 return name;
143 }
144 std::string descriptor(ClassHelper(this).GetDescriptor());
145 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
146 // The descriptor indicates that this is the class for
147 // a primitive type; special-case the return value.
148 const char* c_name = NULL;
149 switch (descriptor[0]) {
150 case 'Z': c_name = "boolean"; break;
151 case 'B': c_name = "byte"; break;
152 case 'C': c_name = "char"; break;
153 case 'S': c_name = "short"; break;
154 case 'I': c_name = "int"; break;
155 case 'J': c_name = "long"; break;
156 case 'F': c_name = "float"; break;
157 case 'D': c_name = "double"; break;
158 case 'V': c_name = "void"; break;
159 default:
160 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
161 }
162 name = String::AllocFromModifiedUtf8(Thread::Current(), c_name);
163 } else {
164 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
165 // components.
166 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
167 descriptor.erase(0, 1);
168 descriptor.erase(descriptor.size() - 1);
169 }
170 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
171 name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str());
172 }
173 SetName(name);
174 return name;
175}
176
177void Class::DumpClass(std::ostream& os, int flags) const {
178 if ((flags & kDumpClassFullDetail) == 0) {
179 os << PrettyClass(this);
180 if ((flags & kDumpClassClassLoader) != 0) {
181 os << ' ' << GetClassLoader();
182 }
183 if ((flags & kDumpClassInitialized) != 0) {
184 os << ' ' << GetStatus();
185 }
186 os << "\n";
187 return;
188 }
189
190 Class* super = GetSuperClass();
191 ClassHelper kh(this);
192 os << "----- " << (IsInterface() ? "interface" : "class") << " "
193 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
194 os << " objectSize=" << SizeOf() << " "
195 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
196 os << StringPrintf(" access=0x%04x.%04x\n",
197 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
198 if (super != NULL) {
199 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
200 }
201 if (IsArrayClass()) {
202 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
203 }
204 if (kh.NumDirectInterfaces() > 0) {
205 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
206 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
207 Class* interface = kh.GetDirectInterface(i);
208 const ClassLoader* cl = interface->GetClassLoader();
209 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
210 }
211 }
212 os << " vtable (" << NumVirtualMethods() << " entries, "
213 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
214 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
215 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
216 }
217 os << " direct methods (" << NumDirectMethods() << " entries):\n";
218 for (size_t i = 0; i < NumDirectMethods(); ++i) {
219 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
220 }
221 if (NumStaticFields() > 0) {
222 os << " static fields (" << NumStaticFields() << " entries):\n";
223 if (IsResolved() || IsErroneous()) {
224 for (size_t i = 0; i < NumStaticFields(); ++i) {
225 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
226 }
227 } else {
228 os << " <not yet available>";
229 }
230 }
231 if (NumInstanceFields() > 0) {
232 os << " instance fields (" << NumInstanceFields() << " entries):\n";
233 if (IsResolved() || IsErroneous()) {
234 for (size_t i = 0; i < NumInstanceFields(); ++i) {
235 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
236 }
237 } else {
238 os << " <not yet available>";
239 }
240 }
241}
242
243void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
244 if (new_reference_offsets != CLASS_WALK_SUPER) {
245 // Sanity check that the number of bits set in the reference offset bitmap
246 // agrees with the number of references
247 size_t count = 0;
248 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
249 count += c->NumReferenceInstanceFieldsDuringLinking();
250 }
251 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
252 }
253 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
254 new_reference_offsets, false);
255}
256
257void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
258 if (new_reference_offsets != CLASS_WALK_SUPER) {
259 // Sanity check that the number of bits set in the reference offset bitmap
260 // agrees with the number of references
261 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
262 NumReferenceStaticFieldsDuringLinking());
263 }
264 SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
265 new_reference_offsets, false);
266}
267
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800268bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
269 size_t i = 0;
270 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
271 ++i;
272 }
273 if (descriptor1.find('/', i) != StringPiece::npos ||
274 descriptor2.find('/', i) != StringPiece::npos) {
275 return false;
276 } else {
277 return true;
278 }
279}
280
281bool Class::IsInSamePackage(const Class* that) const {
282 const Class* klass1 = this;
283 const Class* klass2 = that;
284 if (klass1 == klass2) {
285 return true;
286 }
287 // Class loaders must match.
288 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
289 return false;
290 }
291 // Arrays are in the same package when their element classes are.
292 while (klass1->IsArrayClass()) {
293 klass1 = klass1->GetComponentType();
294 }
295 while (klass2->IsArrayClass()) {
296 klass2 = klass2->GetComponentType();
297 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700298 // trivial check again for array types
299 if (klass1 == klass2) {
300 return true;
301 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 // Compare the package part of the descriptor string.
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700303 if (LIKELY(!klass1->IsProxyClass() && !klass2->IsProxyClass())) {
304 ClassHelper kh(klass1);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700305 const DexFile* dex_file1 = &kh.GetDexFile();
306 const DexFile::TypeId* type_id1 = &dex_file1->GetTypeId(klass1->GetDexTypeIndex());
307 const char* descriptor1 = dex_file1->GetTypeDescriptor(*type_id1);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700308 kh.ChangeClass(klass2);
Brian Carlstrom857fe962013-03-26 22:46:15 -0700309 const DexFile* dex_file2 = &kh.GetDexFile();
310 const DexFile::TypeId* type_id2 = &dex_file2->GetTypeId(klass2->GetDexTypeIndex());
311 const char* descriptor2 = dex_file2->GetTypeDescriptor(*type_id2);
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700312 return IsInSamePackage(descriptor1, descriptor2);
313 }
Brian Carlstrom857fe962013-03-26 22:46:15 -0700314 ClassHelper kh(klass1);
315 std::string descriptor1(kh.GetDescriptor());
316 kh.ChangeClass(klass2);
317 std::string descriptor2(kh.GetDescriptor());
318 return IsInSamePackage(descriptor1, descriptor2);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319}
320
321bool Class::IsClassClass() const {
322 Class* java_lang_Class = GetClass()->GetClass();
323 return this == java_lang_Class;
324}
325
326bool Class::IsStringClass() const {
327 return this == String::GetJavaLangString();
328}
329
330bool Class::IsThrowableClass() const {
331 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
332}
333
Brian Carlstromea46f952013-07-30 01:26:50 -0700334bool Class::IsArtFieldClass() const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700336 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
337 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338}
339
Brian Carlstromea46f952013-07-30 01:26:50 -0700340bool Class::IsArtMethodClass() const {
341 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342}
343
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344void Class::SetClassLoader(ClassLoader* new_class_loader) {
345 SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
346}
347
Brian Carlstromea46f952013-07-30 01:26:50 -0700348ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700350 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 if (method != NULL) {
352 return method;
353 }
354
355 int32_t iftable_count = GetIfTableCount();
356 IfTable* iftable = GetIfTable();
357 for (int32_t i = 0; i < iftable_count; i++) {
358 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
359 if (method != NULL) {
360 return method;
361 }
362 }
363 return NULL;
364}
365
Brian Carlstromea46f952013-07-30 01:26:50 -0700366ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700368 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 if (method != NULL) {
370 return method;
371 }
372
373 int32_t iftable_count = GetIfTableCount();
374 IfTable* iftable = GetIfTable();
375 for (int32_t i = 0; i < iftable_count; i++) {
376 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
377 if (method != NULL) {
378 return method;
379 }
380 }
381 return NULL;
382}
383
384
Brian Carlstromea46f952013-07-30 01:26:50 -0700385ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386 MethodHelper mh;
387 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700388 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 mh.ChangeMethod(method);
390 if (name == mh.GetName() && signature == mh.GetSignature()) {
391 return method;
392 }
393 }
394 return NULL;
395}
396
Brian Carlstromea46f952013-07-30 01:26:50 -0700397ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 if (GetDexCache() == dex_cache) {
399 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 if (method->GetDexMethodIndex() == dex_method_idx) {
402 return method;
403 }
404 }
405 }
406 return NULL;
407}
408
Brian Carlstromea46f952013-07-30 01:26:50 -0700409ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700411 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 if (method != NULL) {
413 return method;
414 }
415 }
416 return NULL;
417}
418
Brian Carlstromea46f952013-07-30 01:26:50 -0700419ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700421 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 if (method != NULL) {
423 return method;
424 }
425 }
426 return NULL;
427}
428
Brian Carlstromea46f952013-07-30 01:26:50 -0700429ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 const StringPiece& signature) const {
431 MethodHelper mh;
432 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700433 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 mh.ChangeMethod(method);
435 if (name == mh.GetName() && signature == mh.GetSignature()) {
436 return method;
437 }
438 }
439 return NULL;
440}
441
Brian Carlstromea46f952013-07-30 01:26:50 -0700442ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 if (GetDexCache() == dex_cache) {
444 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700445 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446 if (method->GetDexMethodIndex() == dex_method_idx) {
447 return method;
448 }
449 }
450 }
451 return NULL;
452}
453
Brian Carlstromea46f952013-07-30 01:26:50 -0700454ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700456 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457 if (method != NULL) {
458 return method;
459 }
460 }
461 return NULL;
462}
463
Brian Carlstromea46f952013-07-30 01:26:50 -0700464ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700466 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 if (method != NULL) {
468 return method;
469 }
470 }
471 return NULL;
472}
473
Brian Carlstromea46f952013-07-30 01:26:50 -0700474ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800475 // Is the field in this class?
476 // Interfaces are not relevant because they can't contain instance fields.
477 FieldHelper fh;
478 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700479 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480 fh.ChangeField(f);
481 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
482 return f;
483 }
484 }
485 return NULL;
486}
487
Brian Carlstromea46f952013-07-30 01:26:50 -0700488ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 if (GetDexCache() == dex_cache) {
490 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700491 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492 if (f->GetDexFieldIndex() == dex_field_idx) {
493 return f;
494 }
495 }
496 }
497 return NULL;
498}
499
Brian Carlstromea46f952013-07-30 01:26:50 -0700500ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800501 // Is the field in this class, or any of its superclasses?
502 // Interfaces are not relevant because they can't contain instance fields.
503 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700504 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505 if (f != NULL) {
506 return f;
507 }
508 }
509 return NULL;
510}
511
Brian Carlstromea46f952013-07-30 01:26:50 -0700512ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 // Is the field in this class, or any of its superclasses?
514 // Interfaces are not relevant because they can't contain instance fields.
515 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700516 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800517 if (f != NULL) {
518 return f;
519 }
520 }
521 return NULL;
522}
523
Brian Carlstromea46f952013-07-30 01:26:50 -0700524ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525 DCHECK(type != NULL);
526 FieldHelper fh;
527 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700528 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800529 fh.ChangeField(f);
530 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
531 return f;
532 }
533 }
534 return NULL;
535}
536
Brian Carlstromea46f952013-07-30 01:26:50 -0700537ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800538 if (dex_cache == GetDexCache()) {
539 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700540 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 if (f->GetDexFieldIndex() == dex_field_idx) {
542 return f;
543 }
544 }
545 }
546 return NULL;
547}
548
Brian Carlstromea46f952013-07-30 01:26:50 -0700549ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550 // Is the field in this class (or its interfaces), or any of its
551 // superclasses (or their interfaces)?
552 ClassHelper kh;
553 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
554 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700555 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800556 if (f != NULL) {
557 return f;
558 }
559 // Is this field in any of this class' interfaces?
560 kh.ChangeClass(k);
561 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
562 Class* interface = kh.GetDirectInterface(i);
563 f = interface->FindStaticField(name, type);
564 if (f != NULL) {
565 return f;
566 }
567 }
568 }
569 return NULL;
570}
571
Brian Carlstromea46f952013-07-30 01:26:50 -0700572ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573 ClassHelper kh;
574 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
575 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 if (f != NULL) {
578 return f;
579 }
580 // Is this field in any of this class' interfaces?
581 kh.ChangeClass(k);
582 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
583 Class* interface = kh.GetDirectInterface(i);
584 f = interface->FindStaticField(dex_cache, dex_field_idx);
585 if (f != NULL) {
586 return f;
587 }
588 }
589 }
590 return NULL;
591}
592
Brian Carlstromea46f952013-07-30 01:26:50 -0700593ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 // Find a field using the JLS field resolution order
595 ClassHelper kh;
596 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
597 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700598 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 if (f != NULL) {
600 return f;
601 }
602 f = k->FindDeclaredStaticField(name, type);
603 if (f != NULL) {
604 return f;
605 }
606 // Is this field in any of this class' interfaces?
607 kh.ChangeClass(k);
608 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
609 Class* interface = kh.GetDirectInterface(i);
610 f = interface->FindStaticField(name, type);
611 if (f != NULL) {
612 return f;
613 }
614 }
615 }
616 return NULL;
617}
618
Brian Carlstromea46f952013-07-30 01:26:50 -0700619static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200620 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
621 if (methods != NULL) {
622 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700623 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200624 DCHECK(method != NULL);
625 method->SetPreverified();
626 }
627 }
628}
629
630void Class::SetPreverifiedFlagOnAllMethods() {
631 DCHECK(IsVerified());
632 SetPreverifiedFlagOnMethods(GetDirectMethods());
633 SetPreverifiedFlagOnMethods(GetVirtualMethods());
634}
635
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636} // namespace mirror
637} // namespace art