blob: 15b69f378bb639890fb8d925c55836bcb73a9df4 [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"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031#include "handle_scope-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#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 }
Ian Rogers98379392014-02-24 16:53:16 -080075 if (UNLIKELY(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.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070080 StackHandleScope<3> hs(self);
81 ThrowLocation old_throw_location;
82 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location)));
83 CHECK(old_exception.Get() != nullptr);
84 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis()));
85 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod()));
86 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087
88 // clear exception to call FindSystemClass
89 self->ClearException();
90 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -080091 Class* eiie_class = class_linker->FindSystemClass(self,
92 "Ljava/lang/ExceptionInInitializerError;");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 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.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700103 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800104 old_throw_dex_pc);
105
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700106 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);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100109 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700110 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100111 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700112 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700114 // Classes that are being resolved or initialized need to notify waiters that the class status
115 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
116 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
117 class_linker_initialized) {
118 NotifyAll(self);
119 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800120}
121
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700123 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124}
125
Ian Rogersef7d42f2014-01-06 12:55:46 -0800126void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
128 DumpClass(LOG(ERROR), kDumpClassFullDetail);
129 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
130 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100131 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700132 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133}
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();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800141 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142 return name;
143 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800144 Thread* self = Thread::Current();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700145 StackHandleScope<1> hs(self);
146 Handle<mirror::Class> handle_c(hs.NewHandle(this));
Ian Rogersdfb325e2013-10-30 01:00:44 -0700147 std::string descriptor(ClassHelper(this).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
149 // The descriptor indicates that this is the class for
150 // a primitive type; special-case the return value.
151 const char* c_name = NULL;
152 switch (descriptor[0]) {
153 case 'Z': c_name = "boolean"; break;
154 case 'B': c_name = "byte"; break;
155 case 'C': c_name = "char"; break;
156 case 'S': c_name = "short"; break;
157 case 'I': c_name = "int"; break;
158 case 'J': c_name = "long"; break;
159 case 'F': c_name = "float"; break;
160 case 'D': c_name = "double"; break;
161 case 'V': c_name = "void"; break;
162 default:
163 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
164 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800165 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 } else {
167 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
168 // components.
169 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
170 descriptor.erase(0, 1);
171 descriptor.erase(descriptor.size() - 1);
172 }
173 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800174 name = String::AllocFromModifiedUtf8(self, descriptor.c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700176 handle_c->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177 return name;
178}
179
Ian Rogersef7d42f2014-01-06 12:55:46 -0800180void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 if ((flags & kDumpClassFullDetail) == 0) {
182 os << PrettyClass(this);
183 if ((flags & kDumpClassClassLoader) != 0) {
184 os << ' ' << GetClassLoader();
185 }
186 if ((flags & kDumpClassInitialized) != 0) {
187 os << ' ' << GetStatus();
188 }
189 os << "\n";
190 return;
191 }
192
193 Class* super = GetSuperClass();
194 ClassHelper kh(this);
195 os << "----- " << (IsInterface() ? "interface" : "class") << " "
196 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
197 os << " objectSize=" << SizeOf() << " "
198 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
199 os << StringPrintf(" access=0x%04x.%04x\n",
200 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
201 if (super != NULL) {
202 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
203 }
204 if (IsArrayClass()) {
205 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
206 }
207 if (kh.NumDirectInterfaces() > 0) {
208 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
209 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
210 Class* interface = kh.GetDirectInterface(i);
211 const ClassLoader* cl = interface->GetClassLoader();
212 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
213 }
214 }
215 os << " vtable (" << NumVirtualMethods() << " entries, "
216 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
217 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
218 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
219 }
220 os << " direct methods (" << NumDirectMethods() << " entries):\n";
221 for (size_t i = 0; i < NumDirectMethods(); ++i) {
222 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
223 }
224 if (NumStaticFields() > 0) {
225 os << " static fields (" << NumStaticFields() << " entries):\n";
226 if (IsResolved() || IsErroneous()) {
227 for (size_t i = 0; i < NumStaticFields(); ++i) {
228 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
229 }
230 } else {
231 os << " <not yet available>";
232 }
233 }
234 if (NumInstanceFields() > 0) {
235 os << " instance fields (" << NumInstanceFields() << " entries):\n";
236 if (IsResolved() || IsErroneous()) {
237 for (size_t i = 0; i < NumInstanceFields(); ++i) {
238 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
239 }
240 } else {
241 os << " <not yet available>";
242 }
243 }
244}
245
246void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
247 if (new_reference_offsets != CLASS_WALK_SUPER) {
248 // Sanity check that the number of bits set in the reference offset bitmap
249 // agrees with the number of references
250 size_t count = 0;
251 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
252 count += c->NumReferenceInstanceFieldsDuringLinking();
253 }
Vladimir Marko81949632014-05-02 11:53:22 +0100254 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100256 // Not called within a transaction.
257 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700258 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259}
260
261void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
262 if (new_reference_offsets != CLASS_WALK_SUPER) {
263 // Sanity check that the number of bits set in the reference offset bitmap
264 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100265 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 NumReferenceStaticFieldsDuringLinking());
267 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100268 // Not called within a transaction.
269 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700270 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271}
272
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
274 size_t i = 0;
275 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
276 ++i;
277 }
278 if (descriptor1.find('/', i) != StringPiece::npos ||
279 descriptor2.find('/', i) != StringPiece::npos) {
280 return false;
281 } else {
282 return true;
283 }
284}
285
Ian Rogersef7d42f2014-01-06 12:55:46 -0800286bool Class::IsInSamePackage(Class* that) {
287 Class* klass1 = this;
288 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 if (klass1 == klass2) {
290 return true;
291 }
292 // Class loaders must match.
293 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
294 return false;
295 }
296 // Arrays are in the same package when their element classes are.
297 while (klass1->IsArrayClass()) {
298 klass1 = klass1->GetComponentType();
299 }
300 while (klass2->IsArrayClass()) {
301 klass2 = klass2->GetComponentType();
302 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700303 // trivial check again for array types
304 if (klass1 == klass2) {
305 return true;
306 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 // Compare the package part of the descriptor string.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700308 return IsInSamePackage(ClassHelper(klass1).GetDescriptor(),
309 ClassHelper(klass2).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310}
311
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312bool Class::IsStringClass() const {
313 return this == String::GetJavaLangString();
314}
315
Ian Rogersef7d42f2014-01-06 12:55:46 -0800316bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
318}
319
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100321 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700322 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100323 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700324 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100325 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326}
327
Ian Rogersef7d42f2014-01-06 12:55:46 -0800328ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700330 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 if (method != NULL) {
332 return method;
333 }
334
335 int32_t iftable_count = GetIfTableCount();
336 IfTable* iftable = GetIfTable();
337 for (int32_t i = 0; i < iftable_count; i++) {
338 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
339 if (method != NULL) {
340 return method;
341 }
342 }
343 return NULL;
344}
345
Ian Rogersef7d42f2014-01-06 12:55:46 -0800346ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700348 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 if (method != NULL) {
350 return method;
351 }
352
353 int32_t iftable_count = GetIfTableCount();
354 IfTable* iftable = GetIfTable();
355 for (int32_t i = 0; i < iftable_count; i++) {
356 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
357 if (method != NULL) {
358 return method;
359 }
360 }
361 return NULL;
362}
363
Ian Rogersef7d42f2014-01-06 12:55:46 -0800364ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 MethodHelper mh;
366 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700367 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700369 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700370 return method;
371 }
372 }
373 return NULL;
374}
375
Ian Rogersef7d42f2014-01-06 12:55:46 -0800376ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700377 MethodHelper mh;
378 for (size_t i = 0; i < NumDirectMethods(); ++i) {
379 ArtMethod* method = GetDirectMethod(i);
380 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700381 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 return method;
383 }
384 }
385 return NULL;
386}
387
Ian Rogersef7d42f2014-01-06 12:55:46 -0800388ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 if (GetDexCache() == dex_cache) {
390 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700391 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 if (method->GetDexMethodIndex() == dex_method_idx) {
393 return method;
394 }
395 }
396 }
397 return NULL;
398}
399
Ian Rogersef7d42f2014-01-06 12:55:46 -0800400ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
401 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700402 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 if (method != NULL) {
404 return method;
405 }
406 }
407 return NULL;
408}
409
Ian Rogersef7d42f2014-01-06 12:55:46 -0800410ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
411 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700412 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
413 if (method != NULL) {
414 return method;
415 }
416 }
417 return NULL;
418}
419
Ian Rogersef7d42f2014-01-06 12:55:46 -0800420ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
421 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700422 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 if (method != NULL) {
424 return method;
425 }
426 }
427 return NULL;
428}
429
Ian Rogersef7d42f2014-01-06 12:55:46 -0800430ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700431 MethodHelper mh;
432 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
433 ArtMethod* method = GetVirtualMethod(i);
434 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700435 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700436 return method;
437 }
438 }
439 return NULL;
440}
441
Brian Carlstromea46f952013-07-30 01:26:50 -0700442ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800443 const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 MethodHelper mh;
445 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700446 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700448 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449 return method;
450 }
451 }
452 return NULL;
453}
454
Ian Rogersef7d42f2014-01-06 12:55:46 -0800455ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456 if (GetDexCache() == dex_cache) {
457 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700458 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459 if (method->GetDexMethodIndex() == dex_method_idx) {
460 return method;
461 }
462 }
463 }
464 return NULL;
465}
466
Ian Rogersef7d42f2014-01-06 12:55:46 -0800467ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
468 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700469 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470 if (method != NULL) {
471 return method;
472 }
473 }
474 return NULL;
475}
476
Ian Rogersef7d42f2014-01-06 12:55:46 -0800477ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
478 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700479 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
480 if (method != NULL) {
481 return method;
482 }
483 }
484 return NULL;
485}
486
Ian Rogersef7d42f2014-01-06 12:55:46 -0800487ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
488 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700489 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 if (method != NULL) {
491 return method;
492 }
493 }
494 return NULL;
495}
496
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700498 for (size_t i = 0; i < NumDirectMethods(); ++i) {
499 ArtMethod* method = GetDirectMethod(i);
500 if (method->IsConstructor() && method->IsStatic()) {
501 if (kIsDebugBuild) {
502 MethodHelper mh(method);
Ian Rogers241b5de2013-10-09 17:58:57 -0700503 CHECK(mh.IsClassInitializer());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700504 CHECK_STREQ(mh.GetName(), "<clinit>");
505 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
506 }
507 return method;
508 }
509 }
510 return NULL;
511}
512
Brian Carlstromea46f952013-07-30 01:26:50 -0700513ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514 // Is the field in this class?
515 // Interfaces are not relevant because they can't contain instance fields.
516 FieldHelper fh;
517 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700518 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800519 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700520 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800521 return f;
522 }
523 }
524 return NULL;
525}
526
Brian Carlstromea46f952013-07-30 01:26:50 -0700527ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800528 if (GetDexCache() == dex_cache) {
529 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700530 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800531 if (f->GetDexFieldIndex() == dex_field_idx) {
532 return f;
533 }
534 }
535 }
536 return NULL;
537}
538
Brian Carlstromea46f952013-07-30 01:26:50 -0700539ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540 // Is the field in this class, or any of its superclasses?
541 // Interfaces are not relevant because they can't contain instance fields.
542 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700543 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544 if (f != NULL) {
545 return f;
546 }
547 }
548 return NULL;
549}
550
Brian Carlstromea46f952013-07-30 01:26:50 -0700551ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 // Is the field in this class, or any of its superclasses?
553 // Interfaces are not relevant because they can't contain instance fields.
554 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700555 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800556 if (f != NULL) {
557 return f;
558 }
559 }
560 return NULL;
561}
562
Brian Carlstromea46f952013-07-30 01:26:50 -0700563ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 DCHECK(type != NULL);
565 FieldHelper fh;
566 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700567 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700569 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570 return f;
571 }
572 }
573 return NULL;
574}
575
Brian Carlstromea46f952013-07-30 01:26:50 -0700576ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 if (dex_cache == GetDexCache()) {
578 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700579 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800580 if (f->GetDexFieldIndex() == dex_field_idx) {
581 return f;
582 }
583 }
584 }
585 return NULL;
586}
587
Brian Carlstromea46f952013-07-30 01:26:50 -0700588ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800589 // Is the field in this class (or its interfaces), or any of its
590 // superclasses (or their interfaces)?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
592 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 if (f != NULL) {
595 return f;
596 }
597 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700598 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
600 Class* interface = kh.GetDirectInterface(i);
601 f = interface->FindStaticField(name, type);
602 if (f != NULL) {
603 return f;
604 }
605 }
606 }
607 return NULL;
608}
609
Brian Carlstromea46f952013-07-30 01:26:50 -0700610ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 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(dex_cache, dex_field_idx);
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(dex_cache, dex_field_idx);
622 if (f != NULL) {
623 return f;
624 }
625 }
626 }
627 return NULL;
628}
629
Brian Carlstromea46f952013-07-30 01:26:50 -0700630ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 // Find a field using the JLS field resolution order
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
633 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700634 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800635 if (f != NULL) {
636 return f;
637 }
638 f = k->FindDeclaredStaticField(name, type);
639 if (f != NULL) {
640 return f;
641 }
642 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700643 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
645 Class* interface = kh.GetDirectInterface(i);
646 f = interface->FindStaticField(name, type);
647 if (f != NULL) {
648 return f;
649 }
650 }
651 }
652 return NULL;
653}
654
Brian Carlstromea46f952013-07-30 01:26:50 -0700655static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200656 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
657 if (methods != NULL) {
658 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700659 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200660 DCHECK(method != NULL);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700661 if (!method->IsNative() && !method->IsAbstract()) {
662 method->SetPreverified();
663 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200664 }
665 }
666}
667
668void Class::SetPreverifiedFlagOnAllMethods() {
669 DCHECK(IsVerified());
670 SetPreverifiedFlagOnMethods(GetDirectMethods());
671 SetPreverifiedFlagOnMethods(GetVirtualMethods());
672}
673
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674} // namespace mirror
675} // namespace art