blob: be05fb8a9b7cc3970f880f294d17bcfc8e063593 [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
Brian Carlstrom004644f2014-06-18 08:34:01 -070040Class* Class::java_lang_Class_ = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
42void Class::SetClassClass(Class* java_lang_Class) {
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070043 CHECK(java_lang_Class_ == nullptr)
44 << ReadBarrier::BarrierForRoot<mirror::Class, kWithReadBarrier>(&java_lang_Class_)
45 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070046 CHECK(java_lang_Class != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047 java_lang_Class_ = java_lang_Class;
48}
49
50void Class::ResetClass() {
Brian Carlstrom004644f2014-06-18 08:34:01 -070051 CHECK(java_lang_Class_ != nullptr);
52 java_lang_Class_ = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053}
54
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080055void Class::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -080056 if (java_lang_Class_ != nullptr) {
Mathieu Chartier815873e2014-02-13 18:02:13 -080057 callback(reinterpret_cast<mirror::Object**>(&java_lang_Class_), arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080058 }
59}
60
Ian Rogers7dfb28c2013-08-22 08:18:36 -070061void Class::SetStatus(Status new_status, Thread* self) {
62 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070063 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
64 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070065 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070066 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
67 new_status != kStatusRetired)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070068 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070069 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070070 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
72 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070073 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070074 << "Attempt to change status of class while not holding its lock: "
75 << PrettyClass(this) << " " << old_status << " -> " << new_status;
76 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077 }
Ian Rogers98379392014-02-24 16:53:16 -080078 if (UNLIKELY(new_status == kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070079 CHECK_NE(GetStatus(), kStatusError)
80 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081
Ian Rogers62d6c772013-02-27 08:32:07 -080082 // Stash current exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 StackHandleScope<3> hs(self);
84 ThrowLocation old_throw_location;
85 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location)));
86 CHECK(old_exception.Get() != nullptr);
87 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis()));
88 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod()));
89 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +020090 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070091 Class* eiie_class;
92 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
93 // allocations and may cause infinite loops.
94 if (old_exception.Get() == nullptr ||
95 old_exception->GetClass()->GetDescriptor() != "Ljava/lang/OutOfMemoryError;") {
96 // Clear exception to call FindSystemClass.
97 self->ClearException();
98 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
99 self, "Ljava/lang/ExceptionInInitializerError;");
100 CHECK(!self->IsExceptionPending());
101 // 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();
104 if (!eiie_class->IsAssignableFrom(exception_class)) {
105 SetVerifyErrorClass(exception_class);
106 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
108
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 // Restore exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700110 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800111 old_throw_dex_pc);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700112 self->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200113 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 }
Ian Rogers03dbc042014-06-02 14:24:56 -0700115 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700117 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700119 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100120 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700121
122 if (!class_linker_initialized) {
123 // When the class linker is being initialized its single threaded and by definition there can be
124 // no waiters. During initialization classes may appear temporary but won't be retired as their
125 // size was statically computed.
126 } else {
127 // Classes that are being resolved or initialized need to notify waiters that the class status
128 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
129 if (IsTemp()) {
130 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
131 // so that they can grab the new version of the class from the class linker's table.
132 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this);
133 if (new_status == kStatusRetired || new_status == kStatusError) {
134 NotifyAll(self);
135 }
136 } else {
137 CHECK_NE(new_status, kStatusRetired);
138 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
139 NotifyAll(self);
140 }
141 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700142 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143}
144
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700146 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147}
148
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700150 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
151 DumpClass(LOG(ERROR), kDumpClassFullDetail);
152 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
153 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700155 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156}
157
158// Return the class' name. The exact format is bizarre, but it's the specified behavior for
159// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
160// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
161// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700162String* Class::ComputeName(Handle<Class> h_this) {
163 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800164 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 return name;
166 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700167 std::string descriptor(h_this->GetDescriptor());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800168 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
170 // The descriptor indicates that this is the class for
171 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700172 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 switch (descriptor[0]) {
174 case 'Z': c_name = "boolean"; break;
175 case 'B': c_name = "byte"; break;
176 case 'C': c_name = "char"; break;
177 case 'S': c_name = "short"; break;
178 case 'I': c_name = "int"; break;
179 case 'J': c_name = "long"; break;
180 case 'F': c_name = "float"; break;
181 case 'D': c_name = "double"; break;
182 case 'V': c_name = "void"; break;
183 default:
184 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
185 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800186 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 } else {
188 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
189 // components.
190 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
191 descriptor.erase(0, 1);
192 descriptor.erase(descriptor.size() - 1);
193 }
194 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800195 name = String::AllocFromModifiedUtf8(self, descriptor.c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700197 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 return name;
199}
200
Ian Rogersef7d42f2014-01-06 12:55:46 -0800201void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 if ((flags & kDumpClassFullDetail) == 0) {
203 os << PrettyClass(this);
204 if ((flags & kDumpClassClassLoader) != 0) {
205 os << ' ' << GetClassLoader();
206 }
207 if ((flags & kDumpClassInitialized) != 0) {
208 os << ' ' << GetStatus();
209 }
210 os << "\n";
211 return;
212 }
213
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214 Thread* self = Thread::Current();
215 StackHandleScope<2> hs(self);
216 Handle<mirror::Class> h_this(hs.NewHandle(this));
217 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
218
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Mathieu Chartierf8322842014-05-16 10:59:25 -0700220 << "'" << GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700222 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 os << StringPrintf(" access=0x%04x.%04x\n",
224 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700225 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700226 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
227 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800228 }
229 if (IsArrayClass()) {
230 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
231 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700232 const size_t num_direct_interfaces = NumDirectInterfaces();
233 if (num_direct_interfaces > 0) {
234 os << " interfaces (" << num_direct_interfaces << "):\n";
235 for (size_t i = 0; i < num_direct_interfaces; ++i) {
236 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 const ClassLoader* cl = interface->GetClassLoader();
238 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
239 }
240 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241 if (!IsLoaded()) {
242 os << " class not yet loaded";
243 } else {
244 // After this point, this may have moved due to GetDirectInterface.
245 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
246 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
247 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
248 os << StringPrintf(" %2zd: %s\n", i,
249 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700251 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
252 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
253 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
254 }
255 if (h_this->NumStaticFields() > 0) {
256 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
257 if (h_this->IsResolved() || h_this->IsErroneous()) {
258 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
259 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
260 }
261 } else {
262 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800263 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700264 }
265 if (h_this->NumInstanceFields() > 0) {
266 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
267 if (h_this->IsResolved() || h_this->IsErroneous()) {
268 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
269 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
270 }
271 } else {
272 os << " <not yet available>";
273 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 }
275 }
276}
277
278void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
279 if (new_reference_offsets != CLASS_WALK_SUPER) {
280 // Sanity check that the number of bits set in the reference offset bitmap
281 // agrees with the number of references
282 size_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700283 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284 count += c->NumReferenceInstanceFieldsDuringLinking();
285 }
Vladimir Marko81949632014-05-02 11:53:22 +0100286 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288 // Not called within a transaction.
289 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700290 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291}
292
293void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
294 if (new_reference_offsets != CLASS_WALK_SUPER) {
295 // Sanity check that the number of bits set in the reference offset bitmap
296 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100297 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 NumReferenceStaticFieldsDuringLinking());
299 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100300 // Not called within a transaction.
301 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700302 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303}
304
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
306 size_t i = 0;
307 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
308 ++i;
309 }
310 if (descriptor1.find('/', i) != StringPiece::npos ||
311 descriptor2.find('/', i) != StringPiece::npos) {
312 return false;
313 } else {
314 return true;
315 }
316}
317
Ian Rogersef7d42f2014-01-06 12:55:46 -0800318bool Class::IsInSamePackage(Class* that) {
319 Class* klass1 = this;
320 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800321 if (klass1 == klass2) {
322 return true;
323 }
324 // Class loaders must match.
325 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
326 return false;
327 }
328 // Arrays are in the same package when their element classes are.
329 while (klass1->IsArrayClass()) {
330 klass1 = klass1->GetComponentType();
331 }
332 while (klass2->IsArrayClass()) {
333 klass2 = klass2->GetComponentType();
334 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700335 // trivial check again for array types
336 if (klass1 == klass2) {
337 return true;
338 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339 // Compare the package part of the descriptor string.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700340 return IsInSamePackage(klass1->GetDescriptor().c_str(), klass2->GetDescriptor().c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341}
342
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343bool Class::IsStringClass() const {
344 return this == String::GetJavaLangString();
345}
346
Ian Rogersef7d42f2014-01-06 12:55:46 -0800347bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800348 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
349}
350
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700353 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100354 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700355 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100356 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357}
358
Brian Carlstrom004644f2014-06-18 08:34:01 -0700359ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700361 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700362 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 return method;
364 }
365
366 int32_t iftable_count = GetIfTableCount();
367 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700368 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700370 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 return method;
372 }
373 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700374 return nullptr;
375}
376
377ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
378 // Check the current class before checking the interfaces.
379 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
380 if (method != nullptr) {
381 return method;
382 }
383
384 int32_t iftable_count = GetIfTableCount();
385 IfTable* iftable = GetIfTable();
386 for (int32_t i = 0; i < iftable_count; ++i) {
387 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
388 if (method != nullptr) {
389 return method;
390 }
391 }
392 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393}
394
Ian Rogersef7d42f2014-01-06 12:55:46 -0800395ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700397 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700398 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 return method;
400 }
401
402 int32_t iftable_count = GetIfTableCount();
403 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700404 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700406 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 return method;
408 }
409 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700410 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411}
412
Ian Rogersef7d42f2014-01-06 12:55:46 -0800413ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700415 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700416 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700417 return method;
418 }
419 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700420 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700421}
422
Ian Rogersef7d42f2014-01-06 12:55:46 -0800423ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700424 for (size_t i = 0; i < NumDirectMethods(); ++i) {
425 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700426 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427 return method;
428 }
429 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700430 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431}
432
Ian Rogersef7d42f2014-01-06 12:55:46 -0800433ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434 if (GetDexCache() == dex_cache) {
435 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437 if (method->GetDexMethodIndex() == dex_method_idx) {
438 return method;
439 }
440 }
441 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443}
444
Ian Rogersef7d42f2014-01-06 12:55:46 -0800445ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700447 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700448 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449 return method;
450 }
451 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453}
454
Ian Rogersef7d42f2014-01-06 12:55:46 -0800455ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700456 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700457 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700459 return method;
460 }
461 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700462 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700463}
464
Ian Rogersef7d42f2014-01-06 12:55:46 -0800465ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700466 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700467 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469 return method;
470 }
471 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700472 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800473}
474
Ian Rogersef7d42f2014-01-06 12:55:46 -0800475ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700476 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
477 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700478 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700479 return method;
480 }
481 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700482 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700483}
484
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700485ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700487 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700488 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 return method;
490 }
491 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700492 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493}
494
Ian Rogersef7d42f2014-01-06 12:55:46 -0800495ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496 if (GetDexCache() == dex_cache) {
497 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700498 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 if (method->GetDexMethodIndex() == dex_method_idx) {
500 return method;
501 }
502 }
503 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700504 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505}
506
Ian Rogersef7d42f2014-01-06 12:55:46 -0800507ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700508 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700509 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700510 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511 return method;
512 }
513 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700514 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515}
516
Ian Rogersef7d42f2014-01-06 12:55:46 -0800517ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700518 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700519 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700520 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700521 return method;
522 }
523 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700524 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700525}
526
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700528 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700529 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700530 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800531 return method;
532 }
533 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700534 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800535}
536
Ian Rogersef7d42f2014-01-06 12:55:46 -0800537ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700538 for (size_t i = 0; i < NumDirectMethods(); ++i) {
539 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700540 if (method->IsClassInitializer()) {
541 DCHECK_STREQ(method->GetName(), "<clinit>");
542 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700543 return method;
544 }
545 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700546 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700547}
548
Brian Carlstromea46f952013-07-30 01:26:50 -0700549ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550 // Is the field in this class?
551 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700553 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700554 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 return f;
556 }
557 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700558 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800559}
560
Brian Carlstromea46f952013-07-30 01:26:50 -0700561ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 if (GetDexCache() == dex_cache) {
563 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 if (f->GetDexFieldIndex() == dex_field_idx) {
566 return f;
567 }
568 }
569 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700570 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571}
572
Brian Carlstromea46f952013-07-30 01:26:50 -0700573ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 // Is the field in this class, or any of its superclasses?
575 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700576 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700578 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 return f;
580 }
581 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700582 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583}
584
Brian Carlstromea46f952013-07-30 01:26:50 -0700585ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 // Is the field in this class, or any of its superclasses?
587 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700588 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700589 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700590 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 return f;
592 }
593 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700594 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595}
596
Brian Carlstromea46f952013-07-30 01:26:50 -0700597ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700598 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700600 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700601 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602 return f;
603 }
604 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700605 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800606}
607
Brian Carlstromea46f952013-07-30 01:26:50 -0700608ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609 if (dex_cache == GetDexCache()) {
610 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800612 if (f->GetDexFieldIndex() == dex_field_idx) {
613 return f;
614 }
615 }
616 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700617 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618}
619
Mathieu Chartierf8322842014-05-16 10:59:25 -0700620ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
621 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800622 // Is the field in this class (or its interfaces), or any of its
623 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700624 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700626 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700627 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 return f;
629 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700630 // Wrap k incase it moves during GetDirectInterface.
631 StackHandleScope<1> hs(self);
632 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800633 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700634 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
635 StackHandleScope<1> hs(self);
636 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
637 f = FindStaticField(self, interface, name, type);
638 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 return f;
640 }
641 }
642 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700643 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644}
645
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
647 uint32_t dex_field_idx) {
648 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800649 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700650 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700651 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 return f;
653 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700654 // Wrap k incase it moves during GetDirectInterface.
655 StackHandleScope<1> hs(self);
656 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700658 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
659 StackHandleScope<1> hs(self);
660 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
661 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
662 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800663 return f;
664 }
665 }
666 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700667 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668}
669
Mathieu Chartierf8322842014-05-16 10:59:25 -0700670ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
671 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800672 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700673 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700675 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700676 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677 return f;
678 }
679 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700680 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800681 return f;
682 }
683 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700684 StackHandleScope<1> hs(self);
685 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
686 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
687 StackHandleScope<1> hs(self);
688 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
689 f = interface->FindStaticField(self, interface, name, type);
690 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800691 return f;
692 }
693 }
694 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700695 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800696}
697
Brian Carlstromea46f952013-07-30 01:26:50 -0700698static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200699 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700700 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200701 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700702 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700703 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700704 if (!method->IsNative() && !method->IsAbstract()) {
705 method->SetPreverified();
706 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200707 }
708 }
709}
710
711void Class::SetPreverifiedFlagOnAllMethods() {
712 DCHECK(IsVerified());
713 SetPreverifiedFlagOnMethods(GetDirectMethods());
714 SetPreverifiedFlagOnMethods(GetVirtualMethods());
715}
716
Mathieu Chartierf8322842014-05-16 10:59:25 -0700717std::string Class::GetDescriptor() {
718 if (UNLIKELY(IsArrayClass())) {
719 return GetArrayDescriptor();
720 } else if (UNLIKELY(IsPrimitive())) {
721 return Primitive::Descriptor(GetPrimitiveType());
722 } else if (UNLIKELY(IsProxyClass())) {
723 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
724 } else {
725 const DexFile& dex_file = GetDexFile();
726 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
727 return dex_file.GetTypeDescriptor(type_id);
728 }
729}
730
731std::string Class::GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
732 return "[" + GetComponentType()->GetDescriptor();
733}
734
735const DexFile::ClassDef* Class::GetClassDef() {
736 uint16_t class_def_idx = GetDexClassDefIndex();
737 if (class_def_idx == DexFile::kDexNoIndex16) {
738 return nullptr;
739 }
740 return &GetDexFile().GetClassDef(class_def_idx);
741}
742
743uint32_t Class::NumDirectInterfaces() {
744 if (IsPrimitive()) {
745 return 0;
746 } else if (IsArrayClass()) {
747 return 2;
748 } else if (IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700749 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700750 return interfaces != nullptr ? interfaces->GetLength() : 0;
751 } else {
752 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
753 if (interfaces == nullptr) {
754 return 0;
755 } else {
756 return interfaces->Size();
757 }
758 }
759}
760
761uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
762 DCHECK(!IsPrimitive());
763 DCHECK(!IsArrayClass());
764 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
765}
766
767mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) {
768 DCHECK(klass.Get() != nullptr);
769 DCHECK(!klass->IsPrimitive());
770 if (klass->IsArrayClass()) {
771 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
772 if (idx == 0) {
773 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
774 } else {
775 DCHECK_EQ(1U, idx);
776 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
777 }
778 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700779 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700780 DCHECK(interfaces != nullptr);
781 return interfaces->Get(idx);
782 } else {
783 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
784 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
785 if (interface == nullptr) {
786 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
787 klass.Get());
788 CHECK(interface != nullptr || self->IsExceptionPending());
789 }
790 return interface;
791 }
792}
793
794const char* Class::GetSourceFile() {
795 std::string descriptor(GetDescriptor());
796 const DexFile& dex_file = GetDexFile();
797 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200798 if (dex_class_def == nullptr) {
799 // Generated classes have no class def.
800 return nullptr;
801 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700802 return dex_file.GetSourceFile(*dex_class_def);
803}
804
805std::string Class::GetLocation() {
806 mirror::DexCache* dex_cache = GetDexCache();
807 if (dex_cache != nullptr && !IsProxyClass()) {
808 return dex_cache->GetLocation()->ToModifiedUtf8();
809 }
810 // Arrays and proxies are generated and have no corresponding dex file location.
811 return "generated class";
812}
813
814const DexFile::TypeList* Class::GetInterfaceTypeList() {
815 const DexFile::ClassDef* class_def = GetClassDef();
816 if (class_def == nullptr) {
817 return nullptr;
818 }
819 return GetDexFile().GetInterfacesList(*class_def);
820}
821
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700822void Class::PopulateEmbeddedImtAndVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
823 ObjectArray<ArtMethod>* table = GetImTable();
824 if (table != nullptr) {
825 for (uint32_t i = 0; i < kImtSize; i++) {
826 SetEmbeddedImTableEntry(i, table->Get(i));
827 }
828 }
829
830 table = GetVTableDuringLinking();
831 CHECK(table != nullptr);
832 for (int32_t i = 0; i < table->GetLength(); i++) {
833 SetEmbeddedVTableEntry(i, table->Get(i));
834 }
835}
836
837Class* Class::CopyOf(Thread* self, int32_t new_length) {
838 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
839 // We may get copied by a compacting GC.
840 StackHandleScope<1> hs(self);
841 Handle<mirror::Class> h_this(hs.NewHandle(this));
842 gc::Heap* heap = Runtime::Current()->GetHeap();
843 InitializeClassVisitor visitor(new_length);
844
845 mirror::Object* new_class =
846 kMovingClasses ? heap->AllocObject<true>(self, java_lang_Class_, new_length, visitor)
847 : heap->AllocNonMovableObject<true>(self, java_lang_Class_, new_length, visitor);
848 if (UNLIKELY(new_class == nullptr)) {
849 CHECK(self->IsExceptionPending()); // Expect an OOME.
850 return NULL;
851 }
852
853 mirror::Class* new_class_obj = new_class->AsClass();
854 memcpy(new_class_obj, h_this.Get(), sizeof(Class));
855
856 new_class_obj->SetStatus(kStatusResolving, self);
857 new_class_obj->PopulateEmbeddedImtAndVTable();
858 // Correct some fields.
859 new_class_obj->SetLockWord(LockWord(), false);
860 new_class_obj->SetClassSize(new_length);
861
862 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class_obj);
863 return new_class_obj;
864}
865
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800866} // namespace mirror
867} // namespace art