blob: bd3bfbf9fed45112c9620e10c629e6c4850287ba [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) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 if (!java_lang_Class_.IsNull()) {
56 java_lang_Class_.VisitRoot(callback, arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057 }
58}
59
Ian Rogers7dfb28c2013-08-22 08:18:36 -070060void Class::SetStatus(Status new_status, Thread* self) {
61 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070062 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
63 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070064 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070065 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
66 new_status != kStatusRetired)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070069 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070070 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
71 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070072 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 << "Attempt to change status of class while not holding its lock: "
74 << PrettyClass(this) << " " << old_status << " -> " << new_status;
75 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 }
Ian Rogers98379392014-02-24 16:53:16 -080077 if (UNLIKELY(new_status == kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070078 CHECK_NE(GetStatus(), kStatusError)
79 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Ian Rogers62d6c772013-02-27 08:32:07 -080081 // Stash current exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 StackHandleScope<3> hs(self);
83 ThrowLocation old_throw_location;
84 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location)));
85 CHECK(old_exception.Get() != nullptr);
86 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis()));
87 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod()));
88 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +020089 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070090 Class* eiie_class;
91 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
92 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070093 bool throw_eiie = (old_exception.Get() == nullptr);
94 if (!throw_eiie) {
95 std::string temp;
96 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
97 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
98 }
99 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700100 // Clear exception to call FindSystemClass.
101 self->ClearException();
102 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
103 self, "Ljava/lang/ExceptionInInitializerError;");
104 CHECK(!self->IsExceptionPending());
105 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700106 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
107 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700108 Class* exception_class = old_exception->GetClass();
109 if (!eiie_class->IsAssignableFrom(exception_class)) {
110 SetVerifyErrorClass(exception_class);
111 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 }
113
Ian Rogers62d6c772013-02-27 08:32:07 -0800114 // Restore exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 old_throw_dex_pc);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 self->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200118 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800120 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100121 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700122 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100123 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700124 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100125 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126
127 if (!class_linker_initialized) {
128 // When the class linker is being initialized its single threaded and by definition there can be
129 // no waiters. During initialization classes may appear temporary but won't be retired as their
130 // size was statically computed.
131 } else {
132 // Classes that are being resolved or initialized need to notify waiters that the class status
133 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
134 if (IsTemp()) {
135 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
136 // so that they can grab the new version of the class from the class linker's table.
137 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this);
138 if (new_status == kStatusRetired || new_status == kStatusError) {
139 NotifyAll(self);
140 }
141 } else {
142 CHECK_NE(new_status, kStatusRetired);
143 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
144 NotifyAll(self);
145 }
146 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700147 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148}
149
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700151 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800152 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153}
154
Ian Rogersef7d42f2014-01-06 12:55:46 -0800155void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700156 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
157 DumpClass(LOG(ERROR), kDumpClassFullDetail);
158 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
159 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100160 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700161 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162}
163
164// Return the class' name. The exact format is bizarre, but it's the specified behavior for
165// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
166// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
167// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700168String* Class::ComputeName(Handle<Class> h_this) {
169 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800170 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 return name;
172 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700173 std::string temp;
174 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800175 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
177 // The descriptor indicates that this is the class for
178 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700179 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 switch (descriptor[0]) {
181 case 'Z': c_name = "boolean"; break;
182 case 'B': c_name = "byte"; break;
183 case 'C': c_name = "char"; break;
184 case 'S': c_name = "short"; break;
185 case 'I': c_name = "int"; break;
186 case 'J': c_name = "long"; break;
187 case 'F': c_name = "float"; break;
188 case 'D': c_name = "double"; break;
189 case 'V': c_name = "void"; break;
190 default:
191 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
192 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800193 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 } else {
195 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
196 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700197 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700199 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 return name;
201}
202
Ian Rogersef7d42f2014-01-06 12:55:46 -0800203void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204 if ((flags & kDumpClassFullDetail) == 0) {
205 os << PrettyClass(this);
206 if ((flags & kDumpClassClassLoader) != 0) {
207 os << ' ' << GetClassLoader();
208 }
209 if ((flags & kDumpClassInitialized) != 0) {
210 os << ' ' << GetStatus();
211 }
212 os << "\n";
213 return;
214 }
215
Mathieu Chartierf8322842014-05-16 10:59:25 -0700216 Thread* self = Thread::Current();
217 StackHandleScope<2> hs(self);
218 Handle<mirror::Class> h_this(hs.NewHandle(this));
219 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
220
Ian Rogers1ff3c982014-08-12 02:30:58 -0700221 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700223 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700225 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 os << StringPrintf(" access=0x%04x.%04x\n",
227 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700228 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
230 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800231 }
232 if (IsArrayClass()) {
233 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
234 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700235 const size_t num_direct_interfaces = NumDirectInterfaces();
236 if (num_direct_interfaces > 0) {
237 os << " interfaces (" << num_direct_interfaces << "):\n";
238 for (size_t i = 0; i < num_direct_interfaces; ++i) {
239 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 const ClassLoader* cl = interface->GetClassLoader();
241 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
242 }
243 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 if (!IsLoaded()) {
245 os << " class not yet loaded";
246 } else {
247 // After this point, this may have moved due to GetDirectInterface.
248 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
249 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
250 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
251 os << StringPrintf(" %2zd: %s\n", i,
252 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800253 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700254 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
255 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
256 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
257 }
258 if (h_this->NumStaticFields() > 0) {
259 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
260 if (h_this->IsResolved() || h_this->IsErroneous()) {
261 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
262 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
263 }
264 } else {
265 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 }
268 if (h_this->NumInstanceFields() > 0) {
269 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
270 if (h_this->IsResolved() || h_this->IsErroneous()) {
271 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
272 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
273 }
274 } else {
275 os << " <not yet available>";
276 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 }
278 }
279}
280
281void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700282 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 // Sanity check that the number of bits set in the reference offset bitmap
284 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700285 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700286 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 count += c->NumReferenceInstanceFieldsDuringLinking();
288 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700289 // +1 for the Class in Object.
290 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100292 // Not called within a transaction.
293 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700294 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295}
296
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
298 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700299 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
300 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 ++i;
302 }
303 if (descriptor1.find('/', i) != StringPiece::npos ||
304 descriptor2.find('/', i) != StringPiece::npos) {
305 return false;
306 } else {
307 return true;
308 }
309}
310
Ian Rogersef7d42f2014-01-06 12:55:46 -0800311bool Class::IsInSamePackage(Class* that) {
312 Class* klass1 = this;
313 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 if (klass1 == klass2) {
315 return true;
316 }
317 // Class loaders must match.
318 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
319 return false;
320 }
321 // Arrays are in the same package when their element classes are.
322 while (klass1->IsArrayClass()) {
323 klass1 = klass1->GetComponentType();
324 }
325 while (klass2->IsArrayClass()) {
326 klass2 = klass2->GetComponentType();
327 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700328 // trivial check again for array types
329 if (klass1 == klass2) {
330 return true;
331 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700333 std::string temp1, temp2;
334 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335}
336
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337bool Class::IsStringClass() const {
338 return this == String::GetJavaLangString();
339}
340
Ian Rogersef7d42f2014-01-06 12:55:46 -0800341bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
343}
344
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700347 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700349 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100350 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351}
352
Brian Carlstrom004644f2014-06-18 08:34:01 -0700353ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700355 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700356 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 return method;
358 }
359
360 int32_t iftable_count = GetIfTableCount();
361 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700362 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700364 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800365 return method;
366 }
367 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700368 return nullptr;
369}
370
371ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
372 // Check the current class before checking the interfaces.
373 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
374 if (method != nullptr) {
375 return method;
376 }
377
378 int32_t iftable_count = GetIfTableCount();
379 IfTable* iftable = GetIfTable();
380 for (int32_t i = 0; i < iftable_count; ++i) {
381 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
382 if (method != nullptr) {
383 return method;
384 }
385 }
386 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800387}
388
Ian Rogersef7d42f2014-01-06 12:55:46 -0800389ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700391 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700392 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 return method;
394 }
395
396 int32_t iftable_count = GetIfTableCount();
397 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700398 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700400 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 return method;
402 }
403 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700404 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405}
406
Ian Rogersef7d42f2014-01-06 12:55:46 -0800407ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700409 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700410 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700411 return method;
412 }
413 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700414 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700415}
416
Ian Rogersef7d42f2014-01-06 12:55:46 -0800417ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700418 for (size_t i = 0; i < NumDirectMethods(); ++i) {
419 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700420 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 return method;
422 }
423 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700424 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425}
426
Ian Rogersef7d42f2014-01-06 12:55:46 -0800427ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428 if (GetDexCache() == dex_cache) {
429 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700430 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431 if (method->GetDexMethodIndex() == dex_method_idx) {
432 return method;
433 }
434 }
435 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700436 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437}
438
Ian Rogersef7d42f2014-01-06 12:55:46 -0800439ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700440 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700441 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 return method;
444 }
445 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447}
448
Ian Rogersef7d42f2014-01-06 12:55:46 -0800449ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700450 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700451 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700453 return method;
454 }
455 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700456 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700457}
458
Ian Rogersef7d42f2014-01-06 12:55:46 -0800459ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700460 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700461 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700462 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800463 return method;
464 }
465 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700466 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467}
468
Ian Rogersef7d42f2014-01-06 12:55:46 -0800469ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700470 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
471 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700472 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700473 return method;
474 }
475 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700476 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700477}
478
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700479ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700481 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700482 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483 return method;
484 }
485 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700486 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487}
488
Ian Rogersef7d42f2014-01-06 12:55:46 -0800489ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 if (GetDexCache() == dex_cache) {
491 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700492 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstromf322c4c2014-10-31 00:01:54 -0700493 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe2b0fa5b2014-10-31 18:12:30 -0700494 // A miranda method may have a different DexCache and is always created by linking,
495 // never *declared* in the class.
496 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800497 return method;
498 }
499 }
500 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700501 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502}
503
Ian Rogersef7d42f2014-01-06 12:55:46 -0800504ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700505 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700506 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700507 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508 return method;
509 }
510 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700511 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800512}
513
Ian Rogersef7d42f2014-01-06 12:55:46 -0800514ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700515 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700516 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700518 return method;
519 }
520 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700522}
523
Ian Rogersef7d42f2014-01-06 12:55:46 -0800524ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700525 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700526 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700527 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800528 return method;
529 }
530 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700531 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800532}
533
Ian Rogersef7d42f2014-01-06 12:55:46 -0800534ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700535 for (size_t i = 0; i < NumDirectMethods(); ++i) {
536 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700537 if (method->IsClassInitializer()) {
538 DCHECK_STREQ(method->GetName(), "<clinit>");
539 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700540 return method;
541 }
542 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700543 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700544}
545
Brian Carlstromea46f952013-07-30 01:26:50 -0700546ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 // Is the field in this class?
548 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700550 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700551 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 return f;
553 }
554 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700555 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800556}
557
Brian Carlstromea46f952013-07-30 01:26:50 -0700558ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800559 if (GetDexCache() == dex_cache) {
560 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700561 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 if (f->GetDexFieldIndex() == dex_field_idx) {
563 return f;
564 }
565 }
566 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700567 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568}
569
Brian Carlstromea46f952013-07-30 01:26:50 -0700570ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 // Is the field in this class, or any of its superclasses?
572 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700573 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700574 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700575 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576 return f;
577 }
578 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700579 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800580}
581
Brian Carlstromea46f952013-07-30 01:26:50 -0700582ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583 // Is the field in this class, or any of its superclasses?
584 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700585 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700587 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588 return f;
589 }
590 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700591 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800592}
593
Brian Carlstromea46f952013-07-30 01:26:50 -0700594ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700595 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700597 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700598 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 return f;
600 }
601 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700602 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800603}
604
Brian Carlstromea46f952013-07-30 01:26:50 -0700605ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800606 if (dex_cache == GetDexCache()) {
607 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700608 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609 if (f->GetDexFieldIndex() == dex_field_idx) {
610 return f;
611 }
612 }
613 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700614 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615}
616
Mathieu Chartierf8322842014-05-16 10:59:25 -0700617ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
618 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800619 // Is the field in this class (or its interfaces), or any of its
620 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700621 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800622 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700623 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700624 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 return f;
626 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700627 // Wrap k incase it moves during GetDirectInterface.
628 StackHandleScope<1> hs(self);
629 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800630 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700631 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800632 StackHandleScope<1> hs2(self);
633 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700634 f = FindStaticField(self, interface, name, type);
635 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636 return f;
637 }
638 }
639 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700640 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641}
642
Mathieu Chartierf8322842014-05-16 10:59:25 -0700643ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
644 uint32_t dex_field_idx) {
645 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800646 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700647 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700648 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800649 return f;
650 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700651 // Wrap k incase it moves during GetDirectInterface.
652 StackHandleScope<1> hs(self);
653 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800654 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700655 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800656 StackHandleScope<1> hs2(self);
657 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700658 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
659 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660 return f;
661 }
662 }
663 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700664 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800665}
666
Mathieu Chartierf8322842014-05-16 10:59:25 -0700667ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
668 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800669 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700670 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700673 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674 return f;
675 }
676 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700677 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800678 return f;
679 }
680 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700681 StackHandleScope<1> hs(self);
682 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
683 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800684 StackHandleScope<1> hs2(self);
685 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700686 f = interface->FindStaticField(self, interface, name, type);
687 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800688 return f;
689 }
690 }
691 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700692 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800693}
694
Brian Carlstromea46f952013-07-30 01:26:50 -0700695static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200696 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700697 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200698 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700699 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700700 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700701 if (!method->IsNative() && !method->IsAbstract()) {
702 method->SetPreverified();
703 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200704 }
705 }
706}
707
708void Class::SetPreverifiedFlagOnAllMethods() {
709 DCHECK(IsVerified());
710 SetPreverifiedFlagOnMethods(GetDirectMethods());
711 SetPreverifiedFlagOnMethods(GetVirtualMethods());
712}
713
Ian Rogers1ff3c982014-08-12 02:30:58 -0700714const char* Class::GetDescriptor(std::string* storage) {
715 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700716 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700717 } else if (IsArrayClass()) {
718 return GetArrayDescriptor(storage);
719 } else if (IsProxyClass()) {
720 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
721 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700722 } else {
723 const DexFile& dex_file = GetDexFile();
724 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
725 return dex_file.GetTypeDescriptor(type_id);
726 }
727}
728
Ian Rogers1ff3c982014-08-12 02:30:58 -0700729const char* Class::GetArrayDescriptor(std::string* storage) {
730 std::string temp;
731 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
732 *storage = "[";
733 *storage += elem_desc;
734 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700735}
736
737const DexFile::ClassDef* Class::GetClassDef() {
738 uint16_t class_def_idx = GetDexClassDefIndex();
739 if (class_def_idx == DexFile::kDexNoIndex16) {
740 return nullptr;
741 }
742 return &GetDexFile().GetClassDef(class_def_idx);
743}
744
Mathieu Chartierf8322842014-05-16 10:59:25 -0700745uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
746 DCHECK(!IsPrimitive());
747 DCHECK(!IsArrayClass());
748 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
749}
750
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700751mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700752 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700753 DCHECK(klass.Get() != nullptr);
754 DCHECK(!klass->IsPrimitive());
755 if (klass->IsArrayClass()) {
756 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
757 if (idx == 0) {
758 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
759 } else {
760 DCHECK_EQ(1U, idx);
761 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
762 }
763 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700764 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700765 DCHECK(interfaces != nullptr);
766 return interfaces->Get(idx);
767 } else {
768 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
769 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
770 if (interface == nullptr) {
771 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
772 klass.Get());
773 CHECK(interface != nullptr || self->IsExceptionPending());
774 }
775 return interface;
776 }
777}
778
779const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700780 const DexFile& dex_file = GetDexFile();
781 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200782 if (dex_class_def == nullptr) {
783 // Generated classes have no class def.
784 return nullptr;
785 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700786 return dex_file.GetSourceFile(*dex_class_def);
787}
788
789std::string Class::GetLocation() {
790 mirror::DexCache* dex_cache = GetDexCache();
791 if (dex_cache != nullptr && !IsProxyClass()) {
792 return dex_cache->GetLocation()->ToModifiedUtf8();
793 }
794 // Arrays and proxies are generated and have no corresponding dex file location.
795 return "generated class";
796}
797
798const DexFile::TypeList* Class::GetInterfaceTypeList() {
799 const DexFile::ClassDef* class_def = GetClassDef();
800 if (class_def == nullptr) {
801 return nullptr;
802 }
803 return GetDexFile().GetInterfacesList(*class_def);
804}
805
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700806void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
807 for (uint32_t i = 0; i < kImtSize; i++) {
808 // Replace null with conflict.
809 mirror::Object* obj = imt_handle_scope->GetReference(i);
810 DCHECK(obj != nullptr);
811 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700812 }
813
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700814 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700815 CHECK(table != nullptr) << PrettyClass(this);
816 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700817 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700818 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700819 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700820
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700821 // Keep java.lang.Object class's vtable around for since it's easier
822 // to be reused by array classes during their linking.
823 if (!IsObjectClass()) {
824 SetVTable(nullptr);
825 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700826}
827
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700828// The pre-fence visitor for Class::CopyOf().
829class CopyClassVisitor {
830 public:
831 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700832 size_t new_length, size_t copy_bytes,
833 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700834 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700835 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700836 }
837
838 void operator()(Object* obj, size_t usable_size) const
839 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
840 UNUSED(usable_size);
841 mirror::Class* new_class_obj = obj->AsClass();
842 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_);
843 new_class_obj->SetStatus(Class::kStatusResolving, self_);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700844 new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700845 new_class_obj->SetClassSize(new_length_);
846 }
847
848 private:
849 Thread* const self_;
850 Handle<mirror::Class>* const orig_;
851 const size_t new_length_;
852 const size_t copy_bytes_;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700853 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700854 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
855};
856
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700857Class* Class::CopyOf(Thread* self, int32_t new_length,
858 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700859 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
860 // We may get copied by a compacting GC.
861 StackHandleScope<1> hs(self);
862 Handle<mirror::Class> h_this(hs.NewHandle(this));
863 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700864 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
865 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700866 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700867 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700868 kMovingClasses
869 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
870 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700871 if (UNLIKELY(new_class == nullptr)) {
872 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700873 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700874 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700875 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700876}
877
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800878} // namespace mirror
879} // namespace art