blob: 3fcb188697ce48aa166cba4a71b720694816aa3f [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) {
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -070055 if (!java_lang_Class_.IsNull()) {
56 java_lang_Class_.VisitRoot(callback, arg, 0, kRootStickyClass);
Mathieu Chartierc528dba2013-11-26 12:00:11 -080057 }
58}
59
Ian Rogers7dfb28c2013-08-22 08:18:36 -070060void Class::SetStatus(Status new_status, Thread* self) {
61 Status old_status = GetStatus();
Mathieu Chartier590fee92013-09-13 13:46:47 -070062 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
63 bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized();
Ian Rogers7dfb28c2013-08-22 08:18:36 -070064 if (LIKELY(class_linker_initialized)) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -070065 if (UNLIKELY(new_status <= old_status && new_status != kStatusError &&
66 new_status != kStatusRetired)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070067 LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " "
Ian Rogers7dfb28c2013-08-22 08:18:36 -070068 << old_status << " -> " << new_status;
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070069 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -070070 if (new_status >= kStatusResolved || old_status >= kStatusResolved) {
71 // When classes are being resolved the resolution code should hold the lock.
Ian Rogersd9c4fc92013-10-01 19:45:43 -070072 CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId())
Ian Rogers7dfb28c2013-08-22 08:18:36 -070073 << "Attempt to change status of class while not holding its lock: "
74 << PrettyClass(this) << " " << old_status << " -> " << new_status;
75 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076 }
Ian Rogers98379392014-02-24 16:53:16 -080077 if (UNLIKELY(new_status == kStatusError)) {
Ian Rogers8f3c9ae2013-08-20 17:26:41 -070078 CHECK_NE(GetStatus(), kStatusError)
79 << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080
Ian Rogers62d6c772013-02-27 08:32:07 -080081 // Stash current exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070082 StackHandleScope<3> hs(self);
83 ThrowLocation old_throw_location;
84 Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location)));
85 CHECK(old_exception.Get() != nullptr);
86 Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis()));
87 Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod()));
88 uint32_t old_throw_dex_pc = old_throw_location.GetDexPc();
Sebastien Hertz9f102032014-05-23 08:59:42 +020089 bool is_exception_reported = self->IsExceptionReportedToInstrumentation();
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -070090 Class* eiie_class;
91 // Do't attempt to use FindClass if we have an OOM error since this can try to do more
92 // allocations and may cause infinite loops.
Ian Rogers1ff3c982014-08-12 02:30:58 -070093 bool throw_eiie = (old_exception.Get() == nullptr);
94 if (!throw_eiie) {
95 std::string temp;
96 const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp);
97 throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0);
98 }
99 if (throw_eiie) {
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700100 // Clear exception to call FindSystemClass.
101 self->ClearException();
102 eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass(
103 self, "Ljava/lang/ExceptionInInitializerError;");
104 CHECK(!self->IsExceptionPending());
105 // Only verification errors, not initialization problems, should set a verify error.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700106 // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that
107 // case.
Mathieu Chartierfd22d5b2014-07-14 10:16:05 -0700108 Class* exception_class = old_exception->GetClass();
109 if (!eiie_class->IsAssignableFrom(exception_class)) {
110 SetVerifyErrorClass(exception_class);
111 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 }
113
Ian Rogers62d6c772013-02-27 08:32:07 -0800114 // Restore exception.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700115 ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800116 old_throw_dex_pc);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700117 self->SetException(gc_safe_throw_location, old_exception.Get());
Sebastien Hertz9f102032014-05-23 08:59:42 +0200118 self->SetExceptionReportedToInstrumentation(is_exception_reported);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800119 }
Ian Rogers03dbc042014-06-02 14:24:56 -0700120 COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100121 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogers03dbc042014-06-02 14:24:56 -0700122 SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100123 } else {
Ian Rogers03dbc042014-06-02 14:24:56 -0700124 SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100125 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700126
127 if (!class_linker_initialized) {
128 // When the class linker is being initialized its single threaded and by definition there can be
129 // no waiters. During initialization classes may appear temporary but won't be retired as their
130 // size was statically computed.
131 } else {
132 // Classes that are being resolved or initialized need to notify waiters that the class status
133 // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass.
134 if (IsTemp()) {
135 // Class is a temporary one, ensure that waiters for resolution get notified of retirement
136 // so that they can grab the new version of the class from the class linker's table.
137 CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(this);
138 if (new_status == kStatusRetired || new_status == kStatusError) {
139 NotifyAll(self);
140 }
141 } else {
142 CHECK_NE(new_status, kStatusRetired);
143 if (old_status >= kStatusResolved || new_status >= kStatusResolved) {
144 NotifyAll(self);
145 }
146 }
Ian Rogers7dfb28c2013-08-22 08:18:36 -0700147 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800148}
149
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800150void Class::SetDexCache(DexCache* new_dex_cache) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700151 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800152}
153
Ian Rogersef7d42f2014-01-06 12:55:46 -0800154void Class::SetClassSize(uint32_t new_class_size) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700155 if (kIsDebugBuild && (new_class_size < GetClassSize())) {
156 DumpClass(LOG(ERROR), kDumpClassFullDetail);
157 CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this);
158 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100159 // Not called within a transaction.
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700160 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800161}
162
163// Return the class' name. The exact format is bizarre, but it's the specified behavior for
164// Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int"
165// but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than
166// slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness.
Mathieu Chartierf8322842014-05-16 10:59:25 -0700167String* Class::ComputeName(Handle<Class> h_this) {
168 String* name = h_this->GetName();
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800169 if (name != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800170 return name;
171 }
Ian Rogers1ff3c982014-08-12 02:30:58 -0700172 std::string temp;
173 const char* descriptor = h_this->GetDescriptor(&temp);
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800174 Thread* self = Thread::Current();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 if ((descriptor[0] != 'L') && (descriptor[0] != '[')) {
176 // The descriptor indicates that this is the class for
177 // a primitive type; special-case the return value.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700178 const char* c_name = nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800179 switch (descriptor[0]) {
180 case 'Z': c_name = "boolean"; break;
181 case 'B': c_name = "byte"; break;
182 case 'C': c_name = "char"; break;
183 case 'S': c_name = "short"; break;
184 case 'I': c_name = "int"; break;
185 case 'J': c_name = "long"; break;
186 case 'F': c_name = "float"; break;
187 case 'D': c_name = "double"; break;
188 case 'V': c_name = "void"; break;
189 default:
190 LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]);
191 }
Mathieu Chartier692fafd2013-11-29 17:24:40 -0800192 name = String::AllocFromModifiedUtf8(self, c_name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800193 } else {
194 // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package
195 // components.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700196 name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700198 h_this->SetName(name);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800199 return name;
200}
201
Ian Rogersef7d42f2014-01-06 12:55:46 -0800202void Class::DumpClass(std::ostream& os, int flags) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800203 if ((flags & kDumpClassFullDetail) == 0) {
204 os << PrettyClass(this);
205 if ((flags & kDumpClassClassLoader) != 0) {
206 os << ' ' << GetClassLoader();
207 }
208 if ((flags & kDumpClassInitialized) != 0) {
209 os << ' ' << GetStatus();
210 }
211 os << "\n";
212 return;
213 }
214
Mathieu Chartierf8322842014-05-16 10:59:25 -0700215 Thread* self = Thread::Current();
216 StackHandleScope<2> hs(self);
217 Handle<mirror::Class> h_this(hs.NewHandle(this));
218 Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass()));
219
Ian Rogers1ff3c982014-08-12 02:30:58 -0700220 std::string temp;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800221 os << "----- " << (IsInterface() ? "interface" : "class") << " "
Ian Rogers1ff3c982014-08-12 02:30:58 -0700222 << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 os << " objectSize=" << SizeOf() << " "
Brian Carlstrom004644f2014-06-18 08:34:01 -0700224 << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n",
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800225 os << StringPrintf(" access=0x%04x.%04x\n",
226 GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700227 if (h_super.Get() != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700228 os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader()
229 << ")\n";
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800230 }
231 if (IsArrayClass()) {
232 os << " componentType=" << PrettyClass(GetComponentType()) << "\n";
233 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700234 const size_t num_direct_interfaces = NumDirectInterfaces();
235 if (num_direct_interfaces > 0) {
236 os << " interfaces (" << num_direct_interfaces << "):\n";
237 for (size_t i = 0; i < num_direct_interfaces; ++i) {
238 Class* interface = GetDirectInterface(self, h_this, i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 const ClassLoader* cl = interface->GetClassLoader();
240 os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl);
241 }
242 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700243 if (!IsLoaded()) {
244 os << " class not yet loaded";
245 } else {
246 // After this point, this may have moved due to GetDirectInterface.
247 os << " vtable (" << h_this->NumVirtualMethods() << " entries, "
248 << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n";
249 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
250 os << StringPrintf(" %2zd: %s\n", i,
251 PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700253 os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n";
254 for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) {
255 os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str());
256 }
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) {
281 if (new_reference_offsets != CLASS_WALK_SUPER) {
282 // Sanity check that the number of bits set in the reference offset bitmap
283 // agrees with the number of references
284 size_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 }
Vladimir Marko81949632014-05-02 11:53:22 +0100288 CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 }
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100290 // Not called within a transaction.
291 SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_),
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700292 new_reference_offsets);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800293}
294
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800295bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) {
296 size_t i = 0;
Ian Rogers6b604a12014-09-25 15:35:37 -0700297 size_t min_length = std::min(descriptor1.size(), descriptor2.size());
298 while (i < min_length && descriptor1[i] == descriptor2[i]) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800299 ++i;
300 }
301 if (descriptor1.find('/', i) != StringPiece::npos ||
302 descriptor2.find('/', i) != StringPiece::npos) {
303 return false;
304 } else {
305 return true;
306 }
307}
308
Ian Rogersef7d42f2014-01-06 12:55:46 -0800309bool Class::IsInSamePackage(Class* that) {
310 Class* klass1 = this;
311 Class* klass2 = that;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 if (klass1 == klass2) {
313 return true;
314 }
315 // Class loaders must match.
316 if (klass1->GetClassLoader() != klass2->GetClassLoader()) {
317 return false;
318 }
319 // Arrays are in the same package when their element classes are.
320 while (klass1->IsArrayClass()) {
321 klass1 = klass1->GetComponentType();
322 }
323 while (klass2->IsArrayClass()) {
324 klass2 = klass2->GetComponentType();
325 }
Anwar Ghuloum9fa3f202013-03-26 14:32:54 -0700326 // trivial check again for array types
327 if (klass1 == klass2) {
328 return true;
329 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800330 // Compare the package part of the descriptor string.
Ian Rogers1ff3c982014-08-12 02:30:58 -0700331 std::string temp1, temp2;
332 return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800333}
334
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800335bool Class::IsStringClass() const {
336 return this == String::GetJavaLangString();
337}
338
Ian Rogersef7d42f2014-01-06 12:55:46 -0800339bool Class::IsThrowableClass() {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800340 return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this);
341}
342
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800343void Class::SetClassLoader(ClassLoader* new_class_loader) {
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100344 if (Runtime::Current()->IsActiveTransaction()) {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700345 SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100346 } else {
Ian Rogersb0fa5dc2014-04-28 16:47:08 -0700347 SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader);
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +0100348 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800349}
350
Brian Carlstrom004644f2014-06-18 08:34:01 -0700351ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700353 ArtMethod* method = 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
358 int32_t iftable_count = GetIfTableCount();
359 IfTable* iftable = GetIfTable();
Brian Carlstrom004644f2014-06-18 08:34:01 -0700360 for (int32_t i = 0; i < iftable_count; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800361 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700362 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800363 return method;
364 }
365 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700366 return nullptr;
367}
368
369ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) {
370 // Check the current class before checking the interfaces.
371 ArtMethod* method = FindDeclaredVirtualMethod(name, signature);
372 if (method != nullptr) {
373 return method;
374 }
375
376 int32_t iftable_count = GetIfTableCount();
377 IfTable* iftable = GetIfTable();
378 for (int32_t i = 0; i < iftable_count; ++i) {
379 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature);
380 if (method != nullptr) {
381 return method;
382 }
383 }
384 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800385}
386
Ian Rogersef7d42f2014-01-06 12:55:46 -0800387ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 // Check the current class before checking the interfaces.
Brian Carlstromea46f952013-07-30 01:26:50 -0700389 ArtMethod* method = 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
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) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800397 method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700398 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800399 return method;
400 }
401 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700402 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800403}
404
Ian Rogersef7d42f2014-01-06 12:55:46 -0800405ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800406 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700407 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700408 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700409 return method;
410 }
411 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700412 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700413}
414
Ian Rogersef7d42f2014-01-06 12:55:46 -0800415ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700416 for (size_t i = 0; i < NumDirectMethods(); ++i) {
417 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700418 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800419 return method;
420 }
421 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700422 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800423}
424
Ian Rogersef7d42f2014-01-06 12:55:46 -0800425ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800426 if (GetDexCache() == dex_cache) {
427 for (size_t i = 0; i < NumDirectMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700428 ArtMethod* method = GetDirectMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800429 if (method->GetDexMethodIndex() == dex_method_idx) {
430 return method;
431 }
432 }
433 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700434 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800435}
436
Ian Rogersef7d42f2014-01-06 12:55:46 -0800437ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700438 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700439 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700440 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800441 return method;
442 }
443 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700444 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800445}
446
Ian Rogersef7d42f2014-01-06 12:55:46 -0800447ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700448 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700449 ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700450 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700451 return method;
452 }
453 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700454 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700455}
456
Ian Rogersef7d42f2014-01-06 12:55:46 -0800457ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700458 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700459 ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700460 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800461 return method;
462 }
463 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700464 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800465}
466
Ian Rogersef7d42f2014-01-06 12:55:46 -0800467ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700468 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
469 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700470 if (name == method->GetName() && method->GetSignature() == signature) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700471 return method;
472 }
473 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700474 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700475}
476
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700477ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700479 ArtMethod* method = GetVirtualMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700480 if (name == method->GetName() && signature == method->GetSignature()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 return method;
482 }
483 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700484 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485}
486
Ian Rogersef7d42f2014-01-06 12:55:46 -0800487ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800488 if (GetDexCache() == dex_cache) {
489 for (size_t i = 0; i < NumVirtualMethods(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700490 ArtMethod* method = GetVirtualMethod(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491 if (method->GetDexMethodIndex() == dex_method_idx) {
492 return method;
493 }
494 }
495 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700496 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800497}
498
Ian Rogersef7d42f2014-01-06 12:55:46 -0800499ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700500 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700501 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700502 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800503 return method;
504 }
505 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700506 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800507}
508
Ian Rogersef7d42f2014-01-06 12:55:46 -0800509ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700510 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700511 ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700512 if (method != nullptr) {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700513 return method;
514 }
515 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700516 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700517}
518
Ian Rogersef7d42f2014-01-06 12:55:46 -0800519ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700520 for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700521 ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700522 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800523 return method;
524 }
525 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700526 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800527}
528
Ian Rogersef7d42f2014-01-06 12:55:46 -0800529ArtMethod* Class::FindClassInitializer() {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700530 for (size_t i = 0; i < NumDirectMethods(); ++i) {
531 ArtMethod* method = GetDirectMethod(i);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700532 if (method->IsClassInitializer()) {
533 DCHECK_STREQ(method->GetName(), "<clinit>");
534 DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V");
Ian Rogersd91d6d62013-09-25 20:26:14 -0700535 return method;
536 }
537 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700538 return nullptr;
Ian Rogersd91d6d62013-09-25 20:26:14 -0700539}
540
Brian Carlstromea46f952013-07-30 01:26:50 -0700541ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800542 // Is the field in this class?
543 // Interfaces are not relevant because they can't contain instance fields.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800544 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700545 ArtField* f = GetInstanceField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700546 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800547 return f;
548 }
549 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700550 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800551}
552
Brian Carlstromea46f952013-07-30 01:26:50 -0700553ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800554 if (GetDexCache() == dex_cache) {
555 for (size_t i = 0; i < NumInstanceFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700556 ArtField* f = GetInstanceField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800557 if (f->GetDexFieldIndex() == dex_field_idx) {
558 return f;
559 }
560 }
561 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700562 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800563}
564
Brian Carlstromea46f952013-07-30 01:26:50 -0700565ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800566 // Is the field in this class, or any of its superclasses?
567 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700568 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700569 ArtField* f = c->FindDeclaredInstanceField(name, type);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700570 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 return f;
572 }
573 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700574 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800575}
576
Brian Carlstromea46f952013-07-30 01:26:50 -0700577ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800578 // Is the field in this class, or any of its superclasses?
579 // Interfaces are not relevant because they can't contain instance fields.
Brian Carlstrom004644f2014-06-18 08:34:01 -0700580 for (Class* c = this; c != nullptr; c = c->GetSuperClass()) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700581 ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700582 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800583 return f;
584 }
585 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700586 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800587}
588
Brian Carlstromea46f952013-07-30 01:26:50 -0700589ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700590 DCHECK(type != nullptr);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800591 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700592 ArtField* f = GetStaticField(i);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700593 if (name == f->GetName() && type == f->GetTypeDescriptor()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800594 return f;
595 }
596 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700597 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800598}
599
Brian Carlstromea46f952013-07-30 01:26:50 -0700600ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800601 if (dex_cache == GetDexCache()) {
602 for (size_t i = 0; i < NumStaticFields(); ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700603 ArtField* f = GetStaticField(i);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800604 if (f->GetDexFieldIndex() == dex_field_idx) {
605 return f;
606 }
607 }
608 }
Brian Carlstrom004644f2014-06-18 08:34:01 -0700609 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800610}
611
Mathieu Chartierf8322842014-05-16 10:59:25 -0700612ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name,
613 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 // Is the field in this class (or its interfaces), or any of its
615 // superclasses (or their interfaces)?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700616 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800617 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700618 ArtField* f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700619 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800620 return f;
621 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700622 // Wrap k incase it moves during GetDirectInterface.
623 StackHandleScope<1> hs(self);
624 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800625 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700626 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
627 StackHandleScope<1> hs(self);
628 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
629 f = FindStaticField(self, interface, name, type);
630 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800631 return f;
632 }
633 }
634 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700635 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636}
637
Mathieu Chartierf8322842014-05-16 10:59:25 -0700638ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache,
639 uint32_t dex_field_idx) {
640 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800641 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700642 ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700643 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800644 return f;
645 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700646 // Wrap k incase it moves during GetDirectInterface.
647 StackHandleScope<1> hs(self);
648 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800649 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700650 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
651 StackHandleScope<1> hs(self);
652 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
653 f = FindStaticField(self, interface, dex_cache, dex_field_idx);
654 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800655 return f;
656 }
657 }
658 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700659 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800660}
661
Mathieu Chartierf8322842014-05-16 10:59:25 -0700662ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name,
663 const StringPiece& type) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800664 // Find a field using the JLS field resolution order
Brian Carlstrom004644f2014-06-18 08:34:01 -0700665 for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800666 // Is the field in this class?
Brian Carlstromea46f952013-07-30 01:26:50 -0700667 ArtField* f = k->FindDeclaredInstanceField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700668 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800669 return f;
670 }
671 f = k->FindDeclaredStaticField(name, type);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700672 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800673 return f;
674 }
675 // Is this field in any of this class' interfaces?
Mathieu Chartierf8322842014-05-16 10:59:25 -0700676 StackHandleScope<1> hs(self);
677 HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k));
678 for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) {
679 StackHandleScope<1> hs(self);
680 Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i)));
681 f = interface->FindStaticField(self, interface, name, type);
682 if (f != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800683 return f;
684 }
685 }
686 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700687 return nullptr;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800688}
689
Brian Carlstromea46f952013-07-30 01:26:50 -0700690static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods)
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200691 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstrom004644f2014-06-18 08:34:01 -0700692 if (methods != nullptr) {
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200693 for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700694 mirror::ArtMethod* method = methods->GetWithoutChecks(index);
Brian Carlstrom004644f2014-06-18 08:34:01 -0700695 DCHECK(method != nullptr);
Ian Rogers1eb512d2013-10-18 15:42:20 -0700696 if (!method->IsNative() && !method->IsAbstract()) {
697 method->SetPreverified();
698 }
Sebastien Hertz233ea8e2013-06-06 11:57:09 +0200699 }
700 }
701}
702
703void Class::SetPreverifiedFlagOnAllMethods() {
704 DCHECK(IsVerified());
705 SetPreverifiedFlagOnMethods(GetDirectMethods());
706 SetPreverifiedFlagOnMethods(GetVirtualMethods());
707}
708
Ian Rogers1ff3c982014-08-12 02:30:58 -0700709const char* Class::GetDescriptor(std::string* storage) {
710 if (IsPrimitive()) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700711 return Primitive::Descriptor(GetPrimitiveType());
Ian Rogers1ff3c982014-08-12 02:30:58 -0700712 } else if (IsArrayClass()) {
713 return GetArrayDescriptor(storage);
714 } else if (IsProxyClass()) {
715 *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this);
716 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700717 } else {
718 const DexFile& dex_file = GetDexFile();
719 const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_);
720 return dex_file.GetTypeDescriptor(type_id);
721 }
722}
723
Ian Rogers1ff3c982014-08-12 02:30:58 -0700724const char* Class::GetArrayDescriptor(std::string* storage) {
725 std::string temp;
726 const char* elem_desc = GetComponentType()->GetDescriptor(&temp);
727 *storage = "[";
728 *storage += elem_desc;
729 return storage->c_str();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700730}
731
732const DexFile::ClassDef* Class::GetClassDef() {
733 uint16_t class_def_idx = GetDexClassDefIndex();
734 if (class_def_idx == DexFile::kDexNoIndex16) {
735 return nullptr;
736 }
737 return &GetDexFile().GetClassDef(class_def_idx);
738}
739
740uint32_t Class::NumDirectInterfaces() {
741 if (IsPrimitive()) {
742 return 0;
743 } else if (IsArrayClass()) {
744 return 2;
745 } else if (IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700746 mirror::ObjectArray<mirror::Class>* interfaces = GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700747 return interfaces != nullptr ? interfaces->GetLength() : 0;
748 } else {
749 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
750 if (interfaces == nullptr) {
751 return 0;
752 } else {
753 return interfaces->Size();
754 }
755 }
756}
757
758uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) {
759 DCHECK(!IsPrimitive());
760 DCHECK(!IsArrayClass());
761 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
762}
763
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700764mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass,
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700765 uint32_t idx) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700766 DCHECK(klass.Get() != nullptr);
767 DCHECK(!klass->IsPrimitive());
768 if (klass->IsArrayClass()) {
769 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
770 if (idx == 0) {
771 return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;");
772 } else {
773 DCHECK_EQ(1U, idx);
774 return class_linker->FindSystemClass(self, "Ljava/io/Serializable;");
775 }
776 } else if (klass->IsProxyClass()) {
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700777 mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces();
Mathieu Chartierf8322842014-05-16 10:59:25 -0700778 DCHECK(interfaces != nullptr);
779 return interfaces->Get(idx);
780 } else {
781 uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx);
782 mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx);
783 if (interface == nullptr) {
784 interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx,
785 klass.Get());
786 CHECK(interface != nullptr || self->IsExceptionPending());
787 }
788 return interface;
789 }
790}
791
792const char* Class::GetSourceFile() {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700793 const DexFile& dex_file = GetDexFile();
794 const DexFile::ClassDef* dex_class_def = GetClassDef();
Sebastien Hertz4206eb52014-06-05 10:15:45 +0200795 if (dex_class_def == nullptr) {
796 // Generated classes have no class def.
797 return nullptr;
798 }
Mathieu Chartierf8322842014-05-16 10:59:25 -0700799 return dex_file.GetSourceFile(*dex_class_def);
800}
801
802std::string Class::GetLocation() {
803 mirror::DexCache* dex_cache = GetDexCache();
804 if (dex_cache != nullptr && !IsProxyClass()) {
805 return dex_cache->GetLocation()->ToModifiedUtf8();
806 }
807 // Arrays and proxies are generated and have no corresponding dex file location.
808 return "generated class";
809}
810
811const DexFile::TypeList* Class::GetInterfaceTypeList() {
812 const DexFile::ClassDef* class_def = GetClassDef();
813 if (class_def == nullptr) {
814 return nullptr;
815 }
816 return GetDexFile().GetInterfacesList(*class_def);
817}
818
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700819void Class::PopulateEmbeddedImtAndVTable() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
820 ObjectArray<ArtMethod>* table = GetImTable();
821 if (table != nullptr) {
822 for (uint32_t i = 0; i < kImtSize; i++) {
823 SetEmbeddedImTableEntry(i, table->Get(i));
824 }
825 }
826
827 table = GetVTableDuringLinking();
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700828 CHECK(table != nullptr) << PrettyClass(this);
829 SetEmbeddedVTableLength(table->GetLength());
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700830 for (int32_t i = 0; i < table->GetLength(); i++) {
831 SetEmbeddedVTableEntry(i, table->Get(i));
832 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700833
834 SetImTable(nullptr);
835 // Keep java.lang.Object class's vtable around for since it's easier
836 // to be reused by array classes during their linking.
837 if (!IsObjectClass()) {
838 SetVTable(nullptr);
839 }
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700840}
841
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700842// The pre-fence visitor for Class::CopyOf().
843class CopyClassVisitor {
844 public:
845 explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig,
846 size_t new_length, size_t copy_bytes)
847 : self_(self), orig_(orig), new_length_(new_length),
848 copy_bytes_(copy_bytes) {
849 }
850
851 void operator()(Object* obj, size_t usable_size) const
852 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
853 UNUSED(usable_size);
854 mirror::Class* new_class_obj = obj->AsClass();
855 mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_);
856 new_class_obj->SetStatus(Class::kStatusResolving, self_);
857 new_class_obj->PopulateEmbeddedImtAndVTable();
858 new_class_obj->SetClassSize(new_length_);
859 }
860
861 private:
862 Thread* const self_;
863 Handle<mirror::Class>* const orig_;
864 const size_t new_length_;
865 const size_t copy_bytes_;
866 DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor);
867};
868
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700869Class* Class::CopyOf(Thread* self, int32_t new_length) {
870 DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class)));
871 // We may get copied by a compacting GC.
872 StackHandleScope<1> hs(self);
873 Handle<mirror::Class> h_this(hs.NewHandle(this));
874 gc::Heap* heap = Runtime::Current()->GetHeap();
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700875 // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf()
876 // to skip copying the tail part that we will overwrite here.
877 CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class));
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700878
879 mirror::Object* new_class =
Hiroshi Yamauchi94f7b492014-07-22 18:08:23 -0700880 kMovingClasses
881 ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor)
882 : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor);
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700883 if (UNLIKELY(new_class == nullptr)) {
884 CHECK(self->IsExceptionPending()); // Expect an OOME.
885 return NULL;
886 }
887
Hiroshi Yamauchi0fbd6e62014-07-17 16:16:31 -0700888 return new_class->AsClass();
Mingyao Yang98d1cc82014-05-15 17:02:16 -0700889}
890
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800891} // namespace mirror
892} // namespace art