blob: 760d54cfa149ae53d26176980c050768ca0070db [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 }
Ian Rogers03dbc042014-06-02 14:24:56 -0700120 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_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);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152}
153
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700155 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
156 DumpClass(LOG(ERROR), kDumpClassFullDetail);
157 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
158 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700160 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161}
162
163// Return the class' name. The exact format is bizarre, but it's the specified behavior for
164// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
165// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
166// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700167String* Class::ComputeName(Handle<Class> h_this) {
168 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800169 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 return name;
171 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700172 std::string temp;
173 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800174 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
176 // The descriptor indicates that this is the class for
177 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700178 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 switch (descriptor[0]) {
180 case 'Z': c_name = "boolean"; break;
181 case 'B': c_name = "byte"; break;
182 case 'C': c_name = "char"; break;
183 case 'S': c_name = "short"; break;
184 case 'I': c_name = "int"; break;
185 case 'J': c_name = "long"; break;
186 case 'F': c_name = "float"; break;
187 case 'D': c_name = "double"; break;
188 case 'V': c_name = "void"; break;
189 default:
190 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
191 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800192 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 } else {
194 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
195 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700196 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700198 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 return name;
200}
201
Ian Rogersef7d42f2014-01-06 12:55:46 -0800202void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 if ((flags & kDumpClassFullDetail) == 0) {
204 os << PrettyClass(this);
205 if ((flags & kDumpClassClassLoader) != 0) {
206 os << ' ' << GetClassLoader();
207 }
208 if ((flags & kDumpClassInitialized) != 0) {
209 os << ' ' << GetStatus();
210 }
211 os << "\n";
212 return;
213 }
214
Mathieu Chartierf8322842014-05-16 10:59:25 -0700215 Thread* self = Thread::Current();
216 StackHandleScope<2> hs(self);
217 Handle<mirror::Class> h_this(hs.NewHandle(this));
218 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
219
Ian Rogers1ff3c982014-08-12 02:30:58 -0700220 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700222 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700224 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 os << StringPrintf(" access=0x%04x.%04x\n",
226 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700227 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700228 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
229 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 }
231 if (IsArrayClass()) {
232 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
233 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700234 const size_t num_direct_interfaces = NumDirectInterfaces();
235 if (num_direct_interfaces > 0) {
236 os << " interfaces (" << num_direct_interfaces << "):\n";
237 for (size_t i = 0; i < num_direct_interfaces; ++i) {
238 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 const ClassLoader* cl = interface->GetClassLoader();
240 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
241 }
242 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700243 if (!IsLoaded()) {
244 os << " class not yet loaded";
245 } else {
246 // After this point, this may have moved due to GetDirectInterface.
247 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
248 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
249 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
250 os << StringPrintf(" %2zd: %s\n", i,
251 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
254 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
255 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
256 }
257 if (h_this->NumStaticFields() > 0) {
258 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
259 if (h_this->IsResolved() || h_this->IsErroneous()) {
260 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
261 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
262 }
263 } else {
264 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700266 }
267 if (h_this->NumInstanceFields() > 0) {
268 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
269 if (h_this->IsResolved() || h_this->IsErroneous()) {
270 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
271 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
272 }
273 } else {
274 os << " <not yet available>";
275 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 }
277 }
278}
279
280void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
281 if (new_reference_offsets != CLASS_WALK_SUPER) {
282 // Sanity check that the number of bits set in the reference offset bitmap
283 // agrees with the number of references
284 size_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700285 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 count += c->NumReferenceInstanceFieldsDuringLinking();
287 }
Vladimir Marko81949632014-05-02 11:53:22 +0100288 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100290 // Not called within a transaction.
291 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700292 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293}
294
295void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) {
296 if (new_reference_offsets != CLASS_WALK_SUPER) {
297 // Sanity check that the number of bits set in the reference offset bitmap
298 // agrees with the number of references
Vladimir Marko81949632014-05-02 11:53:22 +0100299 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 NumReferenceStaticFieldsDuringLinking());
301 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100302 // Not called within a transaction.
303 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700304 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800305}
306
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
308 size_t i = 0;
309 while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) {
310 ++i;
311 }
312 if (descriptor1.find('/', i) != StringPiece::npos ||
313 descriptor2.find('/', i) != StringPiece::npos) {
314 return false;
315 } else {
316 return true;
317 }
318}
319
Ian Rogersef7d42f2014-01-06 12:55:46 -0800320bool Class::IsInSamePackage(Class* that) {
321 Class* klass1 = this;
322 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323 if (klass1 == klass2) {
324 return true;
325 }
326 // Class loaders must match.
327 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
328 return false;
329 }
330 // Arrays are in the same package when their element classes are.
331 while (klass1->IsArrayClass()) {
332 klass1 = klass1->GetComponentType();
333 }
334 while (klass2->IsArrayClass()) {
335 klass2 = klass2->GetComponentType();
336 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700337 // trivial check again for array types
338 if (klass1 == klass2) {
339 return true;
340 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700342 std::string temp1, temp2;
343 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344}
345
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346bool Class::IsStringClass() const {
347 return this == String::GetJavaLangString();
348}
349
Ian Rogersef7d42f2014-01-06 12:55:46 -0800350bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
352}
353
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100355 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700356 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100357 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700358 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100359 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360}
361
Brian Carlstrom004644f2014-06-18 08:34:01 -0700362ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700364 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700365 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800366 return method;
367 }
368
369 int32_t iftable_count = GetIfTableCount();
370 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700371 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700373 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800374 return method;
375 }
376 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700377 return nullptr;
378}
379
380ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
381 // Check the current class before checking the interfaces.
382 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
383 if (method != nullptr) {
384 return method;
385 }
386
387 int32_t iftable_count = GetIfTableCount();
388 IfTable* iftable = GetIfTable();
389 for (int32_t i = 0; i < iftable_count; ++i) {
390 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
391 if (method != nullptr) {
392 return method;
393 }
394 }
395 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396}
397
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700400 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700401 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800402 return method;
403 }
404
405 int32_t iftable_count = GetIfTableCount();
406 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700407 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800408 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700409 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 return method;
411 }
412 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700413 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800414}
415
Ian Rogersef7d42f2014-01-06 12:55:46 -0800416ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700418 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700419 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700420 return method;
421 }
422 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700423 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700424}
425
Ian Rogersef7d42f2014-01-06 12:55:46 -0800426ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700427 for (size_t i = 0; i < NumDirectMethods(); ++i) {
428 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700429 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800430 return method;
431 }
432 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700433 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800434}
435
Ian Rogersef7d42f2014-01-06 12:55:46 -0800436ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437 if (GetDexCache() == dex_cache) {
438 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700439 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800440 if (method->GetDexMethodIndex() == dex_method_idx) {
441 return method;
442 }
443 }
444 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700445 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446}
447
Ian Rogersef7d42f2014-01-06 12:55:46 -0800448ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700449 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700450 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800452 return method;
453 }
454 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700455 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800456}
457
Ian Rogersef7d42f2014-01-06 12:55:46 -0800458ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700459 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700460 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700461 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700462 return method;
463 }
464 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700465 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700466}
467
Ian Rogersef7d42f2014-01-06 12:55:46 -0800468ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700469 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700470 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700471 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 return method;
473 }
474 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700475 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476}
477
Ian Rogersef7d42f2014-01-06 12:55:46 -0800478ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700479 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
480 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700481 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700482 return method;
483 }
484 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700485 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700486}
487
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700488ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700490 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700491 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492 return method;
493 }
494 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700495 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496}
497
Ian Rogersef7d42f2014-01-06 12:55:46 -0800498ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 if (GetDexCache() == dex_cache) {
500 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700501 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502 if (method->GetDexMethodIndex() == dex_method_idx) {
503 return method;
504 }
505 }
506 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700507 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508}
509
Ian Rogersef7d42f2014-01-06 12:55:46 -0800510ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700511 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700512 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514 return method;
515 }
516 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518}
519
Ian Rogersef7d42f2014-01-06 12:55:46 -0800520ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700522 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700523 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700524 return method;
525 }
526 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700527 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700528}
529
Ian Rogersef7d42f2014-01-06 12:55:46 -0800530ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700531 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700532 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700533 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534 return method;
535 }
536 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700537 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800538}
539
Ian Rogersef7d42f2014-01-06 12:55:46 -0800540ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700541 for (size_t i = 0; i < NumDirectMethods(); ++i) {
542 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700543 if (method->IsClassInitializer()) {
544 DCHECK_STREQ(method->GetName(), "<clinit>");
545 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700546 return method;
547 }
548 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700549 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700550}
551
Brian Carlstromea46f952013-07-30 01:26:50 -0700552ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800553 // Is the field in this class?
554 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700557 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 return f;
559 }
560 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700561 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562}
563
Brian Carlstromea46f952013-07-30 01:26:50 -0700564ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 if (GetDexCache() == dex_cache) {
566 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700567 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568 if (f->GetDexFieldIndex() == dex_field_idx) {
569 return f;
570 }
571 }
572 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700573 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574}
575
Brian Carlstromea46f952013-07-30 01:26:50 -0700576ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 // Is the field in this class, or any of its superclasses?
578 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700579 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700580 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700581 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800582 return f;
583 }
584 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700585 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586}
587
Brian Carlstromea46f952013-07-30 01:26:50 -0700588ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800589 // Is the field in this class, or any of its superclasses?
590 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700591 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700593 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 return f;
595 }
596 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700597 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598}
599
Brian Carlstromea46f952013-07-30 01:26:50 -0700600ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700601 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700603 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700604 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605 return f;
606 }
607 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700608 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609}
610
Brian Carlstromea46f952013-07-30 01:26:50 -0700611ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800612 if (dex_cache == GetDexCache()) {
613 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615 if (f->GetDexFieldIndex() == dex_field_idx) {
616 return f;
617 }
618 }
619 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700620 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621}
622
Mathieu Chartierf8322842014-05-16 10:59:25 -0700623ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
624 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 // Is the field in this class (or its interfaces), or any of its
626 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700627 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700629 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700630 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 return f;
632 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700633 // Wrap k incase it moves during GetDirectInterface.
634 StackHandleScope<1> hs(self);
635 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700637 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
638 StackHandleScope<1> hs(self);
639 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
640 f = FindStaticField(self, interface, name, type);
641 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800642 return f;
643 }
644 }
645 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647}
648
Mathieu Chartierf8322842014-05-16 10:59:25 -0700649ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
650 uint32_t dex_field_idx) {
651 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700653 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700654 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800655 return f;
656 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700657 // Wrap k incase it moves during GetDirectInterface.
658 StackHandleScope<1> hs(self);
659 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700661 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
662 StackHandleScope<1> hs(self);
663 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
664 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
665 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800666 return f;
667 }
668 }
669 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700670 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671}
672
Mathieu Chartierf8322842014-05-16 10:59:25 -0700673ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
674 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800675 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700676 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700679 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800680 return f;
681 }
682 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700683 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800684 return f;
685 }
686 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700687 StackHandleScope<1> hs(self);
688 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
689 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
690 StackHandleScope<1> hs(self);
691 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
692 f = interface->FindStaticField(self, interface, name, type);
693 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800694 return f;
695 }
696 }
697 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700698 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800699}
700
Brian Carlstromea46f952013-07-30 01:26:50 -0700701static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200702 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700703 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200704 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700705 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700706 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700707 if (!method->IsNative() && !method->IsAbstract()) {
708 method->SetPreverified();
709 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200710 }
711 }
712}
713
714void Class::SetPreverifiedFlagOnAllMethods() {
715 DCHECK(IsVerified());
716 SetPreverifiedFlagOnMethods(GetDirectMethods());
717 SetPreverifiedFlagOnMethods(GetVirtualMethods());
718}
719
Ian Rogers1ff3c982014-08-12 02:30:58 -0700720const char* Class::GetDescriptor(std::string* storage) {
721 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700722 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700723 } else if (IsArrayClass()) {
724 return GetArrayDescriptor(storage);
725 } else if (IsProxyClass()) {
726 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
727 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700728 } else {
729 const DexFile& dex_file = GetDexFile();
730 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
731 return dex_file.GetTypeDescriptor(type_id);
732 }
733}
734
Ian Rogers1ff3c982014-08-12 02:30:58 -0700735const char* Class::GetArrayDescriptor(std::string* storage) {
736 std::string temp;
737 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
738 *storage = "[";
739 *storage += elem_desc;
740 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700741}
742
743const DexFile::ClassDef* Class::GetClassDef() {
744 uint16_t class_def_idx = GetDexClassDefIndex();
745 if (class_def_idx == DexFile::kDexNoIndex16) {
746 return nullptr;
747 }
748 return &GetDexFile().GetClassDef(class_def_idx);
749}
750
751uint32_t Class::NumDirectInterfaces() {
752 if (IsPrimitive()) {
753 return 0;
754 } else if (IsArrayClass()) {
755 return 2;
756 } else if (IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700757 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700758 return interfaces != nullptr ? interfaces->GetLength() : 0;
759 } else {
760 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
761 if (interfaces == nullptr) {
762 return 0;
763 } else {
764 return interfaces->Size();
765 }
766 }
767}
768
769uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
770 DCHECK(!IsPrimitive());
771 DCHECK(!IsArrayClass());
772 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
773}
774
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700775mirror::Class* Class::GetDirectInterface(Thread* self, ConstHandle<mirror::Class> klass,
776 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700777 DCHECK(klass.Get() != nullptr);
778 DCHECK(!klass->IsPrimitive());
779 if (klass->IsArrayClass()) {
780 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
781 if (idx == 0) {
782 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
783 } else {
784 DCHECK_EQ(1U, idx);
785 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
786 }
787 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700788 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700789 DCHECK(interfaces != nullptr);
790 return interfaces->Get(idx);
791 } else {
792 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
793 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
794 if (interface == nullptr) {
795 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
796 klass.Get());
797 CHECK(interface != nullptr || self->IsExceptionPending());
798 }
799 return interface;
800 }
801}
802
803const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700804 const DexFile& dex_file = GetDexFile();
805 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200806 if (dex_class_def == nullptr) {
807 // Generated classes have no class def.
808 return nullptr;
809 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700810 return dex_file.GetSourceFile(*dex_class_def);
811}
812
813std::string Class::GetLocation() {
814 mirror::DexCache* dex_cache = GetDexCache();
815 if (dex_cache != nullptr && !IsProxyClass()) {
816 return dex_cache->GetLocation()->ToModifiedUtf8();
817 }
818 // Arrays and proxies are generated and have no corresponding dex file location.
819 return "generated class";
820}
821
822const DexFile::TypeList* Class::GetInterfaceTypeList() {
823 const DexFile::ClassDef* class_def = GetClassDef();
824 if (class_def == nullptr) {
825 return nullptr;
826 }
827 return GetDexFile().GetInterfacesList(*class_def);
828}
829
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700830void Class::PopulateEmbeddedImtAndVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
831 ObjectArray<ArtMethod>* table = GetImTable();
832 if (table != nullptr) {
833 for (uint32_t i = 0; i < kImtSize; i++) {
834 SetEmbeddedImTableEntry(i, table->Get(i));
835 }
836 }
837
838 table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700839 CHECK(table != nullptr) << PrettyClass(this);
840 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700841 for (int32_t i = 0; i < table->GetLength(); i++) {
842 SetEmbeddedVTableEntry(i, table->Get(i));
843 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700844
845 SetImTable(nullptr);
846 // Keep java.lang.Object class's vtable around for since it's easier
847 // to be reused by array classes during their linking.
848 if (!IsObjectClass()) {
849 SetVTable(nullptr);
850 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700851}
852
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700853// The pre-fence visitor for Class::CopyOf().
854class CopyClassVisitor {
855 public:
856 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
857 size_t new_length, size_t copy_bytes)
858 : self_(self), orig_(orig), new_length_(new_length),
859 copy_bytes_(copy_bytes) {
860 }
861
862 void operator()(Object* obj, size_t usable_size) const
863 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
864 UNUSED(usable_size);
865 mirror::Class* new_class_obj = obj->AsClass();
866 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_);
867 new_class_obj->SetStatus(Class::kStatusResolving, self_);
868 new_class_obj->PopulateEmbeddedImtAndVTable();
869 new_class_obj->SetClassSize(new_length_);
870 }
871
872 private:
873 Thread* const self_;
874 Handle<mirror::Class>* const orig_;
875 const size_t new_length_;
876 const size_t copy_bytes_;
877 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
878};
879
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700880Class* Class::CopyOf(Thread* self, int32_t new_length) {
881 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
882 // We may get copied by a compacting GC.
883 StackHandleScope<1> hs(self);
884 Handle<mirror::Class> h_this(hs.NewHandle(this));
885 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700886 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
887 // to skip copying the tail part that we will overwrite here.
888 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700889
890 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700891 kMovingClasses
892 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
893 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700894 if (UNLIKELY(new_class == nullptr)) {
895 CHECK(self->IsExceptionPending()); // Expect an OOME.
896 return NULL;
897 }
898
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700899 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700900}
901
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800902} // namespace mirror
903} // namespace art