blob: 2afb4af0e3d21a845faa2cb9c4bf0193c1089cd8 [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"
Vladimir Marko3481ba22015-04-13 12:22:36 +010021#include "class_linker-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#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 Chartierbb87e0f2015-04-03 11:21:55 -070054void Class::VisitRoots(RootVisitor* visitor) {
55 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080056}
57
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070058void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
59 Status old_status = h_this->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)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070065 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
66 << " " << 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.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070070 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070072 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
Ian Rogers98379392014-02-24 16:53:16 -080075 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070076 CHECK_NE(h_this->GetStatus(), kStatusError)
77 << "Attempt to set as erroneous an already erroneous class "
78 << PrettyClass(h_this.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079
Ian Rogers62d6c772013-02-27 08:32:07 -080080 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000081 StackHandleScope<1> hs(self);
82 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070083 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070084 Class* eiie_class;
85 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
86 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070087 bool throw_eiie = (old_exception.Get() == nullptr);
88 if (!throw_eiie) {
89 std::string temp;
90 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
91 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
92 }
93 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070094 // Clear exception to call FindSystemClass.
95 self->ClearException();
96 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
97 self, "Ljava/lang/ExceptionInInitializerError;");
98 CHECK(!self->IsExceptionPending());
99 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700100 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
101 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700102 Class* exception_class = old_exception->GetClass();
103 if (!eiie_class->IsAssignableFrom(exception_class)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700104 h_this->SetVerifyErrorClass(exception_class);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700105 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 }
107
Ian Rogers62d6c772013-02-27 08:32:07 -0800108 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000109 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800111 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100112 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700113 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700115 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700117
118 if (!class_linker_initialized) {
119 // When the class linker is being initialized its single threaded and by definition there can be
120 // no waiters. During initialization classes may appear temporary but won't be retired as their
121 // size was statically computed.
122 } else {
123 // Classes that are being resolved or initialized need to notify waiters that the class status
124 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700125 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
127 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700128 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700129 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700130 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700131 }
132 } else {
133 CHECK_NE(new_status, kStatusRetired);
134 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700135 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700136 }
137 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700138 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139}
140
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700142 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800143 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144}
145
Ian Rogersef7d42f2014-01-06 12:55:46 -0800146void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700147 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
148 DumpClass(LOG(ERROR), kDumpClassFullDetail);
149 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
150 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100151 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700152 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153}
154
155// Return the class' name. The exact format is bizarre, but it's the specified behavior for
156// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
157// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
158// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700159String* Class::ComputeName(Handle<Class> h_this) {
160 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800161 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 return name;
163 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700164 std::string temp;
165 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800166 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
168 // The descriptor indicates that this is the class for
169 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700170 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 switch (descriptor[0]) {
172 case 'Z': c_name = "boolean"; break;
173 case 'B': c_name = "byte"; break;
174 case 'C': c_name = "char"; break;
175 case 'S': c_name = "short"; break;
176 case 'I': c_name = "int"; break;
177 case 'J': c_name = "long"; break;
178 case 'F': c_name = "float"; break;
179 case 'D': c_name = "double"; break;
180 case 'V': c_name = "void"; break;
181 default:
182 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
183 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800184 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800185 } else {
186 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
187 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700190 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 return name;
192}
193
Ian Rogersef7d42f2014-01-06 12:55:46 -0800194void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 if ((flags & kDumpClassFullDetail) == 0) {
196 os << PrettyClass(this);
197 if ((flags & kDumpClassClassLoader) != 0) {
198 os << ' ' << GetClassLoader();
199 }
200 if ((flags & kDumpClassInitialized) != 0) {
201 os << ' ' << GetStatus();
202 }
203 os << "\n";
204 return;
205 }
206
Mathieu Chartierf8322842014-05-16 10:59:25 -0700207 Thread* self = Thread::Current();
208 StackHandleScope<2> hs(self);
209 Handle<mirror::Class> h_this(hs.NewHandle(this));
210 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
211
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700214 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700216 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 os << StringPrintf(" access=0x%04x.%04x\n",
218 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700219 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700220 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
221 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800222 }
223 if (IsArrayClass()) {
224 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
225 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700226 const size_t num_direct_interfaces = NumDirectInterfaces();
227 if (num_direct_interfaces > 0) {
228 os << " interfaces (" << num_direct_interfaces << "):\n";
229 for (size_t i = 0; i < num_direct_interfaces; ++i) {
230 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700231 if (interface == nullptr) {
232 os << StringPrintf(" %2zd: nullptr!\n", i);
233 } else {
234 const ClassLoader* cl = interface->GetClassLoader();
235 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
236 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800237 }
238 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700239 if (!IsLoaded()) {
240 os << " class not yet loaded";
241 } else {
242 // After this point, this may have moved due to GetDirectInterface.
243 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
244 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
245 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
246 os << StringPrintf(" %2zd: %s\n", i,
247 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
250 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
251 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
252 }
253 if (h_this->NumStaticFields() > 0) {
254 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
255 if (h_this->IsResolved() || h_this->IsErroneous()) {
256 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
257 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
258 }
259 } else {
260 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700262 }
263 if (h_this->NumInstanceFields() > 0) {
264 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
265 if (h_this->IsResolved() || h_this->IsErroneous()) {
266 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
267 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
268 }
269 } else {
270 os << " <not yet available>";
271 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 }
273 }
274}
275
276void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700277 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 // Sanity check that the number of bits set in the reference offset bitmap
279 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700280 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700281 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 count += c->NumReferenceInstanceFieldsDuringLinking();
283 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700284 // +1 for the Class in Object.
285 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100287 // Not called within a transaction.
288 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700289 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290}
291
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800292bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
293 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700294 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
295 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296 ++i;
297 }
298 if (descriptor1.find('/', i) != StringPiece::npos ||
299 descriptor2.find('/', i) != StringPiece::npos) {
300 return false;
301 } else {
302 return true;
303 }
304}
305
Ian Rogersef7d42f2014-01-06 12:55:46 -0800306bool Class::IsInSamePackage(Class* that) {
307 Class* klass1 = this;
308 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800309 if (klass1 == klass2) {
310 return true;
311 }
312 // Class loaders must match.
313 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
314 return false;
315 }
316 // Arrays are in the same package when their element classes are.
317 while (klass1->IsArrayClass()) {
318 klass1 = klass1->GetComponentType();
319 }
320 while (klass2->IsArrayClass()) {
321 klass2 = klass2->GetComponentType();
322 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700323 // trivial check again for array types
324 if (klass1 == klass2) {
325 return true;
326 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700328 std::string temp1, temp2;
329 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330}
331
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332bool Class::IsStringClass() const {
333 return this == String::GetJavaLangString();
334}
335
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
338}
339
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700342 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700344 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100345 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346}
347
Brian Carlstrom004644f2014-06-18 08:34:01 -0700348ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700350 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700351 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 return method;
353 }
354
355 int32_t iftable_count = GetIfTableCount();
356 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700357 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700359 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 return method;
361 }
362 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700363 return nullptr;
364}
365
366ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
367 // Check the current class before checking the interfaces.
368 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
369 if (method != nullptr) {
370 return method;
371 }
372
373 int32_t iftable_count = GetIfTableCount();
374 IfTable* iftable = GetIfTable();
375 for (int32_t i = 0; i < iftable_count; ++i) {
376 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
377 if (method != nullptr) {
378 return method;
379 }
380 }
381 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800382}
383
Ian Rogersef7d42f2014-01-06 12:55:46 -0800384ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700386 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700387 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 return method;
389 }
390
391 int32_t iftable_count = GetIfTableCount();
392 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700393 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700395 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 return method;
397 }
398 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700399 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400}
401
Ian Rogersef7d42f2014-01-06 12:55:46 -0800402ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700404 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700405 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700406 return method;
407 }
408 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700409 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700410}
411
Ian Rogersef7d42f2014-01-06 12:55:46 -0800412ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700413 for (size_t i = 0; i < NumDirectMethods(); ++i) {
414 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700415 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800416 return method;
417 }
418 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700419 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800420}
421
Ian Rogersef7d42f2014-01-06 12:55:46 -0800422ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423 if (GetDexCache() == dex_cache) {
424 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700425 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 if (method->GetDexMethodIndex() == dex_method_idx) {
427 return method;
428 }
429 }
430 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700431 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432}
433
Ian Rogersef7d42f2014-01-06 12:55:46 -0800434ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700435 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700436 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700437 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800438 return method;
439 }
440 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700441 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800442}
443
Ian Rogersef7d42f2014-01-06 12:55:46 -0800444ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700445 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700446 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700448 return method;
449 }
450 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700452}
453
Ian Rogersef7d42f2014-01-06 12:55:46 -0800454ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700455 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700456 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800458 return method;
459 }
460 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700461 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800462}
463
Ian Rogersef7d42f2014-01-06 12:55:46 -0800464ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700465 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
466 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700467 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700468 return method;
469 }
470 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700471 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700472}
473
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700474ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800475 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700476 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700477 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 return method;
479 }
480 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700481 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482}
483
Ian Rogersef7d42f2014-01-06 12:55:46 -0800484ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 if (GetDexCache() == dex_cache) {
486 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700487 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstromf322c4c2014-10-31 00:01:54 -0700488 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe2b0fa5b2014-10-31 18:12:30 -0700489 // A miranda method may have a different DexCache and is always created by linking,
490 // never *declared* in the class.
491 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492 return method;
493 }
494 }
495 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700496 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800497}
498
Ian Rogersef7d42f2014-01-06 12:55:46 -0800499ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700500 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700501 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700502 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800503 return method;
504 }
505 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700506 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507}
508
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700510 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700511 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700512 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700513 return method;
514 }
515 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700516 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700517}
518
Ian Rogersef7d42f2014-01-06 12:55:46 -0800519ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700520 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700521 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700522 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523 return method;
524 }
525 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700526 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527}
528
Ian Rogersef7d42f2014-01-06 12:55:46 -0800529ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700530 for (size_t i = 0; i < NumDirectMethods(); ++i) {
531 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700532 if (method->IsClassInitializer()) {
533 DCHECK_STREQ(method->GetName(), "<clinit>");
534 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700535 return method;
536 }
537 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700538 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700539}
540
Brian Carlstromea46f952013-07-30 01:26:50 -0700541ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800542 // Is the field in this class?
543 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700545 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700546 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 return f;
548 }
549 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700550 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551}
552
Brian Carlstromea46f952013-07-30 01:26:50 -0700553ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 if (GetDexCache() == dex_cache) {
555 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 if (f->GetDexFieldIndex() == dex_field_idx) {
558 return f;
559 }
560 }
561 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700562 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800563}
564
Brian Carlstromea46f952013-07-30 01:26:50 -0700565ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 // Is the field in this class, or any of its superclasses?
567 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700568 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700569 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700570 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 return f;
572 }
573 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700574 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575}
576
Brian Carlstromea46f952013-07-30 01:26:50 -0700577ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 // Is the field in this class, or any of its superclasses?
579 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700580 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700581 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700582 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583 return f;
584 }
585 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700586 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800587}
588
Brian Carlstromea46f952013-07-30 01:26:50 -0700589ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700590 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700593 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
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 DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 if (dex_cache == GetDexCache()) {
602 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700603 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604 if (f->GetDexFieldIndex() == dex_field_idx) {
605 return f;
606 }
607 }
608 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700609 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610}
611
Mathieu Chartierf8322842014-05-16 10:59:25 -0700612ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
613 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 // Is the field in this class (or its interfaces), or any of its
615 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700616 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700618 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700619 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 return f;
621 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700622 // Wrap k incase it moves during GetDirectInterface.
623 StackHandleScope<1> hs(self);
624 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700626 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800627 StackHandleScope<1> hs2(self);
628 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700629 f = FindStaticField(self, interface, name, type);
630 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 return f;
632 }
633 }
634 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700635 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636}
637
Mathieu Chartierf8322842014-05-16 10:59:25 -0700638ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
639 uint32_t dex_field_idx) {
640 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700642 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700643 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 return f;
645 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646 // Wrap k incase it moves during GetDirectInterface.
647 StackHandleScope<1> hs(self);
648 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800649 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700650 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800651 StackHandleScope<1> hs2(self);
652 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700653 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
654 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800655 return f;
656 }
657 }
658 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700659 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660}
661
Mathieu Chartierf8322842014-05-16 10:59:25 -0700662ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
663 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800664 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700665 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800666 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700667 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700668 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800669 return f;
670 }
671 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700672 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800673 return f;
674 }
675 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700676 StackHandleScope<1> hs(self);
677 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
678 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800679 StackHandleScope<1> hs2(self);
680 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700681 f = interface->FindStaticField(self, interface, name, type);
682 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683 return f;
684 }
685 }
686 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700687 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800688}
689
Brian Carlstromea46f952013-07-30 01:26:50 -0700690static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200691 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700692 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200693 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700694 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700695 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700696 if (!method->IsNative() && !method->IsAbstract()) {
697 method->SetPreverified();
698 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200699 }
700 }
701}
702
703void Class::SetPreverifiedFlagOnAllMethods() {
704 DCHECK(IsVerified());
705 SetPreverifiedFlagOnMethods(GetDirectMethods());
706 SetPreverifiedFlagOnMethods(GetVirtualMethods());
707}
708
Ian Rogers1ff3c982014-08-12 02:30:58 -0700709const char* Class::GetDescriptor(std::string* storage) {
710 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700711 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700712 } else if (IsArrayClass()) {
713 return GetArrayDescriptor(storage);
714 } else if (IsProxyClass()) {
715 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
716 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700717 } else {
718 const DexFile& dex_file = GetDexFile();
719 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
720 return dex_file.GetTypeDescriptor(type_id);
721 }
722}
723
Ian Rogers1ff3c982014-08-12 02:30:58 -0700724const char* Class::GetArrayDescriptor(std::string* storage) {
725 std::string temp;
726 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
727 *storage = "[";
728 *storage += elem_desc;
729 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700730}
731
732const DexFile::ClassDef* Class::GetClassDef() {
733 uint16_t class_def_idx = GetDexClassDefIndex();
734 if (class_def_idx == DexFile::kDexNoIndex16) {
735 return nullptr;
736 }
737 return &GetDexFile().GetClassDef(class_def_idx);
738}
739
Mathieu Chartierf8322842014-05-16 10:59:25 -0700740uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
741 DCHECK(!IsPrimitive());
742 DCHECK(!IsArrayClass());
743 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
744}
745
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700746mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700747 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700748 DCHECK(klass.Get() != nullptr);
749 DCHECK(!klass->IsPrimitive());
750 if (klass->IsArrayClass()) {
751 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
752 if (idx == 0) {
753 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
754 } else {
755 DCHECK_EQ(1U, idx);
756 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
757 }
758 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700759 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700760 DCHECK(interfaces != nullptr);
761 return interfaces->Get(idx);
762 } else {
763 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
764 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
765 if (interface == nullptr) {
766 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
767 klass.Get());
768 CHECK(interface != nullptr || self->IsExceptionPending());
769 }
770 return interface;
771 }
772}
773
774const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700775 const DexFile& dex_file = GetDexFile();
776 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200777 if (dex_class_def == nullptr) {
778 // Generated classes have no class def.
779 return nullptr;
780 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700781 return dex_file.GetSourceFile(*dex_class_def);
782}
783
784std::string Class::GetLocation() {
785 mirror::DexCache* dex_cache = GetDexCache();
786 if (dex_cache != nullptr && !IsProxyClass()) {
787 return dex_cache->GetLocation()->ToModifiedUtf8();
788 }
789 // Arrays and proxies are generated and have no corresponding dex file location.
790 return "generated class";
791}
792
793const DexFile::TypeList* Class::GetInterfaceTypeList() {
794 const DexFile::ClassDef* class_def = GetClassDef();
795 if (class_def == nullptr) {
796 return nullptr;
797 }
798 return GetDexFile().GetInterfacesList(*class_def);
799}
800
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700801void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
802 for (uint32_t i = 0; i < kImtSize; i++) {
803 // Replace null with conflict.
804 mirror::Object* obj = imt_handle_scope->GetReference(i);
805 DCHECK(obj != nullptr);
806 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700807 }
808
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700809 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700810 CHECK(table != nullptr) << PrettyClass(this);
811 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700812 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700813 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700814 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700815
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700816 // Keep java.lang.Object class's vtable around for since it's easier
817 // to be reused by array classes during their linking.
818 if (!IsObjectClass()) {
819 SetVTable(nullptr);
820 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700821}
822
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700823// The pre-fence visitor for Class::CopyOf().
824class CopyClassVisitor {
825 public:
826 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700827 size_t new_length, size_t copy_bytes,
828 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700829 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700830 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700831 }
832
833 void operator()(Object* obj, size_t usable_size) const
834 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
835 UNUSED(usable_size);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700836 StackHandleScope<1> hs(self_);
837 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
838 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
839 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
840 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
841 h_new_class_obj->SetClassSize(new_length_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700842 }
843
844 private:
845 Thread* const self_;
846 Handle<mirror::Class>* const orig_;
847 const size_t new_length_;
848 const size_t copy_bytes_;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700849 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700850 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
851};
852
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700853Class* Class::CopyOf(Thread* self, int32_t new_length,
854 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700855 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
856 // We may get copied by a compacting GC.
857 StackHandleScope<1> hs(self);
858 Handle<mirror::Class> h_this(hs.NewHandle(this));
859 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700860 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
861 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700862 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700863 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700864 kMovingClasses
865 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
866 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700867 if (UNLIKELY(new_class == nullptr)) {
868 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700869 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700870 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700871 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700872}
873
Vladimir Marko3481ba22015-04-13 12:22:36 +0100874bool Class::ProxyDescriptorEquals(const char* match) {
875 DCHECK(IsProxyClass());
876 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
877}
878
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800879} // namespace mirror
880} // namespace art