blob: 371e984b03328521240b05c1b5a7d64d6e913248 [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();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080091 // clear exception to call FindSystemClass
92 self->ClearException();
93 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers98379392014-02-24 16:53:16 -080094 Class* eiie_class = class_linker->FindSystemClass(self,
95 "Ljava/lang/ExceptionInInitializerError;");
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 CHECK(!self->IsExceptionPending());
97
Ian Rogers62d6c772013-02-27 08:32:07 -080098 // Only verification errors, not initialization problems, should set a verify error.
99 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case.
100 Class* exception_class = old_exception->GetClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 if (!eiie_class->IsAssignableFrom(exception_class)) {
102 SetVerifyErrorClass(exception_class);
103 }
104
Ian Rogers62d6c772013-02-27 08:32:07 -0800105 // Restore exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700106 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800107 old_throw_dex_pc);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700108 self->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200109 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 }
Ian Rogers03dbc042014-06-02 14:24:56 -0700111 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100112 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700113 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700115 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700117
118 if (!class_linker_initialized) {
119 // When the class linker is being initialized its single threaded and by definition there can be
120 // no waiters. During initialization classes may appear temporary but won't be retired as their
121 // size was statically computed.
122 } else {
123 // Classes that are being resolved or initialized need to notify waiters that the class status
124 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
125 if (IsTemp()) {
126 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
127 // so that they can grab the new version of the class from the class linker's table.
128 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this);
129 if (new_status == kStatusRetired || new_status == kStatusError) {
130 NotifyAll(self);
131 }
132 } else {
133 CHECK_NE(new_status, kStatusRetired);
134 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
135 NotifyAll(self);
136 }
137 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700138 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139}
140
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700142 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143}
144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700146 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
147 DumpClass(LOG(ERROR), kDumpClassFullDetail);
148 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
149 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700151 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152}
153
154// Return the class' name. The exact format is bizarre, but it's the specified behavior for
155// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
156// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
157// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700158String* Class::ComputeName(Handle<Class> h_this) {
159 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800160 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 return name;
162 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700163 std::string descriptor(h_this->GetDescriptor());
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800164 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
166 // The descriptor indicates that this is the class for
167 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700168 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 switch (descriptor[0]) {
170 case 'Z': c_name = "boolean"; break;
171 case 'B': c_name = "byte"; break;
172 case 'C': c_name = "char"; break;
173 case 'S': c_name = "short"; break;
174 case 'I': c_name = "int"; break;
175 case 'J': c_name = "long"; break;
176 case 'F': c_name = "float"; break;
177 case 'D': c_name = "double"; break;
178 case 'V': c_name = "void"; break;
179 default:
180 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
181 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800182 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800183 } else {
184 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
185 // components.
186 if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') {
187 descriptor.erase(0, 1);
188 descriptor.erase(descriptor.size() - 1);
189 }
190 std::replace(descriptor.begin(), descriptor.end(), '/', '.');
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800191 name = String::AllocFromModifiedUtf8(self, descriptor.c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700193 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 return name;
195}
196
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 if ((flags & kDumpClassFullDetail) == 0) {
199 os << PrettyClass(this);
200 if ((flags & kDumpClassClassLoader) != 0) {
201 os << ' ' << GetClassLoader();
202 }
203 if ((flags & kDumpClassInitialized) != 0) {
204 os << ' ' << GetStatus();
205 }
206 os << "\n";
207 return;
208 }
209
Mathieu Chartierf8322842014-05-16 10:59:25 -0700210 Thread* self = Thread::Current();
211 StackHandleScope<2> hs(self);
212 Handle<mirror::Class> h_this(hs.NewHandle(this));
213 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
214
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Mathieu Chartierf8322842014-05-16 10:59:25 -0700216 << "'" << GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700218 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 os << StringPrintf(" access=0x%04x.%04x\n",
220 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700221 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700222 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
223 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 }
225 if (IsArrayClass()) {
226 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
227 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700228 const size_t num_direct_interfaces = NumDirectInterfaces();
229 if (num_direct_interfaces > 0) {
230 os << " interfaces (" << num_direct_interfaces << "):\n";
231 for (size_t i = 0; i < num_direct_interfaces; ++i) {
232 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233 const ClassLoader* cl = interface->GetClassLoader();
234 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
235 }
236 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700237 if (!IsLoaded()) {
238 os << " class not yet loaded";
239 } else {
240 // After this point, this may have moved due to GetDirectInterface.
241 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
242 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
243 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
244 os << StringPrintf(" %2zd: %s\n", i,
245 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800246 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700247 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
248 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
249 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
250 }
251 if (h_this->NumStaticFields() > 0) {
252 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
253 if (h_this->IsResolved() || h_this->IsErroneous()) {
254 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
255 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
256 }
257 } else {
258 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800259 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700260 }
261 if (h_this->NumInstanceFields() > 0) {
262 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
263 if (h_this->IsResolved() || h_this->IsErroneous()) {
264 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
265 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
266 }
267 } else {
268 os << " <not yet available>";
269 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 }
271 }
272}
273
274void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
275 if (new_reference_offsets != CLASS_WALK_SUPER) {
276 // Sanity check that the number of bits set in the reference offset bitmap
277 // agrees with the number of references
278 size_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700279 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 count += c->NumReferenceInstanceFieldsDuringLinking();
281 }
Vladimir Marko81949632014-05-02 11:53:22 +0100282 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100284 // Not called within a transaction.
285 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700286 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287}
288
289void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
290 if (new_reference_offsets != CLASS_WALK_SUPER) {
291 // Sanity check that the number of bits set in the reference offset bitmap
292 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100293 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294 NumReferenceStaticFieldsDuringLinking());
295 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100296 // Not called within a transaction.
297 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700298 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299}
300
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
302 size_t i = 0;
303 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
304 ++i;
305 }
306 if (descriptor1.find('/', i) != StringPiece::npos ||
307 descriptor2.find('/', i) != StringPiece::npos) {
308 return false;
309 } else {
310 return true;
311 }
312}
313
Ian Rogersef7d42f2014-01-06 12:55:46 -0800314bool Class::IsInSamePackage(Class* that) {
315 Class* klass1 = this;
316 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800317 if (klass1 == klass2) {
318 return true;
319 }
320 // Class loaders must match.
321 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
322 return false;
323 }
324 // Arrays are in the same package when their element classes are.
325 while (klass1->IsArrayClass()) {
326 klass1 = klass1->GetComponentType();
327 }
328 while (klass2->IsArrayClass()) {
329 klass2 = klass2->GetComponentType();
330 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700331 // trivial check again for array types
332 if (klass1 == klass2) {
333 return true;
334 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335 // Compare the package part of the descriptor string.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700336 return IsInSamePackage(klass1->GetDescriptor().c_str(), klass2->GetDescriptor().c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337}
338
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339bool Class::IsStringClass() const {
340 return this == String::GetJavaLangString();
341}
342
Ian Rogersef7d42f2014-01-06 12:55:46 -0800343bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
345}
346
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700349 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100350 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700351 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353}
354
Brian Carlstrom004644f2014-06-18 08:34:01 -0700355ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700357 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700358 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 return method;
360 }
361
362 int32_t iftable_count = GetIfTableCount();
363 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700364 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700366 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800367 return method;
368 }
369 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700370 return nullptr;
371}
372
373ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
374 // Check the current class before checking the interfaces.
375 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
376 if (method != nullptr) {
377 return method;
378 }
379
380 int32_t iftable_count = GetIfTableCount();
381 IfTable* iftable = GetIfTable();
382 for (int32_t i = 0; i < iftable_count; ++i) {
383 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
384 if (method != nullptr) {
385 return method;
386 }
387 }
388 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389}
390
Ian Rogersef7d42f2014-01-06 12:55:46 -0800391ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700393 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700394 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 return method;
396 }
397
398 int32_t iftable_count = GetIfTableCount();
399 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700400 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700402 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 return method;
404 }
405 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700406 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407}
408
Ian Rogersef7d42f2014-01-06 12:55:46 -0800409ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700411 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700412 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700413 return method;
414 }
415 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700416 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700417}
418
Ian Rogersef7d42f2014-01-06 12:55:46 -0800419ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700420 for (size_t i = 0; i < NumDirectMethods(); ++i) {
421 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700422 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 return method;
424 }
425 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700426 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427}
428
Ian Rogersef7d42f2014-01-06 12:55:46 -0800429ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 if (GetDexCache() == dex_cache) {
431 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700432 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433 if (method->GetDexMethodIndex() == dex_method_idx) {
434 return method;
435 }
436 }
437 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700438 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439}
440
Ian Rogersef7d42f2014-01-06 12:55:46 -0800441ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700443 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700444 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445 return method;
446 }
447 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700448 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800449}
450
Ian Rogersef7d42f2014-01-06 12:55:46 -0800451ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700453 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700455 return method;
456 }
457 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700459}
460
Ian Rogersef7d42f2014-01-06 12:55:46 -0800461ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700462 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700463 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 return method;
466 }
467 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469}
470
Ian Rogersef7d42f2014-01-06 12:55:46 -0800471ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700472 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
473 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700474 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700475 return method;
476 }
477 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700478 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700479}
480
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700481ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700483 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700484 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 return method;
486 }
487 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700488 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489}
490
Ian Rogersef7d42f2014-01-06 12:55:46 -0800491ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492 if (GetDexCache() == dex_cache) {
493 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700494 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 if (method->GetDexMethodIndex() == dex_method_idx) {
496 return method;
497 }
498 }
499 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700500 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800501}
502
Ian Rogersef7d42f2014-01-06 12:55:46 -0800503ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700504 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700505 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700506 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507 return method;
508 }
509 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700510 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511}
512
Ian Rogersef7d42f2014-01-06 12:55:46 -0800513ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700514 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700515 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700516 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700517 return method;
518 }
519 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700520 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700521}
522
Ian Rogersef7d42f2014-01-06 12:55:46 -0800523ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700524 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700525 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700526 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527 return method;
528 }
529 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700530 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800531}
532
Ian Rogersef7d42f2014-01-06 12:55:46 -0800533ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700534 for (size_t i = 0; i < NumDirectMethods(); ++i) {
535 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700536 if (method->IsClassInitializer()) {
537 DCHECK_STREQ(method->GetName(), "<clinit>");
538 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700539 return method;
540 }
541 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700542 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700543}
544
Brian Carlstromea46f952013-07-30 01:26:50 -0700545ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800546 // Is the field in this class?
547 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700549 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700550 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 return f;
552 }
553 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700554 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555}
556
Brian Carlstromea46f952013-07-30 01:26:50 -0700557ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 if (GetDexCache() == dex_cache) {
559 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700560 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 if (f->GetDexFieldIndex() == dex_field_idx) {
562 return f;
563 }
564 }
565 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700566 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567}
568
Brian Carlstromea46f952013-07-30 01:26:50 -0700569ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570 // Is the field in this class, or any of its superclasses?
571 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700572 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700573 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700574 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575 return f;
576 }
577 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700578 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579}
580
Brian Carlstromea46f952013-07-30 01:26:50 -0700581ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800582 // Is the field in this class, or any of its superclasses?
583 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700584 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700585 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700586 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800587 return f;
588 }
589 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700590 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591}
592
Brian Carlstromea46f952013-07-30 01:26:50 -0700593ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700594 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700596 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700597 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598 return f;
599 }
600 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700601 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602}
603
Brian Carlstromea46f952013-07-30 01:26:50 -0700604ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605 if (dex_cache == GetDexCache()) {
606 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700607 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608 if (f->GetDexFieldIndex() == dex_field_idx) {
609 return f;
610 }
611 }
612 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700613 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614}
615
Mathieu Chartierf8322842014-05-16 10:59:25 -0700616ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
617 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 // Is the field in this class (or its interfaces), or any of its
619 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700620 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700622 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700623 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624 return f;
625 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700626 // Wrap k incase it moves during GetDirectInterface.
627 StackHandleScope<1> hs(self);
628 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800629 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700630 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
631 StackHandleScope<1> hs(self);
632 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
633 f = FindStaticField(self, interface, name, type);
634 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800635 return f;
636 }
637 }
638 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700639 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800640}
641
Mathieu Chartierf8322842014-05-16 10:59:25 -0700642ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
643 uint32_t dex_field_idx) {
644 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800645 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700646 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700647 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800648 return f;
649 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700650 // Wrap k incase it moves during GetDirectInterface.
651 StackHandleScope<1> hs(self);
652 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800653 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700654 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
655 StackHandleScope<1> hs(self);
656 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
657 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
658 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800659 return f;
660 }
661 }
662 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700663 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800664}
665
Mathieu Chartierf8322842014-05-16 10:59:25 -0700666ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
667 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700669 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800670 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700671 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700672 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800673 return f;
674 }
675 f = k->FindDeclaredStaticField(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 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700680 StackHandleScope<1> hs(self);
681 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
682 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
683 StackHandleScope<1> hs(self);
684 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
685 f = interface->FindStaticField(self, interface, name, type);
686 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800687 return f;
688 }
689 }
690 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700691 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800692}
693
Brian Carlstromea46f952013-07-30 01:26:50 -0700694static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200695 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700696 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200697 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700698 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700699 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700700 if (!method->IsNative() && !method->IsAbstract()) {
701 method->SetPreverified();
702 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200703 }
704 }
705}
706
707void Class::SetPreverifiedFlagOnAllMethods() {
708 DCHECK(IsVerified());
709 SetPreverifiedFlagOnMethods(GetDirectMethods());
710 SetPreverifiedFlagOnMethods(GetVirtualMethods());
711}
712
Mathieu Chartierf8322842014-05-16 10:59:25 -0700713std::string Class::GetDescriptor() {
714 if (UNLIKELY(IsArrayClass())) {
715 return GetArrayDescriptor();
716 } else if (UNLIKELY(IsPrimitive())) {
717 return Primitive::Descriptor(GetPrimitiveType());
718 } else if (UNLIKELY(IsProxyClass())) {
719 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
720 } else {
721 const DexFile& dex_file = GetDexFile();
722 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
723 return dex_file.GetTypeDescriptor(type_id);
724 }
725}
726
727std::string Class::GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
728 return "[" + GetComponentType()->GetDescriptor();
729}
730
731const DexFile::ClassDef* Class::GetClassDef() {
732 uint16_t class_def_idx = GetDexClassDefIndex();
733 if (class_def_idx == DexFile::kDexNoIndex16) {
734 return nullptr;
735 }
736 return &GetDexFile().GetClassDef(class_def_idx);
737}
738
739uint32_t Class::NumDirectInterfaces() {
740 if (IsPrimitive()) {
741 return 0;
742 } else if (IsArrayClass()) {
743 return 2;
744 } else if (IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700745 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700746 return interfaces != nullptr ? interfaces->GetLength() : 0;
747 } else {
748 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
749 if (interfaces == nullptr) {
750 return 0;
751 } else {
752 return interfaces->Size();
753 }
754 }
755}
756
757uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
758 DCHECK(!IsPrimitive());
759 DCHECK(!IsArrayClass());
760 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
761}
762
763mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) {
764 DCHECK(klass.Get() != nullptr);
765 DCHECK(!klass->IsPrimitive());
766 if (klass->IsArrayClass()) {
767 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
768 if (idx == 0) {
769 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
770 } else {
771 DCHECK_EQ(1U, idx);
772 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
773 }
774 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700775 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700776 DCHECK(interfaces != nullptr);
777 return interfaces->Get(idx);
778 } else {
779 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
780 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
781 if (interface == nullptr) {
782 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
783 klass.Get());
784 CHECK(interface != nullptr || self->IsExceptionPending());
785 }
786 return interface;
787 }
788}
789
790const char* Class::GetSourceFile() {
791 std::string descriptor(GetDescriptor());
792 const DexFile& dex_file = GetDexFile();
793 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200794 if (dex_class_def == nullptr) {
795 // Generated classes have no class def.
796 return nullptr;
797 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700798 return dex_file.GetSourceFile(*dex_class_def);
799}
800
801std::string Class::GetLocation() {
802 mirror::DexCache* dex_cache = GetDexCache();
803 if (dex_cache != nullptr && !IsProxyClass()) {
804 return dex_cache->GetLocation()->ToModifiedUtf8();
805 }
806 // Arrays and proxies are generated and have no corresponding dex file location.
807 return "generated class";
808}
809
810const DexFile::TypeList* Class::GetInterfaceTypeList() {
811 const DexFile::ClassDef* class_def = GetClassDef();
812 if (class_def == nullptr) {
813 return nullptr;
814 }
815 return GetDexFile().GetInterfacesList(*class_def);
816}
817
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700818void Class::PopulateEmbeddedImtAndVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
819 ObjectArray<ArtMethod>* table = GetImTable();
820 if (table != nullptr) {
821 for (uint32_t i = 0; i < kImtSize; i++) {
822 SetEmbeddedImTableEntry(i, table->Get(i));
823 }
824 }
825
826 table = GetVTableDuringLinking();
827 CHECK(table != nullptr);
828 for (int32_t i = 0; i < table->GetLength(); i++) {
829 SetEmbeddedVTableEntry(i, table->Get(i));
830 }
831}
832
833Class* Class::CopyOf(Thread* self, int32_t new_length) {
834 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
835 // We may get copied by a compacting GC.
836 StackHandleScope<1> hs(self);
837 Handle<mirror::Class> h_this(hs.NewHandle(this));
838 gc::Heap* heap = Runtime::Current()->GetHeap();
839 InitializeClassVisitor visitor(new_length);
840
841 mirror::Object* new_class =
842 kMovingClasses ? heap->AllocObject<true>(self, java_lang_Class_, new_length, visitor)
843 : heap->AllocNonMovableObject<true>(self, java_lang_Class_, new_length, visitor);
844 if (UNLIKELY(new_class == nullptr)) {
845 CHECK(self->IsExceptionPending()); // Expect an OOME.
846 return NULL;
847 }
848
849 mirror::Class* new_class_obj = new_class->AsClass();
850 memcpy(new_class_obj, h_this.Get(), sizeof(Class));
851
852 new_class_obj->SetStatus(kStatusResolving, self);
853 new_class_obj->PopulateEmbeddedImtAndVTable();
854 // Correct some fields.
855 new_class_obj->SetLockWord(LockWord(), false);
856 new_class_obj->SetClassSize(new_length);
857
858 Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(new_class_obj);
859 return new_class_obj;
860}
861
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800862} // namespace mirror
863} // namespace art