blob: c6472c66d175ffe1ce4775df687a346dc99055f0 [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)) {
66 if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070069 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070070 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
71 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070072 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 << "Attempt to change status of class while not holding its lock: "
74 << PrettyClass(this) << " " << old_status << " -> " << new_status;
75 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 }
Ian Rogers98379392014-02-24 16:53:16 -080077 if (UNLIKELY(new_status == kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070078 CHECK_NE(GetStatus(), kStatusError)
79 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Ian Rogers62d6c772013-02-27 08:32:07 -080081 // Stash current exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 StackHandleScope<3> hs(self);
83 ThrowLocation old_throw_location;
84 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location)));
85 CHECK(old_exception.Get() != nullptr);
86 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis()));
87 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod()));
88 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +020089 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080090 // clear exception to call FindSystemClass
91 self->ClearException();
92 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -080093 Class* eiie_class = class_linker->FindSystemClass(self,
94 "Ljava/lang/ExceptionInInitializerError;");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095 CHECK(!self->IsExceptionPending());
96
Ian Rogers62d6c772013-02-27 08:32:07 -080097 // Only verification errors, not initialization problems, should set a verify error.
98 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
99 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 if (!eiie_class->IsAssignableFrom(exception_class)) {
101 SetVerifyErrorClass(exception_class);
102 }
103
Ian Rogers62d6c772013-02-27 08:32:07 -0800104 // Restore exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700105 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800106 old_throw_dex_pc);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700107 self->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200108 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 }
Ian Rogers03dbc042014-06-02 14:24:56 -0700110 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100111 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700112 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700114 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700116 // Classes that are being resolved or initialized need to notify waiters that the class status
117 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
118 if ((old_status >= kStatusResolved || new_status >= kStatusResolved) &&
119 class_linker_initialized) {
120 NotifyAll(self);
121 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800122}
123
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800124void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700125 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800126}
127
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700129 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
130 DumpClass(LOG(ERROR), kDumpClassFullDetail);
131 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
132 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100133 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700134 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800135}
136
137// Return the class' name. The exact format is bizarre, but it's the specified behavior for
138// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
139// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
140// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700141String* Class::ComputeName(Handle<Class> h_this) {
142 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800143 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 return name;
145 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700146 std::string descriptor(h_this->GetDescriptor());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800147 Thread* self = Thread::Current();
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.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700151 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152 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 Chartierf8322842014-05-16 10:59:25 -0700176 h_this->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
Mathieu Chartierf8322842014-05-16 10:59:25 -0700193 Thread* self = Thread::Current();
194 StackHandleScope<2> hs(self);
195 Handle<mirror::Class> h_this(hs.NewHandle(this));
196 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
197
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Mathieu Chartierf8322842014-05-16 10:59:25 -0700199 << "'" << GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700201 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 os << StringPrintf(" access=0x%04x.%04x\n",
203 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700204 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700205 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
206 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 }
208 if (IsArrayClass()) {
209 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
210 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700211 const size_t num_direct_interfaces = NumDirectInterfaces();
212 if (num_direct_interfaces > 0) {
213 os << " interfaces (" << num_direct_interfaces << "):\n";
214 for (size_t i = 0; i < num_direct_interfaces; ++i) {
215 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 const ClassLoader* cl = interface->GetClassLoader();
217 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
218 }
219 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700220 // After this point, this may have moved due to GetDirectInterface.
221 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700222 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700224 os << StringPrintf(" %2zd: %s\n", i,
225 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700227 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
228 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
229 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700231 if (h_this->NumStaticFields() > 0) {
232 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
233 if (h_this->IsResolved() || h_this->IsErroneous()) {
234 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
235 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800236 }
237 } else {
238 os << " <not yet available>";
239 }
240 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700241 if (h_this->NumInstanceFields() > 0) {
242 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
243 if (h_this->IsResolved() || h_this->IsErroneous()) {
244 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
245 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 }
247 } else {
248 os << " <not yet available>";
249 }
250 }
251}
252
253void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
254 if (new_reference_offsets != CLASS_WALK_SUPER) {
255 // Sanity check that the number of bits set in the reference offset bitmap
256 // agrees with the number of references
257 size_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700258 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259 count += c->NumReferenceInstanceFieldsDuringLinking();
260 }
Vladimir Marko81949632014-05-02 11:53:22 +0100261 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100263 // Not called within a transaction.
264 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700265 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266}
267
268void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
269 if (new_reference_offsets != CLASS_WALK_SUPER) {
270 // Sanity check that the number of bits set in the reference offset bitmap
271 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100272 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 NumReferenceStaticFieldsDuringLinking());
274 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100275 // Not called within a transaction.
276 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700277 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278}
279
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
281 size_t i = 0;
282 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
283 ++i;
284 }
285 if (descriptor1.find('/', i) != StringPiece::npos ||
286 descriptor2.find('/', i) != StringPiece::npos) {
287 return false;
288 } else {
289 return true;
290 }
291}
292
Ian Rogersef7d42f2014-01-06 12:55:46 -0800293bool Class::IsInSamePackage(Class* that) {
294 Class* klass1 = this;
295 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 if (klass1 == klass2) {
297 return true;
298 }
299 // Class loaders must match.
300 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
301 return false;
302 }
303 // Arrays are in the same package when their element classes are.
304 while (klass1->IsArrayClass()) {
305 klass1 = klass1->GetComponentType();
306 }
307 while (klass2->IsArrayClass()) {
308 klass2 = klass2->GetComponentType();
309 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700310 // trivial check again for array types
311 if (klass1 == klass2) {
312 return true;
313 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 // Compare the package part of the descriptor string.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700315 return IsInSamePackage(klass1->GetDescriptor().c_str(), klass2->GetDescriptor().c_str());
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
Brian Carlstrom004644f2014-06-18 08:34:01 -0700334ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& 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);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700337 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 return method;
339 }
340
341 int32_t iftable_count = GetIfTableCount();
342 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700343 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700345 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346 return method;
347 }
348 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700349 return nullptr;
350}
351
352ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
353 // Check the current class before checking the interfaces.
354 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
355 if (method != nullptr) {
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(name, signature);
363 if (method != nullptr) {
364 return method;
365 }
366 }
367 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368}
369
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700372 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700373 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 return method;
375 }
376
377 int32_t iftable_count = GetIfTableCount();
378 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700379 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700381 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382 return method;
383 }
384 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700385 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386}
387
Ian Rogersef7d42f2014-01-06 12:55:46 -0800388ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700390 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700391 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700392 return method;
393 }
394 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700395 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700396}
397
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700399 for (size_t i = 0; i < NumDirectMethods(); ++i) {
400 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700401 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 return method;
403 }
404 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700405 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406}
407
Ian Rogersef7d42f2014-01-06 12:55:46 -0800408ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 if (GetDexCache() == dex_cache) {
410 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700411 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800412 if (method->GetDexMethodIndex() == dex_method_idx) {
413 return method;
414 }
415 }
416 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700417 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800418}
419
Ian Rogersef7d42f2014-01-06 12:55:46 -0800420ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700421 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700422 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700423 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424 return method;
425 }
426 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700427 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428}
429
Ian Rogersef7d42f2014-01-06 12:55:46 -0800430ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700431 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700432 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700433 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700434 return method;
435 }
436 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700437 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700438}
439
Ian Rogersef7d42f2014-01-06 12:55:46 -0800440ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700441 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700442 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700443 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 return method;
445 }
446 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448}
449
Ian Rogersef7d42f2014-01-06 12:55:46 -0800450ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700451 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
452 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700453 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700454 return method;
455 }
456 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700458}
459
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700460ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700462 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700463 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800464 return method;
465 }
466 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700467 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468}
469
Ian Rogersef7d42f2014-01-06 12:55:46 -0800470ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 if (GetDexCache() == dex_cache) {
472 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700473 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800474 if (method->GetDexMethodIndex() == dex_method_idx) {
475 return method;
476 }
477 }
478 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700479 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480}
481
Ian Rogersef7d42f2014-01-06 12:55:46 -0800482ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700483 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700484 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700485 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 return method;
487 }
488 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700489 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490}
491
Ian Rogersef7d42f2014-01-06 12:55:46 -0800492ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700493 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700494 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700495 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700496 return method;
497 }
498 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700499 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700500}
501
Ian Rogersef7d42f2014-01-06 12:55:46 -0800502ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700503 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700504 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700505 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800506 return method;
507 }
508 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700509 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800510}
511
Ian Rogersef7d42f2014-01-06 12:55:46 -0800512ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700513 for (size_t i = 0; i < NumDirectMethods(); ++i) {
514 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700515 if (method->IsClassInitializer()) {
516 DCHECK_STREQ(method->GetName(), "<clinit>");
517 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700518 return method;
519 }
520 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700522}
523
Brian Carlstromea46f952013-07-30 01:26:50 -0700524ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800525 // Is the field in this class?
526 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700528 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700529 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530 return f;
531 }
532 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700533 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534}
535
Brian Carlstromea46f952013-07-30 01:26:50 -0700536ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 if (GetDexCache() == dex_cache) {
538 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700539 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540 if (f->GetDexFieldIndex() == dex_field_idx) {
541 return f;
542 }
543 }
544 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700545 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800546}
547
Brian Carlstromea46f952013-07-30 01:26:50 -0700548ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549 // Is the field in this class, or any of its superclasses?
550 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700551 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700552 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700553 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 return f;
555 }
556 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700557 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558}
559
Brian Carlstromea46f952013-07-30 01:26:50 -0700560ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 // Is the field in this class, or any of its superclasses?
562 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700563 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700565 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 return f;
567 }
568 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700569 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570}
571
Brian Carlstromea46f952013-07-30 01:26:50 -0700572ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700573 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700575 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700576 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 return f;
578 }
579 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700580 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581}
582
Brian Carlstromea46f952013-07-30 01:26:50 -0700583ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584 if (dex_cache == GetDexCache()) {
585 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800587 if (f->GetDexFieldIndex() == dex_field_idx) {
588 return f;
589 }
590 }
591 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700592 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593}
594
Mathieu Chartierf8322842014-05-16 10:59:25 -0700595ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
596 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597 // Is the field in this class (or its interfaces), or any of its
598 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700599 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700601 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700602 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800603 return f;
604 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700605 // Wrap k incase it moves during GetDirectInterface.
606 StackHandleScope<1> hs(self);
607 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700609 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
610 StackHandleScope<1> hs(self);
611 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
612 f = FindStaticField(self, interface, name, type);
613 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 return f;
615 }
616 }
617 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700618 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800619}
620
Mathieu Chartierf8322842014-05-16 10:59:25 -0700621ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
622 uint32_t dex_field_idx) {
623 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700625 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700626 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 return f;
628 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700629 // Wrap k incase it moves during GetDirectInterface.
630 StackHandleScope<1> hs(self);
631 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700633 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
634 StackHandleScope<1> hs(self);
635 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
636 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
637 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800638 return f;
639 }
640 }
641 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700642 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800643}
644
Mathieu Chartierf8322842014-05-16 10:59:25 -0700645ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
646 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700648 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->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700651 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 return f;
653 }
654 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700655 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800656 return f;
657 }
658 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700659 StackHandleScope<1> hs(self);
660 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
661 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
662 StackHandleScope<1> hs(self);
663 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
664 f = interface->FindStaticField(self, interface, name, type);
665 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800666 return f;
667 }
668 }
669 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700670 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671}
672
Brian Carlstromea46f952013-07-30 01:26:50 -0700673static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200674 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700675 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200676 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700677 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700678 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700679 if (!method->IsNative() && !method->IsAbstract()) {
680 method->SetPreverified();
681 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200682 }
683 }
684}
685
686void Class::SetPreverifiedFlagOnAllMethods() {
687 DCHECK(IsVerified());
688 SetPreverifiedFlagOnMethods(GetDirectMethods());
689 SetPreverifiedFlagOnMethods(GetVirtualMethods());
690}
691
Mathieu Chartierf8322842014-05-16 10:59:25 -0700692std::string Class::GetDescriptor() {
693 if (UNLIKELY(IsArrayClass())) {
694 return GetArrayDescriptor();
695 } else if (UNLIKELY(IsPrimitive())) {
696 return Primitive::Descriptor(GetPrimitiveType());
697 } else if (UNLIKELY(IsProxyClass())) {
698 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
699 } else {
700 const DexFile& dex_file = GetDexFile();
701 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
702 return dex_file.GetTypeDescriptor(type_id);
703 }
704}
705
706std::string Class::GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
707 return "[" + GetComponentType()->GetDescriptor();
708}
709
710const DexFile::ClassDef* Class::GetClassDef() {
711 uint16_t class_def_idx = GetDexClassDefIndex();
712 if (class_def_idx == DexFile::kDexNoIndex16) {
713 return nullptr;
714 }
715 return &GetDexFile().GetClassDef(class_def_idx);
716}
717
718uint32_t Class::NumDirectInterfaces() {
719 if (IsPrimitive()) {
720 return 0;
721 } else if (IsArrayClass()) {
722 return 2;
723 } else if (IsProxyClass()) {
724 mirror::SynthesizedProxyClass* proxy_class=
725 reinterpret_cast<mirror::SynthesizedProxyClass*>(this);
726 mirror::ObjectArray<mirror::Class>* interfaces = proxy_class->GetInterfaces();
727 return interfaces != nullptr ? interfaces->GetLength() : 0;
728 } else {
729 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
730 if (interfaces == nullptr) {
731 return 0;
732 } else {
733 return interfaces->Size();
734 }
735 }
736}
737
738uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
739 DCHECK(!IsPrimitive());
740 DCHECK(!IsArrayClass());
741 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
742}
743
744mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) {
745 DCHECK(klass.Get() != nullptr);
746 DCHECK(!klass->IsPrimitive());
747 if (klass->IsArrayClass()) {
748 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
749 if (idx == 0) {
750 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
751 } else {
752 DCHECK_EQ(1U, idx);
753 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
754 }
755 } else if (klass->IsProxyClass()) {
756 mirror::SynthesizedProxyClass* proxy_class =
757 reinterpret_cast<mirror::SynthesizedProxyClass*>(klass.Get());
758 mirror::ObjectArray<mirror::Class>* interfaces = proxy_class->GetInterfaces();
759 DCHECK(interfaces != nullptr);
760 return interfaces->Get(idx);
761 } else {
762 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
763 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
764 if (interface == nullptr) {
765 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
766 klass.Get());
767 CHECK(interface != nullptr || self->IsExceptionPending());
768 }
769 return interface;
770 }
771}
772
773const char* Class::GetSourceFile() {
774 std::string descriptor(GetDescriptor());
775 const DexFile& dex_file = GetDexFile();
776 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200777 if (dex_class_def == nullptr) {
778 // Generated classes have no class def.
779 return nullptr;
780 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700781 return dex_file.GetSourceFile(*dex_class_def);
782}
783
784std::string Class::GetLocation() {
785 mirror::DexCache* dex_cache = GetDexCache();
786 if (dex_cache != nullptr && !IsProxyClass()) {
787 return dex_cache->GetLocation()->ToModifiedUtf8();
788 }
789 // Arrays and proxies are generated and have no corresponding dex file location.
790 return "generated class";
791}
792
793const DexFile::TypeList* Class::GetInterfaceTypeList() {
794 const DexFile::ClassDef* class_def = GetClassDef();
795 if (class_def == nullptr) {
796 return nullptr;
797 }
798 return GetDexFile().GetInterfacesList(*class_def);
799}
800
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800801} // namespace mirror
802} // namespace art