blob: f0b7bfddea474cdb75b40fc207a65d171ad2013f [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);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070047 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080048}
49
50void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070051 CHECK(!java_lang_Class_.IsNull());
52 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080053}
54
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -070055void Class::VisitRoots(RootVisitor* visitor) {
56 java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057}
58
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070059void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) {
60 Status old_status = h_this->GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070061 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
62 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070063 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070064 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
65 new_status != kStatusRetired)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070066 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get())
67 << " " << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070068 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070069 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
70 // When classes are being resolved the resolution code should hold the lock.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070071 CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070072 << "Attempt to change status of class while not holding its lock: "
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070073 << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status;
Ian Rogers7dfb28c2013-08-22 08:18:36 -070074 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075 }
Ian Rogers98379392014-02-24 16:53:16 -080076 if (UNLIKELY(new_status == kStatusError)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -070077 CHECK_NE(h_this->GetStatus(), kStatusError)
78 << "Attempt to set as erroneous an already erroneous class "
79 << PrettyClass(h_this.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Ian Rogers62d6c772013-02-27 08:32:07 -080081 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000082 StackHandleScope<1> hs(self);
83 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070084 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070085 Class* eiie_class;
86 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
87 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070088 bool throw_eiie = (old_exception.Get() == nullptr);
89 if (!throw_eiie) {
90 std::string temp;
91 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
92 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
93 }
94 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070095 // Clear exception to call FindSystemClass.
96 self->ClearException();
97 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
98 self, "Ljava/lang/ExceptionInInitializerError;");
99 CHECK(!self->IsExceptionPending());
100 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700101 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
102 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700103 Class* exception_class = old_exception->GetClass();
104 if (!eiie_class->IsAssignableFrom(exception_class)) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700105 h_this->SetVerifyErrorClass(exception_class);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700106 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800107 }
108
Ian Rogers62d6c772013-02-27 08:32:07 -0800109 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000110 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800111 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800112 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 if (Runtime::Current()->IsActiveTransaction()) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700114 h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 } else {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700116 h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100117 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700118
119 if (!class_linker_initialized) {
120 // When the class linker is being initialized its single threaded and by definition there can be
121 // no waiters. During initialization classes may appear temporary but won't be retired as their
122 // size was statically computed.
123 } else {
124 // Classes that are being resolved or initialized need to notify waiters that the class status
125 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700126 if (h_this->IsTemp()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700127 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
128 // so that they can grab the new version of the class from the class linker's table.
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700129 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700130 if (new_status == kStatusRetired || new_status == kStatusError) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700131 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700132 }
133 } else {
134 CHECK_NE(new_status, kStatusRetired);
135 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700136 h_this->NotifyAll(self);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700137 }
138 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700139 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140}
141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700143 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800144 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145}
146
Ian Rogersef7d42f2014-01-06 12:55:46 -0800147void Class::SetClassSize(uint32_t new_class_size) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 if (kIsDebugBuild && new_class_size < GetClassSize()) {
149 DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail);
150 LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize();
151 LOG(FATAL) << " class=" << PrettyTypeOf(this);
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700152 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100153 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700154 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155}
156
157// Return the class' name. The exact format is bizarre, but it's the specified behavior for
158// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
159// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
160// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161String* Class::ComputeName(Handle<Class> h_this) {
162 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800163 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800164 return name;
165 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700166 std::string temp;
167 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800168 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800169 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
170 // The descriptor indicates that this is the class for
171 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700172 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 switch (descriptor[0]) {
174 case 'Z': c_name = "boolean"; break;
175 case 'B': c_name = "byte"; break;
176 case 'C': c_name = "char"; break;
177 case 'S': c_name = "short"; break;
178 case 'I': c_name = "int"; break;
179 case 'J': c_name = "long"; break;
180 case 'F': c_name = "float"; break;
181 case 'D': c_name = "double"; break;
182 case 'V': c_name = "void"; break;
183 default:
184 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
185 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800186 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 } else {
188 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
189 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700190 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800191 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700192 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 return name;
194}
195
Ian Rogersef7d42f2014-01-06 12:55:46 -0800196void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 if ((flags & kDumpClassFullDetail) == 0) {
198 os << PrettyClass(this);
199 if ((flags & kDumpClassClassLoader) != 0) {
200 os << ' ' << GetClassLoader();
201 }
202 if ((flags & kDumpClassInitialized) != 0) {
203 os << ' ' << GetStatus();
204 }
205 os << "\n";
206 return;
207 }
208
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 Thread* const self = Thread::Current();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700210 StackHandleScope<2> hs(self);
211 Handle<mirror::Class> h_this(hs.NewHandle(this));
212 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700214
Ian Rogers1ff3c982014-08-12 02:30:58 -0700215 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700217 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700219 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800220 os << StringPrintf(" access=0x%04x.%04x\n",
221 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700222 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700223 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
224 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 }
226 if (IsArrayClass()) {
227 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
228 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700229 const size_t num_direct_interfaces = NumDirectInterfaces();
230 if (num_direct_interfaces > 0) {
231 os << " interfaces (" << num_direct_interfaces << "):\n";
232 for (size_t i = 0; i < num_direct_interfaces; ++i) {
233 Class* interface = GetDirectInterface(self, h_this, i);
Andreas Gampe16f149c2015-03-23 10:10:20 -0700234 if (interface == nullptr) {
235 os << StringPrintf(" %2zd: nullptr!\n", i);
236 } else {
237 const ClassLoader* cl = interface->GetClassLoader();
238 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
239 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800240 }
241 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700242 if (!IsLoaded()) {
243 os << " class not yet loaded";
244 } else {
245 // After this point, this may have moved due to GetDirectInterface.
246 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
247 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
248 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
250 h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700252 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
253 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(
255 h_this->GetDirectMethod(i, image_pointer_size)).c_str());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700256 }
257 if (h_this->NumStaticFields() > 0) {
258 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
259 if (h_this->IsResolved() || h_this->IsErroneous()) {
260 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
261 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
262 }
263 } else {
264 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800265 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700266 }
267 if (h_this->NumInstanceFields() > 0) {
268 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
269 if (h_this->IsResolved() || h_this->IsErroneous()) {
270 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
271 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
272 }
273 } else {
274 os << " <not yet available>";
275 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800276 }
277 }
278}
279
280void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282 // Sanity check that the number of bits set in the reference offset bitmap
283 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700284 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700285 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 count += c->NumReferenceInstanceFieldsDuringLinking();
287 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700288 // +1 for the Class in Object.
289 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800290 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100291 // Not called within a transaction.
292 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700293 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294}
295
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800296bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
297 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700298 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
299 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800300 ++i;
301 }
302 if (descriptor1.find('/', i) != StringPiece::npos ||
303 descriptor2.find('/', i) != StringPiece::npos) {
304 return false;
305 } else {
306 return true;
307 }
308}
309
Ian Rogersef7d42f2014-01-06 12:55:46 -0800310bool Class::IsInSamePackage(Class* that) {
311 Class* klass1 = this;
312 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800313 if (klass1 == klass2) {
314 return true;
315 }
316 // Class loaders must match.
317 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
318 return false;
319 }
320 // Arrays are in the same package when their element classes are.
321 while (klass1->IsArrayClass()) {
322 klass1 = klass1->GetComponentType();
323 }
324 while (klass2->IsArrayClass()) {
325 klass2 = klass2->GetComponentType();
326 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700327 // trivial check again for array types
328 if (klass1 == klass2) {
329 return true;
330 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800331 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700332 std::string temp1, temp2;
333 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800334}
335
Ian Rogersef7d42f2014-01-06 12:55:46 -0800336bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800337 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
338}
339
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100341 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700342 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100343 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700344 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100345 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800346}
347
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature,
349 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800350 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700352 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 return method;
354 }
355
356 int32_t iftable_count = GetIfTableCount();
357 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700358 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700360 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 return method;
362 }
363 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700364 return nullptr;
365}
366
Mathieu Chartiere401d142015-04-22 13:56:20 -0700367ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature,
368 size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700369 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700370 ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700371 if (method != nullptr) {
372 return method;
373 }
374
375 int32_t iftable_count = GetIfTableCount();
376 IfTable* iftable = GetIfTable();
377 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700378 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700379 if (method != nullptr) {
380 return method;
381 }
382 }
383 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800384}
385
Mathieu Chartiere401d142015-04-22 13:56:20 -0700386ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
387 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 // Check the current class before checking the interfaces.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700389 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700390 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 return method;
392 }
393
394 int32_t iftable_count = GetIfTableCount();
395 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700396 for (int32_t i = 0; i < iftable_count; ++i) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(
398 dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700399 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800400 return method;
401 }
402 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700403 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800404}
405
Mathieu Chartiere401d142015-04-22 13:56:20 -0700406ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature,
407 size_t pointer_size) {
408 for (auto& method : GetDirectMethods(pointer_size)) {
409 if (name == method.GetName() && method.GetSignature() == signature) {
410 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700411 }
412 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700413 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700414}
415
Mathieu Chartiere401d142015-04-22 13:56:20 -0700416ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature,
417 size_t pointer_size) {
418 for (auto& method : GetDirectMethods(pointer_size)) {
419 if (name == method.GetName() && signature == method.GetSignature()) {
420 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 }
422 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700423 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800424}
425
Mathieu Chartiere401d142015-04-22 13:56:20 -0700426ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
427 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800428 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700429 for (auto& method : GetDirectMethods(pointer_size)) {
430 if (method.GetDexMethodIndex() == dex_method_idx) {
431 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800432 }
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 StringPiece& 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 Rogers2dd0e2c2013-01-24 12:42:14 -0800443 return method;
444 }
445 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800447}
448
Mathieu Chartiere401d142015-04-22 13:56:20 -0700449ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature,
450 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(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700453 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700454 return method;
455 }
456 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700457 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700458}
459
Mathieu Chartiere401d142015-04-22 13:56:20 -0700460ArtMethod* Class::FindDirectMethod(
461 const DexCache* dex_cache, uint32_t dex_method_idx, 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(dex_cache, dex_method_idx, 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::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature,
472 size_t pointer_size) {
473 for (auto& method : GetVirtualMethods(pointer_size)) {
474 if (name == method.GetName() && method.GetSignature() == signature) {
475 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700476 }
477 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700478 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700479}
480
Mathieu Chartiere401d142015-04-22 13:56:20 -0700481ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature,
482 size_t pointer_size) {
483 for (auto& method : GetVirtualMethods(pointer_size)) {
484 if (name == method.GetName() && signature == method.GetSignature()) {
485 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800486 }
487 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700488 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800489}
490
Mathieu Chartiere401d142015-04-22 13:56:20 -0700491ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx,
492 size_t pointer_size) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800493 if (GetDexCache() == dex_cache) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700494 for (auto& method : GetVirtualMethods(pointer_size)) {
495 // A miranda method may have a different DexCache and is always created by linking,
496 // never *declared* in the class.
497 if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) {
498 return &method;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800499 }
500 }
501 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700502 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800503}
504
Mathieu Chartiere401d142015-04-22 13:56:20 -0700505ArtMethod* Class::FindVirtualMethod(
506 const StringPiece& name, const StringPiece& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700507 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700508 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700509 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800510 return method;
511 }
512 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700513 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800514}
515
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516ArtMethod* Class::FindVirtualMethod(
517 const StringPiece& name, const Signature& signature, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700518 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700519 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700520 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700521 return method;
522 }
523 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700524 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700525}
526
Mathieu Chartiere401d142015-04-22 13:56:20 -0700527ArtMethod* Class::FindVirtualMethod(
528 const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700529 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700530 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700531 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800532 return method;
533 }
534 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700535 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800536}
537
Mathieu Chartiere401d142015-04-22 13:56:20 -0700538ArtMethod* Class::FindClassInitializer(size_t pointer_size) {
539 for (ArtMethod& method : GetDirectMethods(pointer_size)) {
540 if (method.IsClassInitializer()) {
541 DCHECK_STREQ(method.GetName(), "<clinit>");
542 DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V");
543 return &method;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700544 }
545 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700546 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700547}
548
Brian Carlstromea46f952013-07-30 01:26:50 -0700549ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800550 // Is the field in this class?
551 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700553 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700554 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800555 return f;
556 }
557 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700558 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800559}
560
Brian Carlstromea46f952013-07-30 01:26:50 -0700561ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800562 if (GetDexCache() == dex_cache) {
563 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 if (f->GetDexFieldIndex() == dex_field_idx) {
566 return f;
567 }
568 }
569 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700570 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571}
572
Brian Carlstromea46f952013-07-30 01:26:50 -0700573ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800574 // Is the field in this class, or any of its superclasses?
575 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700576 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700577 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700578 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800579 return f;
580 }
581 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700582 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583}
584
Brian Carlstromea46f952013-07-30 01:26:50 -0700585ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 // Is the field in this class, or any of its superclasses?
587 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700588 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700589 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700590 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 return f;
592 }
593 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700594 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800595}
596
Brian Carlstromea46f952013-07-30 01:26:50 -0700597ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700598 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700600 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700601 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602 return f;
603 }
604 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700605 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800606}
607
Brian Carlstromea46f952013-07-30 01:26:50 -0700608ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609 if (dex_cache == GetDexCache()) {
610 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700611 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800612 if (f->GetDexFieldIndex() == dex_field_idx) {
613 return f;
614 }
615 }
616 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700617 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618}
619
Mathieu Chartierf8322842014-05-16 10:59:25 -0700620ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
621 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800622 // Is the field in this class (or its interfaces), or any of its
623 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700624 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700626 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700627 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 return f;
629 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700630 // Wrap k incase it moves during GetDirectInterface.
631 StackHandleScope<1> hs(self);
632 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800633 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700634 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800635 StackHandleScope<1> hs2(self);
636 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700637 f = FindStaticField(self, interface, name, type);
638 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 return f;
640 }
641 }
642 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700643 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644}
645
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
647 uint32_t dex_field_idx) {
648 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800649 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700650 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700651 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800652 return f;
653 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700654 // Wrap k incase it moves during GetDirectInterface.
655 StackHandleScope<1> hs(self);
656 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800657 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700658 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800659 StackHandleScope<1> hs2(self);
660 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700661 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
662 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800663 return f;
664 }
665 }
666 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700667 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668}
669
Mathieu Chartierf8322842014-05-16 10:59:25 -0700670ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
671 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800672 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700673 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800674 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700675 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700676 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800677 return f;
678 }
679 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700680 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800681 return f;
682 }
683 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700684 StackHandleScope<1> hs(self);
685 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
686 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800687 StackHandleScope<1> hs2(self);
688 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700689 f = interface->FindStaticField(self, interface, name, type);
690 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800691 return f;
692 }
693 }
694 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700695 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800696}
697
Mathieu Chartiere401d142015-04-22 13:56:20 -0700698void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) {
699 DCHECK(IsVerified());
700 for (auto& m : GetDirectMethods(pointer_size)) {
701 if (!m.IsNative() && !m.IsAbstract()) {
702 m.SetPreverified();
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200703 }
704 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700705 for (auto& m : GetVirtualMethods(pointer_size)) {
706 if (!m.IsNative() && !m.IsAbstract()) {
707 m.SetPreverified();
708 }
709 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200710}
711
Ian Rogers1ff3c982014-08-12 02:30:58 -0700712const char* Class::GetDescriptor(std::string* storage) {
713 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700714 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700715 } else if (IsArrayClass()) {
716 return GetArrayDescriptor(storage);
717 } else if (IsProxyClass()) {
718 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
719 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700720 } else {
721 const DexFile& dex_file = GetDexFile();
722 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
723 return dex_file.GetTypeDescriptor(type_id);
724 }
725}
726
Ian Rogers1ff3c982014-08-12 02:30:58 -0700727const char* Class::GetArrayDescriptor(std::string* storage) {
728 std::string temp;
729 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
730 *storage = "[";
731 *storage += elem_desc;
732 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700733}
734
735const DexFile::ClassDef* Class::GetClassDef() {
736 uint16_t class_def_idx = GetDexClassDefIndex();
737 if (class_def_idx == DexFile::kDexNoIndex16) {
738 return nullptr;
739 }
740 return &GetDexFile().GetClassDef(class_def_idx);
741}
742
Mathieu Chartierf8322842014-05-16 10:59:25 -0700743uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
744 DCHECK(!IsPrimitive());
745 DCHECK(!IsArrayClass());
746 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
747}
748
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700749mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700750 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700751 DCHECK(klass.Get() != nullptr);
752 DCHECK(!klass->IsPrimitive());
753 if (klass->IsArrayClass()) {
754 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
755 if (idx == 0) {
756 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
757 } else {
758 DCHECK_EQ(1U, idx);
759 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
760 }
761 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700762 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700763 DCHECK(interfaces != nullptr);
764 return interfaces->Get(idx);
765 } else {
766 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
767 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
768 if (interface == nullptr) {
769 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
770 klass.Get());
771 CHECK(interface != nullptr || self->IsExceptionPending());
772 }
773 return interface;
774 }
775}
776
777const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700778 const DexFile& dex_file = GetDexFile();
779 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200780 if (dex_class_def == nullptr) {
781 // Generated classes have no class def.
782 return nullptr;
783 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700784 return dex_file.GetSourceFile(*dex_class_def);
785}
786
787std::string Class::GetLocation() {
788 mirror::DexCache* dex_cache = GetDexCache();
789 if (dex_cache != nullptr && !IsProxyClass()) {
790 return dex_cache->GetLocation()->ToModifiedUtf8();
791 }
792 // Arrays and proxies are generated and have no corresponding dex file location.
793 return "generated class";
794}
795
796const DexFile::TypeList* Class::GetInterfaceTypeList() {
797 const DexFile::ClassDef* class_def = GetClassDef();
798 if (class_def == nullptr) {
799 return nullptr;
800 }
801 return GetDexFile().GetInterfacesList(*class_def);
802}
803
Mathieu Chartiere401d142015-04-22 13:56:20 -0700804void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize],
805 size_t pointer_size) {
806 for (size_t i = 0; i < kImtSize; i++) {
807 auto method = methods[i];
808 DCHECK(method != nullptr);
Mathieu Chartier4edd8472015-06-01 10:47:36 -0700809 SetEmbeddedImTableEntry(i, method, pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700810 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700811 PointerArray* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700812 CHECK(table != nullptr) << PrettyClass(this);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700813 const size_t table_length = table->GetLength();
814 SetEmbeddedVTableLength(table_length);
815 for (size_t i = 0; i < table_length; i++) {
816 SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700817 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700818 // Keep java.lang.Object class's vtable around for since it's easier
819 // to be reused by array classes during their linking.
820 if (!IsObjectClass()) {
821 SetVTable(nullptr);
822 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700823}
824
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700825// The pre-fence visitor for Class::CopyOf().
826class CopyClassVisitor {
827 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700828 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length,
829 size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize],
830 size_t pointer_size)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700831 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700832 copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700833 }
834
Mathieu Chartiere401d142015-04-22 13:56:20 -0700835 void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700836 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700837 StackHandleScope<1> hs(self_);
838 Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass()));
839 mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_);
840 mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700841 h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_);
Hiroshi Yamauchi5b783e62015-03-18 17:20:11 -0700842 h_new_class_obj->SetClassSize(new_length_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700843 }
844
845 private:
846 Thread* const self_;
847 Handle<mirror::Class>* const orig_;
848 const size_t new_length_;
849 const size_t copy_bytes_;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850 ArtMethod* const (&imt_)[mirror::Class::kImtSize];
851 const size_t pointer_size_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700852 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
853};
854
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700855Class* Class::CopyOf(Thread* self, int32_t new_length,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700856 ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700857 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
858 // We may get copied by a compacting GC.
859 StackHandleScope<1> hs(self);
860 Handle<mirror::Class> h_this(hs.NewHandle(this));
861 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700862 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
863 // to skip copying the tail part that we will overwrite here.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700864 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size);
865 mirror::Object* new_class = kMovingClasses ?
866 heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) :
867 heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700868 if (UNLIKELY(new_class == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700869 self->AssertPendingOOMException();
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700870 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700871 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700872 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700873}
874
Vladimir Marko3481ba22015-04-13 12:22:36 +0100875bool Class::ProxyDescriptorEquals(const char* match) {
876 DCHECK(IsProxyClass());
877 return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match;
878}
879
Mathieu Chartiere401d142015-04-22 13:56:20 -0700880// TODO: Move this to java_lang_Class.cc?
881ArtMethod* Class::GetDeclaredConstructor(
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700882 Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700883 for (auto& m : GetDirectMethods(sizeof(void*))) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700884 // Skip <clinit> which is a static constructor, as well as non constructors.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700885 if (m.IsStatic() || !m.IsConstructor()) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700886 continue;
887 }
888 // May cause thread suspension and exceptions.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700889 if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) {
890 return &m;
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700891 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700892 if (UNLIKELY(self->IsExceptionPending())) {
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700893 return nullptr;
894 }
895 }
896 return nullptr;
897}
898
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899uint32_t Class::Depth() {
900 uint32_t depth = 0;
901 for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) {
902 depth++;
903 }
904 return depth;
905}
906
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800907} // namespace mirror
908} // namespace art