blob: ae684b162c87da1989a22c0a0a734348d8ab8649 [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_linker.h"
22#include "class_loader.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070023#include "class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#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"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070027#include "handle_scope-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "object_array-inl.h"
29#include "object-inl.h"
30#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "thread.h"
32#include "throwable.h"
33#include "utils.h"
34#include "well_known_classes.h"
35
36namespace art {
37namespace mirror {
38
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070039GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
41void Class::SetClassClass(Class* java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070042 CHECK(java_lang_Class_.IsNull())
43 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070044 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070045 CHECK(java_lang_Class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070046 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047}
48
49void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070050 CHECK(!java_lang_Class_.IsNull());
51 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080054void Class::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080055 java_lang_Class_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080056}
57
Ian Rogers7dfb28c2013-08-22 08:18:36 -070058void Class::SetStatus(Status new_status, Thread* self) {
59 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070062 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070063 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
64 new_status != kStatusRetired)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070065 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070066 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
69 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070070 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071 << "Attempt to change status of class while not holding its lock: "
72 << PrettyClass(this) << " " << old_status << " -> " << new_status;
73 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
Ian Rogers98379392014-02-24 16:53:16 -080075 if (UNLIKELY(new_status == kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070076 CHECK_NE(GetStatus(), kStatusError)
77 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078
Ian Rogers62d6c772013-02-27 08:32:07 -080079 // Stash current exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070080 StackHandleScope<3> hs(self);
81 ThrowLocation old_throw_location;
82 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location)));
83 CHECK(old_exception.Get() != nullptr);
84 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis()));
85 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod()));
86 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +020087 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070088 Class* eiie_class;
89 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
90 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070091 bool throw_eiie = (old_exception.Get() == nullptr);
92 if (!throw_eiie) {
93 std::string temp;
94 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
95 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
96 }
97 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070098 // Clear exception to call FindSystemClass.
99 self->ClearException();
100 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
101 self, "Ljava/lang/ExceptionInInitializerError;");
102 CHECK(!self->IsExceptionPending());
103 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700104 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
105 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700106 Class* exception_class = old_exception->GetClass();
107 if (!eiie_class->IsAssignableFrom(exception_class)) {
108 SetVerifyErrorClass(exception_class);
109 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 }
111
Ian Rogers62d6c772013-02-27 08:32:07 -0800112 // Restore exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700113 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800114 old_throw_dex_pc);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 self->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200116 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800117 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800118 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100119 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700120 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100121 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700122 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100123 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700124
125 if (!class_linker_initialized) {
126 // When the class linker is being initialized its single threaded and by definition there can be
127 // no waiters. During initialization classes may appear temporary but won't be retired as their
128 // size was statically computed.
129 } else {
130 // Classes that are being resolved or initialized need to notify waiters that the class status
131 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
132 if (IsTemp()) {
133 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
134 // so that they can grab the new version of the class from the class linker's table.
135 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this);
136 if (new_status == kStatusRetired || new_status == kStatusError) {
137 NotifyAll(self);
138 }
139 } else {
140 CHECK_NE(new_status, kStatusRetired);
141 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
142 NotifyAll(self);
143 }
144 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700145 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146}
147
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700149 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800150 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151}
152
Ian Rogersef7d42f2014-01-06 12:55:46 -0800153void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700154 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
155 DumpClass(LOG(ERROR), kDumpClassFullDetail);
156 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
157 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100158 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700159 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800160}
161
162// Return the class' name. The exact format is bizarre, but it's the specified behavior for
163// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
164// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
165// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700166String* Class::ComputeName(Handle<Class> h_this) {
167 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800168 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 return name;
170 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700171 std::string temp;
172 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800173 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
175 // The descriptor indicates that this is the class for
176 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700177 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800178 switch (descriptor[0]) {
179 case 'Z': c_name = "boolean"; break;
180 case 'B': c_name = "byte"; break;
181 case 'C': c_name = "char"; break;
182 case 'S': c_name = "short"; break;
183 case 'I': c_name = "int"; break;
184 case 'J': c_name = "long"; break;
185 case 'F': c_name = "float"; break;
186 case 'D': c_name = "double"; break;
187 case 'V': c_name = "void"; break;
188 default:
189 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
190 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800191 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 } else {
193 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
194 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700195 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(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 Rogers1ff3c982014-08-12 02:30:58 -0700219 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700221 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700223 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 os << StringPrintf(" access=0x%04x.%04x\n",
225 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700226 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700227 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
228 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 }
230 if (IsArrayClass()) {
231 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
232 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700233 const size_t num_direct_interfaces = NumDirectInterfaces();
234 if (num_direct_interfaces > 0) {
235 os << " interfaces (" << num_direct_interfaces << "):\n";
236 for (size_t i = 0; i < num_direct_interfaces; ++i) {
237 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 const ClassLoader* cl = interface->GetClassLoader();
239 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
240 }
241 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 if (!IsLoaded()) {
243 os << " class not yet loaded";
244 } else {
245 // After this point, this may have moved due to GetDirectInterface.
246 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
247 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
248 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
249 os << StringPrintf(" %2zd: %s\n", i,
250 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
253 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
254 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
255 }
256 if (h_this->NumStaticFields() > 0) {
257 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
258 if (h_this->IsResolved() || h_this->IsErroneous()) {
259 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
260 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
261 }
262 } else {
263 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700265 }
266 if (h_this->NumInstanceFields() > 0) {
267 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
268 if (h_this->IsResolved() || h_this->IsErroneous()) {
269 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
270 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
271 }
272 } else {
273 os << " <not yet available>";
274 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 }
276 }
277}
278
279void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700280 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 // Sanity check that the number of bits set in the reference offset bitmap
282 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700283 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700284 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285 count += c->NumReferenceInstanceFieldsDuringLinking();
286 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700287 // +1 for the Class in Object.
288 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100290 // Not called within a transaction.
291 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700292 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293}
294
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
296 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700297 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
298 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 ++i;
300 }
301 if (descriptor1.find('/', i) != StringPiece::npos ||
302 descriptor2.find('/', i) != StringPiece::npos) {
303 return false;
304 } else {
305 return true;
306 }
307}
308
Ian Rogersef7d42f2014-01-06 12:55:46 -0800309bool Class::IsInSamePackage(Class* that) {
310 Class* klass1 = this;
311 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 if (klass1 == klass2) {
313 return true;
314 }
315 // Class loaders must match.
316 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
317 return false;
318 }
319 // Arrays are in the same package when their element classes are.
320 while (klass1->IsArrayClass()) {
321 klass1 = klass1->GetComponentType();
322 }
323 while (klass2->IsArrayClass()) {
324 klass2 = klass2->GetComponentType();
325 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700326 // trivial check again for array types
327 if (klass1 == klass2) {
328 return true;
329 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700331 std::string temp1, temp2;
332 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333}
334
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335bool Class::IsStringClass() const {
336 return this == String::GetJavaLangString();
337}
338
Ian Rogersef7d42f2014-01-06 12:55:46 -0800339bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
341}
342
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100344 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700345 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700347 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349}
350
Brian Carlstrom004644f2014-06-18 08:34:01 -0700351ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700353 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700354 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 return method;
356 }
357
358 int32_t iftable_count = GetIfTableCount();
359 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700360 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 method = iftable->GetInterface(i)->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 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700366 return nullptr;
367}
368
369ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
370 // Check the current class before checking the interfaces.
371 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
372 if (method != nullptr) {
373 return method;
374 }
375
376 int32_t iftable_count = GetIfTableCount();
377 IfTable* iftable = GetIfTable();
378 for (int32_t i = 0; i < iftable_count; ++i) {
379 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
380 if (method != nullptr) {
381 return method;
382 }
383 }
384 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385}
386
Ian Rogersef7d42f2014-01-06 12:55:46 -0800387ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700389 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700390 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 return method;
392 }
393
394 int32_t iftable_count = GetIfTableCount();
395 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700396 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 method = iftable->GetInterface(i)->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 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700402 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403}
404
Ian Rogersef7d42f2014-01-06 12:55:46 -0800405ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700407 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700408 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700409 return method;
410 }
411 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700412 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700413}
414
Ian Rogersef7d42f2014-01-06 12:55:46 -0800415ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700416 for (size_t i = 0; i < NumDirectMethods(); ++i) {
417 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700418 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419 return method;
420 }
421 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700422 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423}
424
Ian Rogersef7d42f2014-01-06 12:55:46 -0800425ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 if (GetDexCache() == dex_cache) {
427 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700428 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 if (method->GetDexMethodIndex() == dex_method_idx) {
430 return method;
431 }
432 }
433 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700434 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435}
436
Ian Rogersef7d42f2014-01-06 12:55:46 -0800437ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700438 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700439 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700440 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 return method;
442 }
443 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700444 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445}
446
Ian Rogersef7d42f2014-01-06 12:55:46 -0800447ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700448 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700449 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700450 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700451 return method;
452 }
453 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700455}
456
Ian Rogersef7d42f2014-01-06 12:55:46 -0800457ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700459 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700460 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 return method;
462 }
463 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465}
466
Ian Rogersef7d42f2014-01-06 12:55:46 -0800467ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700468 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
469 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700470 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700471 return method;
472 }
473 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700474 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700475}
476
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700477ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700479 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700480 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 return method;
482 }
483 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700484 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485}
486
Ian Rogersef7d42f2014-01-06 12:55:46 -0800487ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 if (GetDexCache() == dex_cache) {
489 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700490 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstromf322c4c2014-10-31 00:01:54 -0700491 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe2b0fa5b2014-10-31 18:12:30 -0700492 // A miranda method may have a different DexCache and is always created by linking,
493 // never *declared* in the class.
494 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 return method;
496 }
497 }
498 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700499 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800500}
501
Ian Rogersef7d42f2014-01-06 12:55:46 -0800502ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
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(name, signature);
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::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700514 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700515 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700516 return method;
517 }
518 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700519 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700520}
521
Ian Rogersef7d42f2014-01-06 12:55:46 -0800522ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700523 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700524 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700525 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800526 return method;
527 }
528 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700529 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800530}
531
Ian Rogersef7d42f2014-01-06 12:55:46 -0800532ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700533 for (size_t i = 0; i < NumDirectMethods(); ++i) {
534 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700535 if (method->IsClassInitializer()) {
536 DCHECK_STREQ(method->GetName(), "<clinit>");
537 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700538 return method;
539 }
540 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700541 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700542}
543
Brian Carlstromea46f952013-07-30 01:26:50 -0700544ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800545 // Is the field in this class?
546 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700548 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700549 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550 return f;
551 }
552 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700553 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554}
555
Brian Carlstromea46f952013-07-30 01:26:50 -0700556ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 if (GetDexCache() == dex_cache) {
558 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700559 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560 if (f->GetDexFieldIndex() == dex_field_idx) {
561 return f;
562 }
563 }
564 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700565 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566}
567
Brian Carlstromea46f952013-07-30 01:26:50 -0700568ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 // Is the field in this class, or any of its superclasses?
570 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700571 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700572 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700573 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 return f;
575 }
576 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700577 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578}
579
Brian Carlstromea46f952013-07-30 01:26:50 -0700580ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581 // Is the field in this class, or any of its superclasses?
582 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700583 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700584 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700585 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 return f;
587 }
588 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700589 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800590}
591
Brian Carlstromea46f952013-07-30 01:26:50 -0700592ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700593 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700595 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700596 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597 return f;
598 }
599 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700600 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601}
602
Brian Carlstromea46f952013-07-30 01:26:50 -0700603ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604 if (dex_cache == GetDexCache()) {
605 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700606 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800607 if (f->GetDexFieldIndex() == dex_field_idx) {
608 return f;
609 }
610 }
611 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700612 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800613}
614
Mathieu Chartierf8322842014-05-16 10:59:25 -0700615ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
616 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 // Is the field in this class (or its interfaces), or any of its
618 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700619 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700621 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700622 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800623 return f;
624 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700625 // Wrap k incase it moves during GetDirectInterface.
626 StackHandleScope<1> hs(self);
627 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700629 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800630 StackHandleScope<1> hs2(self);
631 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700632 f = FindStaticField(self, interface, name, type);
633 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800634 return f;
635 }
636 }
637 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700638 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639}
640
Mathieu Chartierf8322842014-05-16 10:59:25 -0700641ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
642 uint32_t dex_field_idx) {
643 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700645 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700646 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647 return f;
648 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700649 // Wrap k incase it moves during GetDirectInterface.
650 StackHandleScope<1> hs(self);
651 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700653 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800654 StackHandleScope<1> hs2(self);
655 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700656 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
657 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800658 return f;
659 }
660 }
661 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700662 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800663}
664
Mathieu Chartierf8322842014-05-16 10:59:25 -0700665ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
666 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800667 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700668 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800669 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700670 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700671 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800672 return f;
673 }
674 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700675 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800676 return f;
677 }
678 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700679 StackHandleScope<1> hs(self);
680 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
681 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800682 StackHandleScope<1> hs2(self);
683 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700684 f = interface->FindStaticField(self, interface, name, type);
685 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800686 return f;
687 }
688 }
689 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700690 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800691}
692
Brian Carlstromea46f952013-07-30 01:26:50 -0700693static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200694 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700695 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200696 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700697 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700698 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700699 if (!method->IsNative() && !method->IsAbstract()) {
700 method->SetPreverified();
701 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200702 }
703 }
704}
705
706void Class::SetPreverifiedFlagOnAllMethods() {
707 DCHECK(IsVerified());
708 SetPreverifiedFlagOnMethods(GetDirectMethods());
709 SetPreverifiedFlagOnMethods(GetVirtualMethods());
710}
711
Ian Rogers1ff3c982014-08-12 02:30:58 -0700712const char* Class::GetDescriptor(std::string* storage) {
713 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700714 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700715 } else if (IsArrayClass()) {
716 return GetArrayDescriptor(storage);
717 } else if (IsProxyClass()) {
718 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
719 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700720 } 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
Ian Rogers1ff3c982014-08-12 02:30:58 -0700727const char* Class::GetArrayDescriptor(std::string* storage) {
728 std::string temp;
729 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
730 *storage = "[";
731 *storage += elem_desc;
732 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700733}
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
Mathieu Chartierf8322842014-05-16 10:59:25 -0700743uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
744 DCHECK(!IsPrimitive());
745 DCHECK(!IsArrayClass());
746 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
747}
748
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700749mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700750 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700751 DCHECK(klass.Get() != nullptr);
752 DCHECK(!klass->IsPrimitive());
753 if (klass->IsArrayClass()) {
754 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
755 if (idx == 0) {
756 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
757 } else {
758 DCHECK_EQ(1U, idx);
759 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
760 }
761 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700762 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700763 DCHECK(interfaces != nullptr);
764 return interfaces->Get(idx);
765 } else {
766 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
767 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
768 if (interface == nullptr) {
769 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
770 klass.Get());
771 CHECK(interface != nullptr || self->IsExceptionPending());
772 }
773 return interface;
774 }
775}
776
777const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700778 const DexFile& dex_file = GetDexFile();
779 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200780 if (dex_class_def == nullptr) {
781 // Generated classes have no class def.
782 return nullptr;
783 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700784 return dex_file.GetSourceFile(*dex_class_def);
785}
786
787std::string Class::GetLocation() {
788 mirror::DexCache* dex_cache = GetDexCache();
789 if (dex_cache != nullptr && !IsProxyClass()) {
790 return dex_cache->GetLocation()->ToModifiedUtf8();
791 }
792 // Arrays and proxies are generated and have no corresponding dex file location.
793 return "generated class";
794}
795
796const DexFile::TypeList* Class::GetInterfaceTypeList() {
797 const DexFile::ClassDef* class_def = GetClassDef();
798 if (class_def == nullptr) {
799 return nullptr;
800 }
801 return GetDexFile().GetInterfacesList(*class_def);
802}
803
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700804void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
805 for (uint32_t i = 0; i < kImtSize; i++) {
806 // Replace null with conflict.
807 mirror::Object* obj = imt_handle_scope->GetReference(i);
808 DCHECK(obj != nullptr);
809 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700810 }
811
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700812 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700813 CHECK(table != nullptr) << PrettyClass(this);
814 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700815 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700816 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700817 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700818
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700819 // Keep java.lang.Object class's vtable around for since it's easier
820 // to be reused by array classes during their linking.
821 if (!IsObjectClass()) {
822 SetVTable(nullptr);
823 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700824}
825
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700826// The pre-fence visitor for Class::CopyOf().
827class CopyClassVisitor {
828 public:
829 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700830 size_t new_length, size_t copy_bytes,
831 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700832 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700833 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700834 }
835
836 void operator()(Object* obj, size_t usable_size) const
837 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
838 UNUSED(usable_size);
839 mirror::Class* new_class_obj = obj->AsClass();
840 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_);
841 new_class_obj->SetStatus(Class::kStatusResolving, self_);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700842 new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700843 new_class_obj->SetClassSize(new_length_);
844 }
845
846 private:
847 Thread* const self_;
848 Handle<mirror::Class>* const orig_;
849 const size_t new_length_;
850 const size_t copy_bytes_;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700851 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700852 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
853};
854
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700855Class* Class::CopyOf(Thread* self, int32_t new_length,
856 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700857 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
858 // We may get copied by a compacting GC.
859 StackHandleScope<1> hs(self);
860 Handle<mirror::Class> h_this(hs.NewHandle(this));
861 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700862 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
863 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700864 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700865 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700866 kMovingClasses
867 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
868 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700869 if (UNLIKELY(new_class == nullptr)) {
870 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700871 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700872 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700873 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700874}
875
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800876} // namespace mirror
877} // namespace art