blob: 055b3e51106ee0f441be340ba72db956ea662c84 [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) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 if (kIsDebugBuild && new_class_size < GetClassSize()) {
149 DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail);
150 LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize();
151 LOG(FATAL) << " class=" << PrettyTypeOf(this);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700152 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700154 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155}
156
157// Return the class' name. The exact format is bizarre, but it's the specified behavior for
158// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
159// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
160// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161String* Class::ComputeName(Handle<Class> h_this) {
162 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800163 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 return name;
165 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700166 std::string temp;
167 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800168 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
170 // The descriptor indicates that this is the class for
171 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700172 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 switch (descriptor[0]) {
174 case 'Z': c_name = "boolean"; break;
175 case 'B': c_name = "byte"; break;
176 case 'C': c_name = "char"; break;
177 case 'S': c_name = "short"; break;
178 case 'I': c_name = "int"; break;
179 case 'J': c_name = "long"; break;
180 case 'F': c_name = "float"; break;
181 case 'D': c_name = "double"; break;
182 case 'V': c_name = "void"; break;
183 default:
184 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
185 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800186 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 } else {
188 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
189 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700190 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700192 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 return name;
194}
195
Ian Rogersef7d42f2014-01-06 12:55:46 -0800196void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 if ((flags & kDumpClassFullDetail) == 0) {
198 os << PrettyClass(this);
199 if ((flags & kDumpClassClassLoader) != 0) {
200 os << ' ' << GetClassLoader();
201 }
202 if ((flags & kDumpClassInitialized) != 0) {
203 os << ' ' << GetStatus();
204 }
205 os << "\n";
206 return;
207 }
208
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700210 StackHandleScope<2> hs(self);
211 Handle<mirror::Class> h_this(hs.NewHandle(this));
212 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700219 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 os << StringPrintf(" access=0x%04x.%04x\n",
221 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700222 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700223 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
224 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 }
226 if (IsArrayClass()) {
227 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
228 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 const size_t num_direct_interfaces = NumDirectInterfaces();
230 if (num_direct_interfaces > 0) {
231 os << " interfaces (" << num_direct_interfaces << "):\n";
232 for (size_t i = 0; i < num_direct_interfaces; ++i) {
233 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700234 if (interface == nullptr) {
235 os << StringPrintf(" %2zd: nullptr!\n", i);
236 } else {
237 const ClassLoader* cl = interface->GetClassLoader();
238 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
239 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 }
241 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 if (!IsLoaded()) {
243 os << " class not yet loaded";
244 } else {
245 // After this point, this may have moved due to GetDirectInterface.
246 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
247 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
248 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
250 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
253 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
255 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256 }
257 if (h_this->NumStaticFields() > 0) {
258 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
259 if (h_this->IsResolved() || h_this->IsErroneous()) {
260 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
261 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
262 }
263 } else {
264 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700266 }
267 if (h_this->NumInstanceFields() > 0) {
268 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
269 if (h_this->IsResolved() || h_this->IsErroneous()) {
270 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
271 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
272 }
273 } else {
274 os << " <not yet available>";
275 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 }
277 }
278}
279
280void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 // Sanity check that the number of bits set in the reference offset bitmap
283 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700284 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700285 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 count += c->NumReferenceInstanceFieldsDuringLinking();
287 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700288 // +1 for the Class in Object.
289 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100291 // Not called within a transaction.
292 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700293 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294}
295
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
297 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700298 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
299 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 ++i;
301 }
302 if (descriptor1.find('/', i) != StringPiece::npos ||
303 descriptor2.find('/', i) != StringPiece::npos) {
304 return false;
305 } else {
306 return true;
307 }
308}
309
Ian Rogersef7d42f2014-01-06 12:55:46 -0800310bool Class::IsInSamePackage(Class* that) {
311 Class* klass1 = this;
312 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 if (klass1 == klass2) {
314 return true;
315 }
316 // Class loaders must match.
317 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
318 return false;
319 }
320 // Arrays are in the same package when their element classes are.
321 while (klass1->IsArrayClass()) {
322 klass1 = klass1->GetComponentType();
323 }
324 while (klass2->IsArrayClass()) {
325 klass2 = klass2->GetComponentType();
326 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700327 // trivial check again for array types
328 if (klass1 == klass2) {
329 return true;
330 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700332 std::string temp1, temp2;
333 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334}
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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature,
349 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
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) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700367ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature,
368 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700369 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700370 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700371 if (method != nullptr) {
372 return method;
373 }
374
375 int32_t iftable_count = GetIfTableCount();
376 IfTable* iftable = GetIfTable();
377 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700379 if (method != nullptr) {
380 return method;
381 }
382 }
383 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384}
385
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
387 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700390 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 return method;
392 }
393
394 int32_t iftable_count = GetIfTableCount();
395 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700396 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
398 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700399 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 return method;
401 }
402 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700403 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404}
405
Mathieu Chartiere401d142015-04-22 13:56:20 -0700406ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature,
407 size_t pointer_size) {
408 for (auto& method : GetDirectMethods(pointer_size)) {
409 if (name == method.GetName() && method.GetSignature() == signature) {
410 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700411 }
412 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700413 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700414}
415
Mathieu Chartiere401d142015-04-22 13:56:20 -0700416ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature,
417 size_t pointer_size) {
418 for (auto& method : GetDirectMethods(pointer_size)) {
419 if (name == method.GetName() && signature == method.GetSignature()) {
420 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 }
422 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700423 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424}
425
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
427 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429 for (auto& method : GetDirectMethods(pointer_size)) {
430 if (method.GetDexMethodIndex() == dex_method_idx) {
431 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 }
433 }
434 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700435 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436}
437
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature,
439 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700440 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 return method;
444 }
445 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447}
448
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature,
450 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700453 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700454 return method;
455 }
456 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700458}
459
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460ArtMethod* Class::FindDirectMethod(
461 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700462 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 return method;
466 }
467 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469}
470
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature,
472 size_t pointer_size) {
473 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700474 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
475 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700476 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700477 }
478 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700479 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700480}
481
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature,
483 size_t pointer_size) {
484 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700485 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
486 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 }
489 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700490 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491}
492
Mathieu Chartiere401d142015-04-22 13:56:20 -0700493ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
494 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700496 for (auto& method : GetVirtualMethods(pointer_size)) {
497 // A miranda method may have a different DexCache and is always created by linking,
498 // never *declared* in the class.
499 if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) {
500 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800501 }
502 }
503 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700504 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800505}
506
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507ArtMethod* Class::FindVirtualMethod(
508 const StringPiece& name, const StringPiece& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700509 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700510 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700511 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800512 return method;
513 }
514 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700515 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800516}
517
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518ArtMethod* Class::FindVirtualMethod(
519 const StringPiece& name, const Signature& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700520 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700522 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700523 return method;
524 }
525 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700526 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700527}
528
Mathieu Chartiere401d142015-04-22 13:56:20 -0700529ArtMethod* Class::FindVirtualMethod(
530 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700531 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700532 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700533 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800534 return method;
535 }
536 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700537 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800538}
539
Mathieu Chartiere401d142015-04-22 13:56:20 -0700540ArtMethod* Class::FindClassInitializer(size_t pointer_size) {
541 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
542 if (method.IsClassInitializer()) {
543 DCHECK_STREQ(method.GetName(), "<clinit>");
544 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
545 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700546 }
547 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700548 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700549}
550
Brian Carlstromea46f952013-07-30 01:26:50 -0700551ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 // Is the field in this class?
553 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700555 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700556 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 return f;
558 }
559 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700560 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561}
562
Brian Carlstromea46f952013-07-30 01:26:50 -0700563ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 if (GetDexCache() == dex_cache) {
565 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700566 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800567 if (f->GetDexFieldIndex() == dex_field_idx) {
568 return f;
569 }
570 }
571 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700572 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573}
574
Brian Carlstromea46f952013-07-30 01:26:50 -0700575ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800576 // Is the field in this class, or any of its superclasses?
577 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700578 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700579 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700580 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800581 return f;
582 }
583 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700584 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800585}
586
Brian Carlstromea46f952013-07-30 01:26:50 -0700587ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588 // Is the field in this class, or any of its superclasses?
589 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700590 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700591 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700592 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593 return f;
594 }
595 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700596 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800597}
598
Brian Carlstromea46f952013-07-30 01:26:50 -0700599ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700600 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700602 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700603 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604 return f;
605 }
606 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700607 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608}
609
Brian Carlstromea46f952013-07-30 01:26:50 -0700610ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 if (dex_cache == GetDexCache()) {
612 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700613 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 if (f->GetDexFieldIndex() == dex_field_idx) {
615 return f;
616 }
617 }
618 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700619 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620}
621
Mathieu Chartierf8322842014-05-16 10:59:25 -0700622ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
623 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624 // Is the field in this class (or its interfaces), or any of its
625 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700626 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700628 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700629 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800630 return f;
631 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700632 // Wrap k incase it moves during GetDirectInterface.
633 StackHandleScope<1> hs(self);
634 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800635 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700636 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800637 StackHandleScope<1> hs2(self);
638 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700639 f = FindStaticField(self, interface, name, type);
640 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 return f;
642 }
643 }
644 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700645 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800646}
647
Mathieu Chartierf8322842014-05-16 10:59:25 -0700648ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
649 uint32_t dex_field_idx) {
650 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700652 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700653 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800654 return f;
655 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700656 // Wrap k incase it moves during GetDirectInterface.
657 StackHandleScope<1> hs(self);
658 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800659 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700660 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800661 StackHandleScope<1> hs2(self);
662 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700663 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
664 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800665 return f;
666 }
667 }
668 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700669 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800670}
671
Mathieu Chartierf8322842014-05-16 10:59:25 -0700672ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
673 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700675 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800676 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700677 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700678 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800679 return f;
680 }
681 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700682 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683 return f;
684 }
685 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700686 StackHandleScope<1> hs(self);
687 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
688 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800689 StackHandleScope<1> hs2(self);
690 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700691 f = interface->FindStaticField(self, interface, name, type);
692 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800693 return f;
694 }
695 }
696 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700697 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800698}
699
Mathieu Chartiere401d142015-04-22 13:56:20 -0700700void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) {
701 DCHECK(IsVerified());
702 for (auto& m : GetDirectMethods(pointer_size)) {
703 if (!m.IsNative() && !m.IsAbstract()) {
704 m.SetPreverified();
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200705 }
706 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700707 for (auto& m : GetVirtualMethods(pointer_size)) {
708 if (!m.IsNative() && !m.IsAbstract()) {
709 m.SetPreverified();
710 }
711 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200712}
713
Ian Rogers1ff3c982014-08-12 02:30:58 -0700714const char* Class::GetDescriptor(std::string* storage) {
715 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700716 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700717 } else if (IsArrayClass()) {
718 return GetArrayDescriptor(storage);
719 } else if (IsProxyClass()) {
720 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
721 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700722 } else {
723 const DexFile& dex_file = GetDexFile();
724 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
725 return dex_file.GetTypeDescriptor(type_id);
726 }
727}
728
Ian Rogers1ff3c982014-08-12 02:30:58 -0700729const char* Class::GetArrayDescriptor(std::string* storage) {
730 std::string temp;
731 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
732 *storage = "[";
733 *storage += elem_desc;
734 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700735}
736
737const DexFile::ClassDef* Class::GetClassDef() {
738 uint16_t class_def_idx = GetDexClassDefIndex();
739 if (class_def_idx == DexFile::kDexNoIndex16) {
740 return nullptr;
741 }
742 return &GetDexFile().GetClassDef(class_def_idx);
743}
744
Mathieu Chartierf8322842014-05-16 10:59:25 -0700745uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
746 DCHECK(!IsPrimitive());
747 DCHECK(!IsArrayClass());
748 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
749}
750
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700751mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700752 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700753 DCHECK(klass.Get() != nullptr);
754 DCHECK(!klass->IsPrimitive());
755 if (klass->IsArrayClass()) {
756 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
757 if (idx == 0) {
758 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
759 } else {
760 DCHECK_EQ(1U, idx);
761 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
762 }
763 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700764 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700765 DCHECK(interfaces != nullptr);
766 return interfaces->Get(idx);
767 } else {
768 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
769 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
770 if (interface == nullptr) {
771 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
772 klass.Get());
773 CHECK(interface != nullptr || self->IsExceptionPending());
774 }
775 return interface;
776 }
777}
778
779const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700780 const DexFile& dex_file = GetDexFile();
781 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200782 if (dex_class_def == nullptr) {
783 // Generated classes have no class def.
784 return nullptr;
785 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700786 return dex_file.GetSourceFile(*dex_class_def);
787}
788
789std::string Class::GetLocation() {
790 mirror::DexCache* dex_cache = GetDexCache();
791 if (dex_cache != nullptr && !IsProxyClass()) {
792 return dex_cache->GetLocation()->ToModifiedUtf8();
793 }
794 // Arrays and proxies are generated and have no corresponding dex file location.
795 return "generated class";
796}
797
798const DexFile::TypeList* Class::GetInterfaceTypeList() {
799 const DexFile::ClassDef* class_def = GetClassDef();
800 if (class_def == nullptr) {
801 return nullptr;
802 }
803 return GetDexFile().GetInterfacesList(*class_def);
804}
805
Mathieu Chartiere401d142015-04-22 13:56:20 -0700806void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize],
807 size_t pointer_size) {
808 for (size_t i = 0; i < kImtSize; i++) {
809 auto method = methods[i];
810 DCHECK(method != nullptr);
Mathieu Chartier4edd8472015-06-01 10:47:36 -0700811 SetEmbeddedImTableEntry(i, method, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700812 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700813 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700814 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700815 const size_t table_length = table->GetLength();
816 SetEmbeddedVTableLength(table_length);
817 for (size_t i = 0; i < table_length; i++) {
818 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700819 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700820 // Keep java.lang.Object class's vtable around for since it's easier
821 // to be reused by array classes during their linking.
822 if (!IsObjectClass()) {
823 SetVTable(nullptr);
824 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700825}
826
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700827class ReadBarrierOnNativeRootsVisitor {
828 public:
829 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
830 MemberOffset offset ATTRIBUTE_UNUSED,
831 bool is_static ATTRIBUTE_UNUSED) const {}
832
833 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
834 SHARED_REQUIRES(Locks::mutator_lock_) {
835 if (!root->IsNull()) {
836 VisitRoot(root);
837 }
838 }
839
840 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
841 SHARED_REQUIRES(Locks::mutator_lock_) {
842 mirror::Object* old_ref = root->AsMirrorPtr();
843 mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root);
844 if (old_ref != new_ref) {
845 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
846 auto* atomic_root =
847 reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
848 atomic_root->CompareExchangeStrongSequentiallyConsistent(
849 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref),
850 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref));
851 }
852 }
853};
854
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700855// The pre-fence visitor for Class::CopyOf().
856class CopyClassVisitor {
857 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100858 CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length,
859 size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize],
860 size_t pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700861 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700862 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700863 }
864
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700866 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700867 StackHandleScope<1> hs(self_);
868 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
869 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
870 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700871 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700872 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700873 // Visit all of the references to make sure there is no from space references in the native
874 // roots.
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700875 static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences(
876 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700877 }
878
879 private:
880 Thread* const self_;
881 Handle<mirror::Class>* const orig_;
882 const size_t new_length_;
883 const size_t copy_bytes_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700884 ArtMethod* const (&imt_)[mirror::Class::kImtSize];
885 const size_t pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700886 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
887};
888
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700889Class* Class::CopyOf(Thread* self, int32_t new_length,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890 ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700891 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
892 // We may get copied by a compacting GC.
893 StackHandleScope<1> hs(self);
894 Handle<mirror::Class> h_this(hs.NewHandle(this));
895 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700896 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
897 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700898 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
899 mirror::Object* new_class = kMovingClasses ?
900 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
901 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700902 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700904 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700905 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700906 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700907}
908
Vladimir Marko3481ba22015-04-13 12:22:36 +0100909bool Class::ProxyDescriptorEquals(const char* match) {
910 DCHECK(IsProxyClass());
911 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
912}
913
Mathieu Chartiere401d142015-04-22 13:56:20 -0700914// TODO: Move this to java_lang_Class.cc?
915ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700916 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917 for (auto& m : GetDirectMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700918 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700919 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700920 continue;
921 }
922 // May cause thread suspension and exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700923 if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) {
924 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700925 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700926 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700927 return nullptr;
928 }
929 }
930 return nullptr;
931}
932
Mathieu Chartiere401d142015-04-22 13:56:20 -0700933uint32_t Class::Depth() {
934 uint32_t depth = 0;
935 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
936 depth++;
937 }
938 return depth;
939}
940
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800941} // namespace mirror
942} // namespace art