blob: 3208de95661cc119f4dbc7fa4bab46d43cae7657 [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 Chartier83c8ee02014-01-28 14:50:23 -080053void Class::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080054 if (java_lang_Class_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -080055 callback(reinterpret_cast<mirror::Object**>(&java_lang_Class_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080056 }
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);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 if (Runtime::Current()->IsActiveTransaction()) {
116 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
117 } else {
118 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false);
119 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700120 // Classes that are being resolved or initialized need to notify waiters that the class status
121 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
122 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
123 class_linker_initialized) {
124 NotifyAll(self);
125 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126}
127
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800128void Class::SetDexCache(DexCache* new_dex_cache) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100129 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130}
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700133 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
134 DumpClass(LOG(ERROR), kDumpClassFullDetail);
135 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
136 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137 // Not called within a transaction.
138 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139}
140
141// Return the class' name. The exact format is bizarre, but it's the specified behavior for
142// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
143// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
144// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
145String* Class::ComputeName() {
146 String* name = GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800147 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 return name;
149 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800150 Thread* self = Thread::Current();
151 SirtRef<mirror::Class> sirt_c(self, this);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700152 std::string descriptor(ClassHelper(this).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
154 // The descriptor indicates that this is the class for
155 // a primitive type; special-case the return value.
156 const char* c_name = NULL;
157 switch (descriptor[0]) {
158 case 'Z': c_name = "boolean"; break;
159 case 'B': c_name = "byte"; break;
160 case 'C': c_name = "char"; break;
161 case 'S': c_name = "short"; break;
162 case 'I': c_name = "int"; break;
163 case 'J': c_name = "long"; break;
164 case 'F': c_name = "float"; break;
165 case 'D': c_name = "double"; break;
166 case 'V': c_name = "void"; break;
167 default:
168 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
169 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800170 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 } else {
172 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
173 // components.
174 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
175 descriptor.erase(0, 1);
176 descriptor.erase(descriptor.size() - 1);
177 }
178 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800179 name = String::AllocFromModifiedUtf8(self, descriptor.c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800181 sirt_c->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 return name;
183}
184
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 if ((flags & kDumpClassFullDetail) == 0) {
187 os << PrettyClass(this);
188 if ((flags & kDumpClassClassLoader) != 0) {
189 os << ' ' << GetClassLoader();
190 }
191 if ((flags & kDumpClassInitialized) != 0) {
192 os << ' ' << GetStatus();
193 }
194 os << "\n";
195 return;
196 }
197
198 Class* super = GetSuperClass();
199 ClassHelper kh(this);
200 os << "----- " << (IsInterface() ? "interface" : "class") << " "
201 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
202 os << " objectSize=" << SizeOf() << " "
203 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
204 os << StringPrintf(" access=0x%04x.%04x\n",
205 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
206 if (super != NULL) {
207 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
208 }
209 if (IsArrayClass()) {
210 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
211 }
212 if (kh.NumDirectInterfaces() > 0) {
213 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
214 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
215 Class* interface = kh.GetDirectInterface(i);
216 const ClassLoader* cl = interface->GetClassLoader();
217 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
218 }
219 }
220 os << " vtable (" << NumVirtualMethods() << " entries, "
221 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
222 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
223 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
224 }
225 os << " direct methods (" << NumDirectMethods() << " entries):\n";
226 for (size_t i = 0; i < NumDirectMethods(); ++i) {
227 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
228 }
229 if (NumStaticFields() > 0) {
230 os << " static fields (" << NumStaticFields() << " entries):\n";
231 if (IsResolved() || IsErroneous()) {
232 for (size_t i = 0; i < NumStaticFields(); ++i) {
233 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
234 }
235 } else {
236 os << " <not yet available>";
237 }
238 }
239 if (NumInstanceFields() > 0) {
240 os << " instance fields (" << NumInstanceFields() << " entries):\n";
241 if (IsResolved() || IsErroneous()) {
242 for (size_t i = 0; i < NumInstanceFields(); ++i) {
243 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
244 }
245 } else {
246 os << " <not yet available>";
247 }
248 }
249}
250
251void Class::SetReferenceInstanceOffsets(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 size_t count = 0;
256 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
257 count += c->NumReferenceInstanceFieldsDuringLinking();
258 }
259 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count);
260 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100261 // Not called within a transaction.
262 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
263 new_reference_offsets, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264}
265
266void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
267 if (new_reference_offsets != CLASS_WALK_SUPER) {
268 // Sanity check that the number of bits set in the reference offset bitmap
269 // agrees with the number of references
270 CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets),
271 NumReferenceStaticFieldsDuringLinking());
272 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100273 // Not called within a transaction.
274 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
275 new_reference_offsets, false);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276}
277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
279 size_t i = 0;
280 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
281 ++i;
282 }
283 if (descriptor1.find('/', i) != StringPiece::npos ||
284 descriptor2.find('/', i) != StringPiece::npos) {
285 return false;
286 } else {
287 return true;
288 }
289}
290
Ian Rogersef7d42f2014-01-06 12:55:46 -0800291bool Class::IsInSamePackage(Class* that) {
292 Class* klass1 = this;
293 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 if (klass1 == klass2) {
295 return true;
296 }
297 // Class loaders must match.
298 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
299 return false;
300 }
301 // Arrays are in the same package when their element classes are.
302 while (klass1->IsArrayClass()) {
303 klass1 = klass1->GetComponentType();
304 }
305 while (klass2->IsArrayClass()) {
306 klass2 = klass2->GetComponentType();
307 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700308 // trivial check again for array types
309 if (klass1 == klass2) {
310 return true;
311 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 // Compare the package part of the descriptor string.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700313 return IsInSamePackage(ClassHelper(klass1).GetDescriptor(),
314 ClassHelper(klass2).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800315}
316
Ian Rogersef7d42f2014-01-06 12:55:46 -0800317bool Class::IsClassClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 Class* java_lang_Class = GetClass()->GetClass();
319 return this == java_lang_Class;
320}
321
322bool Class::IsStringClass() const {
323 return this == String::GetJavaLangString();
324}
325
Ian Rogersef7d42f2014-01-06 12:55:46 -0800326bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
328}
329
Ian Rogersef7d42f2014-01-06 12:55:46 -0800330bool Class::IsArtFieldClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 Class* java_lang_Class = GetClass();
Brian Carlstromea46f952013-07-30 01:26:50 -0700332 Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass();
333 return this == java_lang_reflect_ArtField;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334}
335
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336bool Class::IsArtMethodClass() {
Brian Carlstromea46f952013-07-30 01:26:50 -0700337 return this == ArtMethod::GetJavaLangReflectArtMethod();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338}
339
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 if (Runtime::Current()->IsActiveTransaction()) {
342 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
343 } else {
344 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false);
345 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346}
347
Ian Rogersef7d42f2014-01-06 12:55:46 -0800348ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800366ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
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
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 MethodHelper mh;
386 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700387 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700389 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700390 return method;
391 }
392 }
393 return NULL;
394}
395
Ian Rogersef7d42f2014-01-06 12:55:46 -0800396ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700397 MethodHelper mh;
398 for (size_t i = 0; i < NumDirectMethods(); ++i) {
399 ArtMethod* method = GetDirectMethod(i);
400 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700401 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 return method;
403 }
404 }
405 return NULL;
406}
407
Ian Rogersef7d42f2014-01-06 12:55:46 -0800408ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 if (GetDexCache() == dex_cache) {
410 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700411 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 if (method->GetDexMethodIndex() == dex_method_idx) {
413 return method;
414 }
415 }
416 }
417 return NULL;
418}
419
Ian Rogersef7d42f2014-01-06 12:55:46 -0800420ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
421 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700422 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 if (method != NULL) {
424 return method;
425 }
426 }
427 return NULL;
428}
429
Ian Rogersef7d42f2014-01-06 12:55:46 -0800430ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
431 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700432 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
433 if (method != NULL) {
434 return method;
435 }
436 }
437 return NULL;
438}
439
Ian Rogersef7d42f2014-01-06 12:55:46 -0800440ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
441 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700442 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 if (method != NULL) {
444 return method;
445 }
446 }
447 return NULL;
448}
449
Ian Rogersef7d42f2014-01-06 12:55:46 -0800450ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700451 MethodHelper mh;
452 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
453 ArtMethod* method = GetVirtualMethod(i);
454 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700455 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700456 return method;
457 }
458 }
459 return NULL;
460}
461
Brian Carlstromea46f952013-07-30 01:26:50 -0700462ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800463 const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800464 MethodHelper mh;
465 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700466 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700468 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469 return method;
470 }
471 }
472 return NULL;
473}
474
Ian Rogersef7d42f2014-01-06 12:55:46 -0800475ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476 if (GetDexCache() == dex_cache) {
477 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700478 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800479 if (method->GetDexMethodIndex() == dex_method_idx) {
480 return method;
481 }
482 }
483 }
484 return NULL;
485}
486
Ian Rogersef7d42f2014-01-06 12:55:46 -0800487ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
488 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700489 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 if (method != NULL) {
491 return method;
492 }
493 }
494 return NULL;
495}
496
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
498 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700499 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
500 if (method != NULL) {
501 return method;
502 }
503 }
504 return NULL;
505}
506
Ian Rogersef7d42f2014-01-06 12:55:46 -0800507ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
508 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700509 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800510 if (method != NULL) {
511 return method;
512 }
513 }
514 return NULL;
515}
516
Ian Rogersef7d42f2014-01-06 12:55:46 -0800517ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700518 for (size_t i = 0; i < NumDirectMethods(); ++i) {
519 ArtMethod* method = GetDirectMethod(i);
520 if (method->IsConstructor() && method->IsStatic()) {
521 if (kIsDebugBuild) {
522 MethodHelper mh(method);
Ian Rogers241b5de2013-10-09 17:58:57 -0700523 CHECK(mh.IsClassInitializer());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700524 CHECK_STREQ(mh.GetName(), "<clinit>");
525 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
526 }
527 return method;
528 }
529 }
530 return NULL;
531}
532
Brian Carlstromea46f952013-07-30 01:26:50 -0700533ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534 // Is the field in this class?
535 // Interfaces are not relevant because they can't contain instance fields.
536 FieldHelper fh;
537 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700538 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700540 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 return f;
542 }
543 }
544 return NULL;
545}
546
Brian Carlstromea46f952013-07-30 01:26:50 -0700547ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548 if (GetDexCache() == dex_cache) {
549 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700550 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 if (f->GetDexFieldIndex() == dex_field_idx) {
552 return f;
553 }
554 }
555 }
556 return NULL;
557}
558
Brian Carlstromea46f952013-07-30 01:26:50 -0700559ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560 // Is the field in this class, or any of its superclasses?
561 // Interfaces are not relevant because they can't contain instance fields.
562 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700563 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 if (f != NULL) {
565 return f;
566 }
567 }
568 return NULL;
569}
570
Brian Carlstromea46f952013-07-30 01:26:50 -0700571ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800572 // Is the field in this class, or any of its superclasses?
573 // Interfaces are not relevant because they can't contain instance fields.
574 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700575 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576 if (f != NULL) {
577 return f;
578 }
579 }
580 return NULL;
581}
582
Brian Carlstromea46f952013-07-30 01:26:50 -0700583ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584 DCHECK(type != NULL);
585 FieldHelper fh;
586 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700587 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700589 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800590 return f;
591 }
592 }
593 return NULL;
594}
595
Brian Carlstromea46f952013-07-30 01:26:50 -0700596ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597 if (dex_cache == GetDexCache()) {
598 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600 if (f->GetDexFieldIndex() == dex_field_idx) {
601 return f;
602 }
603 }
604 }
605 return NULL;
606}
607
Brian Carlstromea46f952013-07-30 01:26:50 -0700608ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609 // Is the field in this class (or its interfaces), or any of its
610 // superclasses (or their interfaces)?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
612 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700613 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 if (f != NULL) {
615 return f;
616 }
617 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700618 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800619 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
620 Class* interface = kh.GetDirectInterface(i);
621 f = interface->FindStaticField(name, type);
622 if (f != NULL) {
623 return f;
624 }
625 }
626 }
627 return NULL;
628}
629
Brian Carlstromea46f952013-07-30 01:26:50 -0700630ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
632 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700633 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800634 if (f != NULL) {
635 return f;
636 }
637 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700638 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
640 Class* interface = kh.GetDirectInterface(i);
641 f = interface->FindStaticField(dex_cache, dex_field_idx);
642 if (f != NULL) {
643 return f;
644 }
645 }
646 }
647 return NULL;
648}
649
Brian Carlstromea46f952013-07-30 01:26:50 -0700650ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651 // Find a field using the JLS field resolution order
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
653 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700654 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800655 if (f != NULL) {
656 return f;
657 }
658 f = k->FindDeclaredStaticField(name, type);
659 if (f != NULL) {
660 return f;
661 }
662 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700663 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800664 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
665 Class* interface = kh.GetDirectInterface(i);
666 f = interface->FindStaticField(name, type);
667 if (f != NULL) {
668 return f;
669 }
670 }
671 }
672 return NULL;
673}
674
Brian Carlstromea46f952013-07-30 01:26:50 -0700675static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200676 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
677 if (methods != NULL) {
678 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700679 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200680 DCHECK(method != NULL);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700681 if (!method->IsNative() && !method->IsAbstract()) {
682 method->SetPreverified();
683 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200684 }
685 }
686}
687
688void Class::SetPreverifiedFlagOnAllMethods() {
689 DCHECK(IsVerified());
690 SetPreverifiedFlagOnMethods(GetDirectMethods());
691 SetPreverifiedFlagOnMethods(GetVirtualMethods());
692}
693
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800694} // namespace mirror
695} // namespace art