blob: b2012934a2c430325774dfe5c07efd27ad514dd4 [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
Andreas Gampe99babb62015-11-02 16:20:00 -080060inline void Class::SetVerifyError(mirror::Object* error) {
61 CHECK(error != nullptr) << PrettyClass(this);
62 if (Runtime::Current()->IsActiveTransaction()) {
63 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error);
64 } else {
65 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error);
66 }
67}
68
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070069void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
70 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070071 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
72 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070074 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
75 new_status != kStatusRetired)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070076 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
77 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070078 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070079 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
80 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070081 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070082 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070083 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070084 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080085 }
Ian Rogers98379392014-02-24 16:53:16 -080086 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070087 CHECK_NE(h_this->GetStatus(), kStatusError)
88 << "Attempt to set as erroneous an already erroneous class "
89 << PrettyClass(h_this.Get());
Andreas Gampe31decb12015-08-24 21:09:05 -070090 if (VLOG_IS_ON(class_linker)) {
91 LOG(ERROR) << "Setting " << PrettyDescriptor(h_this.Get()) << " to erroneous.";
92 if (self->IsExceptionPending()) {
93 LOG(ERROR) << "Exception: " << self->GetException()->Dump();
94 }
95 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096
Andreas Gampecb086952015-11-02 16:20:00 -080097 // Remember the current exception.
98 CHECK(self->GetException() != nullptr);
99 h_this->SetVerifyError(self->GetException());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800101 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100102 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700103 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100104 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700105 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100106 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700107
108 if (!class_linker_initialized) {
109 // When the class linker is being initialized its single threaded and by definition there can be
110 // no waiters. During initialization classes may appear temporary but won't be retired as their
111 // size was statically computed.
112 } else {
113 // Classes that are being resolved or initialized need to notify waiters that the class status
114 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700115 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700116 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
117 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700118 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700119 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700120 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700121 }
122 } else {
123 CHECK_NE(new_status, kStatusRetired);
124 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700125 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126 }
127 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700128 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129}
130
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700132 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800133 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134}
135
Ian Rogersef7d42f2014-01-06 12:55:46 -0800136void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700137 if (kIsDebugBuild && new_class_size < GetClassSize()) {
138 DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail);
139 LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize();
140 LOG(FATAL) << " class=" << PrettyTypeOf(this);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700141 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100142 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700143 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144}
145
146// Return the class' name. The exact format is bizarre, but it's the specified behavior for
147// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
148// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
149// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700150String* Class::ComputeName(Handle<Class> h_this) {
151 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800152 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800153 return name;
154 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700155 std::string temp;
156 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800157 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
159 // The descriptor indicates that this is the class for
160 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700161 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162 switch (descriptor[0]) {
163 case 'Z': c_name = "boolean"; break;
164 case 'B': c_name = "byte"; break;
165 case 'C': c_name = "char"; break;
166 case 'S': c_name = "short"; break;
167 case 'I': c_name = "int"; break;
168 case 'J': c_name = "long"; break;
169 case 'F': c_name = "float"; break;
170 case 'D': c_name = "double"; break;
171 case 'V': c_name = "void"; break;
172 default:
173 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
174 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800175 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800176 } else {
177 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
178 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700179 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800180 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700181 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800182 return name;
183}
184
Ian Rogersef7d42f2014-01-06 12:55:46 -0800185void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 if ((flags & kDumpClassFullDetail) == 0) {
187 os << PrettyClass(this);
188 if ((flags & kDumpClassClassLoader) != 0) {
189 os << ' ' << GetClassLoader();
190 }
191 if ((flags & kDumpClassInitialized) != 0) {
192 os << ' ' << GetStatus();
193 }
194 os << "\n";
195 return;
196 }
197
Mathieu Chartiere401d142015-04-22 13:56:20 -0700198 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700199 StackHandleScope<2> hs(self);
200 Handle<mirror::Class> h_this(hs.NewHandle(this));
201 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700202 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700203
Ian Rogers1ff3c982014-08-12 02:30:58 -0700204 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800205 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700206 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700208 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 os << StringPrintf(" access=0x%04x.%04x\n",
210 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700211 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700212 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
213 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 }
215 if (IsArrayClass()) {
216 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
217 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700218 const size_t num_direct_interfaces = NumDirectInterfaces();
219 if (num_direct_interfaces > 0) {
220 os << " interfaces (" << num_direct_interfaces << "):\n";
221 for (size_t i = 0; i < num_direct_interfaces; ++i) {
222 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700223 if (interface == nullptr) {
224 os << StringPrintf(" %2zd: nullptr!\n", i);
225 } else {
226 const ClassLoader* cl = interface->GetClassLoader();
227 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
228 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 }
230 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700231 if (!IsLoaded()) {
232 os << " class not yet loaded";
233 } else {
234 // After this point, this may have moved due to GetDirectInterface.
235 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
236 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
237 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700238 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
239 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700241 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
242 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700243 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
244 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700245 }
246 if (h_this->NumStaticFields() > 0) {
247 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
248 if (h_this->IsResolved() || h_this->IsErroneous()) {
249 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
250 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
251 }
252 } else {
253 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800254 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700255 }
256 if (h_this->NumInstanceFields() > 0) {
257 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
258 if (h_this->IsResolved() || h_this->IsErroneous()) {
259 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
260 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
261 }
262 } else {
263 os << " <not yet available>";
264 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 }
266 }
267}
268
269void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700270 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800271 // Sanity check that the number of bits set in the reference offset bitmap
272 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700273 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700274 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800275 count += c->NumReferenceInstanceFieldsDuringLinking();
276 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700277 // +1 for the Class in Object.
278 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100280 // Not called within a transaction.
281 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700282 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283}
284
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
286 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700287 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
288 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 ++i;
290 }
291 if (descriptor1.find('/', i) != StringPiece::npos ||
292 descriptor2.find('/', i) != StringPiece::npos) {
293 return false;
294 } else {
295 return true;
296 }
297}
298
Ian Rogersef7d42f2014-01-06 12:55:46 -0800299bool Class::IsInSamePackage(Class* that) {
300 Class* klass1 = this;
301 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800302 if (klass1 == klass2) {
303 return true;
304 }
305 // Class loaders must match.
306 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
307 return false;
308 }
309 // Arrays are in the same package when their element classes are.
310 while (klass1->IsArrayClass()) {
311 klass1 = klass1->GetComponentType();
312 }
313 while (klass2->IsArrayClass()) {
314 klass2 = klass2->GetComponentType();
315 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700316 // trivial check again for array types
317 if (klass1 == klass2) {
318 return true;
319 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800320 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700321 std::string temp1, temp2;
322 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800323}
324
Ian Rogersef7d42f2014-01-06 12:55:46 -0800325bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800326 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
327}
328
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100330 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700331 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100332 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700333 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100334 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335}
336
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature,
338 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800339 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700341 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800342 return method;
343 }
344
345 int32_t iftable_count = GetIfTableCount();
346 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700347 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700349 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 return method;
351 }
352 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700353 return nullptr;
354}
355
Mathieu Chartiere401d142015-04-22 13:56:20 -0700356ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature,
357 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700358 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700360 if (method != nullptr) {
361 return method;
362 }
363
364 int32_t iftable_count = GetIfTableCount();
365 IfTable* iftable = GetIfTable();
366 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700367 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700368 if (method != nullptr) {
369 return method;
370 }
371 }
372 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373}
374
Mathieu Chartiere401d142015-04-22 13:56:20 -0700375ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
376 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700379 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 return method;
381 }
382
383 int32_t iftable_count = GetIfTableCount();
384 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700385 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
387 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700388 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 return method;
390 }
391 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700392 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393}
394
Mathieu Chartiere401d142015-04-22 13:56:20 -0700395ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature,
396 size_t pointer_size) {
397 for (auto& method : GetDirectMethods(pointer_size)) {
398 if (name == method.GetName() && method.GetSignature() == signature) {
399 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700400 }
401 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700402 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700403}
404
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature,
406 size_t pointer_size) {
407 for (auto& method : GetDirectMethods(pointer_size)) {
408 if (name == method.GetName() && signature == method.GetSignature()) {
409 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 }
411 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700412 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413}
414
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
416 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800417 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700418 for (auto& method : GetDirectMethods(pointer_size)) {
419 if (method.GetDexMethodIndex() == dex_method_idx) {
420 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 }
422 }
423 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700424 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800425}
426
Mathieu Chartiere401d142015-04-22 13:56:20 -0700427ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature,
428 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700429 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700430 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700431 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 return method;
433 }
434 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700435 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436}
437
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature,
439 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700440 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700443 return method;
444 }
445 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700447}
448
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449ArtMethod* Class::FindDirectMethod(
450 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700451 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700453 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 return method;
455 }
456 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800458}
459
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature,
461 size_t pointer_size) {
462 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700463 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
464 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700465 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700466 }
467 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700469}
470
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature,
472 size_t pointer_size) {
473 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700474 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
475 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700476 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477 }
478 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700479 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480}
481
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
483 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800484 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 for (auto& method : GetVirtualMethods(pointer_size)) {
486 // A miranda method may have a different DexCache and is always created by linking,
487 // never *declared* in the class.
488 if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) {
489 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800490 }
491 }
492 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700493 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800494}
495
Jeff Hao13e748b2015-08-25 20:44:19 +0000496ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name, size_t pointer_size) {
497 for (auto& method : GetVirtualMethods(pointer_size)) {
498 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
499 if (name == np_method->GetName()) {
500 return &method;
501 }
502 }
503 return nullptr;
504}
505
Mathieu Chartiere401d142015-04-22 13:56:20 -0700506ArtMethod* Class::FindVirtualMethod(
507 const StringPiece& name, const StringPiece& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700508 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700509 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700510 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800511 return method;
512 }
513 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700514 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800515}
516
Mathieu Chartiere401d142015-04-22 13:56:20 -0700517ArtMethod* Class::FindVirtualMethod(
518 const StringPiece& name, const Signature& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700519 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700520 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700522 return method;
523 }
524 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700525 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700526}
527
Mathieu Chartiere401d142015-04-22 13:56:20 -0700528ArtMethod* Class::FindVirtualMethod(
529 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700530 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700531 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700532 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800533 return method;
534 }
535 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700536 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537}
538
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539ArtMethod* Class::FindClassInitializer(size_t pointer_size) {
540 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
Igor Murashkin457e8742015-10-22 17:37:50 -0700541 DCHECK(reinterpret_cast<volatile void*>(&method) != nullptr);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 if (method.IsClassInitializer()) {
543 DCHECK_STREQ(method.GetName(), "<clinit>");
544 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
545 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700546 }
547 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700548 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700549}
550
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700551// Custom binary search to avoid double comparisons from std::binary_search.
552static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
553 const StringPiece& name,
554 const StringPiece& type)
555 SHARED_REQUIRES(Locks::mutator_lock_) {
556 if (fields == nullptr) {
557 return nullptr;
558 }
559 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100560 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700561 ArtField* ret = nullptr;
562 while (low < high) {
563 size_t mid = (low + high) / 2;
564 ArtField& field = fields->At(mid);
565 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
566 // verifier. There can be multiple fields with the same in the same class name due to proguard.
567 int result = StringPiece(field.GetName()).Compare(name);
568 if (result == 0) {
569 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
570 }
571 if (result < 0) {
572 low = mid + 1;
573 } else if (result > 0) {
574 high = mid;
575 } else {
576 ret = &field;
577 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 }
579 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700580 if (kIsDebugBuild) {
581 ArtField* found = nullptr;
582 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
583 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
584 found = &field;
585 break;
586 }
587 }
588 CHECK_EQ(found, ret) << "Found " << PrettyField(found) << " vs " << PrettyField(ret);
589 }
590 return ret;
591}
592
593ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
594 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
595 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596}
597
Brian Carlstromea46f952013-07-30 01:26:50 -0700598ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700600 for (ArtField& field : GetIFields()) {
601 if (field.GetDexFieldIndex() == dex_field_idx) {
602 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800603 }
604 }
605 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700606 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800607}
608
Brian Carlstromea46f952013-07-30 01:26:50 -0700609ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610 // Is the field in this class, or any of its superclasses?
611 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700612 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700613 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700614 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615 return f;
616 }
617 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700618 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800619}
620
Brian Carlstromea46f952013-07-30 01:26:50 -0700621ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800622 // Is the field in this class, or any of its superclasses?
623 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700624 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700625 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700626 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627 return f;
628 }
629 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700630 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631}
632
Brian Carlstromea46f952013-07-30 01:26:50 -0700633ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700634 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700635 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636}
637
Brian Carlstromea46f952013-07-30 01:26:50 -0700638ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700640 for (ArtField& field : GetSFields()) {
641 if (field.GetDexFieldIndex() == dex_field_idx) {
642 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800643 }
644 }
645 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700646 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647}
648
Mathieu Chartierf8322842014-05-16 10:59:25 -0700649ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
650 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651 // Is the field in this class (or its interfaces), or any of its
652 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700653 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800654 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700655 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700656 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657 return f;
658 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700659 // Wrap k incase it moves during GetDirectInterface.
660 StackHandleScope<1> hs(self);
661 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800662 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700663 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800664 StackHandleScope<1> hs2(self);
665 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700666 f = FindStaticField(self, interface, name, type);
667 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668 return f;
669 }
670 }
671 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700672 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800673}
674
Mathieu Chartierf8322842014-05-16 10:59:25 -0700675ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
676 uint32_t dex_field_idx) {
677 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800678 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700679 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700680 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800681 return f;
682 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700683 // Wrap k incase it moves during GetDirectInterface.
684 StackHandleScope<1> hs(self);
685 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800686 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700687 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800688 StackHandleScope<1> hs2(self);
689 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700690 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
691 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800692 return f;
693 }
694 }
695 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700696 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800697}
698
Mathieu Chartierf8322842014-05-16 10:59:25 -0700699ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
700 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800701 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700702 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800703 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700704 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700705 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800706 return f;
707 }
708 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700709 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800710 return f;
711 }
712 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700713 StackHandleScope<1> hs(self);
714 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
715 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800716 StackHandleScope<1> hs2(self);
717 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700718 f = interface->FindStaticField(self, interface, name, type);
719 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800720 return f;
721 }
722 }
723 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700724 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800725}
726
Mathieu Chartiere401d142015-04-22 13:56:20 -0700727void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) {
728 DCHECK(IsVerified());
729 for (auto& m : GetDirectMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700730 if (!m.IsNative() && m.IsInvokable()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700731 m.SetPreverified();
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200732 }
733 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700734 for (auto& m : GetVirtualMethods(pointer_size)) {
Alex Light9139e002015-10-09 15:59:48 -0700735 if (!m.IsNative() && m.IsInvokable()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700736 m.SetPreverified();
737 }
738 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200739}
740
Ian Rogers1ff3c982014-08-12 02:30:58 -0700741const char* Class::GetDescriptor(std::string* storage) {
742 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700743 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700744 } else if (IsArrayClass()) {
745 return GetArrayDescriptor(storage);
Igor Murashkin457e8742015-10-22 17:37:50 -0700746 } else if (IsAnyProxyClass()) {
747 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForAnyProxy(this);
Ian Rogers1ff3c982014-08-12 02:30:58 -0700748 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700749 } else {
750 const DexFile& dex_file = GetDexFile();
751 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
752 return dex_file.GetTypeDescriptor(type_id);
753 }
754}
755
Ian Rogers1ff3c982014-08-12 02:30:58 -0700756const char* Class::GetArrayDescriptor(std::string* storage) {
757 std::string temp;
758 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
759 *storage = "[";
760 *storage += elem_desc;
761 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700762}
763
764const DexFile::ClassDef* Class::GetClassDef() {
765 uint16_t class_def_idx = GetDexClassDefIndex();
766 if (class_def_idx == DexFile::kDexNoIndex16) {
767 return nullptr;
768 }
769 return &GetDexFile().GetClassDef(class_def_idx);
770}
771
Mathieu Chartierf8322842014-05-16 10:59:25 -0700772uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
773 DCHECK(!IsPrimitive());
774 DCHECK(!IsArrayClass());
775 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
776}
777
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700778mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700779 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700780 DCHECK(klass.Get() != nullptr);
781 DCHECK(!klass->IsPrimitive());
782 if (klass->IsArrayClass()) {
783 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
784 if (idx == 0) {
785 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
786 } else {
787 DCHECK_EQ(1U, idx);
788 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
789 }
Igor Murashkin457e8742015-10-22 17:37:50 -0700790 } else if (klass->IsAnyProxyClass()) {
791 // Proxies don't have a dex cache, so look at the
792 // interfaces through the magic static field "interfaces" from the proxy class itself.
793 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfacesForAnyProxy();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700794 DCHECK(interfaces != nullptr);
795 return interfaces->Get(idx);
796 } else {
797 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
798 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
799 if (interface == nullptr) {
800 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
801 klass.Get());
802 CHECK(interface != nullptr || self->IsExceptionPending());
803 }
804 return interface;
805 }
806}
807
Calin Juravle52503d82015-11-11 16:58:31 +0000808mirror::Class* Class::GetCommonSuperClass(Handle<Class> klass) {
809 DCHECK(klass.Get() != nullptr);
810 DCHECK(!klass->IsInterface());
811 DCHECK(!IsInterface());
812 mirror::Class* common_super_class = this;
813 while (!common_super_class->IsAssignableFrom(klass.Get())) {
814 common_super_class = common_super_class->GetSuperClass();
815 }
816 DCHECK(common_super_class != nullptr);
817 return common_super_class;
818}
819
Mathieu Chartierf8322842014-05-16 10:59:25 -0700820const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700821 const DexFile& dex_file = GetDexFile();
822 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200823 if (dex_class_def == nullptr) {
824 // Generated classes have no class def.
825 return nullptr;
826 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700827 return dex_file.GetSourceFile(*dex_class_def);
828}
829
830std::string Class::GetLocation() {
831 mirror::DexCache* dex_cache = GetDexCache();
Igor Murashkin457e8742015-10-22 17:37:50 -0700832 if (dex_cache != nullptr && !IsAnyProxyClass()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700833 return dex_cache->GetLocation()->ToModifiedUtf8();
834 }
835 // Arrays and proxies are generated and have no corresponding dex file location.
836 return "generated class";
837}
838
839const DexFile::TypeList* Class::GetInterfaceTypeList() {
840 const DexFile::ClassDef* class_def = GetClassDef();
841 if (class_def == nullptr) {
842 return nullptr;
843 }
844 return GetDexFile().GetInterfacesList(*class_def);
845}
846
Mathieu Chartiere401d142015-04-22 13:56:20 -0700847void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize],
848 size_t pointer_size) {
849 for (size_t i = 0; i < kImtSize; i++) {
850 auto method = methods[i];
851 DCHECK(method != nullptr);
Mathieu Chartier4edd8472015-06-01 10:47:36 -0700852 SetEmbeddedImTableEntry(i, method, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700853 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700854 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700855 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700856 const size_t table_length = table->GetLength();
857 SetEmbeddedVTableLength(table_length);
858 for (size_t i = 0; i < table_length; i++) {
859 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700860 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700861 // Keep java.lang.Object class's vtable around for since it's easier
862 // to be reused by array classes during their linking.
863 if (!IsObjectClass()) {
864 SetVTable(nullptr);
865 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700866}
867
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700868class ReadBarrierOnNativeRootsVisitor {
869 public:
870 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
871 MemberOffset offset ATTRIBUTE_UNUSED,
872 bool is_static ATTRIBUTE_UNUSED) const {}
873
874 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
875 SHARED_REQUIRES(Locks::mutator_lock_) {
876 if (!root->IsNull()) {
877 VisitRoot(root);
878 }
879 }
880
881 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
882 SHARED_REQUIRES(Locks::mutator_lock_) {
883 mirror::Object* old_ref = root->AsMirrorPtr();
884 mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root);
885 if (old_ref != new_ref) {
886 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
887 auto* atomic_root =
888 reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
889 atomic_root->CompareExchangeStrongSequentiallyConsistent(
890 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref),
891 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref));
892 }
893 }
894};
895
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700896// The pre-fence visitor for Class::CopyOf().
897class CopyClassVisitor {
898 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100899 CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length,
900 size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize],
901 size_t pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700902 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700904 }
905
Mathieu Chartiere401d142015-04-22 13:56:20 -0700906 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700907 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700908 StackHandleScope<1> hs(self_);
909 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
910 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
911 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700912 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700913 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700914 // Visit all of the references to make sure there is no from space references in the native
915 // roots.
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700916 static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences(
917 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700918 }
919
920 private:
921 Thread* const self_;
922 Handle<mirror::Class>* const orig_;
923 const size_t new_length_;
924 const size_t copy_bytes_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700925 ArtMethod* const (&imt_)[mirror::Class::kImtSize];
926 const size_t pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700927 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
928};
929
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700930Class* Class::CopyOf(Thread* self, int32_t new_length,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700931 ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700932 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
933 // We may get copied by a compacting GC.
934 StackHandleScope<1> hs(self);
935 Handle<mirror::Class> h_this(hs.NewHandle(this));
936 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700937 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
938 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700939 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
940 mirror::Object* new_class = kMovingClasses ?
941 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
942 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700943 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700944 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700945 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700946 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700947 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700948}
949
Igor Murashkin457e8742015-10-22 17:37:50 -0700950bool Class::AnyProxyDescriptorEquals(const char* match) {
951 DCHECK(IsAnyProxyClass());
952 return Runtime::Current()->GetClassLinker()->GetDescriptorForAnyProxy(this) == match;
Vladimir Marko3481ba22015-04-13 12:22:36 +0100953}
954
Mathieu Chartiere401d142015-04-22 13:56:20 -0700955// TODO: Move this to java_lang_Class.cc?
956ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700957 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700958 for (auto& m : GetDirectMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700959 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700960 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700961 continue;
962 }
963 // May cause thread suspension and exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700964 if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) {
965 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700966 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700967 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700968 return nullptr;
969 }
970 }
971 return nullptr;
972}
973
Mathieu Chartiere401d142015-04-22 13:56:20 -0700974uint32_t Class::Depth() {
975 uint32_t depth = 0;
976 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
977 depth++;
978 }
979 return depth;
980}
981
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800982} // namespace mirror
983} // namespace art