blob: ff63782314d4e08c2fcf3303c41c856a39debdd4 [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 }
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.
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();
Ian Rogers98379392014-02-24 16:53:16 -080098 Class* eiie_class = class_linker->FindSystemClass(self,
99 "Ljava/lang/ExceptionInInitializerError;");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 CHECK(!self->IsExceptionPending());
101
Ian Rogers62d6c772013-02-27 08:32:07 -0800102 // Only verification errors, not initialization problems, should set a verify error.
103 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
104 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 if (!eiie_class->IsAssignableFrom(exception_class)) {
106 SetVerifyErrorClass(exception_class);
107 }
108
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 // Restore exception.
110 ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(),
111 old_throw_dex_pc);
112
113 self->SetException(gc_safe_throw_location, old_exception.get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 }
Ian Rogers8f3c9ae2013-08-20 17:26:41 -0700115 CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700117 SetField32<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700119 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100120 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700121 // Classes that are being resolved or initialized need to notify waiters that the class status
122 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
123 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
124 class_linker_initialized) {
125 NotifyAll(self);
126 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800127}
128
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700130 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131}
132
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700134 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
135 DumpClass(LOG(ERROR), kDumpClassFullDetail);
136 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
137 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100138 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700139 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140}
141
142// Return the class' name. The exact format is bizarre, but it's the specified behavior for
143// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
144// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
145// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
146String* Class::ComputeName() {
147 String* name = GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800148 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149 return name;
150 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800151 Thread* self = Thread::Current();
152 SirtRef<mirror::Class> sirt_c(self, this);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700153 std::string descriptor(ClassHelper(this).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
155 // The descriptor indicates that this is the class for
156 // a primitive type; special-case the return value.
157 const char* c_name = NULL;
158 switch (descriptor[0]) {
159 case 'Z': c_name = "boolean"; break;
160 case 'B': c_name = "byte"; break;
161 case 'C': c_name = "char"; break;
162 case 'S': c_name = "short"; break;
163 case 'I': c_name = "int"; break;
164 case 'J': c_name = "long"; break;
165 case 'F': c_name = "float"; break;
166 case 'D': c_name = "double"; break;
167 case 'V': c_name = "void"; break;
168 default:
169 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
170 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800171 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 } else {
173 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
174 // components.
175 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
176 descriptor.erase(0, 1);
177 descriptor.erase(descriptor.size() - 1);
178 }
179 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800180 name = String::AllocFromModifiedUtf8(self, descriptor.c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800181 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800182 sirt_c->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183 return name;
184}
185
Ian Rogersef7d42f2014-01-06 12:55:46 -0800186void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 if ((flags & kDumpClassFullDetail) == 0) {
188 os << PrettyClass(this);
189 if ((flags & kDumpClassClassLoader) != 0) {
190 os << ' ' << GetClassLoader();
191 }
192 if ((flags & kDumpClassInitialized) != 0) {
193 os << ' ' << GetStatus();
194 }
195 os << "\n";
196 return;
197 }
198
199 Class* super = GetSuperClass();
200 ClassHelper kh(this);
201 os << "----- " << (IsInterface() ? "interface" : "class") << " "
202 << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
203 os << " objectSize=" << SizeOf() << " "
204 << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n",
205 os << StringPrintf(" access=0x%04x.%04x\n",
206 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
207 if (super != NULL) {
208 os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n";
209 }
210 if (IsArrayClass()) {
211 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
212 }
213 if (kh.NumDirectInterfaces() > 0) {
214 os << " interfaces (" << kh.NumDirectInterfaces() << "):\n";
215 for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
216 Class* interface = kh.GetDirectInterface(i);
217 const ClassLoader* cl = interface->GetClassLoader();
218 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
219 }
220 }
221 os << " vtable (" << NumVirtualMethods() << " entries, "
222 << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n";
223 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
224 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str());
225 }
226 os << " direct methods (" << NumDirectMethods() << " entries):\n";
227 for (size_t i = 0; i < NumDirectMethods(); ++i) {
228 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str());
229 }
230 if (NumStaticFields() > 0) {
231 os << " static fields (" << NumStaticFields() << " entries):\n";
232 if (IsResolved() || IsErroneous()) {
233 for (size_t i = 0; i < NumStaticFields(); ++i) {
234 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str());
235 }
236 } else {
237 os << " <not yet available>";
238 }
239 }
240 if (NumInstanceFields() > 0) {
241 os << " instance fields (" << NumInstanceFields() << " entries):\n";
242 if (IsResolved() || IsErroneous()) {
243 for (size_t i = 0; i < NumInstanceFields(); ++i) {
244 os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str());
245 }
246 } else {
247 os << " <not yet available>";
248 }
249 }
250}
251
252void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
253 if (new_reference_offsets != CLASS_WALK_SUPER) {
254 // Sanity check that the number of bits set in the reference offset bitmap
255 // agrees with the number of references
256 size_t count = 0;
257 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
258 count += c->NumReferenceInstanceFieldsDuringLinking();
259 }
Vladimir Marko81949632014-05-02 11:53:22 +0100260 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100262 // Not called within a transaction.
263 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700264 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265}
266
267void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
268 if (new_reference_offsets != CLASS_WALK_SUPER) {
269 // Sanity check that the number of bits set in the reference offset bitmap
270 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100271 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 NumReferenceStaticFieldsDuringLinking());
273 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100274 // Not called within a transaction.
275 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700276 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277}
278
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
280 size_t i = 0;
281 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
282 ++i;
283 }
284 if (descriptor1.find('/', i) != StringPiece::npos ||
285 descriptor2.find('/', i) != StringPiece::npos) {
286 return false;
287 } else {
288 return true;
289 }
290}
291
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292bool Class::IsInSamePackage(Class* that) {
293 Class* klass1 = this;
294 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295 if (klass1 == klass2) {
296 return true;
297 }
298 // Class loaders must match.
299 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
300 return false;
301 }
302 // Arrays are in the same package when their element classes are.
303 while (klass1->IsArrayClass()) {
304 klass1 = klass1->GetComponentType();
305 }
306 while (klass2->IsArrayClass()) {
307 klass2 = klass2->GetComponentType();
308 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700309 // trivial check again for array types
310 if (klass1 == klass2) {
311 return true;
312 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 // Compare the package part of the descriptor string.
Ian Rogersdfb325e2013-10-30 01:00:44 -0700314 return IsInSamePackage(ClassHelper(klass1).GetDescriptor(),
315 ClassHelper(klass2).GetDescriptor());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316}
317
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318bool Class::IsStringClass() const {
319 return this == String::GetJavaLangString();
320}
321
Ian Rogersef7d42f2014-01-06 12:55:46 -0800322bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
324}
325
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100327 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700328 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100329 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700330 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100331 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332}
333
Ian Rogersef7d42f2014-01-06 12:55:46 -0800334ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700336 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 if (method != NULL) {
338 return method;
339 }
340
341 int32_t iftable_count = GetIfTableCount();
342 IfTable* iftable = GetIfTable();
343 for (int32_t i = 0; i < iftable_count; i++) {
344 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
345 if (method != NULL) {
346 return method;
347 }
348 }
349 return NULL;
350}
351
Ian Rogersef7d42f2014-01-06 12:55:46 -0800352ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700354 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 if (method != NULL) {
356 return method;
357 }
358
359 int32_t iftable_count = GetIfTableCount();
360 IfTable* iftable = GetIfTable();
361 for (int32_t i = 0; i < iftable_count; i++) {
362 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
363 if (method != NULL) {
364 return method;
365 }
366 }
367 return NULL;
368}
369
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 MethodHelper mh;
372 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700373 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700375 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700376 return method;
377 }
378 }
379 return NULL;
380}
381
Ian Rogersef7d42f2014-01-06 12:55:46 -0800382ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700383 MethodHelper mh;
384 for (size_t i = 0; i < NumDirectMethods(); ++i) {
385 ArtMethod* method = GetDirectMethod(i);
386 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700387 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 return method;
389 }
390 }
391 return NULL;
392}
393
Ian Rogersef7d42f2014-01-06 12:55:46 -0800394ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 if (GetDexCache() == dex_cache) {
396 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700397 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 if (method->GetDexMethodIndex() == dex_method_idx) {
399 return method;
400 }
401 }
402 }
403 return NULL;
404}
405
Ian Rogersef7d42f2014-01-06 12:55:46 -0800406ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
407 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700408 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 if (method != NULL) {
410 return method;
411 }
412 }
413 return NULL;
414}
415
Ian Rogersef7d42f2014-01-06 12:55:46 -0800416ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
417 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700418 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
419 if (method != NULL) {
420 return method;
421 }
422 }
423 return NULL;
424}
425
Ian Rogersef7d42f2014-01-06 12:55:46 -0800426ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
427 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700428 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 if (method != NULL) {
430 return method;
431 }
432 }
433 return NULL;
434}
435
Ian Rogersef7d42f2014-01-06 12:55:46 -0800436ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700437 MethodHelper mh;
438 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
439 ArtMethod* method = GetVirtualMethod(i);
440 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700441 if (name == mh.GetName() && mh.GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700442 return method;
443 }
444 }
445 return NULL;
446}
447
Brian Carlstromea46f952013-07-30 01:26:50 -0700448ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800449 const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 MethodHelper mh;
451 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700452 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 mh.ChangeMethod(method);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700454 if (name == mh.GetName() && signature == mh.GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455 return method;
456 }
457 }
458 return NULL;
459}
460
Ian Rogersef7d42f2014-01-06 12:55:46 -0800461ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800462 if (GetDexCache() == dex_cache) {
463 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700464 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 if (method->GetDexMethodIndex() == dex_method_idx) {
466 return method;
467 }
468 }
469 }
470 return NULL;
471}
472
Ian Rogersef7d42f2014-01-06 12:55:46 -0800473ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
474 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700475 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476 if (method != NULL) {
477 return method;
478 }
479 }
480 return NULL;
481}
482
Ian Rogersef7d42f2014-01-06 12:55:46 -0800483ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
484 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700485 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
486 if (method != NULL) {
487 return method;
488 }
489 }
490 return NULL;
491}
492
Ian Rogersef7d42f2014-01-06 12:55:46 -0800493ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
494 for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700495 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496 if (method != NULL) {
497 return method;
498 }
499 }
500 return NULL;
501}
502
Ian Rogersef7d42f2014-01-06 12:55:46 -0800503ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700504 for (size_t i = 0; i < NumDirectMethods(); ++i) {
505 ArtMethod* method = GetDirectMethod(i);
506 if (method->IsConstructor() && method->IsStatic()) {
507 if (kIsDebugBuild) {
508 MethodHelper mh(method);
Ian Rogers241b5de2013-10-09 17:58:57 -0700509 CHECK(mh.IsClassInitializer());
Ian Rogersd91d6d62013-09-25 20:26:14 -0700510 CHECK_STREQ(mh.GetName(), "<clinit>");
511 CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V");
512 }
513 return method;
514 }
515 }
516 return NULL;
517}
518
Brian Carlstromea46f952013-07-30 01:26:50 -0700519ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520 // Is the field in this class?
521 // Interfaces are not relevant because they can't contain instance fields.
522 FieldHelper fh;
523 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700524 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700526 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527 return f;
528 }
529 }
530 return NULL;
531}
532
Brian Carlstromea46f952013-07-30 01:26:50 -0700533ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534 if (GetDexCache() == dex_cache) {
535 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700536 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 if (f->GetDexFieldIndex() == dex_field_idx) {
538 return f;
539 }
540 }
541 }
542 return NULL;
543}
544
Brian Carlstromea46f952013-07-30 01:26:50 -0700545ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800546 // Is the field in this class, or any of its superclasses?
547 // Interfaces are not relevant because they can't contain instance fields.
548 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700549 ArtField* f = c->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550 if (f != NULL) {
551 return f;
552 }
553 }
554 return NULL;
555}
556
Brian Carlstromea46f952013-07-30 01:26:50 -0700557ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 // Is the field in this class, or any of its superclasses?
559 // Interfaces are not relevant because they can't contain instance fields.
560 for (Class* c = this; c != NULL; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700561 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 if (f != NULL) {
563 return f;
564 }
565 }
566 return NULL;
567}
568
Brian Carlstromea46f952013-07-30 01:26:50 -0700569ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570 DCHECK(type != NULL);
571 FieldHelper fh;
572 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700573 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 fh.ChangeField(f);
Ian Rogersdfb325e2013-10-30 01:00:44 -0700575 if (name == fh.GetName() && type == fh.GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576 return f;
577 }
578 }
579 return NULL;
580}
581
Brian Carlstromea46f952013-07-30 01:26:50 -0700582ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583 if (dex_cache == GetDexCache()) {
584 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700585 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 if (f->GetDexFieldIndex() == dex_field_idx) {
587 return f;
588 }
589 }
590 }
591 return NULL;
592}
593
Brian Carlstromea46f952013-07-30 01:26:50 -0700594ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595 // Is the field in this class (or its interfaces), or any of its
596 // superclasses (or their interfaces)?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
598 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700599 ArtField* f = k->FindDeclaredStaticField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600 if (f != NULL) {
601 return f;
602 }
603 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700604 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
606 Class* interface = kh.GetDirectInterface(i);
607 f = interface->FindStaticField(name, type);
608 if (f != NULL) {
609 return f;
610 }
611 }
612 }
613 return NULL;
614}
615
Brian Carlstromea46f952013-07-30 01:26:50 -0700616ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
618 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700619 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 if (f != NULL) {
621 return f;
622 }
623 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700624 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
626 Class* interface = kh.GetDirectInterface(i);
627 f = interface->FindStaticField(dex_cache, dex_field_idx);
628 if (f != NULL) {
629 return f;
630 }
631 }
632 }
633 return NULL;
634}
635
Brian Carlstromea46f952013-07-30 01:26:50 -0700636ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800637 // Find a field using the JLS field resolution order
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800638 for (Class* k = this; k != NULL; k = k->GetSuperClass()) {
639 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700640 ArtField* f = k->FindDeclaredInstanceField(name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 if (f != NULL) {
642 return f;
643 }
644 f = k->FindDeclaredStaticField(name, type);
645 if (f != NULL) {
646 return f;
647 }
648 // Is this field in any of this class' interfaces?
Mathieu Chartier590fee92013-09-13 13:46:47 -0700649 ClassHelper kh(k);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800650 for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) {
651 Class* interface = kh.GetDirectInterface(i);
652 f = interface->FindStaticField(name, type);
653 if (f != NULL) {
654 return f;
655 }
656 }
657 }
658 return NULL;
659}
660
Brian Carlstromea46f952013-07-30 01:26:50 -0700661static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200662 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
663 if (methods != NULL) {
664 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700665 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200666 DCHECK(method != NULL);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700667 if (!method->IsNative() && !method->IsAbstract()) {
668 method->SetPreverified();
669 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200670 }
671 }
672}
673
674void Class::SetPreverifiedFlagOnAllMethods() {
675 DCHECK(IsVerified());
676 SetPreverifiedFlagOnMethods(GetDirectMethods());
677 SetPreverifiedFlagOnMethods(GetVirtualMethods());
678}
679
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800680} // namespace mirror
681} // namespace art