blob: 6f4ef60e85d2490b11e54efb1b91894c9d14a012 [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "class_linker.h"
22#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"
Ian Rogers22d5e732014-07-15 22:23:51 -070028#include "object_array-inl.h"
29#include "object-inl.h"
30#include "runtime.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "thread.h"
32#include "throwable.h"
33#include "utils.h"
34#include "well_known_classes.h"
35
36namespace art {
37namespace mirror {
38
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070039GcRoot<Class> Class::java_lang_Class_;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040
41void Class::SetClassClass(Class* java_lang_Class) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070042 CHECK(java_lang_Class_.IsNull())
43 << java_lang_Class_.Read()
Hiroshi Yamauchi4f1ebc22014-06-25 14:30:41 -070044 << " " << java_lang_Class;
Brian Carlstrom004644f2014-06-18 08:34:01 -070045 CHECK(java_lang_Class != nullptr);
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070046 java_lang_Class_ = GcRoot<Class>(java_lang_Class);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080047}
48
49void Class::ResetClass() {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070050 CHECK(!java_lang_Class_.IsNull());
51 java_lang_Class_ = GcRoot<Class>(nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080052}
53
Mathieu Chartier83c8ee02014-01-28 14:50:23 -080054void Class::VisitRoots(RootCallback* callback, void* arg) {
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080055 java_lang_Class_.VisitRootIfNonNull(callback, arg, RootInfo(kRootStickyClass));
Mathieu Chartierc528dba2013-11-26 12:00:11 -080056}
57
Ian Rogers7dfb28c2013-08-22 08:18:36 -070058void Class::SetStatus(Status new_status, Thread* self) {
59 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070060 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
61 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070062 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070063 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
64 new_status != kStatusRetired)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070065 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070066 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
69 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070070 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070071 << "Attempt to change status of class while not holding its lock: "
72 << PrettyClass(this) << " " << old_status << " -> " << new_status;
73 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 }
Ian Rogers98379392014-02-24 16:53:16 -080075 if (UNLIKELY(new_status == kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070076 CHECK_NE(GetStatus(), kStatusError)
77 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080078
Ian Rogers62d6c772013-02-27 08:32:07 -080079 // Stash current exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +000080 StackHandleScope<1> hs(self);
81 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 CHECK(old_exception.Get() != nullptr);
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070083 Class* eiie_class;
84 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
85 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070086 bool throw_eiie = (old_exception.Get() == nullptr);
87 if (!throw_eiie) {
88 std::string temp;
89 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
90 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
91 }
92 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070093 // Clear exception to call FindSystemClass.
94 self->ClearException();
95 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
96 self, "Ljava/lang/ExceptionInInitializerError;");
97 CHECK(!self->IsExceptionPending());
98 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -070099 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
100 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700101 Class* exception_class = old_exception->GetClass();
102 if (!eiie_class->IsAssignableFrom(exception_class)) {
103 SetVerifyErrorClass(exception_class);
104 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 }
106
Ian Rogers62d6c772013-02-27 08:32:07 -0800107 // Restore exception.
Nicolas Geoffray14691c52015-03-05 10:40:17 +0000108 self->SetException(old_exception.Get());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 }
Andreas Gampe575e78c2014-11-03 23:41:03 -0800110 static_assert(sizeof(Status) == sizeof(uint32_t), "Size of status not equal to uint32");
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100111 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700112 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100113 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700114 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100115 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700116
117 if (!class_linker_initialized) {
118 // When the class linker is being initialized its single threaded and by definition there can be
119 // no waiters. During initialization classes may appear temporary but won't be retired as their
120 // size was statically computed.
121 } else {
122 // Classes that are being resolved or initialized need to notify waiters that the class status
123 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
124 if (IsTemp()) {
125 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
126 // so that they can grab the new version of the class from the class linker's table.
127 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this);
128 if (new_status == kStatusRetired || new_status == kStatusError) {
129 NotifyAll(self);
130 }
131 } else {
132 CHECK_NE(new_status, kStatusRetired);
133 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
134 NotifyAll(self);
135 }
136 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700137 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138}
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700141 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Mathieu Chartier91a6dc42014-12-01 10:31:15 -0800142 SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800143}
144
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700146 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
147 DumpClass(LOG(ERROR), kDumpClassFullDetail);
148 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
149 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100150 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700151 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152}
153
154// Return the class' name. The exact format is bizarre, but it's the specified behavior for
155// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
156// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
157// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700158String* Class::ComputeName(Handle<Class> h_this) {
159 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800160 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161 return name;
162 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700163 std::string temp;
164 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800165 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800166 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
167 // The descriptor indicates that this is the class for
168 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700169 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 switch (descriptor[0]) {
171 case 'Z': c_name = "boolean"; break;
172 case 'B': c_name = "byte"; break;
173 case 'C': c_name = "char"; break;
174 case 'S': c_name = "short"; break;
175 case 'I': c_name = "int"; break;
176 case 'J': c_name = "long"; break;
177 case 'F': c_name = "float"; break;
178 case 'D': c_name = "double"; break;
179 case 'V': c_name = "void"; break;
180 default:
181 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
182 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800183 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800184 } else {
185 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
186 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700187 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800188 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700189 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 return name;
191}
192
Ian Rogersef7d42f2014-01-06 12:55:46 -0800193void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800194 if ((flags & kDumpClassFullDetail) == 0) {
195 os << PrettyClass(this);
196 if ((flags & kDumpClassClassLoader) != 0) {
197 os << ' ' << GetClassLoader();
198 }
199 if ((flags & kDumpClassInitialized) != 0) {
200 os << ' ' << GetStatus();
201 }
202 os << "\n";
203 return;
204 }
205
Mathieu Chartierf8322842014-05-16 10:59:25 -0700206 Thread* self = Thread::Current();
207 StackHandleScope<2> hs(self);
208 Handle<mirror::Class> h_this(hs.NewHandle(this));
209 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
210
Ian Rogers1ff3c982014-08-12 02:30:58 -0700211 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800212 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700213 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800214 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700215 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 os << StringPrintf(" access=0x%04x.%04x\n",
217 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700218 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700219 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
220 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 }
222 if (IsArrayClass()) {
223 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
224 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700225 const size_t num_direct_interfaces = NumDirectInterfaces();
226 if (num_direct_interfaces > 0) {
227 os << " interfaces (" << num_direct_interfaces << "):\n";
228 for (size_t i = 0; i < num_direct_interfaces; ++i) {
229 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 const ClassLoader* cl = interface->GetClassLoader();
231 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
232 }
233 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700234 if (!IsLoaded()) {
235 os << " class not yet loaded";
236 } else {
237 // After this point, this may have moved due to GetDirectInterface.
238 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
239 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
240 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
241 os << StringPrintf(" %2zd: %s\n", i,
242 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800243 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700244 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
245 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
246 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
247 }
248 if (h_this->NumStaticFields() > 0) {
249 os << " static fields (" << h_this->NumStaticFields() << " entries):\n";
250 if (h_this->IsResolved() || h_this->IsErroneous()) {
251 for (size_t i = 0; i < h_this->NumStaticFields(); ++i) {
252 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str());
253 }
254 } else {
255 os << " <not yet available>";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800256 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700257 }
258 if (h_this->NumInstanceFields() > 0) {
259 os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n";
260 if (h_this->IsResolved() || h_this->IsErroneous()) {
261 for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) {
262 os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str());
263 }
264 } else {
265 os << " <not yet available>";
266 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800267 }
268 }
269}
270
271void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) {
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700272 if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 // Sanity check that the number of bits set in the reference offset bitmap
274 // agrees with the number of references
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700275 uint32_t count = 0;
Brian Carlstrom004644f2014-06-18 08:34:01 -0700276 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800277 count += c->NumReferenceInstanceFieldsDuringLinking();
278 }
Ian Rogerscdc1aaf2014-10-09 13:21:38 -0700279 // +1 for the Class in Object.
280 CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100282 // Not called within a transaction.
283 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700284 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800285}
286
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
288 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700289 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
290 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800291 ++i;
292 }
293 if (descriptor1.find('/', i) != StringPiece::npos ||
294 descriptor2.find('/', i) != StringPiece::npos) {
295 return false;
296 } else {
297 return true;
298 }
299}
300
Ian Rogersef7d42f2014-01-06 12:55:46 -0800301bool Class::IsInSamePackage(Class* that) {
302 Class* klass1 = this;
303 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800304 if (klass1 == klass2) {
305 return true;
306 }
307 // Class loaders must match.
308 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
309 return false;
310 }
311 // Arrays are in the same package when their element classes are.
312 while (klass1->IsArrayClass()) {
313 klass1 = klass1->GetComponentType();
314 }
315 while (klass2->IsArrayClass()) {
316 klass2 = klass2->GetComponentType();
317 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700318 // trivial check again for array types
319 if (klass1 == klass2) {
320 return true;
321 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700323 std::string temp1, temp2;
324 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325}
326
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800327bool Class::IsStringClass() const {
328 return this == String::GetJavaLangString();
329}
330
Ian Rogersef7d42f2014-01-06 12:55:46 -0800331bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800332 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
333}
334
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100336 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700337 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100338 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700339 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100340 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800341}
342
Brian Carlstrom004644f2014-06-18 08:34:01 -0700343ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800344 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700345 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700346 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 return method;
348 }
349
350 int32_t iftable_count = GetIfTableCount();
351 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700352 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700354 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800355 return method;
356 }
357 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700358 return nullptr;
359}
360
361ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
362 // Check the current class before checking the interfaces.
363 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
364 if (method != nullptr) {
365 return method;
366 }
367
368 int32_t iftable_count = GetIfTableCount();
369 IfTable* iftable = GetIfTable();
370 for (int32_t i = 0; i < iftable_count; ++i) {
371 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
372 if (method != nullptr) {
373 return method;
374 }
375 }
376 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800377}
378
Ian Rogersef7d42f2014-01-06 12:55:46 -0800379ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800380 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700381 ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700382 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 return method;
384 }
385
386 int32_t iftable_count = GetIfTableCount();
387 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700388 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800389 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700390 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 return method;
392 }
393 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700394 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800395}
396
Ian Rogersef7d42f2014-01-06 12:55:46 -0800397ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800398 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700399 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700400 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700401 return method;
402 }
403 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700404 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700405}
406
Ian Rogersef7d42f2014-01-06 12:55:46 -0800407ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700408 for (size_t i = 0; i < NumDirectMethods(); ++i) {
409 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700410 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800411 return method;
412 }
413 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700414 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800415}
416
Ian Rogersef7d42f2014-01-06 12:55:46 -0800417ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800418 if (GetDexCache() == dex_cache) {
419 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700420 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800421 if (method->GetDexMethodIndex() == dex_method_idx) {
422 return method;
423 }
424 }
425 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700426 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427}
428
Ian Rogersef7d42f2014-01-06 12:55:46 -0800429ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700430 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700431 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700432 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800433 return method;
434 }
435 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700436 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800437}
438
Ian Rogersef7d42f2014-01-06 12:55:46 -0800439ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700440 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700441 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700442 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700443 return method;
444 }
445 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700446 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700447}
448
Ian Rogersef7d42f2014-01-06 12:55:46 -0800449ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700450 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700451 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700452 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800453 return method;
454 }
455 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700456 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800457}
458
Ian Rogersef7d42f2014-01-06 12:55:46 -0800459ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700460 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
461 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700462 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700463 return method;
464 }
465 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700466 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700467}
468
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700469ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800470 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700471 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700472 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800473 return method;
474 }
475 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700476 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800477}
478
Ian Rogersef7d42f2014-01-06 12:55:46 -0800479ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800480 if (GetDexCache() == dex_cache) {
481 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700482 ArtMethod* method = GetVirtualMethod(i);
Brian Carlstromf322c4c2014-10-31 00:01:54 -0700483 if (method->GetDexMethodIndex() == dex_method_idx &&
Andreas Gampe2b0fa5b2014-10-31 18:12:30 -0700484 // A miranda method may have a different DexCache and is always created by linking,
485 // never *declared* in the class.
486 !method->IsMiranda()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800487 return method;
488 }
489 }
490 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700491 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800492}
493
Ian Rogersef7d42f2014-01-06 12:55:46 -0800494ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700495 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700496 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700497 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800498 return method;
499 }
500 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700501 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800502}
503
Ian Rogersef7d42f2014-01-06 12:55:46 -0800504ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700505 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700506 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700507 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700508 return method;
509 }
510 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700511 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700512}
513
Ian Rogersef7d42f2014-01-06 12:55:46 -0800514ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700515 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700516 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700517 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800518 return method;
519 }
520 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700521 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800522}
523
Ian Rogersef7d42f2014-01-06 12:55:46 -0800524ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700525 for (size_t i = 0; i < NumDirectMethods(); ++i) {
526 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700527 if (method->IsClassInitializer()) {
528 DCHECK_STREQ(method->GetName(), "<clinit>");
529 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700530 return method;
531 }
532 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700533 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700534}
535
Brian Carlstromea46f952013-07-30 01:26:50 -0700536ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800537 // Is the field in this class?
538 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700540 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700541 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800542 return f;
543 }
544 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700545 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800546}
547
Brian Carlstromea46f952013-07-30 01:26:50 -0700548ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800549 if (GetDexCache() == dex_cache) {
550 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700551 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800552 if (f->GetDexFieldIndex() == dex_field_idx) {
553 return f;
554 }
555 }
556 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700557 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800558}
559
Brian Carlstromea46f952013-07-30 01:26:50 -0700560ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800561 // Is the field in this class, or any of its superclasses?
562 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700563 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700564 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700565 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 return f;
567 }
568 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700569 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800570}
571
Brian Carlstromea46f952013-07-30 01:26:50 -0700572ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800573 // Is the field in this class, or any of its superclasses?
574 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700575 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700576 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700577 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 return f;
579 }
580 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700581 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800582}
583
Brian Carlstromea46f952013-07-30 01:26:50 -0700584ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700585 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800586 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700587 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700588 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800589 return f;
590 }
591 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700592 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800593}
594
Brian Carlstromea46f952013-07-30 01:26:50 -0700595ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800596 if (dex_cache == GetDexCache()) {
597 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700598 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800599 if (f->GetDexFieldIndex() == dex_field_idx) {
600 return f;
601 }
602 }
603 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700604 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800605}
606
Mathieu Chartierf8322842014-05-16 10:59:25 -0700607ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
608 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800609 // Is the field in this class (or its interfaces), or any of its
610 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700611 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800612 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700613 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700614 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800615 return f;
616 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700617 // Wrap k incase it moves during GetDirectInterface.
618 StackHandleScope<1> hs(self);
619 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700621 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800622 StackHandleScope<1> hs2(self);
623 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700624 f = FindStaticField(self, interface, name, type);
625 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800626 return f;
627 }
628 }
629 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700630 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631}
632
Mathieu Chartierf8322842014-05-16 10:59:25 -0700633ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
634 uint32_t dex_field_idx) {
635 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700637 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700638 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800639 return f;
640 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700641 // Wrap k incase it moves during GetDirectInterface.
642 StackHandleScope<1> hs(self);
643 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700645 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800646 StackHandleScope<1> hs2(self);
647 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700648 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
649 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800650 return f;
651 }
652 }
653 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700654 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800655}
656
Mathieu Chartierf8322842014-05-16 10:59:25 -0700657ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
658 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800659 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700660 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800661 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700662 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700663 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800664 return f;
665 }
666 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700667 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800668 return f;
669 }
670 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700671 StackHandleScope<1> hs(self);
672 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
673 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800674 StackHandleScope<1> hs2(self);
675 Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i)));
Mathieu Chartierf8322842014-05-16 10:59:25 -0700676 f = interface->FindStaticField(self, interface, name, type);
677 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800678 return f;
679 }
680 }
681 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700682 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683}
684
Brian Carlstromea46f952013-07-30 01:26:50 -0700685static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200686 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700687 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200688 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700689 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700690 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700691 if (!method->IsNative() && !method->IsAbstract()) {
692 method->SetPreverified();
693 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200694 }
695 }
696}
697
698void Class::SetPreverifiedFlagOnAllMethods() {
699 DCHECK(IsVerified());
700 SetPreverifiedFlagOnMethods(GetDirectMethods());
701 SetPreverifiedFlagOnMethods(GetVirtualMethods());
702}
703
Ian Rogers1ff3c982014-08-12 02:30:58 -0700704const char* Class::GetDescriptor(std::string* storage) {
705 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700706 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700707 } else if (IsArrayClass()) {
708 return GetArrayDescriptor(storage);
709 } else if (IsProxyClass()) {
710 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
711 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700712 } else {
713 const DexFile& dex_file = GetDexFile();
714 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
715 return dex_file.GetTypeDescriptor(type_id);
716 }
717}
718
Ian Rogers1ff3c982014-08-12 02:30:58 -0700719const char* Class::GetArrayDescriptor(std::string* storage) {
720 std::string temp;
721 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
722 *storage = "[";
723 *storage += elem_desc;
724 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700725}
726
727const DexFile::ClassDef* Class::GetClassDef() {
728 uint16_t class_def_idx = GetDexClassDefIndex();
729 if (class_def_idx == DexFile::kDexNoIndex16) {
730 return nullptr;
731 }
732 return &GetDexFile().GetClassDef(class_def_idx);
733}
734
Mathieu Chartierf8322842014-05-16 10:59:25 -0700735uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
736 DCHECK(!IsPrimitive());
737 DCHECK(!IsArrayClass());
738 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
739}
740
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700741mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700742 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700743 DCHECK(klass.Get() != nullptr);
744 DCHECK(!klass->IsPrimitive());
745 if (klass->IsArrayClass()) {
746 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
747 if (idx == 0) {
748 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
749 } else {
750 DCHECK_EQ(1U, idx);
751 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
752 }
753 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700754 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700755 DCHECK(interfaces != nullptr);
756 return interfaces->Get(idx);
757 } else {
758 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
759 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
760 if (interface == nullptr) {
761 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
762 klass.Get());
763 CHECK(interface != nullptr || self->IsExceptionPending());
764 }
765 return interface;
766 }
767}
768
769const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700770 const DexFile& dex_file = GetDexFile();
771 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200772 if (dex_class_def == nullptr) {
773 // Generated classes have no class def.
774 return nullptr;
775 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700776 return dex_file.GetSourceFile(*dex_class_def);
777}
778
779std::string Class::GetLocation() {
780 mirror::DexCache* dex_cache = GetDexCache();
781 if (dex_cache != nullptr && !IsProxyClass()) {
782 return dex_cache->GetLocation()->ToModifiedUtf8();
783 }
784 // Arrays and proxies are generated and have no corresponding dex file location.
785 return "generated class";
786}
787
788const DexFile::TypeList* Class::GetInterfaceTypeList() {
789 const DexFile::ClassDef* class_def = GetClassDef();
790 if (class_def == nullptr) {
791 return nullptr;
792 }
793 return GetDexFile().GetInterfacesList(*class_def);
794}
795
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700796void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) {
797 for (uint32_t i = 0; i < kImtSize; i++) {
798 // Replace null with conflict.
799 mirror::Object* obj = imt_handle_scope->GetReference(i);
800 DCHECK(obj != nullptr);
801 SetEmbeddedImTableEntry(i, obj->AsArtMethod());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700802 }
803
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700804 ObjectArray<ArtMethod>* table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700805 CHECK(table != nullptr) << PrettyClass(this);
806 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700807 for (int32_t i = 0; i < table->GetLength(); i++) {
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700808 SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700809 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700810
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700811 // Keep java.lang.Object class's vtable around for since it's easier
812 // to be reused by array classes during their linking.
813 if (!IsObjectClass()) {
814 SetVTable(nullptr);
815 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700816}
817
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700818// The pre-fence visitor for Class::CopyOf().
819class CopyClassVisitor {
820 public:
821 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700822 size_t new_length, size_t copy_bytes,
823 StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope)
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700824 : self_(self), orig_(orig), new_length_(new_length),
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700825 copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) {
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700826 }
827
828 void operator()(Object* obj, size_t usable_size) const
829 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
830 UNUSED(usable_size);
831 mirror::Class* new_class_obj = obj->AsClass();
832 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_);
833 new_class_obj->SetStatus(Class::kStatusResolving, self_);
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700834 new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_);
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700835 new_class_obj->SetClassSize(new_length_);
836 }
837
838 private:
839 Thread* const self_;
840 Handle<mirror::Class>* const orig_;
841 const size_t new_length_;
842 const size_t copy_bytes_;
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700843 StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_;
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700844 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
845};
846
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700847Class* Class::CopyOf(Thread* self, int32_t new_length,
848 StackHandleScope<kImtSize>* imt_handle_scope) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700849 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
850 // We may get copied by a compacting GC.
851 StackHandleScope<1> hs(self);
852 Handle<mirror::Class> h_this(hs.NewHandle(this));
853 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700854 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
855 // to skip copying the tail part that we will overwrite here.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700856 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700857 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700858 kMovingClasses
859 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
860 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700861 if (UNLIKELY(new_class == nullptr)) {
862 CHECK(self->IsExceptionPending()); // Expect an OOME.
Mathieu Chartier2d2621a2014-10-23 16:48:06 -0700863 return nullptr;
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700864 }
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700865 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700866}
867
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800868} // namespace mirror
869} // namespace art