blob: 77275f007bd98735d3902e0339b998eb32f9a3be [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
Ian Rogers62d6c772013-02-27 08:32:07 -080097 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000098 StackHandleScope<1> hs(self);
99 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700100 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700101 Class* eiie_class;
102 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
103 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700104 bool throw_eiie = (old_exception.Get() == nullptr);
105 if (!throw_eiie) {
106 std::string temp;
107 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
108 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
109 }
110 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700111 // Clear exception to call FindSystemClass.
112 self->ClearException();
113 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
114 self, "Ljava/lang/ExceptionInInitializerError;");
115 CHECK(!self->IsExceptionPending());
116 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700117 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
118 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700119 Class* exception_class = old_exception->GetClass();
120 if (!eiie_class->IsAssignableFrom(exception_class)) {
Andreas Gampe99babb62015-11-02 16:20:00 -0800121 // Store the exception class when this is the AoT compiler. Don't store full exceptions,
122 // as they need to be trimmed (native components are not storable in an image).
123 if (Runtime::Current()->IsAotCompiler()) {
124 h_this->SetVerifyError(exception_class);
125 } else {
126 h_this->SetVerifyError(old_exception.Get());
127 }
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700128 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 }
130
Ian Rogers62d6c772013-02-27 08:32:07 -0800131 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000132 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800134 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100135 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700136 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100137 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700138 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100139 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700140
141 if (!class_linker_initialized) {
142 // When the class linker is being initialized its single threaded and by definition there can be
143 // no waiters. During initialization classes may appear temporary but won't be retired as their
144 // size was statically computed.
145 } else {
146 // Classes that are being resolved or initialized need to notify waiters that the class status
147 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700148 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700149 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
150 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700151 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700152 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700153 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700154 }
155 } else {
156 CHECK_NE(new_status, kStatusRetired);
157 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700158 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700159 }
160 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700161 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800162}
163
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700165 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800166 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167}
168
Ian Rogersef7d42f2014-01-06 12:55:46 -0800169void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 if (kIsDebugBuild && new_class_size < GetClassSize()) {
171 DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail);
172 LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize();
173 LOG(FATAL) << " class=" << PrettyTypeOf(this);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100175 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700176 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800177}
178
179// Return the class' name. The exact format is bizarre, but it's the specified behavior for
180// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
181// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
182// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700183String* Class::ComputeName(Handle<Class> h_this) {
184 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800185 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 return name;
187 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700188 std::string temp;
189 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800190 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
192 // The descriptor indicates that this is the class for
193 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700194 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800195 switch (descriptor[0]) {
196 case 'Z': c_name = "boolean"; break;
197 case 'B': c_name = "byte"; break;
198 case 'C': c_name = "char"; break;
199 case 'S': c_name = "short"; break;
200 case 'I': c_name = "int"; break;
201 case 'J': c_name = "long"; break;
202 case 'F': c_name = "float"; break;
203 case 'D': c_name = "double"; break;
204 case 'V': c_name = "void"; break;
205 default:
206 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
207 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800208 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 } else {
210 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
211 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700212 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 return name;
216}
217
Ian Rogersef7d42f2014-01-06 12:55:46 -0800218void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 if ((flags & kDumpClassFullDetail) == 0) {
220 os << PrettyClass(this);
221 if ((flags & kDumpClassClassLoader) != 0) {
222 os << ' ' << GetClassLoader();
223 }
224 if ((flags & kDumpClassInitialized) != 0) {
225 os << ' ' << GetStatus();
226 }
227 os << "\n";
228 return;
229 }
230
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700232 StackHandleScope<2> hs(self);
233 Handle<mirror::Class> h_this(hs.NewHandle(this));
234 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700236
Ian Rogers1ff3c982014-08-12 02:30:58 -0700237 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700239 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700241 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800242 os << StringPrintf(" access=0x%04x.%04x\n",
243 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700244 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700245 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
246 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800247 }
248 if (IsArrayClass()) {
249 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
250 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700251 const size_t num_direct_interfaces = NumDirectInterfaces();
252 if (num_direct_interfaces > 0) {
253 os << " interfaces (" << num_direct_interfaces << "):\n";
254 for (size_t i = 0; i < num_direct_interfaces; ++i) {
255 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700256 if (interface == nullptr) {
257 os << StringPrintf(" %2zd: nullptr!\n", i);
258 } else {
259 const ClassLoader* cl = interface->GetClassLoader();
260 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
261 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262 }
263 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700264 if (!IsLoaded()) {
265 os << " class not yet loaded";
266 } else {
267 // After this point, this may have moved due to GetDirectInterface.
268 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
269 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
270 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700271 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
272 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700274 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
275 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700276 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
277 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700278 }
279 if (h_this->NumStaticFields() > 0) {
280 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
281 if (h_this->IsResolved() || h_this->IsErroneous()) {
282 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
283 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
284 }
285 } else {
286 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700288 }
289 if (h_this->NumInstanceFields() > 0) {
290 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
291 if (h_this->IsResolved() || h_this->IsErroneous()) {
292 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
293 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
294 }
295 } else {
296 os << " <not yet available>";
297 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800298 }
299 }
300}
301
302void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 // Sanity check that the number of bits set in the reference offset bitmap
305 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700306 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700307 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 count += c->NumReferenceInstanceFieldsDuringLinking();
309 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700310 // +1 for the Class in Object.
311 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100313 // Not called within a transaction.
314 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700315 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316}
317
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
319 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700320 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
321 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 ++i;
323 }
324 if (descriptor1.find('/', i) != StringPiece::npos ||
325 descriptor2.find('/', i) != StringPiece::npos) {
326 return false;
327 } else {
328 return true;
329 }
330}
331
Ian Rogersef7d42f2014-01-06 12:55:46 -0800332bool Class::IsInSamePackage(Class* that) {
333 Class* klass1 = this;
334 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335 if (klass1 == klass2) {
336 return true;
337 }
338 // Class loaders must match.
339 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
340 return false;
341 }
342 // Arrays are in the same package when their element classes are.
343 while (klass1->IsArrayClass()) {
344 klass1 = klass1->GetComponentType();
345 }
346 while (klass2->IsArrayClass()) {
347 klass2 = klass2->GetComponentType();
348 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700349 // trivial check again for array types
350 if (klass1 == klass2) {
351 return true;
352 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700354 std::string temp1, temp2;
355 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800356}
357
Ian Rogersef7d42f2014-01-06 12:55:46 -0800358bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800359 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
360}
361
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100363 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700364 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100365 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700366 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100367 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800368}
369
Mathieu Chartiere401d142015-04-22 13:56:20 -0700370ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature,
371 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700373 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700374 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800375 return method;
376 }
377
378 int32_t iftable_count = GetIfTableCount();
379 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700380 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700381 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700382 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 return method;
384 }
385 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700386 return nullptr;
387}
388
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature,
390 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700391 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700392 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700393 if (method != nullptr) {
394 return method;
395 }
396
397 int32_t iftable_count = GetIfTableCount();
398 IfTable* iftable = GetIfTable();
399 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700400 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700401 if (method != nullptr) {
402 return method;
403 }
404 }
405 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406}
407
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
409 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800410 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700412 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 return method;
414 }
415
416 int32_t iftable_count = GetIfTableCount();
417 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700418 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700419 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
420 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700421 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800422 return method;
423 }
424 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700425 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426}
427
Mathieu Chartiere401d142015-04-22 13:56:20 -0700428ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature,
429 size_t pointer_size) {
430 for (auto& method : GetDirectMethods(pointer_size)) {
431 if (name == method.GetName() && method.GetSignature() == signature) {
432 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700433 }
434 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700435 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700436}
437
Mathieu Chartiere401d142015-04-22 13:56:20 -0700438ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature,
439 size_t pointer_size) {
440 for (auto& method : GetDirectMethods(pointer_size)) {
441 if (name == method.GetName() && signature == method.GetSignature()) {
442 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800443 }
444 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700445 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800446}
447
Mathieu Chartiere401d142015-04-22 13:56:20 -0700448ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
449 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800450 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451 for (auto& method : GetDirectMethods(pointer_size)) {
452 if (method.GetDexMethodIndex() == dex_method_idx) {
453 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800454 }
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::FindDirectMethod(const StringPiece& name, const StringPiece& signature,
461 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700462 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700463 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465 return method;
466 }
467 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700468 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800469}
470
Mathieu Chartiere401d142015-04-22 13:56:20 -0700471ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature,
472 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700473 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700474 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700475 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700476 return method;
477 }
478 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700479 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700480}
481
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482ArtMethod* Class::FindDirectMethod(
483 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700484 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700486 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487 return method;
488 }
489 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700490 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491}
492
Mathieu Chartiere401d142015-04-22 13:56:20 -0700493ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature,
494 size_t pointer_size) {
495 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700496 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
497 if (name == np_method->GetName() && np_method->GetSignature() == signature) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700498 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700499 }
500 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700501 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700502}
503
Mathieu Chartiere401d142015-04-22 13:56:20 -0700504ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature,
505 size_t pointer_size) {
506 for (auto& method : GetVirtualMethods(pointer_size)) {
Mathieu Chartier72156e22015-07-10 18:26:41 -0700507 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
508 if (name == np_method->GetName() && signature == np_method->GetSignature()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700509 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800510 }
511 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700512 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800513}
514
Mathieu Chartiere401d142015-04-22 13:56:20 -0700515ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
516 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800517 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 for (auto& method : GetVirtualMethods(pointer_size)) {
519 // A miranda method may have a different DexCache and is always created by linking,
520 // never *declared* in the class.
521 if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) {
522 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523 }
524 }
525 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700526 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527}
528
Jeff Hao13e748b2015-08-25 20:44:19 +0000529ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name, size_t pointer_size) {
530 for (auto& method : GetVirtualMethods(pointer_size)) {
531 ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size);
532 if (name == np_method->GetName()) {
533 return &method;
534 }
535 }
536 return nullptr;
537}
538
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539ArtMethod* Class::FindVirtualMethod(
540 const StringPiece& name, const StringPiece& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700541 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700543 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544 return method;
545 }
546 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700547 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800548}
549
Mathieu Chartiere401d142015-04-22 13:56:20 -0700550ArtMethod* Class::FindVirtualMethod(
551 const StringPiece& name, const Signature& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700552 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700553 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700554 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700555 return method;
556 }
557 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700558 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700559}
560
Mathieu Chartiere401d142015-04-22 13:56:20 -0700561ArtMethod* Class::FindVirtualMethod(
562 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700563 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700564 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700565 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 return method;
567 }
568 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700569 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570}
571
Mathieu Chartiere401d142015-04-22 13:56:20 -0700572ArtMethod* Class::FindClassInitializer(size_t pointer_size) {
573 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
574 if (method.IsClassInitializer()) {
575 DCHECK_STREQ(method.GetName(), "<clinit>");
576 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
577 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700578 }
579 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700580 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700581}
582
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700583// Custom binary search to avoid double comparisons from std::binary_search.
584static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields,
585 const StringPiece& name,
586 const StringPiece& type)
587 SHARED_REQUIRES(Locks::mutator_lock_) {
588 if (fields == nullptr) {
589 return nullptr;
590 }
591 size_t low = 0;
Vladimir Marko35831e82015-09-11 11:59:18 +0100592 size_t high = fields->size();
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700593 ArtField* ret = nullptr;
594 while (low < high) {
595 size_t mid = (low + high) / 2;
596 ArtField& field = fields->At(mid);
597 // Fields are sorted by class, then name, then type descriptor. This is verified in dex file
598 // verifier. There can be multiple fields with the same in the same class name due to proguard.
599 int result = StringPiece(field.GetName()).Compare(name);
600 if (result == 0) {
601 result = StringPiece(field.GetTypeDescriptor()).Compare(type);
602 }
603 if (result < 0) {
604 low = mid + 1;
605 } else if (result > 0) {
606 high = mid;
607 } else {
608 ret = &field;
609 break;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610 }
611 }
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700612 if (kIsDebugBuild) {
613 ArtField* found = nullptr;
614 for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) {
615 if (name == field.GetName() && type == field.GetTypeDescriptor()) {
616 found = &field;
617 break;
618 }
619 }
620 CHECK_EQ(found, ret) << "Found " << PrettyField(found) << " vs " << PrettyField(ret);
621 }
622 return ret;
623}
624
625ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
626 // Binary search by name. Interfaces are not relevant because they can't contain instance fields.
627 return FindFieldByNameAndType(GetIFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628}
629
Brian Carlstromea46f952013-07-30 01:26:50 -0700630ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 if (GetDexCache() == dex_cache) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700632 for (ArtField& field : GetIFields()) {
633 if (field.GetDexFieldIndex() == dex_field_idx) {
634 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800635 }
636 }
637 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700638 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639}
640
Brian Carlstromea46f952013-07-30 01:26:50 -0700641ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800642 // Is the field in this class, or any of its superclasses?
643 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700644 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700645 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700646 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800647 return f;
648 }
649 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700650 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800651}
652
Brian Carlstromea46f952013-07-30 01:26:50 -0700653ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800654 // Is the field in this class, or any of its superclasses?
655 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700656 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700657 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700658 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800659 return f;
660 }
661 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700662 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800663}
664
Brian Carlstromea46f952013-07-30 01:26:50 -0700665ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700666 DCHECK(type != nullptr);
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700667 return FindFieldByNameAndType(GetSFieldsPtr(), name, type);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668}
669
Brian Carlstromea46f952013-07-30 01:26:50 -0700670ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800671 if (dex_cache == GetDexCache()) {
Mathieu Chartiere2aa3262015-10-20 18:30:03 -0700672 for (ArtField& field : GetSFields()) {
673 if (field.GetDexFieldIndex() == dex_field_idx) {
674 return &field;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800675 }
676 }
677 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700678 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800679}
680
Mathieu Chartierf8322842014-05-16 10:59:25 -0700681ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
682 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683 // Is the field in this class (or its interfaces), or any of its
684 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700685 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800686 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700687 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700688 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800689 return f;
690 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700691 // Wrap k incase it moves during GetDirectInterface.
692 StackHandleScope<1> hs(self);
693 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800694 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700695 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800696 StackHandleScope<1> hs2(self);
697 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700698 f = FindStaticField(self, interface, name, type);
699 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800700 return f;
701 }
702 }
703 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700704 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800705}
706
Mathieu Chartierf8322842014-05-16 10:59:25 -0700707ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
708 uint32_t dex_field_idx) {
709 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800710 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700711 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700712 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800713 return f;
714 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700715 // Wrap k incase it moves during GetDirectInterface.
716 StackHandleScope<1> hs(self);
717 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800718 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700719 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800720 StackHandleScope<1> hs2(self);
721 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700722 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
723 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800724 return f;
725 }
726 }
727 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700728 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800729}
730
Mathieu Chartierf8322842014-05-16 10:59:25 -0700731ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
732 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800733 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700734 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800735 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700736 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700737 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800738 return f;
739 }
740 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700741 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800742 return f;
743 }
744 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700745 StackHandleScope<1> hs(self);
746 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
747 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800748 StackHandleScope<1> hs2(self);
749 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700750 f = interface->FindStaticField(self, interface, name, type);
751 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800752 return f;
753 }
754 }
755 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700756 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800757}
758
Mathieu Chartiere401d142015-04-22 13:56:20 -0700759void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) {
760 DCHECK(IsVerified());
761 for (auto& m : GetDirectMethods(pointer_size)) {
762 if (!m.IsNative() && !m.IsAbstract()) {
763 m.SetPreverified();
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200764 }
765 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700766 for (auto& m : GetVirtualMethods(pointer_size)) {
767 if (!m.IsNative() && !m.IsAbstract()) {
768 m.SetPreverified();
769 }
770 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200771}
772
Ian Rogers1ff3c982014-08-12 02:30:58 -0700773const char* Class::GetDescriptor(std::string* storage) {
774 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700775 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700776 } else if (IsArrayClass()) {
777 return GetArrayDescriptor(storage);
778 } else if (IsProxyClass()) {
779 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
780 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700781 } else {
782 const DexFile& dex_file = GetDexFile();
783 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
784 return dex_file.GetTypeDescriptor(type_id);
785 }
786}
787
Ian Rogers1ff3c982014-08-12 02:30:58 -0700788const char* Class::GetArrayDescriptor(std::string* storage) {
789 std::string temp;
790 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
791 *storage = "[";
792 *storage += elem_desc;
793 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700794}
795
796const DexFile::ClassDef* Class::GetClassDef() {
797 uint16_t class_def_idx = GetDexClassDefIndex();
798 if (class_def_idx == DexFile::kDexNoIndex16) {
799 return nullptr;
800 }
801 return &GetDexFile().GetClassDef(class_def_idx);
802}
803
Mathieu Chartierf8322842014-05-16 10:59:25 -0700804uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
805 DCHECK(!IsPrimitive());
806 DCHECK(!IsArrayClass());
807 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
808}
809
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700810mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700811 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700812 DCHECK(klass.Get() != nullptr);
813 DCHECK(!klass->IsPrimitive());
814 if (klass->IsArrayClass()) {
815 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
816 if (idx == 0) {
817 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
818 } else {
819 DCHECK_EQ(1U, idx);
820 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
821 }
822 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700823 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700824 DCHECK(interfaces != nullptr);
825 return interfaces->Get(idx);
826 } else {
827 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
828 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
829 if (interface == nullptr) {
830 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
831 klass.Get());
832 CHECK(interface != nullptr || self->IsExceptionPending());
833 }
834 return interface;
835 }
836}
837
838const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700839 const DexFile& dex_file = GetDexFile();
840 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200841 if (dex_class_def == nullptr) {
842 // Generated classes have no class def.
843 return nullptr;
844 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700845 return dex_file.GetSourceFile(*dex_class_def);
846}
847
848std::string Class::GetLocation() {
849 mirror::DexCache* dex_cache = GetDexCache();
850 if (dex_cache != nullptr && !IsProxyClass()) {
851 return dex_cache->GetLocation()->ToModifiedUtf8();
852 }
853 // Arrays and proxies are generated and have no corresponding dex file location.
854 return "generated class";
855}
856
857const DexFile::TypeList* Class::GetInterfaceTypeList() {
858 const DexFile::ClassDef* class_def = GetClassDef();
859 if (class_def == nullptr) {
860 return nullptr;
861 }
862 return GetDexFile().GetInterfacesList(*class_def);
863}
864
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize],
866 size_t pointer_size) {
867 for (size_t i = 0; i < kImtSize; i++) {
868 auto method = methods[i];
869 DCHECK(method != nullptr);
Mathieu Chartier4edd8472015-06-01 10:47:36 -0700870 SetEmbeddedImTableEntry(i, method, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700871 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700873 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700874 const size_t table_length = table->GetLength();
875 SetEmbeddedVTableLength(table_length);
876 for (size_t i = 0; i < table_length; i++) {
877 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700878 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700879 // Keep java.lang.Object class's vtable around for since it's easier
880 // to be reused by array classes during their linking.
881 if (!IsObjectClass()) {
882 SetVTable(nullptr);
883 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700884}
885
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700886class ReadBarrierOnNativeRootsVisitor {
887 public:
888 void operator()(mirror::Object* obj ATTRIBUTE_UNUSED,
889 MemberOffset offset ATTRIBUTE_UNUSED,
890 bool is_static ATTRIBUTE_UNUSED) const {}
891
892 void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const
893 SHARED_REQUIRES(Locks::mutator_lock_) {
894 if (!root->IsNull()) {
895 VisitRoot(root);
896 }
897 }
898
899 void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const
900 SHARED_REQUIRES(Locks::mutator_lock_) {
901 mirror::Object* old_ref = root->AsMirrorPtr();
902 mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root);
903 if (old_ref != new_ref) {
904 // Update the field atomically. This may fail if mutator updates before us, but it's ok.
905 auto* atomic_root =
906 reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root);
907 atomic_root->CompareExchangeStrongSequentiallyConsistent(
908 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref),
909 mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref));
910 }
911 }
912};
913
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700914// The pre-fence visitor for Class::CopyOf().
915class CopyClassVisitor {
916 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100917 CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length,
918 size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize],
919 size_t pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700920 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700921 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700922 }
923
Mathieu Chartiere401d142015-04-22 13:56:20 -0700924 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Mathieu Chartier90443472015-07-16 20:32:27 -0700925 SHARED_REQUIRES(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700926 StackHandleScope<1> hs(self_);
927 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
928 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
929 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700930 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700931 h_new_class_obj->SetClassSize(new_length_);
Mathieu Chartier3ee25bb2015-08-10 10:13:02 -0700932 // Visit all of the references to make sure there is no from space references in the native
933 // roots.
Mathieu Chartier059ef3d2015-08-18 13:54:21 -0700934 static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences(
935 ReadBarrierOnNativeRootsVisitor(), VoidFunctor());
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700936 }
937
938 private:
939 Thread* const self_;
940 Handle<mirror::Class>* const orig_;
941 const size_t new_length_;
942 const size_t copy_bytes_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700943 ArtMethod* const (&imt_)[mirror::Class::kImtSize];
944 const size_t pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700945 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
946};
947
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700948Class* Class::CopyOf(Thread* self, int32_t new_length,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700949 ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700950 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
951 // We may get copied by a compacting GC.
952 StackHandleScope<1> hs(self);
953 Handle<mirror::Class> h_this(hs.NewHandle(this));
954 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700955 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
956 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700957 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
958 mirror::Object* new_class = kMovingClasses ?
959 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
960 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700961 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700962 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700963 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700964 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700965 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700966}
967
Vladimir Marko3481ba22015-04-13 12:22:36 +0100968bool Class::ProxyDescriptorEquals(const char* match) {
969 DCHECK(IsProxyClass());
970 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
971}
972
Mathieu Chartiere401d142015-04-22 13:56:20 -0700973// TODO: Move this to java_lang_Class.cc?
974ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700975 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700976 for (auto& m : GetDirectMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700977 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700978 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700979 continue;
980 }
981 // May cause thread suspension and exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700982 if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) {
983 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700984 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700985 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700986 return nullptr;
987 }
988 }
989 return nullptr;
990}
991
Mathieu Chartiere401d142015-04-22 13:56:20 -0700992uint32_t Class::Depth() {
993 uint32_t depth = 0;
994 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
995 depth++;
996 }
997 return depth;
998}
999
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001000} // namespace mirror
1001} // namespace art