blob: 53fedab377befadcf865590128d3364b8eeebaeb [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 Chartier66c2d2d2015-08-25 14:32:32 -070047 java_lang_Class->SetClassFlags(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
Jeff Hao13e748b2015-08-25 20:44:19 +0000514ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name, size_t pointer_size) {
515 for (auto& method : GetVirtualMethods(pointer_size)) {
516 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
517 if (name == np_method->GetName()) {
518 return &method;
519 }
520 }
521 return nullptr;
522}
523
Mathieu Chartiere401d142015-04-22 13:56:20 -0700524ArtMethod* Class::FindVirtualMethod(
525 const StringPiece& name, const StringPiece& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700526 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700528 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800529 return method;
530 }
531 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700532 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533}
534
Mathieu Chartiere401d142015-04-22 13:56:20 -0700535ArtMethod* Class::FindVirtualMethod(
536 const StringPiece& name, const Signature& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700537 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700538 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700539 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700540 return method;
541 }
542 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700543 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700544}
545
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546ArtMethod* Class::FindVirtualMethod(
547 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700548 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700550 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551 return method;
552 }
553 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700554 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555}
556
Mathieu Chartiere401d142015-04-22 13:56:20 -0700557ArtMethod* Class::FindClassInitializer(size_t pointer_size) {
558 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
559 if (method.IsClassInitializer()) {
560 DCHECK_STREQ(method.GetName(), "<clinit>");
561 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
562 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700563 }
564 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700565 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700566}
567
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700568// Custom binary search to avoid double comparisons from std::binary_search.
569static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
570 const StringPiece& name,
571 const StringPiece& type)
572 SHARED_REQUIRES(Locks::mutator_lock_) {
573 if (fields == nullptr) {
574 return nullptr;
575 }
576 size_t low = 0;
577 size_t high = fields->Length();
578 ArtField* ret = nullptr;
579 while (low < high) {
580 size_t mid = (low + high) / 2;
581 ArtField& field = fields->At(mid);
582 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
583 // verifier. There can be multiple fields with the same in the same class name due to proguard.
584 int result = StringPiece(field.GetName()).Compare(name);
585 if (result == 0) {
586 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
587 }
588 if (result < 0) {
589 low = mid + 1;
590 } else if (result > 0) {
591 high = mid;
592 } else {
593 ret = &field;
594 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595 }
596 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700597 if (kIsDebugBuild) {
598 ArtField* found = nullptr;
599 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
600 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
601 found = &field;
602 break;
603 }
604 }
605 CHECK_EQ(found, ret) << "Found " << PrettyField(found) << " vs " << PrettyField(ret);
606 }
607 return ret;
608}
609
610ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
611 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
612 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800613}
614
Brian Carlstromea46f952013-07-30 01:26:50 -0700615ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800616 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700617 for (ArtField& field : GetIFields()) {
618 if (field.GetDexFieldIndex() == dex_field_idx) {
619 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 }
621 }
622 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700623 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624}
625
Brian Carlstromea46f952013-07-30 01:26:50 -0700626ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 // Is the field in this class, or any of its superclasses?
628 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700629 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700630 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700631 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 return f;
633 }
634 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700635 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636}
637
Brian Carlstromea46f952013-07-30 01:26:50 -0700638ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 // Is the field in this class, or any of its superclasses?
640 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700641 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700642 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700643 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 return f;
645 }
646 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700647 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800648}
649
Brian Carlstromea46f952013-07-30 01:26:50 -0700650ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700651 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700652 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800653}
654
Brian Carlstromea46f952013-07-30 01:26:50 -0700655ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800656 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700657 for (ArtField& field : GetSFields()) {
658 if (field.GetDexFieldIndex() == dex_field_idx) {
659 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660 }
661 }
662 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700663 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800664}
665
Mathieu Chartierf8322842014-05-16 10:59:25 -0700666ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
667 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668 // Is the field in this class (or its interfaces), or any of its
669 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700670 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700672 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700673 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674 return f;
675 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700676 // Wrap k incase it moves during GetDirectInterface.
677 StackHandleScope<1> hs(self);
678 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800679 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700680 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800681 StackHandleScope<1> hs2(self);
682 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700683 f = FindStaticField(self, interface, name, type);
684 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800685 return f;
686 }
687 }
688 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700689 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800690}
691
Mathieu Chartierf8322842014-05-16 10:59:25 -0700692ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
693 uint32_t dex_field_idx) {
694 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800695 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700696 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700697 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800698 return f;
699 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700700 // Wrap k incase it moves during GetDirectInterface.
701 StackHandleScope<1> hs(self);
702 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800703 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700704 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800705 StackHandleScope<1> hs2(self);
706 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700707 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
708 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800709 return f;
710 }
711 }
712 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700713 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800714}
715
Mathieu Chartierf8322842014-05-16 10:59:25 -0700716ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
717 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800718 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700719 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800720 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700721 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700722 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800723 return f;
724 }
725 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700726 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800727 return f;
728 }
729 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700730 StackHandleScope<1> hs(self);
731 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
732 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800733 StackHandleScope<1> hs2(self);
734 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700735 f = interface->FindStaticField(self, interface, name, type);
736 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800737 return f;
738 }
739 }
740 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700741 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800742}
743
Mathieu Chartiere401d142015-04-22 13:56:20 -0700744void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) {
745 DCHECK(IsVerified());
746 for (auto& m : GetDirectMethods(pointer_size)) {
747 if (!m.IsNative() && !m.IsAbstract()) {
748 m.SetPreverified();
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200749 }
750 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700751 for (auto& m : GetVirtualMethods(pointer_size)) {
752 if (!m.IsNative() && !m.IsAbstract()) {
753 m.SetPreverified();
754 }
755 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200756}
757
Ian Rogers1ff3c982014-08-12 02:30:58 -0700758const char* Class::GetDescriptor(std::string* storage) {
759 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700760 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700761 } else if (IsArrayClass()) {
762 return GetArrayDescriptor(storage);
763 } else if (IsProxyClass()) {
764 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
765 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700766 } else {
767 const DexFile& dex_file = GetDexFile();
768 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
769 return dex_file.GetTypeDescriptor(type_id);
770 }
771}
772
Ian Rogers1ff3c982014-08-12 02:30:58 -0700773const char* Class::GetArrayDescriptor(std::string* storage) {
774 std::string temp;
775 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
776 *storage = "[";
777 *storage += elem_desc;
778 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700779}
780
781const DexFile::ClassDef* Class::GetClassDef() {
782 uint16_t class_def_idx = GetDexClassDefIndex();
783 if (class_def_idx == DexFile::kDexNoIndex16) {
784 return nullptr;
785 }
786 return &GetDexFile().GetClassDef(class_def_idx);
787}
788
Mathieu Chartierf8322842014-05-16 10:59:25 -0700789uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
790 DCHECK(!IsPrimitive());
791 DCHECK(!IsArrayClass());
792 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
793}
794
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700795mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700796 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700797 DCHECK(klass.Get() != nullptr);
798 DCHECK(!klass->IsPrimitive());
799 if (klass->IsArrayClass()) {
800 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
801 if (idx == 0) {
802 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
803 } else {
804 DCHECK_EQ(1U, idx);
805 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
806 }
807 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700808 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700809 DCHECK(interfaces != nullptr);
810 return interfaces->Get(idx);
811 } else {
812 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
813 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
814 if (interface == nullptr) {
815 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
816 klass.Get());
817 CHECK(interface != nullptr || self->IsExceptionPending());
818 }
819 return interface;
820 }
821}
822
823const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700824 const DexFile& dex_file = GetDexFile();
825 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200826 if (dex_class_def == nullptr) {
827 // Generated classes have no class def.
828 return nullptr;
829 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700830 return dex_file.GetSourceFile(*dex_class_def);
831}
832
833std::string Class::GetLocation() {
834 mirror::DexCache* dex_cache = GetDexCache();
835 if (dex_cache != nullptr && !IsProxyClass()) {
836 return dex_cache->GetLocation()->ToModifiedUtf8();
837 }
838 // Arrays and proxies are generated and have no corresponding dex file location.
839 return "generated class";
840}
841
842const DexFile::TypeList* Class::GetInterfaceTypeList() {
843 const DexFile::ClassDef* class_def = GetClassDef();
844 if (class_def == nullptr) {
845 return nullptr;
846 }
847 return GetDexFile().GetInterfacesList(*class_def);
848}
849
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize],
851 size_t pointer_size) {
852 for (size_t i = 0; i < kImtSize; i++) {
853 auto method = methods[i];
854 DCHECK(method != nullptr);
Mathieu Chartier4edd8472015-06-01 10:47:36 -0700855 SetEmbeddedImTableEntry(i, method, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700856 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700857 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700858 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700859 const size_t table_length = table->GetLength();
860 SetEmbeddedVTableLength(table_length);
861 for (size_t i = 0; i < table_length; i++) {
862 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700863 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700864 // Keep java.lang.Object class's vtable around for since it's easier
865 // to be reused by array classes during their linking.
866 if (!IsObjectClass()) {
867 SetVTable(nullptr);
868 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700869}
870
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700871class ReadBarrierOnNativeRootsVisitor {
872 public:
873 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
874 MemberOffset offset ATTRIBUTE_UNUSED,
875 bool is_static ATTRIBUTE_UNUSED) const {}
876
877 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
878 SHARED_REQUIRES(Locks::mutator_lock_) {
879 if (!root->IsNull()) {
880 VisitRoot(root);
881 }
882 }
883
884 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
885 SHARED_REQUIRES(Locks::mutator_lock_) {
886 mirror::Object* old_ref = root->AsMirrorPtr();
887 mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root);
888 if (old_ref != new_ref) {
889 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
890 auto* atomic_root =
891 reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
892 atomic_root->CompareExchangeStrongSequentiallyConsistent(
893 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref),
894 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref));
895 }
896 }
897};
898
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700899// The pre-fence visitor for Class::CopyOf().
900class CopyClassVisitor {
901 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100902 CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length,
903 size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize],
904 size_t pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700905 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700906 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700907 }
908
Mathieu Chartiere401d142015-04-22 13:56:20 -0700909 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700910 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700911 StackHandleScope<1> hs(self_);
912 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
913 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
914 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700915 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700916 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700917 // Visit all of the references to make sure there is no from space references in the native
918 // roots.
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700919 static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences(
920 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700921 }
922
923 private:
924 Thread* const self_;
925 Handle<mirror::Class>* const orig_;
926 const size_t new_length_;
927 const size_t copy_bytes_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700928 ArtMethod* const (&imt_)[mirror::Class::kImtSize];
929 const size_t pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700930 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
931};
932
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700933Class* Class::CopyOf(Thread* self, int32_t new_length,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700934 ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700935 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
936 // We may get copied by a compacting GC.
937 StackHandleScope<1> hs(self);
938 Handle<mirror::Class> h_this(hs.NewHandle(this));
939 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700940 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
941 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700942 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
943 mirror::Object* new_class = kMovingClasses ?
944 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
945 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700946 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700947 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700948 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700949 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700950 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700951}
952
Vladimir Marko3481ba22015-04-13 12:22:36 +0100953bool Class::ProxyDescriptorEquals(const char* match) {
954 DCHECK(IsProxyClass());
955 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
956}
957
Mathieu Chartiere401d142015-04-22 13:56:20 -0700958// TODO: Move this to java_lang_Class.cc?
959ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700960 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700961 for (auto& m : GetDirectMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700962 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700963 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700964 continue;
965 }
966 // May cause thread suspension and exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700967 if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) {
968 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700969 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700970 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700971 return nullptr;
972 }
973 }
974 return nullptr;
975}
976
Mathieu Chartiere401d142015-04-22 13:56:20 -0700977uint32_t Class::Depth() {
978 uint32_t depth = 0;
979 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
980 depth++;
981 }
982 return depth;
983}
984
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800985} // namespace mirror
986} // namespace art