Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 19 | #include "art_field-inl.h" |
| 20 | #include "art_method-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "class-inl.h" |
| 22 | #include "class_linker.h" |
| 23 | #include "class_loader.h" |
| 24 | #include "dex_cache.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 26 | #include "gc/accounting/card_table-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "object-inl.h" |
| 28 | #include "object_array-inl.h" |
| 29 | #include "object_utils.h" |
| 30 | #include "runtime.h" |
| 31 | #include "sirt_ref.h" |
| 32 | #include "thread.h" |
| 33 | #include "throwable.h" |
| 34 | #include "utils.h" |
| 35 | #include "well_known_classes.h" |
| 36 | |
| 37 | namespace art { |
| 38 | namespace mirror { |
| 39 | |
| 40 | Class* Class::java_lang_Class_ = NULL; |
| 41 | |
| 42 | void Class::SetClassClass(Class* java_lang_Class) { |
| 43 | CHECK(java_lang_Class_ == NULL) << java_lang_Class_ << " " << java_lang_Class; |
| 44 | CHECK(java_lang_Class != NULL); |
| 45 | java_lang_Class_ = java_lang_Class; |
| 46 | } |
| 47 | |
| 48 | void Class::ResetClass() { |
| 49 | CHECK(java_lang_Class_ != NULL); |
| 50 | java_lang_Class_ = NULL; |
| 51 | } |
| 52 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 53 | void Class::SetStatus(Status new_status, Thread* self) { |
| 54 | Status old_status = GetStatus(); |
| 55 | bool class_linker_initialized = Runtime::Current()->GetClassLinker() != nullptr; |
| 56 | if (LIKELY(class_linker_initialized)) { |
| 57 | if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 58 | LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 59 | << old_status << " -> " << new_status; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 60 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 61 | if (new_status >= kStatusResolved || old_status >= kStatusResolved) { |
| 62 | // When classes are being resolved the resolution code should hold the lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame^] | 63 | CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId()) |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 64 | << "Attempt to change status of class while not holding its lock: " |
| 65 | << PrettyClass(this) << " " << old_status << " -> " << new_status; |
| 66 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 67 | } |
| 68 | if (new_status == kStatusError) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 69 | CHECK_NE(GetStatus(), kStatusError) |
| 70 | << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 71 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 72 | // Stash current exception. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 73 | SirtRef<mirror::Object> old_throw_this_object(self, NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 74 | SirtRef<mirror::ArtMethod> old_throw_method(self, NULL); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 75 | SirtRef<mirror::Throwable> old_exception(self, NULL); |
| 76 | uint32_t old_throw_dex_pc; |
| 77 | { |
| 78 | ThrowLocation old_throw_location; |
| 79 | mirror::Throwable* old_exception_obj = self->GetException(&old_throw_location); |
| 80 | old_throw_this_object.reset(old_throw_location.GetThis()); |
| 81 | old_throw_method.reset(old_throw_location.GetMethod()); |
| 82 | old_exception.reset(old_exception_obj); |
| 83 | old_throw_dex_pc = old_throw_location.GetDexPc(); |
| 84 | self->ClearException(); |
| 85 | } |
| 86 | CHECK(old_exception.get() != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 87 | |
| 88 | // clear exception to call FindSystemClass |
| 89 | self->ClearException(); |
| 90 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 91 | Class* eiie_class = class_linker->FindSystemClass("Ljava/lang/ExceptionInInitializerError;"); |
| 92 | CHECK(!self->IsExceptionPending()); |
| 93 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 94 | // Only verification errors, not initialization problems, should set a verify error. |
| 95 | // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case. |
| 96 | Class* exception_class = old_exception->GetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 97 | if (!eiie_class->IsAssignableFrom(exception_class)) { |
| 98 | SetVerifyErrorClass(exception_class); |
| 99 | } |
| 100 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 101 | // Restore exception. |
| 102 | ThrowLocation gc_safe_throw_location(old_throw_this_object.get(), old_throw_method.get(), |
| 103 | old_throw_dex_pc); |
| 104 | |
| 105 | self->SetException(gc_safe_throw_location, old_exception.get()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 107 | CHECK(sizeof(Status) == sizeof(uint32_t)) << PrettyClass(this); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 108 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status, false); |
| 109 | // Classes that are being resolved or initialized need to notify waiters that the class status |
| 110 | // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. |
| 111 | if ((old_status >= kStatusResolved || new_status >= kStatusResolved) && |
| 112 | class_linker_initialized) { |
| 113 | NotifyAll(self); |
| 114 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 117 | void Class::SetDexCache(DexCache* new_dex_cache) { |
| 118 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache, false); |
| 119 | } |
| 120 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 121 | void Class::SetClassSize(size_t new_class_size) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 122 | if (kIsDebugBuild && (new_class_size < GetClassSize())) { |
| 123 | DumpClass(LOG(ERROR), kDumpClassFullDetail); |
| 124 | CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); |
| 125 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false); |
| 127 | } |
| 128 | |
| 129 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 130 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 131 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 132 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
| 133 | String* Class::ComputeName() { |
| 134 | String* name = GetName(); |
| 135 | if (name != NULL) { |
| 136 | return name; |
| 137 | } |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 138 | std::string descriptor(ClassHelper(this).GetDescriptorAsStringPiece().as_string()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 139 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 140 | // The descriptor indicates that this is the class for |
| 141 | // a primitive type; special-case the return value. |
| 142 | const char* c_name = NULL; |
| 143 | switch (descriptor[0]) { |
| 144 | case 'Z': c_name = "boolean"; break; |
| 145 | case 'B': c_name = "byte"; break; |
| 146 | case 'C': c_name = "char"; break; |
| 147 | case 'S': c_name = "short"; break; |
| 148 | case 'I': c_name = "int"; break; |
| 149 | case 'J': c_name = "long"; break; |
| 150 | case 'F': c_name = "float"; break; |
| 151 | case 'D': c_name = "double"; break; |
| 152 | case 'V': c_name = "void"; break; |
| 153 | default: |
| 154 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 155 | } |
| 156 | name = String::AllocFromModifiedUtf8(Thread::Current(), c_name); |
| 157 | } else { |
| 158 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 159 | // components. |
| 160 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 161 | descriptor.erase(0, 1); |
| 162 | descriptor.erase(descriptor.size() - 1); |
| 163 | } |
| 164 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 165 | name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str()); |
| 166 | } |
| 167 | SetName(name); |
| 168 | return name; |
| 169 | } |
| 170 | |
| 171 | void Class::DumpClass(std::ostream& os, int flags) const { |
| 172 | if ((flags & kDumpClassFullDetail) == 0) { |
| 173 | os << PrettyClass(this); |
| 174 | if ((flags & kDumpClassClassLoader) != 0) { |
| 175 | os << ' ' << GetClassLoader(); |
| 176 | } |
| 177 | if ((flags & kDumpClassInitialized) != 0) { |
| 178 | os << ' ' << GetStatus(); |
| 179 | } |
| 180 | os << "\n"; |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | Class* super = GetSuperClass(); |
| 185 | ClassHelper kh(this); |
| 186 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
| 187 | << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", |
| 188 | os << " objectSize=" << SizeOf() << " " |
| 189 | << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n", |
| 190 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 191 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
| 192 | if (super != NULL) { |
| 193 | os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n"; |
| 194 | } |
| 195 | if (IsArrayClass()) { |
| 196 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 197 | } |
| 198 | if (kh.NumDirectInterfaces() > 0) { |
| 199 | os << " interfaces (" << kh.NumDirectInterfaces() << "):\n"; |
| 200 | for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 201 | Class* interface = kh.GetDirectInterface(i); |
| 202 | const ClassLoader* cl = interface->GetClassLoader(); |
| 203 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
| 204 | } |
| 205 | } |
| 206 | os << " vtable (" << NumVirtualMethods() << " entries, " |
| 207 | << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n"; |
| 208 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 209 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str()); |
| 210 | } |
| 211 | os << " direct methods (" << NumDirectMethods() << " entries):\n"; |
| 212 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 213 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str()); |
| 214 | } |
| 215 | if (NumStaticFields() > 0) { |
| 216 | os << " static fields (" << NumStaticFields() << " entries):\n"; |
| 217 | if (IsResolved() || IsErroneous()) { |
| 218 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 219 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str()); |
| 220 | } |
| 221 | } else { |
| 222 | os << " <not yet available>"; |
| 223 | } |
| 224 | } |
| 225 | if (NumInstanceFields() > 0) { |
| 226 | os << " instance fields (" << NumInstanceFields() << " entries):\n"; |
| 227 | if (IsResolved() || IsErroneous()) { |
| 228 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 229 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str()); |
| 230 | } |
| 231 | } else { |
| 232 | os << " <not yet available>"; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 238 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 239 | // Sanity check that the number of bits set in the reference offset bitmap |
| 240 | // agrees with the number of references |
| 241 | size_t count = 0; |
| 242 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 243 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
| 244 | } |
| 245 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count); |
| 246 | } |
| 247 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 248 | new_reference_offsets, false); |
| 249 | } |
| 250 | |
| 251 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 252 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 253 | // Sanity check that the number of bits set in the reference offset bitmap |
| 254 | // agrees with the number of references |
| 255 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 256 | NumReferenceStaticFieldsDuringLinking()); |
| 257 | } |
| 258 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 259 | new_reference_offsets, false); |
| 260 | } |
| 261 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 262 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
| 263 | size_t i = 0; |
| 264 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 265 | ++i; |
| 266 | } |
| 267 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 268 | descriptor2.find('/', i) != StringPiece::npos) { |
| 269 | return false; |
| 270 | } else { |
| 271 | return true; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | bool Class::IsInSamePackage(const Class* that) const { |
| 276 | const Class* klass1 = this; |
| 277 | const Class* klass2 = that; |
| 278 | if (klass1 == klass2) { |
| 279 | return true; |
| 280 | } |
| 281 | // Class loaders must match. |
| 282 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 283 | return false; |
| 284 | } |
| 285 | // Arrays are in the same package when their element classes are. |
| 286 | while (klass1->IsArrayClass()) { |
| 287 | klass1 = klass1->GetComponentType(); |
| 288 | } |
| 289 | while (klass2->IsArrayClass()) { |
| 290 | klass2 = klass2->GetComponentType(); |
| 291 | } |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 292 | // trivial check again for array types |
| 293 | if (klass1 == klass2) { |
| 294 | return true; |
| 295 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 296 | // Compare the package part of the descriptor string. |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 297 | return IsInSamePackage(ClassHelper(klass1).GetDescriptorAsStringPiece(), |
| 298 | ClassHelper(klass2).GetDescriptorAsStringPiece()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | bool Class::IsClassClass() const { |
| 302 | Class* java_lang_Class = GetClass()->GetClass(); |
| 303 | return this == java_lang_Class; |
| 304 | } |
| 305 | |
| 306 | bool Class::IsStringClass() const { |
| 307 | return this == String::GetJavaLangString(); |
| 308 | } |
| 309 | |
| 310 | bool Class::IsThrowableClass() const { |
| 311 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
| 312 | } |
| 313 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 314 | bool Class::IsArtFieldClass() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 315 | Class* java_lang_Class = GetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 316 | Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass(); |
| 317 | return this == java_lang_reflect_ArtField; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 320 | bool Class::IsArtMethodClass() const { |
| 321 | return this == ArtMethod::GetJavaLangReflectArtMethod(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 322 | } |
| 323 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 324 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
| 325 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false); |
| 326 | } |
| 327 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 328 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 329 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 330 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 331 | if (method != NULL) { |
| 332 | return method; |
| 333 | } |
| 334 | |
| 335 | int32_t iftable_count = GetIfTableCount(); |
| 336 | IfTable* iftable = GetIfTable(); |
| 337 | for (int32_t i = 0; i < iftable_count; i++) { |
| 338 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); |
| 339 | if (method != NULL) { |
| 340 | return method; |
| 341 | } |
| 342 | } |
| 343 | return NULL; |
| 344 | } |
| 345 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 346 | ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 347 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 348 | ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 349 | if (method != NULL) { |
| 350 | return method; |
| 351 | } |
| 352 | |
| 353 | int32_t iftable_count = GetIfTableCount(); |
| 354 | IfTable* iftable = GetIfTable(); |
| 355 | for (int32_t i = 0; i < iftable_count; i++) { |
| 356 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 357 | if (method != NULL) { |
| 358 | return method; |
| 359 | } |
| 360 | } |
| 361 | return NULL; |
| 362 | } |
| 363 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 364 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 365 | MethodHelper mh; |
| 366 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 367 | ArtMethod* method = GetDirectMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 368 | mh.ChangeMethod(method); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 369 | if (name == mh.GetNameAsStringPiece() && mh.GetSignature() == signature) { |
| 370 | return method; |
| 371 | } |
| 372 | } |
| 373 | return NULL; |
| 374 | } |
| 375 | |
| 376 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) const { |
| 377 | MethodHelper mh; |
| 378 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 379 | ArtMethod* method = GetDirectMethod(i); |
| 380 | mh.ChangeMethod(method); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 381 | if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 382 | return method; |
| 383 | } |
| 384 | } |
| 385 | return NULL; |
| 386 | } |
| 387 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 388 | ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 389 | if (GetDexCache() == dex_cache) { |
| 390 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 391 | ArtMethod* method = GetDirectMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 392 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 393 | return method; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | return NULL; |
| 398 | } |
| 399 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 400 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 401 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 402 | ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 403 | if (method != NULL) { |
| 404 | return method; |
| 405 | } |
| 406 | } |
| 407 | return NULL; |
| 408 | } |
| 409 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 410 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) const { |
| 411 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 412 | ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); |
| 413 | if (method != NULL) { |
| 414 | return method; |
| 415 | } |
| 416 | } |
| 417 | return NULL; |
| 418 | } |
| 419 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 420 | ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 421 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 422 | ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 423 | if (method != NULL) { |
| 424 | return method; |
| 425 | } |
| 426 | } |
| 427 | return NULL; |
| 428 | } |
| 429 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 430 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) const { |
| 431 | MethodHelper mh; |
| 432 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 433 | ArtMethod* method = GetVirtualMethod(i); |
| 434 | mh.ChangeMethod(method); |
| 435 | if (name == mh.GetNameAsStringPiece() && mh.GetSignature() == signature) { |
| 436 | return method; |
| 437 | } |
| 438 | } |
| 439 | return NULL; |
| 440 | } |
| 441 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 442 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 443 | const Signature& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 444 | MethodHelper mh; |
| 445 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 446 | ArtMethod* method = GetVirtualMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 447 | mh.ChangeMethod(method); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 448 | if (name == mh.GetNameAsStringPiece() && signature == mh.GetSignature()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 449 | return method; |
| 450 | } |
| 451 | } |
| 452 | return NULL; |
| 453 | } |
| 454 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 455 | ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 456 | if (GetDexCache() == dex_cache) { |
| 457 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 458 | ArtMethod* method = GetVirtualMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 459 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 460 | return method; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | return NULL; |
| 465 | } |
| 466 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 467 | ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 468 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 469 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 470 | if (method != NULL) { |
| 471 | return method; |
| 472 | } |
| 473 | } |
| 474 | return NULL; |
| 475 | } |
| 476 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 477 | ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) const { |
| 478 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
| 479 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
| 480 | if (method != NULL) { |
| 481 | return method; |
| 482 | } |
| 483 | } |
| 484 | return NULL; |
| 485 | } |
| 486 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 487 | ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 488 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 489 | ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 490 | if (method != NULL) { |
| 491 | return method; |
| 492 | } |
| 493 | } |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 497 | ArtMethod* Class::FindClassInitializer() const { |
| 498 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 499 | ArtMethod* method = GetDirectMethod(i); |
| 500 | if (method->IsConstructor() && method->IsStatic()) { |
| 501 | if (kIsDebugBuild) { |
| 502 | MethodHelper mh(method); |
| 503 | CHECK_STREQ(mh.GetName(), "<clinit>"); |
| 504 | CHECK_STREQ(mh.GetSignature().ToString().c_str(), "()V"); |
| 505 | } |
| 506 | return method; |
| 507 | } |
| 508 | } |
| 509 | return NULL; |
| 510 | } |
| 511 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 512 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 513 | // Is the field in this class? |
| 514 | // Interfaces are not relevant because they can't contain instance fields. |
| 515 | FieldHelper fh; |
| 516 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 517 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 518 | fh.ChangeField(f); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 519 | if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 520 | return f; |
| 521 | } |
| 522 | } |
| 523 | return NULL; |
| 524 | } |
| 525 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 526 | ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 527 | if (GetDexCache() == dex_cache) { |
| 528 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 529 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 530 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 531 | return f; |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | return NULL; |
| 536 | } |
| 537 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 538 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 539 | // Is the field in this class, or any of its superclasses? |
| 540 | // Interfaces are not relevant because they can't contain instance fields. |
| 541 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 542 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 543 | if (f != NULL) { |
| 544 | return f; |
| 545 | } |
| 546 | } |
| 547 | return NULL; |
| 548 | } |
| 549 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 550 | ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 551 | // Is the field in this class, or any of its superclasses? |
| 552 | // Interfaces are not relevant because they can't contain instance fields. |
| 553 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 554 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 555 | if (f != NULL) { |
| 556 | return f; |
| 557 | } |
| 558 | } |
| 559 | return NULL; |
| 560 | } |
| 561 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 562 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 563 | DCHECK(type != NULL); |
| 564 | FieldHelper fh; |
| 565 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 566 | ArtField* f = GetStaticField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 567 | fh.ChangeField(f); |
Ian Rogers | fc0e94b | 2013-09-23 23:51:32 -0700 | [diff] [blame] | 568 | if (name == fh.GetNameAsStringPiece() && type == fh.GetTypeDescriptorAsStringPiece()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 569 | return f; |
| 570 | } |
| 571 | } |
| 572 | return NULL; |
| 573 | } |
| 574 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 575 | ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 576 | if (dex_cache == GetDexCache()) { |
| 577 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 578 | ArtField* f = GetStaticField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 579 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 580 | return f; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | return NULL; |
| 585 | } |
| 586 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 587 | ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 588 | // Is the field in this class (or its interfaces), or any of its |
| 589 | // superclasses (or their interfaces)? |
| 590 | ClassHelper kh; |
| 591 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 592 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 593 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 594 | if (f != NULL) { |
| 595 | return f; |
| 596 | } |
| 597 | // Is this field in any of this class' interfaces? |
| 598 | kh.ChangeClass(k); |
| 599 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 600 | Class* interface = kh.GetDirectInterface(i); |
| 601 | f = interface->FindStaticField(name, type); |
| 602 | if (f != NULL) { |
| 603 | return f; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | return NULL; |
| 608 | } |
| 609 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 610 | ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 611 | ClassHelper kh; |
| 612 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 613 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 614 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 615 | if (f != NULL) { |
| 616 | return f; |
| 617 | } |
| 618 | // Is this field in any of this class' interfaces? |
| 619 | kh.ChangeClass(k); |
| 620 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 621 | Class* interface = kh.GetDirectInterface(i); |
| 622 | f = interface->FindStaticField(dex_cache, dex_field_idx); |
| 623 | if (f != NULL) { |
| 624 | return f; |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | return NULL; |
| 629 | } |
| 630 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 631 | ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 632 | // Find a field using the JLS field resolution order |
| 633 | ClassHelper kh; |
| 634 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 635 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 636 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 637 | if (f != NULL) { |
| 638 | return f; |
| 639 | } |
| 640 | f = k->FindDeclaredStaticField(name, type); |
| 641 | if (f != NULL) { |
| 642 | return f; |
| 643 | } |
| 644 | // Is this field in any of this class' interfaces? |
| 645 | kh.ChangeClass(k); |
| 646 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 647 | Class* interface = kh.GetDirectInterface(i); |
| 648 | f = interface->FindStaticField(name, type); |
| 649 | if (f != NULL) { |
| 650 | return f; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | return NULL; |
| 655 | } |
| 656 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 657 | static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods) |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 658 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 659 | if (methods != NULL) { |
| 660 | for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 661 | mirror::ArtMethod* method = methods->GetWithoutChecks(index); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 662 | DCHECK(method != NULL); |
| 663 | method->SetPreverified(); |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void Class::SetPreverifiedFlagOnAllMethods() { |
| 669 | DCHECK(IsVerified()); |
| 670 | SetPreverifiedFlagOnMethods(GetDirectMethods()); |
| 671 | SetPreverifiedFlagOnMethods(GetVirtualMethods()); |
| 672 | } |
| 673 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 674 | } // namespace mirror |
| 675 | } // namespace art |