blob: f9791fa944c1c3710554f6a34dbcdd0bf421f399 [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 Chartier12f74232015-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 Rogerscb6b0f32014-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 Rogerscb6b0f32014-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 }
Ian Rogers03dbc042014-06-02 14:24:56 -0700118 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_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 Chartierea1c3d72014-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 Rogerscb6b0f32014-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 Rogerscb6b0f32014-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 Rogerscb6b0f32014-08-12 02:30:58 -0700219 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogerscb6b0f32014-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) {
280 if (new_reference_offsets != CLASS_WALK_SUPER) {
281 // Sanity check that the number of bits set in the reference offset bitmap
282 // agrees with the number of references
283 size_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 }
Vladimir Marko81949632014-05-02 11:53:22 +0100287 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100289 // Not called within a transaction.
290 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700291 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800292}
293
294void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
295 if (new_reference_offsets != CLASS_WALK_SUPER) {
296 // Sanity check that the number of bits set in the reference offset bitmap
297 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100298 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 NumReferenceStaticFieldsDuringLinking());
300 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100301 // Not called within a transaction.
302 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700303 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304}
305
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800306bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
307 size_t i = 0;
308 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
309 ++i;
310 }
311 if (descriptor1.find('/', i) != StringPiece::npos ||
312 descriptor2.find('/', i) != StringPiece::npos) {
313 return false;
314 } else {
315 return true;
316 }
317}
318
Ian Rogersef7d42f2014-01-06 12:55:46 -0800319bool Class::IsInSamePackage(Class* that) {
320 Class* klass1 = this;
321 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 if (klass1 == klass2) {
323 return true;
324 }
325 // Class loaders must match.
326 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
327 return false;
328 }
329 // Arrays are in the same package when their element classes are.
330 while (klass1->IsArrayClass()) {
331 klass1 = klass1->GetComponentType();
332 }
333 while (klass2->IsArrayClass()) {
334 klass2 = klass2->GetComponentType();
335 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700336 // trivial check again for array types
337 if (klass1 == klass2) {
338 return true;
339 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 // Compare the package part of the descriptor string.
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700341 std::string temp1, temp2;
342 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343}
344
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800345bool Class::IsStringClass() const {
346 return this == String::GetJavaLangString();
347}
348
Ian Rogersef7d42f2014-01-06 12:55:46 -0800349bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
351}
352
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100354 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700355 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100356 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700357 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100358 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359}
360
Brian Carlstrom004644f2014-06-18 08:34:01 -0700361ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700363 ArtMethod* method = 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
368 int32_t iftable_count = GetIfTableCount();
369 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700370 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800371 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700372 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373 return method;
374 }
375 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700376 return nullptr;
377}
378
379ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
380 // Check the current class before checking the interfaces.
381 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
382 if (method != nullptr) {
383 return method;
384 }
385
386 int32_t iftable_count = GetIfTableCount();
387 IfTable* iftable = GetIfTable();
388 for (int32_t i = 0; i < iftable_count; ++i) {
389 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
390 if (method != nullptr) {
391 return method;
392 }
393 }
394 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395}
396
Ian Rogersef7d42f2014-01-06 12:55:46 -0800397ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700399 ArtMethod* method = 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
404 int32_t iftable_count = GetIfTableCount();
405 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700406 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700408 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800409 return method;
410 }
411 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700412 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413}
414
Ian Rogersef7d42f2014-01-06 12:55:46 -0800415ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700417 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700418 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700419 return method;
420 }
421 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700422 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700423}
424
Ian Rogersef7d42f2014-01-06 12:55:46 -0800425ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700426 for (size_t i = 0; i < NumDirectMethods(); ++i) {
427 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700428 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 return method;
430 }
431 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700432 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433}
434
Ian Rogersef7d42f2014-01-06 12:55:46 -0800435ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436 if (GetDexCache() == dex_cache) {
437 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700438 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439 if (method->GetDexMethodIndex() == dex_method_idx) {
440 return method;
441 }
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 StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700448 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700449 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700450 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800451 return method;
452 }
453 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800455}
456
Ian Rogersef7d42f2014-01-06 12:55:46 -0800457ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700459 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700460 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700461 return method;
462 }
463 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700465}
466
Ian Rogersef7d42f2014-01-06 12:55:46 -0800467ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700469 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700470 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800471 return method;
472 }
473 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700474 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800475}
476
Ian Rogersef7d42f2014-01-06 12:55:46 -0800477ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700478 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
479 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700480 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700481 return method;
482 }
483 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700484 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700485}
486
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700487ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700489 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700490 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491 return method;
492 }
493 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700494 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495}
496
Ian Rogersef7d42f2014-01-06 12:55:46 -0800497ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498 if (GetDexCache() == dex_cache) {
499 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700500 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstrom9a783d62014-10-31 00:01:54 -0700501 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe1d0611c2014-10-31 18:12:30 -0700502 // A miranda method may have a different DexCache and is always created by linking,
503 // never *declared* in the class.
504 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505 return method;
506 }
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 StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700514 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700515 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516 return method;
517 }
518 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700519 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800520}
521
Ian Rogersef7d42f2014-01-06 12:55:46 -0800522ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700523 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700524 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700525 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700526 return method;
527 }
528 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700529 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700530}
531
Ian Rogersef7d42f2014-01-06 12:55:46 -0800532ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700533 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700534 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700535 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536 return method;
537 }
538 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700539 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800540}
541
Ian Rogersef7d42f2014-01-06 12:55:46 -0800542ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700543 for (size_t i = 0; i < NumDirectMethods(); ++i) {
544 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700545 if (method->IsClassInitializer()) {
546 DCHECK_STREQ(method->GetName(), "<clinit>");
547 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700548 return method;
549 }
550 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700551 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700552}
553
Brian Carlstromea46f952013-07-30 01:26:50 -0700554ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 // Is the field in this class?
556 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700558 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700559 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800560 return f;
561 }
562 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700563 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564}
565
Brian Carlstromea46f952013-07-30 01:26:50 -0700566ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567 if (GetDexCache() == dex_cache) {
568 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700569 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570 if (f->GetDexFieldIndex() == dex_field_idx) {
571 return f;
572 }
573 }
574 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700575 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576}
577
Brian Carlstromea46f952013-07-30 01:26:50 -0700578ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 // Is the field in this class, or any of its superclasses?
580 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700581 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700582 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700583 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584 return f;
585 }
586 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700587 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588}
589
Brian Carlstromea46f952013-07-30 01:26:50 -0700590ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 // Is the field in this class, or any of its superclasses?
592 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700593 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700594 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700595 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596 return f;
597 }
598 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700599 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600}
601
Brian Carlstromea46f952013-07-30 01:26:50 -0700602ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700603 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700605 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700606 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800607 return f;
608 }
609 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700610 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611}
612
Brian Carlstromea46f952013-07-30 01:26:50 -0700613ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 if (dex_cache == GetDexCache()) {
615 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700616 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 if (f->GetDexFieldIndex() == dex_field_idx) {
618 return f;
619 }
620 }
621 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700622 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800623}
624
Mathieu Chartierf8322842014-05-16 10:59:25 -0700625ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
626 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 // Is the field in this class (or its interfaces), or any of its
628 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700629 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800630 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700631 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700632 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800633 return f;
634 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700635 // Wrap k incase it moves during GetDirectInterface.
636 StackHandleScope<1> hs(self);
637 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800638 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700639 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
640 StackHandleScope<1> hs(self);
641 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
642 f = FindStaticField(self, interface, name, type);
643 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 return f;
645 }
646 }
647 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700648 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800649}
650
Mathieu Chartierf8322842014-05-16 10:59:25 -0700651ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
652 uint32_t dex_field_idx) {
653 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800654 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700655 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700656 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657 return f;
658 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700659 // Wrap k incase it moves during GetDirectInterface.
660 StackHandleScope<1> hs(self);
661 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800662 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700663 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
664 StackHandleScope<1> hs(self);
665 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
666 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
667 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668 return f;
669 }
670 }
671 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700672 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800673}
674
Mathieu Chartierf8322842014-05-16 10:59:25 -0700675ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
676 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700678 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800679 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700680 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700681 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800682 return f;
683 }
684 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700685 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800686 return f;
687 }
688 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700689 StackHandleScope<1> hs(self);
690 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
691 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
692 StackHandleScope<1> hs(self);
693 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
694 f = interface->FindStaticField(self, interface, name, type);
695 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800696 return f;
697 }
698 }
699 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700700 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800701}
702
Brian Carlstromea46f952013-07-30 01:26:50 -0700703static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200704 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700705 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200706 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700707 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700708 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700709 if (!method->IsNative() && !method->IsAbstract()) {
710 method->SetPreverified();
711 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200712 }
713 }
714}
715
716void Class::SetPreverifiedFlagOnAllMethods() {
717 DCHECK(IsVerified());
718 SetPreverifiedFlagOnMethods(GetDirectMethods());
719 SetPreverifiedFlagOnMethods(GetVirtualMethods());
720}
721
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700722const char* Class::GetDescriptor(std::string* storage) {
723 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700724 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700725 } else if (IsArrayClass()) {
726 return GetArrayDescriptor(storage);
727 } else if (IsProxyClass()) {
728 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
729 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700730 } else {
731 const DexFile& dex_file = GetDexFile();
732 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
733 return dex_file.GetTypeDescriptor(type_id);
734 }
735}
736
Ian Rogerscb6b0f32014-08-12 02:30:58 -0700737const char* Class::GetArrayDescriptor(std::string* storage) {
738 std::string temp;
739 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
740 *storage = "[";
741 *storage += elem_desc;
742 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700743}
744
745const DexFile::ClassDef* Class::GetClassDef() {
746 uint16_t class_def_idx = GetDexClassDefIndex();
747 if (class_def_idx == DexFile::kDexNoIndex16) {
748 return nullptr;
749 }
750 return &GetDexFile().GetClassDef(class_def_idx);
751}
752
Mathieu Chartierf8322842014-05-16 10:59:25 -0700753uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
754 DCHECK(!IsPrimitive());
755 DCHECK(!IsArrayClass());
756 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
757}
758
759mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) {
760 DCHECK(klass.Get() != nullptr);
761 DCHECK(!klass->IsPrimitive());
762 if (klass->IsArrayClass()) {
763 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
764 if (idx == 0) {
765 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
766 } else {
767 DCHECK_EQ(1U, idx);
768 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
769 }
770 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700771 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700772 DCHECK(interfaces != nullptr);
773 return interfaces->Get(idx);
774 } else {
775 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
776 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
777 if (interface == nullptr) {
778 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
779 klass.Get());
780 CHECK(interface != nullptr || self->IsExceptionPending());
781 }
782 return interface;
783 }
784}
785
786const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700787 const DexFile& dex_file = GetDexFile();
788 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200789 if (dex_class_def == nullptr) {
790 // Generated classes have no class def.
791 return nullptr;
792 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700793 return dex_file.GetSourceFile(*dex_class_def);
794}
795
796std::string Class::GetLocation() {
797 mirror::DexCache* dex_cache = GetDexCache();
798 if (dex_cache != nullptr && !IsProxyClass()) {
799 return dex_cache->GetLocation()->ToModifiedUtf8();
800 }
801 // Arrays and proxies are generated and have no corresponding dex file location.
802 return "generated class";
803}
804
805const DexFile::TypeList* Class::GetInterfaceTypeList() {
806 const DexFile::ClassDef* class_def = GetClassDef();
807 if (class_def == nullptr) {
808 return nullptr;
809 }
810 return GetDexFile().GetInterfacesList(*class_def);
811}
812
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700813void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
814 for (uint32_t i = 0; i < kImtSize; i++) {
815 // Replace null with conflict.
816 mirror::Object* obj = imt_handle_scope->GetReference(i);
817 DCHECK(obj != nullptr);
818 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700819 }
820
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700821 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700822 CHECK(table != nullptr) << PrettyClass(this);
823 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700824 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700825 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700826 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700827
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700828 // Keep java.lang.Object class's vtable around for since it's easier
829 // to be reused by array classes during their linking.
830 if (!IsObjectClass()) {
831 SetVTable(nullptr);
832 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700833}
834
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700835// The pre-fence visitor for Class::CopyOf().
836class CopyClassVisitor {
837 public:
838 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700839 size_t new_length, size_t copy_bytes,
840 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700841 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700842 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700843 }
844
845 void operator()(Object* obj, size_t usable_size) const
846 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
847 UNUSED(usable_size);
848 mirror::Class* new_class_obj = obj->AsClass();
849 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_);
850 new_class_obj->SetStatus(Class::kStatusResolving, self_);
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700851 new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700852 new_class_obj->SetClassSize(new_length_);
853 }
854
855 private:
856 Thread* const self_;
857 Handle<mirror::Class>* const orig_;
858 const size_t new_length_;
859 const size_t copy_bytes_;
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700860 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700861 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
862};
863
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700864Class* Class::CopyOf(Thread* self, int32_t new_length,
865 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700866 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
867 // We may get copied by a compacting GC.
868 StackHandleScope<1> hs(self);
869 Handle<mirror::Class> h_this(hs.NewHandle(this));
870 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700871 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
872 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700873 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700874 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700875 kMovingClasses
876 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
877 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700878 if (UNLIKELY(new_class == nullptr)) {
879 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier1fb463e2014-10-23 16:48:06 -0700880 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700881 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700882 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700883}
884
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800885} // namespace mirror
886} // namespace art