blob: 3bea97897476f84694254562d89599d29d2bb265 [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());
Andreas Gampe31decb12015-08-24 21:09:05 -070081 if (VLOG_IS_ON(class_linker)) {
82 LOG(ERROR) << "Setting " << PrettyDescriptor(h_this.Get()) << " to erroneous.";
83 if (self->IsExceptionPending()) {
84 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
85 }
86 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087
Ian Rogers62d6c772013-02-27 08:32:07 -080088 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000089 StackHandleScope<1> hs(self);
90 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070091 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070092 Class* eiie_class;
93 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
94 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070095 bool throw_eiie = (old_exception.Get() == nullptr);
96 if (!throw_eiie) {
97 std::string temp;
98 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
99 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
100 }
101 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700102 // Clear exception to call FindSystemClass.
103 self->ClearException();
104 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
105 self, "Ljava/lang/ExceptionInInitializerError;");
106 CHECK(!self->IsExceptionPending());
107 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700108 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
109 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700110 Class* exception_class = old_exception->GetClass();
111 if (!eiie_class->IsAssignableFrom(exception_class)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700112 h_this->SetVerifyErrorClass(exception_class);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700113 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 }
115
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000117 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800118 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800119 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100120 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700121 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100122 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700123 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100124 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700125
126 if (!class_linker_initialized) {
127 // When the class linker is being initialized its single threaded and by definition there can be
128 // no waiters. During initialization classes may appear temporary but won't be retired as their
129 // size was statically computed.
130 } else {
131 // Classes that are being resolved or initialized need to notify waiters that the class status
132 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700133 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700134 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
135 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700136 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700138 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700139 }
140 } else {
141 CHECK_NE(new_status, kStatusRetired);
142 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700143 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700144 }
145 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700146 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147}
148
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700150 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800151 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152}
153
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700155 if (kIsDebugBuild && new_class_size < GetClassSize()) {
156 DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail);
157 LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize();
158 LOG(FATAL) << " class=" << PrettyTypeOf(this);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700159 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100160 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700161 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162}
163
164// Return the class' name. The exact format is bizarre, but it's the specified behavior for
165// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
166// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
167// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700168String* Class::ComputeName(Handle<Class> h_this) {
169 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800170 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171 return name;
172 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700173 std::string temp;
174 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800175 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
177 // The descriptor indicates that this is the class for
178 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700179 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 switch (descriptor[0]) {
181 case 'Z': c_name = "boolean"; break;
182 case 'B': c_name = "byte"; break;
183 case 'C': c_name = "char"; break;
184 case 'S': c_name = "short"; break;
185 case 'I': c_name = "int"; break;
186 case 'J': c_name = "long"; break;
187 case 'F': c_name = "float"; break;
188 case 'D': c_name = "double"; break;
189 case 'V': c_name = "void"; break;
190 default:
191 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
192 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800193 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 } else {
195 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
196 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700197 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800198 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700199 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 return name;
201}
202
Ian Rogersef7d42f2014-01-06 12:55:46 -0800203void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204 if ((flags & kDumpClassFullDetail) == 0) {
205 os << PrettyClass(this);
206 if ((flags & kDumpClassClassLoader) != 0) {
207 os << ' ' << GetClassLoader();
208 }
209 if ((flags & kDumpClassInitialized) != 0) {
210 os << ' ' << GetStatus();
211 }
212 os << "\n";
213 return;
214 }
215
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700217 StackHandleScope<2> hs(self);
218 Handle<mirror::Class> h_this(hs.NewHandle(this));
219 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700220 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700221
Ian Rogers1ff3c982014-08-12 02:30:58 -0700222 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700224 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700226 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800227 os << StringPrintf(" access=0x%04x.%04x\n",
228 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700229 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700230 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
231 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800232 }
233 if (IsArrayClass()) {
234 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
235 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700236 const size_t num_direct_interfaces = NumDirectInterfaces();
237 if (num_direct_interfaces > 0) {
238 os << " interfaces (" << num_direct_interfaces << "):\n";
239 for (size_t i = 0; i < num_direct_interfaces; ++i) {
240 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700241 if (interface == nullptr) {
242 os << StringPrintf(" %2zd: nullptr!\n", i);
243 } else {
244 const ClassLoader* cl = interface->GetClassLoader();
245 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
246 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 }
248 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700249 if (!IsLoaded()) {
250 os << " class not yet loaded";
251 } else {
252 // After this point, this may have moved due to GetDirectInterface.
253 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
254 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
255 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
257 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800258 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700259 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
260 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
262 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700263 }
264 if (h_this->NumStaticFields() > 0) {
265 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
266 if (h_this->IsResolved() || h_this->IsErroneous()) {
267 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
268 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
269 }
270 } else {
271 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700273 }
274 if (h_this->NumInstanceFields() > 0) {
275 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
276 if (h_this->IsResolved() || h_this->IsErroneous()) {
277 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
278 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
279 }
280 } else {
281 os << " <not yet available>";
282 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 }
284 }
285}
286
287void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 // Sanity check that the number of bits set in the reference offset bitmap
290 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700291 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700292 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293 count += c->NumReferenceInstanceFieldsDuringLinking();
294 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700295 // +1 for the Class in Object.
296 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100298 // Not called within a transaction.
299 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700300 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301}
302
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
304 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700305 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
306 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 ++i;
308 }
309 if (descriptor1.find('/', i) != StringPiece::npos ||
310 descriptor2.find('/', i) != StringPiece::npos) {
311 return false;
312 } else {
313 return true;
314 }
315}
316
Ian Rogersef7d42f2014-01-06 12:55:46 -0800317bool Class::IsInSamePackage(Class* that) {
318 Class* klass1 = this;
319 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 if (klass1 == klass2) {
321 return true;
322 }
323 // Class loaders must match.
324 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
325 return false;
326 }
327 // Arrays are in the same package when their element classes are.
328 while (klass1->IsArrayClass()) {
329 klass1 = klass1->GetComponentType();
330 }
331 while (klass2->IsArrayClass()) {
332 klass2 = klass2->GetComponentType();
333 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700334 // trivial check again for array types
335 if (klass1 == klass2) {
336 return true;
337 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800338 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700339 std::string temp1, temp2;
340 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341}
342
Ian Rogersef7d42f2014-01-06 12:55:46 -0800343bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
345}
346
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700349 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100350 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700351 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100352 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353}
354
Mathieu Chartiere401d142015-04-22 13:56:20 -0700355ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature,
356 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700359 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800360 return method;
361 }
362
363 int32_t iftable_count = GetIfTableCount();
364 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700365 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700366 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700367 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368 return method;
369 }
370 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700371 return nullptr;
372}
373
Mathieu Chartiere401d142015-04-22 13:56:20 -0700374ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature,
375 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700376 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700377 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700378 if (method != nullptr) {
379 return method;
380 }
381
382 int32_t iftable_count = GetIfTableCount();
383 IfTable* iftable = GetIfTable();
384 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700385 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700386 if (method != nullptr) {
387 return method;
388 }
389 }
390 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391}
392
Mathieu Chartiere401d142015-04-22 13:56:20 -0700393ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
394 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700397 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 return method;
399 }
400
401 int32_t iftable_count = GetIfTableCount();
402 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700403 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700404 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
405 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700406 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800407 return method;
408 }
409 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700410 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411}
412
Mathieu Chartiere401d142015-04-22 13:56:20 -0700413ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature,
414 size_t pointer_size) {
415 for (auto& method : GetDirectMethods(pointer_size)) {
416 if (name == method.GetName() && method.GetSignature() == signature) {
417 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700418 }
419 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700420 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700421}
422
Mathieu Chartiere401d142015-04-22 13:56:20 -0700423ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature,
424 size_t pointer_size) {
425 for (auto& method : GetDirectMethods(pointer_size)) {
426 if (name == method.GetName() && signature == method.GetSignature()) {
427 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428 }
429 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700430 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800431}
432
Mathieu Chartiere401d142015-04-22 13:56:20 -0700433ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
434 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 for (auto& method : GetDirectMethods(pointer_size)) {
437 if (method.GetDexMethodIndex() == dex_method_idx) {
438 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800439 }
440 }
441 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443}
444
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature,
446 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700447 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700449 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 return method;
451 }
452 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700453 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454}
455
Mathieu Chartiere401d142015-04-22 13:56:20 -0700456ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature,
457 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700459 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700460 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700461 return method;
462 }
463 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700465}
466
Mathieu Chartiere401d142015-04-22 13:56:20 -0700467ArtMethod* Class::FindDirectMethod(
468 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700469 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700471 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800472 return method;
473 }
474 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700475 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800476}
477
Mathieu Chartiere401d142015-04-22 13:56:20 -0700478ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature,
479 size_t pointer_size) {
480 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700481 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
482 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700483 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700484 }
485 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700486 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700487}
488
Mathieu Chartiere401d142015-04-22 13:56:20 -0700489ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature,
490 size_t pointer_size) {
491 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700492 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
493 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700494 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800495 }
496 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700497 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498}
499
Mathieu Chartiere401d142015-04-22 13:56:20 -0700500ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
501 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700503 for (auto& method : GetVirtualMethods(pointer_size)) {
504 // A miranda method may have a different DexCache and is always created by linking,
505 // never *declared* in the class.
506 if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) {
507 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800508 }
509 }
510 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700511 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800512}
513
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514ArtMethod* Class::FindVirtualMethod(
515 const StringPiece& name, const StringPiece& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700516 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700518 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800519 return method;
520 }
521 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700522 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523}
524
Mathieu Chartiere401d142015-04-22 13:56:20 -0700525ArtMethod* Class::FindVirtualMethod(
526 const StringPiece& name, const Signature& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700527 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700528 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700529 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700530 return method;
531 }
532 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700533 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700534}
535
Mathieu Chartiere401d142015-04-22 13:56:20 -0700536ArtMethod* Class::FindVirtualMethod(
537 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700538 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700540 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800541 return method;
542 }
543 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700544 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800545}
546
Mathieu Chartiere401d142015-04-22 13:56:20 -0700547ArtMethod* Class::FindClassInitializer(size_t pointer_size) {
548 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
549 if (method.IsClassInitializer()) {
550 DCHECK_STREQ(method.GetName(), "<clinit>");
551 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
552 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700553 }
554 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700555 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700556}
557
Brian Carlstromea46f952013-07-30 01:26:50 -0700558ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800559 // Is the field in this class?
560 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700562 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700563 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800564 return f;
565 }
566 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700567 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800568}
569
Brian Carlstromea46f952013-07-30 01:26:50 -0700570ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 if (GetDexCache() == dex_cache) {
572 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700573 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 if (f->GetDexFieldIndex() == dex_field_idx) {
575 return f;
576 }
577 }
578 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700579 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800580}
581
Brian Carlstromea46f952013-07-30 01:26:50 -0700582ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583 // Is the field in this class, or any of its superclasses?
584 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700585 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700586 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700587 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800588 return f;
589 }
590 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700591 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800592}
593
Brian Carlstromea46f952013-07-30 01:26:50 -0700594ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595 // Is the field in this class, or any of its superclasses?
596 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700597 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700598 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700599 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800600 return f;
601 }
602 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700603 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604}
605
Brian Carlstromea46f952013-07-30 01:26:50 -0700606ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700607 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800608 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700609 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700610 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800611 return f;
612 }
613 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700614 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615}
616
Brian Carlstromea46f952013-07-30 01:26:50 -0700617ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 if (dex_cache == GetDexCache()) {
619 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700620 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800621 if (f->GetDexFieldIndex() == dex_field_idx) {
622 return f;
623 }
624 }
625 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700626 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627}
628
Mathieu Chartierf8322842014-05-16 10:59:25 -0700629ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
630 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 // Is the field in this class (or its interfaces), or any of its
632 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700633 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800634 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700635 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700636 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800637 return f;
638 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700639 // Wrap k incase it moves during GetDirectInterface.
640 StackHandleScope<1> hs(self);
641 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800642 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700643 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800644 StackHandleScope<1> hs2(self);
645 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646 f = FindStaticField(self, interface, name, type);
647 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800648 return f;
649 }
650 }
651 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700652 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800653}
654
Mathieu Chartierf8322842014-05-16 10:59:25 -0700655ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
656 uint32_t dex_field_idx) {
657 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800658 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700659 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700660 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800661 return f;
662 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700663 // Wrap k incase it moves during GetDirectInterface.
664 StackHandleScope<1> hs(self);
665 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800666 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700667 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800668 StackHandleScope<1> hs2(self);
669 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700670 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
671 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800672 return f;
673 }
674 }
675 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700676 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677}
678
Mathieu Chartierf8322842014-05-16 10:59:25 -0700679ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
680 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800681 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700682 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700684 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700685 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800686 return f;
687 }
688 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700689 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800690 return f;
691 }
692 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700693 StackHandleScope<1> hs(self);
694 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
695 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800696 StackHandleScope<1> hs2(self);
697 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700698 f = interface->FindStaticField(self, interface, name, type);
699 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800700 return f;
701 }
702 }
703 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700704 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800705}
706
Mathieu Chartiere401d142015-04-22 13:56:20 -0700707void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) {
708 DCHECK(IsVerified());
709 for (auto& m : GetDirectMethods(pointer_size)) {
710 if (!m.IsNative() && !m.IsAbstract()) {
711 m.SetPreverified();
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200712 }
713 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700714 for (auto& m : GetVirtualMethods(pointer_size)) {
715 if (!m.IsNative() && !m.IsAbstract()) {
716 m.SetPreverified();
717 }
718 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200719}
720
Ian Rogers1ff3c982014-08-12 02:30:58 -0700721const char* Class::GetDescriptor(std::string* storage) {
722 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700723 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700724 } else if (IsArrayClass()) {
725 return GetArrayDescriptor(storage);
726 } else if (IsProxyClass()) {
727 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
728 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700729 } else {
730 const DexFile& dex_file = GetDexFile();
731 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
732 return dex_file.GetTypeDescriptor(type_id);
733 }
734}
735
Ian Rogers1ff3c982014-08-12 02:30:58 -0700736const char* Class::GetArrayDescriptor(std::string* storage) {
737 std::string temp;
738 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
739 *storage = "[";
740 *storage += elem_desc;
741 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700742}
743
744const DexFile::ClassDef* Class::GetClassDef() {
745 uint16_t class_def_idx = GetDexClassDefIndex();
746 if (class_def_idx == DexFile::kDexNoIndex16) {
747 return nullptr;
748 }
749 return &GetDexFile().GetClassDef(class_def_idx);
750}
751
Mathieu Chartierf8322842014-05-16 10:59:25 -0700752uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
753 DCHECK(!IsPrimitive());
754 DCHECK(!IsArrayClass());
755 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
756}
757
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700758mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700759 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700760 DCHECK(klass.Get() != nullptr);
761 DCHECK(!klass->IsPrimitive());
762 if (klass->IsArrayClass()) {
763 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
764 if (idx == 0) {
765 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
766 } else {
767 DCHECK_EQ(1U, idx);
768 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
769 }
770 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700771 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700772 DCHECK(interfaces != nullptr);
773 return interfaces->Get(idx);
774 } else {
775 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
776 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
777 if (interface == nullptr) {
778 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
779 klass.Get());
780 CHECK(interface != nullptr || self->IsExceptionPending());
781 }
782 return interface;
783 }
784}
785
786const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700787 const DexFile& dex_file = GetDexFile();
788 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200789 if (dex_class_def == nullptr) {
790 // Generated classes have no class def.
791 return nullptr;
792 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700793 return dex_file.GetSourceFile(*dex_class_def);
794}
795
796std::string Class::GetLocation() {
797 mirror::DexCache* dex_cache = GetDexCache();
798 if (dex_cache != nullptr && !IsProxyClass()) {
799 return dex_cache->GetLocation()->ToModifiedUtf8();
800 }
801 // Arrays and proxies are generated and have no corresponding dex file location.
802 return "generated class";
803}
804
805const DexFile::TypeList* Class::GetInterfaceTypeList() {
806 const DexFile::ClassDef* class_def = GetClassDef();
807 if (class_def == nullptr) {
808 return nullptr;
809 }
810 return GetDexFile().GetInterfacesList(*class_def);
811}
812
Mathieu Chartiere401d142015-04-22 13:56:20 -0700813void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize],
814 size_t pointer_size) {
815 for (size_t i = 0; i < kImtSize; i++) {
816 auto method = methods[i];
817 DCHECK(method != nullptr);
Mathieu Chartier4edd8472015-06-01 10:47:36 -0700818 SetEmbeddedImTableEntry(i, method, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700819 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700820 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700821 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700822 const size_t table_length = table->GetLength();
823 SetEmbeddedVTableLength(table_length);
824 for (size_t i = 0; i < table_length; i++) {
825 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700826 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700827 // Keep java.lang.Object class's vtable around for since it's easier
828 // to be reused by array classes during their linking.
829 if (!IsObjectClass()) {
830 SetVTable(nullptr);
831 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700832}
833
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700834class ReadBarrierOnNativeRootsVisitor {
835 public:
836 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
837 MemberOffset offset ATTRIBUTE_UNUSED,
838 bool is_static ATTRIBUTE_UNUSED) const {}
839
840 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
841 SHARED_REQUIRES(Locks::mutator_lock_) {
842 if (!root->IsNull()) {
843 VisitRoot(root);
844 }
845 }
846
847 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
848 SHARED_REQUIRES(Locks::mutator_lock_) {
849 mirror::Object* old_ref = root->AsMirrorPtr();
850 mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root);
851 if (old_ref != new_ref) {
852 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
853 auto* atomic_root =
854 reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
855 atomic_root->CompareExchangeStrongSequentiallyConsistent(
856 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref),
857 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref));
858 }
859 }
860};
861
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700862// The pre-fence visitor for Class::CopyOf().
863class CopyClassVisitor {
864 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100865 CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length,
866 size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize],
867 size_t pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700868 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700869 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700870 }
871
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700873 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700874 StackHandleScope<1> hs(self_);
875 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
876 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
877 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700878 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700879 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700880 // Visit all of the references to make sure there is no from space references in the native
881 // roots.
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700882 static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences(
883 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700884 }
885
886 private:
887 Thread* const self_;
888 Handle<mirror::Class>* const orig_;
889 const size_t new_length_;
890 const size_t copy_bytes_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700891 ArtMethod* const (&imt_)[mirror::Class::kImtSize];
892 const size_t pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700893 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
894};
895
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700896Class* Class::CopyOf(Thread* self, int32_t new_length,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700897 ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700898 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
899 // We may get copied by a compacting GC.
900 StackHandleScope<1> hs(self);
901 Handle<mirror::Class> h_this(hs.NewHandle(this));
902 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700903 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
904 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700905 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
906 mirror::Object* new_class = kMovingClasses ?
907 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
908 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700909 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700910 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700911 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700912 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700913 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700914}
915
Vladimir Marko3481ba22015-04-13 12:22:36 +0100916bool Class::ProxyDescriptorEquals(const char* match) {
917 DCHECK(IsProxyClass());
918 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
919}
920
Mathieu Chartiere401d142015-04-22 13:56:20 -0700921// TODO: Move this to java_lang_Class.cc?
922ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700923 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700924 for (auto& m : GetDirectMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700925 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700926 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700927 continue;
928 }
929 // May cause thread suspension and exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700930 if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) {
931 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700932 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700933 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700934 return nullptr;
935 }
936 }
937 return nullptr;
938}
939
Mathieu Chartiere401d142015-04-22 13:56:20 -0700940uint32_t Class::Depth() {
941 uint32_t depth = 0;
942 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
943 depth++;
944 }
945 return depth;
946}
947
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800948} // namespace mirror
949} // namespace art