blob: 228fd27f28174d10fc3e89aba71b3695a1c6806c [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);
Mathieu Chartier52a7f5c2015-08-18 18:35:52 -070047 java_lang_Class->SetClassFlags(java_lang_Class->GetClassFlags() | mirror::kClassFlagClass);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070048 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049}
50
51void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070052 CHECK(!java_lang_Class_.IsNull());
53 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080054}
55
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070056void Class::VisitRoots(RootVisitor* visitor) {
57 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080058}
59
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070060void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
61 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070062 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
63 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070064 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070065 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
66 new_status != kStatusRetired)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070067 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
68 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070069 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070070 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
71 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070072 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070074 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070075 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 }
Ian Rogers98379392014-02-24 16:53:16 -080077 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070078 CHECK_NE(h_this->GetStatus(), kStatusError)
79 << "Attempt to set as erroneous an already erroneous class "
80 << PrettyClass(h_this.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081
Ian Rogers62d6c772013-02-27 08:32:07 -080082 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000083 StackHandleScope<1> hs(self);
84 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070085 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070086 Class* eiie_class;
87 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
88 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070089 bool throw_eiie = (old_exception.Get() == nullptr);
90 if (!throw_eiie) {
91 std::string temp;
92 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
93 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
94 }
95 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070096 // Clear exception to call FindSystemClass.
97 self->ClearException();
98 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
99 self, "Ljava/lang/ExceptionInInitializerError;");
100 CHECK(!self->IsExceptionPending());
101 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700102 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
103 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700104 Class* exception_class = old_exception->GetClass();
105 if (!eiie_class->IsAssignableFrom(exception_class)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700106 h_this->SetVerifyErrorClass(exception_class);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700107 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108 }
109
Ian Rogers62d6c772013-02-27 08:32:07 -0800110 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000111 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800113 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100114 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700115 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100116 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700117 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100118 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700119
120 if (!class_linker_initialized) {
121 // When the class linker is being initialized its single threaded and by definition there can be
122 // no waiters. During initialization classes may appear temporary but won't be retired as their
123 // size was statically computed.
124 } else {
125 // Classes that are being resolved or initialized need to notify waiters that the class status
126 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700127 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700128 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
129 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700130 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700131 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700132 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700133 }
134 } else {
135 CHECK_NE(new_status, kStatusRetired);
136 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700137 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700138 }
139 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700140 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800141}
142
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700144 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800145 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800146}
147
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700149 if (kIsDebugBuild && new_class_size < GetClassSize()) {
150 DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail);
151 LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize();
152 LOG(FATAL) << " class=" << PrettyTypeOf(this);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700153 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100154 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700155 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800156}
157
158// Return the class' name. The exact format is bizarre, but it's the specified behavior for
159// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
160// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
161// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700162String* Class::ComputeName(Handle<Class> h_this) {
163 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800164 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 return name;
166 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700167 std::string temp;
168 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800169 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
171 // The descriptor indicates that this is the class for
172 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700173 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800174 switch (descriptor[0]) {
175 case 'Z': c_name = "boolean"; break;
176 case 'B': c_name = "byte"; break;
177 case 'C': c_name = "char"; break;
178 case 'S': c_name = "short"; break;
179 case 'I': c_name = "int"; break;
180 case 'J': c_name = "long"; break;
181 case 'F': c_name = "float"; break;
182 case 'D': c_name = "double"; break;
183 case 'V': c_name = "void"; break;
184 default:
185 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
186 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800187 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 } else {
189 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
190 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700191 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700193 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 return name;
195}
196
Ian Rogersef7d42f2014-01-06 12:55:46 -0800197void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 if ((flags & kDumpClassFullDetail) == 0) {
199 os << PrettyClass(this);
200 if ((flags & kDumpClassClassLoader) != 0) {
201 os << ' ' << GetClassLoader();
202 }
203 if ((flags & kDumpClassInitialized) != 0) {
204 os << ' ' << GetStatus();
205 }
206 os << "\n";
207 return;
208 }
209
Mathieu Chartiere401d142015-04-22 13:56:20 -0700210 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700211 StackHandleScope<2> hs(self);
212 Handle<mirror::Class> h_this(hs.NewHandle(this));
213 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700215
Ian Rogers1ff3c982014-08-12 02:30:58 -0700216 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700218 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700220 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 os << StringPrintf(" access=0x%04x.%04x\n",
222 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700223 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700224 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
225 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800226 }
227 if (IsArrayClass()) {
228 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
229 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700230 const size_t num_direct_interfaces = NumDirectInterfaces();
231 if (num_direct_interfaces > 0) {
232 os << " interfaces (" << num_direct_interfaces << "):\n";
233 for (size_t i = 0; i < num_direct_interfaces; ++i) {
234 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700235 if (interface == nullptr) {
236 os << StringPrintf(" %2zd: nullptr!\n", i);
237 } else {
238 const ClassLoader* cl = interface->GetClassLoader();
239 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
240 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800241 }
242 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700243 if (!IsLoaded()) {
244 os << " class not yet loaded";
245 } else {
246 // After this point, this may have moved due to GetDirectInterface.
247 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
248 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
249 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700250 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
251 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
254 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
256 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700257 }
258 if (h_this->NumStaticFields() > 0) {
259 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
260 if (h_this->IsResolved() || h_this->IsErroneous()) {
261 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
262 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
263 }
264 } else {
265 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800266 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700267 }
268 if (h_this->NumInstanceFields() > 0) {
269 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
270 if (h_this->IsResolved() || h_this->IsErroneous()) {
271 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
272 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
273 }
274 } else {
275 os << " <not yet available>";
276 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 }
278 }
279}
280
281void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700282 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 // Sanity check that the number of bits set in the reference offset bitmap
284 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700285 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700286 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 count += c->NumReferenceInstanceFieldsDuringLinking();
288 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700289 // +1 for the Class in Object.
290 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100292 // Not called within a transaction.
293 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700294 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295}
296
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
298 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700299 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
300 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 ++i;
302 }
303 if (descriptor1.find('/', i) != StringPiece::npos ||
304 descriptor2.find('/', i) != StringPiece::npos) {
305 return false;
306 } else {
307 return true;
308 }
309}
310
Ian Rogersef7d42f2014-01-06 12:55:46 -0800311bool Class::IsInSamePackage(Class* that) {
312 Class* klass1 = this;
313 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 if (klass1 == klass2) {
315 return true;
316 }
317 // Class loaders must match.
318 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
319 return false;
320 }
321 // Arrays are in the same package when their element classes are.
322 while (klass1->IsArrayClass()) {
323 klass1 = klass1->GetComponentType();
324 }
325 while (klass2->IsArrayClass()) {
326 klass2 = klass2->GetComponentType();
327 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700328 // trivial check again for array types
329 if (klass1 == klass2) {
330 return true;
331 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700333 std::string temp1, temp2;
334 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335}
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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700349ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature,
350 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700352 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700353 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 return method;
355 }
356
357 int32_t iftable_count = GetIfTableCount();
358 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700359 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700360 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700361 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 return method;
363 }
364 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700365 return nullptr;
366}
367
Mathieu Chartiere401d142015-04-22 13:56:20 -0700368ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature,
369 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700370 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700371 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700372 if (method != nullptr) {
373 return method;
374 }
375
376 int32_t iftable_count = GetIfTableCount();
377 IfTable* iftable = GetIfTable();
378 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700379 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700380 if (method != nullptr) {
381 return method;
382 }
383 }
384 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385}
386
Mathieu Chartiere401d142015-04-22 13:56:20 -0700387ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
388 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700390 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700391 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 return method;
393 }
394
395 int32_t iftable_count = GetIfTableCount();
396 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700397 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
399 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700400 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800401 return method;
402 }
403 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700404 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800405}
406
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature,
408 size_t pointer_size) {
409 for (auto& method : GetDirectMethods(pointer_size)) {
410 if (name == method.GetName() && method.GetSignature() == signature) {
411 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700412 }
413 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700414 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700415}
416
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature,
418 size_t pointer_size) {
419 for (auto& method : GetDirectMethods(pointer_size)) {
420 if (name == method.GetName() && signature == method.GetSignature()) {
421 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 }
423 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700424 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425}
426
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
428 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430 for (auto& method : GetDirectMethods(pointer_size)) {
431 if (method.GetDexMethodIndex() == dex_method_idx) {
432 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433 }
434 }
435 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700436 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437}
438
Mathieu Chartiere401d142015-04-22 13:56:20 -0700439ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature,
440 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700441 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700443 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800444 return method;
445 }
446 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800448}
449
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature,
451 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700453 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700455 return method;
456 }
457 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700459}
460
Mathieu Chartiere401d142015-04-22 13:56:20 -0700461ArtMethod* Class::FindDirectMethod(
462 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700463 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700465 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800466 return method;
467 }
468 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700469 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470}
471
Mathieu Chartiere401d142015-04-22 13:56:20 -0700472ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature,
473 size_t pointer_size) {
474 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700475 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
476 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700477 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700478 }
479 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700480 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700481}
482
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature,
484 size_t pointer_size) {
485 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700486 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
487 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489 }
490 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700491 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492}
493
Mathieu Chartiere401d142015-04-22 13:56:20 -0700494ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
495 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800496 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700497 for (auto& method : GetVirtualMethods(pointer_size)) {
498 // A miranda method may have a different DexCache and is always created by linking,
499 // never *declared* in the class.
500 if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) {
501 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502 }
503 }
504 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700505 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800506}
507
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508ArtMethod* Class::FindVirtualMethod(
509 const StringPiece& name, const StringPiece& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700510 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700512 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513 return method;
514 }
515 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700516 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800517}
518
Mathieu Chartiere401d142015-04-22 13:56:20 -0700519ArtMethod* Class::FindVirtualMethod(
520 const StringPiece& name, const Signature& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700523 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700524 return method;
525 }
526 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700527 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700528}
529
Mathieu Chartiere401d142015-04-22 13:56:20 -0700530ArtMethod* Class::FindVirtualMethod(
531 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700532 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700533 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700534 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800535 return method;
536 }
537 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700538 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539}
540
Mathieu Chartiere401d142015-04-22 13:56:20 -0700541ArtMethod* Class::FindClassInitializer(size_t pointer_size) {
542 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
543 if (method.IsClassInitializer()) {
544 DCHECK_STREQ(method.GetName(), "<clinit>");
545 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
546 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700547 }
548 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700549 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700550}
551
Brian Carlstromea46f952013-07-30 01:26:50 -0700552ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800553 // Is the field in this class?
554 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700557 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558 return f;
559 }
560 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700561 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562}
563
Brian Carlstromea46f952013-07-30 01:26:50 -0700564ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 if (GetDexCache() == dex_cache) {
566 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700567 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568 if (f->GetDexFieldIndex() == dex_field_idx) {
569 return f;
570 }
571 }
572 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700573 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574}
575
Brian Carlstromea46f952013-07-30 01:26:50 -0700576ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800577 // Is the field in this class, or any of its superclasses?
578 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700579 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700580 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700581 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800582 return f;
583 }
584 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700585 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586}
587
Brian Carlstromea46f952013-07-30 01:26:50 -0700588ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800589 // Is the field in this class, or any of its superclasses?
590 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700591 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700593 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 return f;
595 }
596 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700597 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598}
599
Brian Carlstromea46f952013-07-30 01:26:50 -0700600ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700601 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700603 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700604 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605 return f;
606 }
607 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700608 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609}
610
Brian Carlstromea46f952013-07-30 01:26:50 -0700611ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800612 if (dex_cache == GetDexCache()) {
613 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700614 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615 if (f->GetDexFieldIndex() == dex_field_idx) {
616 return f;
617 }
618 }
619 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700620 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621}
622
Mathieu Chartierf8322842014-05-16 10:59:25 -0700623ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
624 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 // Is the field in this class (or its interfaces), or any of its
626 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700627 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700629 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700630 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 return f;
632 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700633 // Wrap k incase it moves during GetDirectInterface.
634 StackHandleScope<1> hs(self);
635 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700637 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800638 StackHandleScope<1> hs2(self);
639 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700640 f = FindStaticField(self, interface, name, type);
641 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800642 return f;
643 }
644 }
645 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647}
648
Mathieu Chartierf8322842014-05-16 10:59:25 -0700649ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
650 uint32_t dex_field_idx) {
651 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700653 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700654 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800655 return f;
656 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700657 // Wrap k incase it moves during GetDirectInterface.
658 StackHandleScope<1> hs(self);
659 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700661 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800662 StackHandleScope<1> hs2(self);
663 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700664 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
665 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800666 return f;
667 }
668 }
669 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700670 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671}
672
Mathieu Chartierf8322842014-05-16 10:59:25 -0700673ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
674 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800675 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700676 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700678 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700679 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800680 return f;
681 }
682 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700683 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800684 return f;
685 }
686 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700687 StackHandleScope<1> hs(self);
688 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
689 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800690 StackHandleScope<1> hs2(self);
691 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700692 f = interface->FindStaticField(self, interface, name, type);
693 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800694 return f;
695 }
696 }
697 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700698 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800699}
700
Mathieu Chartiere401d142015-04-22 13:56:20 -0700701void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) {
702 DCHECK(IsVerified());
703 for (auto& m : GetDirectMethods(pointer_size)) {
704 if (!m.IsNative() && !m.IsAbstract()) {
705 m.SetPreverified();
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200706 }
707 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700708 for (auto& m : GetVirtualMethods(pointer_size)) {
709 if (!m.IsNative() && !m.IsAbstract()) {
710 m.SetPreverified();
711 }
712 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200713}
714
Ian Rogers1ff3c982014-08-12 02:30:58 -0700715const char* Class::GetDescriptor(std::string* storage) {
716 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700717 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700718 } else if (IsArrayClass()) {
719 return GetArrayDescriptor(storage);
720 } else if (IsProxyClass()) {
721 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
722 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700723 } else {
724 const DexFile& dex_file = GetDexFile();
725 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
726 return dex_file.GetTypeDescriptor(type_id);
727 }
728}
729
Ian Rogers1ff3c982014-08-12 02:30:58 -0700730const char* Class::GetArrayDescriptor(std::string* storage) {
731 std::string temp;
732 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
733 *storage = "[";
734 *storage += elem_desc;
735 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700736}
737
738const DexFile::ClassDef* Class::GetClassDef() {
739 uint16_t class_def_idx = GetDexClassDefIndex();
740 if (class_def_idx == DexFile::kDexNoIndex16) {
741 return nullptr;
742 }
743 return &GetDexFile().GetClassDef(class_def_idx);
744}
745
Mathieu Chartierf8322842014-05-16 10:59:25 -0700746uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
747 DCHECK(!IsPrimitive());
748 DCHECK(!IsArrayClass());
749 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
750}
751
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700752mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700753 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700754 DCHECK(klass.Get() != nullptr);
755 DCHECK(!klass->IsPrimitive());
756 if (klass->IsArrayClass()) {
757 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
758 if (idx == 0) {
759 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
760 } else {
761 DCHECK_EQ(1U, idx);
762 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
763 }
764 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700765 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700766 DCHECK(interfaces != nullptr);
767 return interfaces->Get(idx);
768 } else {
769 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
770 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
771 if (interface == nullptr) {
772 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
773 klass.Get());
774 CHECK(interface != nullptr || self->IsExceptionPending());
775 }
776 return interface;
777 }
778}
779
780const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700781 const DexFile& dex_file = GetDexFile();
782 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200783 if (dex_class_def == nullptr) {
784 // Generated classes have no class def.
785 return nullptr;
786 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700787 return dex_file.GetSourceFile(*dex_class_def);
788}
789
790std::string Class::GetLocation() {
791 mirror::DexCache* dex_cache = GetDexCache();
792 if (dex_cache != nullptr && !IsProxyClass()) {
793 return dex_cache->GetLocation()->ToModifiedUtf8();
794 }
795 // Arrays and proxies are generated and have no corresponding dex file location.
796 return "generated class";
797}
798
799const DexFile::TypeList* Class::GetInterfaceTypeList() {
800 const DexFile::ClassDef* class_def = GetClassDef();
801 if (class_def == nullptr) {
802 return nullptr;
803 }
804 return GetDexFile().GetInterfacesList(*class_def);
805}
806
Mathieu Chartiere401d142015-04-22 13:56:20 -0700807void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize],
808 size_t pointer_size) {
809 for (size_t i = 0; i < kImtSize; i++) {
810 auto method = methods[i];
811 DCHECK(method != nullptr);
Mathieu Chartier4edd8472015-06-01 10:47:36 -0700812 SetEmbeddedImTableEntry(i, method, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700813 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700814 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700815 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700816 const size_t table_length = table->GetLength();
817 SetEmbeddedVTableLength(table_length);
818 for (size_t i = 0; i < table_length; i++) {
819 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700820 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700821 // Keep java.lang.Object class's vtable around for since it's easier
822 // to be reused by array classes during their linking.
823 if (!IsObjectClass()) {
824 SetVTable(nullptr);
825 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700826}
827
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700828class ReadBarrierOnNativeRootsVisitor {
829 public:
830 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
831 MemberOffset offset ATTRIBUTE_UNUSED,
832 bool is_static ATTRIBUTE_UNUSED) const {}
833
834 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
835 SHARED_REQUIRES(Locks::mutator_lock_) {
836 if (!root->IsNull()) {
837 VisitRoot(root);
838 }
839 }
840
841 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
842 SHARED_REQUIRES(Locks::mutator_lock_) {
843 mirror::Object* old_ref = root->AsMirrorPtr();
844 mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root);
845 if (old_ref != new_ref) {
846 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
847 auto* atomic_root =
848 reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
849 atomic_root->CompareExchangeStrongSequentiallyConsistent(
850 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref),
851 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref));
852 }
853 }
854};
855
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700856// The pre-fence visitor for Class::CopyOf().
857class CopyClassVisitor {
858 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100859 CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length,
860 size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize],
861 size_t pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700862 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700863 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700864 }
865
Mathieu Chartiere401d142015-04-22 13:56:20 -0700866 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700867 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700868 StackHandleScope<1> hs(self_);
869 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
870 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
871 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700873 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700874 // Visit all of the references to make sure there is no from space references in the native
875 // roots.
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700876 static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences(
877 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700878 }
879
880 private:
881 Thread* const self_;
882 Handle<mirror::Class>* const orig_;
883 const size_t new_length_;
884 const size_t copy_bytes_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700885 ArtMethod* const (&imt_)[mirror::Class::kImtSize];
886 const size_t pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700887 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
888};
889
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700890Class* Class::CopyOf(Thread* self, int32_t new_length,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700891 ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700892 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
893 // We may get copied by a compacting GC.
894 StackHandleScope<1> hs(self);
895 Handle<mirror::Class> h_this(hs.NewHandle(this));
896 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700897 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
898 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
900 mirror::Object* new_class = kMovingClasses ?
901 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
902 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700903 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700904 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700905 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700906 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700907 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700908}
909
Vladimir Marko3481ba22015-04-13 12:22:36 +0100910bool Class::ProxyDescriptorEquals(const char* match) {
911 DCHECK(IsProxyClass());
912 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
913}
914
Mathieu Chartiere401d142015-04-22 13:56:20 -0700915// TODO: Move this to java_lang_Class.cc?
916ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700917 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700918 for (auto& m : GetDirectMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700919 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700920 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700921 continue;
922 }
923 // May cause thread suspension and exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700924 if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) {
925 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700926 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700927 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700928 return nullptr;
929 }
930 }
931 return nullptr;
932}
933
Mathieu Chartiere401d142015-04-22 13:56:20 -0700934uint32_t Class::Depth() {
935 uint32_t depth = 0;
936 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
937 depth++;
938 }
939 return depth;
940}
941
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800942} // namespace mirror
943} // namespace art