blob: 1739019755cfb722e244fad7e30bbb9c4cbb5811 [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"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070028#include "method.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070029#include "object_array-inl.h"
30#include "object-inl.h"
31#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "thread.h"
33#include "throwable.h"
34#include "utils.h"
35#include "well_known_classes.h"
36
37namespace art {
38namespace mirror {
39
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070040GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041
42void Class::SetClassClass(Class* java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070043 CHECK(java_lang_Class_.IsNull())
44 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070045 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070046 CHECK(java_lang_Class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070047 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048}
49
50void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070051 CHECK(!java_lang_Class_.IsNull());
52 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053}
54
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070055void Class::VisitRoots(RootVisitor* visitor) {
56 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057}
58
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070059void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
60 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070063 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070064 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
65 new_status != kStatusRetired)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070066 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
67 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070068 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070069 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
70 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070071 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070072 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070073 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070074 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 }
Ian Rogers98379392014-02-24 16:53:16 -080076 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070077 CHECK_NE(h_this->GetStatus(), kStatusError)
78 << "Attempt to set as erroneous an already erroneous class "
79 << PrettyClass(h_this.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Ian Rogers62d6c772013-02-27 08:32:07 -080081 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000082 StackHandleScope<1> hs(self);
83 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070085 Class* eiie_class;
86 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
87 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070088 bool throw_eiie = (old_exception.Get() == nullptr);
89 if (!throw_eiie) {
90 std::string temp;
91 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
92 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
93 }
94 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070095 // Clear exception to call FindSystemClass.
96 self->ClearException();
97 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
98 self, "Ljava/lang/ExceptionInInitializerError;");
99 CHECK(!self->IsExceptionPending());
100 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700101 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
102 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700103 Class* exception_class = old_exception->GetClass();
104 if (!eiie_class->IsAssignableFrom(exception_class)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700105 h_this->SetVerifyErrorClass(exception_class);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700106 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
108
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000110 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800112 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700114 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700116 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118
119 if (!class_linker_initialized) {
120 // When the class linker is being initialized its single threaded and by definition there can be
121 // no waiters. During initialization classes may appear temporary but won't be retired as their
122 // size was statically computed.
123 } else {
124 // Classes that are being resolved or initialized need to notify waiters that the class status
125 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700126 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
128 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700129 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700130 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700131 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700132 }
133 } else {
134 CHECK_NE(new_status, kStatusRetired);
135 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700136 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 }
138 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700139 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140}
141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700143 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800144 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145}
146
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700148 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
149 DumpClass(LOG(ERROR), kDumpClassFullDetail);
150 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
151 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100152 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700153 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154}
155
156// Return the class' name. The exact format is bizarre, but it's the specified behavior for
157// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
158// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
159// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700160String* Class::ComputeName(Handle<Class> h_this) {
161 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800162 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 return name;
164 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700165 std::string temp;
166 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800167 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800168 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
169 // The descriptor indicates that this is the class for
170 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700171 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800172 switch (descriptor[0]) {
173 case 'Z': c_name = "boolean"; break;
174 case 'B': c_name = "byte"; break;
175 case 'C': c_name = "char"; break;
176 case 'S': c_name = "short"; break;
177 case 'I': c_name = "int"; break;
178 case 'J': c_name = "long"; break;
179 case 'F': c_name = "float"; break;
180 case 'D': c_name = "double"; break;
181 case 'V': c_name = "void"; break;
182 default:
183 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
184 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800185 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 } else {
187 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
188 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700189 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700191 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 return name;
193}
194
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 if ((flags & kDumpClassFullDetail) == 0) {
197 os << PrettyClass(this);
198 if ((flags & kDumpClassClassLoader) != 0) {
199 os << ' ' << GetClassLoader();
200 }
201 if ((flags & kDumpClassInitialized) != 0) {
202 os << ' ' << GetStatus();
203 }
204 os << "\n";
205 return;
206 }
207
Mathieu Chartierf8322842014-05-16 10:59:25 -0700208 Thread* self = Thread::Current();
209 StackHandleScope<2> hs(self);
210 Handle<mirror::Class> h_this(hs.NewHandle(this));
211 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
212
Ian Rogers1ff3c982014-08-12 02:30:58 -0700213 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700217 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 os << StringPrintf(" access=0x%04x.%04x\n",
219 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700220 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700221 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
222 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 }
224 if (IsArrayClass()) {
225 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
226 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700227 const size_t num_direct_interfaces = NumDirectInterfaces();
228 if (num_direct_interfaces > 0) {
229 os << " interfaces (" << num_direct_interfaces << "):\n";
230 for (size_t i = 0; i < num_direct_interfaces; ++i) {
231 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700232 if (interface == nullptr) {
233 os << StringPrintf(" %2zd: nullptr!\n", i);
234 } else {
235 const ClassLoader* cl = interface->GetClassLoader();
236 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
237 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 }
239 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700240 if (!IsLoaded()) {
241 os << " class not yet loaded";
242 } else {
243 // After this point, this may have moved due to GetDirectInterface.
244 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
245 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
246 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
247 os << StringPrintf(" %2zd: %s\n", i,
248 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700250 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
251 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
252 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
253 }
254 if (h_this->NumStaticFields() > 0) {
255 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
256 if (h_this->IsResolved() || h_this->IsErroneous()) {
257 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
258 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
259 }
260 } else {
261 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700263 }
264 if (h_this->NumInstanceFields() > 0) {
265 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
266 if (h_this->IsResolved() || h_this->IsErroneous()) {
267 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
268 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
269 }
270 } else {
271 os << " <not yet available>";
272 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 }
274 }
275}
276
277void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700278 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 // Sanity check that the number of bits set in the reference offset bitmap
280 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700281 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700282 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 count += c->NumReferenceInstanceFieldsDuringLinking();
284 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700285 // +1 for the Class in Object.
286 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100288 // Not called within a transaction.
289 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700290 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291}
292
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
294 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700295 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
296 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 ++i;
298 }
299 if (descriptor1.find('/', i) != StringPiece::npos ||
300 descriptor2.find('/', i) != StringPiece::npos) {
301 return false;
302 } else {
303 return true;
304 }
305}
306
Ian Rogersef7d42f2014-01-06 12:55:46 -0800307bool Class::IsInSamePackage(Class* that) {
308 Class* klass1 = this;
309 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800310 if (klass1 == klass2) {
311 return true;
312 }
313 // Class loaders must match.
314 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
315 return false;
316 }
317 // Arrays are in the same package when their element classes are.
318 while (klass1->IsArrayClass()) {
319 klass1 = klass1->GetComponentType();
320 }
321 while (klass2->IsArrayClass()) {
322 klass2 = klass2->GetComponentType();
323 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700324 // trivial check again for array types
325 if (klass1 == klass2) {
326 return true;
327 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700329 std::string temp1, temp2;
330 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331}
332
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333bool Class::IsStringClass() const {
334 return this == String::GetJavaLangString();
335}
336
Ian Rogersef7d42f2014-01-06 12:55:46 -0800337bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
339}
340
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100342 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700343 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100344 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700345 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347}
348
Brian Carlstrom004644f2014-06-18 08:34:01 -0700349ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700351 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700352 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 return method;
354 }
355
356 int32_t iftable_count = GetIfTableCount();
357 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700358 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700360 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 return method;
362 }
363 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700364 return nullptr;
365}
366
367ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
368 // Check the current class before checking the interfaces.
369 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
370 if (method != nullptr) {
371 return method;
372 }
373
374 int32_t iftable_count = GetIfTableCount();
375 IfTable* iftable = GetIfTable();
376 for (int32_t i = 0; i < iftable_count; ++i) {
377 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
378 if (method != nullptr) {
379 return method;
380 }
381 }
382 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383}
384
Ian Rogersef7d42f2014-01-06 12:55:46 -0800385ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800386 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700387 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700388 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 return method;
390 }
391
392 int32_t iftable_count = GetIfTableCount();
393 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700394 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700396 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 return method;
398 }
399 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700400 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401}
402
Ian Rogersef7d42f2014-01-06 12:55:46 -0800403ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700405 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700406 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700407 return method;
408 }
409 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700410 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700411}
412
Ian Rogersef7d42f2014-01-06 12:55:46 -0800413ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700414 for (size_t i = 0; i < NumDirectMethods(); ++i) {
415 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700416 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 return method;
418 }
419 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700420 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421}
422
Ian Rogersef7d42f2014-01-06 12:55:46 -0800423ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424 if (GetDexCache() == dex_cache) {
425 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700426 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427 if (method->GetDexMethodIndex() == dex_method_idx) {
428 return method;
429 }
430 }
431 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700432 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433}
434
Ian Rogersef7d42f2014-01-06 12:55:46 -0800435ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700436 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700437 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700438 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439 return method;
440 }
441 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443}
444
Ian Rogersef7d42f2014-01-06 12:55:46 -0800445ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700447 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700448 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700449 return method;
450 }
451 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700453}
454
Ian Rogersef7d42f2014-01-06 12:55:46 -0800455ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700456 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700457 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800459 return method;
460 }
461 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700462 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800463}
464
Ian Rogersef7d42f2014-01-06 12:55:46 -0800465ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700466 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
467 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700468 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700469 return method;
470 }
471 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700472 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700473}
474
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700475ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700477 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700478 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800479 return method;
480 }
481 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700482 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800483}
484
Ian Rogersef7d42f2014-01-06 12:55:46 -0800485ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 if (GetDexCache() == dex_cache) {
487 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700488 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstromf322c4c2014-10-31 00:01:54 -0700489 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe2b0fa5b2014-10-31 18:12:30 -0700490 // A miranda method may have a different DexCache and is always created by linking,
491 // never *declared* in the class.
492 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493 return method;
494 }
495 }
496 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700497 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498}
499
Ian Rogersef7d42f2014-01-06 12:55:46 -0800500ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700501 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700502 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700503 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800504 return method;
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 Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700511 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700512 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700514 return method;
515 }
516 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700518}
519
Ian Rogersef7d42f2014-01-06 12:55:46 -0800520ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700522 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700523 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800524 return method;
525 }
526 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700527 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800528}
529
Ian Rogersef7d42f2014-01-06 12:55:46 -0800530ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700531 for (size_t i = 0; i < NumDirectMethods(); ++i) {
532 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700533 if (method->IsClassInitializer()) {
534 DCHECK_STREQ(method->GetName(), "<clinit>");
535 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700536 return method;
537 }
538 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700539 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700540}
541
Brian Carlstromea46f952013-07-30 01:26:50 -0700542ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800543 // Is the field in this class?
544 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800545 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700546 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700547 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548 return f;
549 }
550 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700551 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552}
553
Brian Carlstromea46f952013-07-30 01:26:50 -0700554ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 if (GetDexCache() == dex_cache) {
556 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700557 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 if (f->GetDexFieldIndex() == dex_field_idx) {
559 return f;
560 }
561 }
562 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700563 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564}
565
Brian Carlstromea46f952013-07-30 01:26:50 -0700566ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567 // Is the field in this class, or any of its superclasses?
568 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700569 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700570 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700571 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800572 return f;
573 }
574 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700575 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576}
577
Brian Carlstromea46f952013-07-30 01:26:50 -0700578ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 // Is the field in this class, or any of its superclasses?
580 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700581 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700582 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700583 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800584 return f;
585 }
586 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700587 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588}
589
Brian Carlstromea46f952013-07-30 01:26:50 -0700590ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700591 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800592 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700593 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700594 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595 return f;
596 }
597 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700598 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599}
600
Brian Carlstromea46f952013-07-30 01:26:50 -0700601ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602 if (dex_cache == GetDexCache()) {
603 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700604 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605 if (f->GetDexFieldIndex() == dex_field_idx) {
606 return f;
607 }
608 }
609 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700610 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611}
612
Mathieu Chartierf8322842014-05-16 10:59:25 -0700613ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
614 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615 // Is the field in this class (or its interfaces), or any of its
616 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700617 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700619 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700620 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 return f;
622 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700623 // Wrap k incase it moves during GetDirectInterface.
624 StackHandleScope<1> hs(self);
625 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800626 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700627 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800628 StackHandleScope<1> hs2(self);
629 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700630 f = FindStaticField(self, interface, name, type);
631 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 return f;
633 }
634 }
635 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700636 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800637}
638
Mathieu Chartierf8322842014-05-16 10:59:25 -0700639ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
640 uint32_t dex_field_idx) {
641 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800642 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700643 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700644 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800645 return f;
646 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700647 // Wrap k incase it moves during GetDirectInterface.
648 StackHandleScope<1> hs(self);
649 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800650 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700651 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800652 StackHandleScope<1> hs2(self);
653 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700654 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
655 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800656 return f;
657 }
658 }
659 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700660 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800661}
662
Mathieu Chartierf8322842014-05-16 10:59:25 -0700663ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
664 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800665 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700666 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800667 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700668 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700669 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800670 return f;
671 }
672 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700673 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674 return f;
675 }
676 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700677 StackHandleScope<1> hs(self);
678 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
679 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800680 StackHandleScope<1> hs2(self);
681 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700682 f = interface->FindStaticField(self, interface, name, type);
683 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800684 return f;
685 }
686 }
687 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700688 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800689}
690
Brian Carlstromea46f952013-07-30 01:26:50 -0700691static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200692 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700693 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200694 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700695 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700696 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700697 if (!method->IsNative() && !method->IsAbstract()) {
698 method->SetPreverified();
699 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200700 }
701 }
702}
703
704void Class::SetPreverifiedFlagOnAllMethods() {
705 DCHECK(IsVerified());
706 SetPreverifiedFlagOnMethods(GetDirectMethods());
707 SetPreverifiedFlagOnMethods(GetVirtualMethods());
708}
709
Ian Rogers1ff3c982014-08-12 02:30:58 -0700710const char* Class::GetDescriptor(std::string* storage) {
711 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700712 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700713 } else if (IsArrayClass()) {
714 return GetArrayDescriptor(storage);
715 } else if (IsProxyClass()) {
716 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
717 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700718 } else {
719 const DexFile& dex_file = GetDexFile();
720 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
721 return dex_file.GetTypeDescriptor(type_id);
722 }
723}
724
Ian Rogers1ff3c982014-08-12 02:30:58 -0700725const char* Class::GetArrayDescriptor(std::string* storage) {
726 std::string temp;
727 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
728 *storage = "[";
729 *storage += elem_desc;
730 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700731}
732
733const DexFile::ClassDef* Class::GetClassDef() {
734 uint16_t class_def_idx = GetDexClassDefIndex();
735 if (class_def_idx == DexFile::kDexNoIndex16) {
736 return nullptr;
737 }
738 return &GetDexFile().GetClassDef(class_def_idx);
739}
740
Mathieu Chartierf8322842014-05-16 10:59:25 -0700741uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
742 DCHECK(!IsPrimitive());
743 DCHECK(!IsArrayClass());
744 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
745}
746
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700747mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700748 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700749 DCHECK(klass.Get() != nullptr);
750 DCHECK(!klass->IsPrimitive());
751 if (klass->IsArrayClass()) {
752 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
753 if (idx == 0) {
754 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
755 } else {
756 DCHECK_EQ(1U, idx);
757 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
758 }
759 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700760 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700761 DCHECK(interfaces != nullptr);
762 return interfaces->Get(idx);
763 } else {
764 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
765 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
766 if (interface == nullptr) {
767 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
768 klass.Get());
769 CHECK(interface != nullptr || self->IsExceptionPending());
770 }
771 return interface;
772 }
773}
774
775const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700776 const DexFile& dex_file = GetDexFile();
777 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200778 if (dex_class_def == nullptr) {
779 // Generated classes have no class def.
780 return nullptr;
781 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700782 return dex_file.GetSourceFile(*dex_class_def);
783}
784
785std::string Class::GetLocation() {
786 mirror::DexCache* dex_cache = GetDexCache();
787 if (dex_cache != nullptr && !IsProxyClass()) {
788 return dex_cache->GetLocation()->ToModifiedUtf8();
789 }
790 // Arrays and proxies are generated and have no corresponding dex file location.
791 return "generated class";
792}
793
794const DexFile::TypeList* Class::GetInterfaceTypeList() {
795 const DexFile::ClassDef* class_def = GetClassDef();
796 if (class_def == nullptr) {
797 return nullptr;
798 }
799 return GetDexFile().GetInterfacesList(*class_def);
800}
801
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700802void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
803 for (uint32_t i = 0; i < kImtSize; i++) {
804 // Replace null with conflict.
805 mirror::Object* obj = imt_handle_scope->GetReference(i);
806 DCHECK(obj != nullptr);
807 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700808 }
809
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700810 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700811 CHECK(table != nullptr) << PrettyClass(this);
812 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700813 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700814 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700815 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700816
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700817 // Keep java.lang.Object class's vtable around for since it's easier
818 // to be reused by array classes during their linking.
819 if (!IsObjectClass()) {
820 SetVTable(nullptr);
821 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700822}
823
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700824// The pre-fence visitor for Class::CopyOf().
825class CopyClassVisitor {
826 public:
827 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700828 size_t new_length, size_t copy_bytes,
829 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700830 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700831 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700832 }
833
834 void operator()(Object* obj, size_t usable_size) const
835 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
836 UNUSED(usable_size);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700837 StackHandleScope<1> hs(self_);
838 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
839 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
840 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
841 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
842 h_new_class_obj->SetClassSize(new_length_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700843 }
844
845 private:
846 Thread* const self_;
847 Handle<mirror::Class>* const orig_;
848 const size_t new_length_;
849 const size_t copy_bytes_;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700850 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700851 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
852};
853
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700854Class* Class::CopyOf(Thread* self, int32_t new_length,
855 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700856 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
857 // We may get copied by a compacting GC.
858 StackHandleScope<1> hs(self);
859 Handle<mirror::Class> h_this(hs.NewHandle(this));
860 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700861 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
862 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700863 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700864 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700865 kMovingClasses
866 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
867 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700868 if (UNLIKELY(new_class == nullptr)) {
869 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700870 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700871 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700872 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700873}
874
Vladimir Marko3481ba22015-04-13 12:22:36 +0100875bool Class::ProxyDescriptorEquals(const char* match) {
876 DCHECK(IsProxyClass());
877 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
878}
879
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700880mirror::ArtMethod* Class::GetDeclaredConstructor(
881 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
882 auto* direct_methods = GetDirectMethods();
883 size_t count = direct_methods != nullptr ? direct_methods->GetLength() : 0u;
884 for (size_t i = 0; i < count; ++i) {
885 auto* m = direct_methods->GetWithoutChecks(i);
886 // Skip <clinit> which is a static constructor, as well as non constructors.
887 if (m->IsStatic() || !m->IsConstructor()) {
888 continue;
889 }
890 // May cause thread suspension and exceptions.
891 if (m->EqualParameters(args)) {
892 return m;
893 }
894 if (self->IsExceptionPending()) {
895 return nullptr;
896 }
897 }
898 return nullptr;
899}
900
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800901} // namespace mirror
902} // namespace art