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. |
| 63 | CHECK_EQ(GetThinLockId(), self->GetThinLockId()) |
| 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 | |
| 121 | Object* Class::AllocObject(Thread* self) { |
| 122 | DCHECK(!IsArrayClass()) << PrettyClass(this); |
| 123 | DCHECK(IsInstantiable()) << PrettyClass(this); |
| 124 | // TODO: decide whether we want this check. It currently fails during bootstrap. |
| 125 | // DCHECK(!Runtime::Current()->IsStarted() || IsInitializing()) << PrettyClass(this); |
| 126 | DCHECK_GE(this->object_size_, sizeof(Object)); |
| 127 | return Runtime::Current()->GetHeap()->AllocObject(self, this, this->object_size_); |
| 128 | } |
| 129 | |
| 130 | void Class::SetClassSize(size_t new_class_size) { |
| 131 | DCHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); |
| 132 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size, false); |
| 133 | } |
| 134 | |
| 135 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 136 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 137 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 138 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
| 139 | String* Class::ComputeName() { |
| 140 | String* name = GetName(); |
| 141 | if (name != NULL) { |
| 142 | return name; |
| 143 | } |
| 144 | std::string descriptor(ClassHelper(this).GetDescriptor()); |
| 145 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 146 | // The descriptor indicates that this is the class for |
| 147 | // a primitive type; special-case the return value. |
| 148 | const char* c_name = NULL; |
| 149 | switch (descriptor[0]) { |
| 150 | case 'Z': c_name = "boolean"; break; |
| 151 | case 'B': c_name = "byte"; break; |
| 152 | case 'C': c_name = "char"; break; |
| 153 | case 'S': c_name = "short"; break; |
| 154 | case 'I': c_name = "int"; break; |
| 155 | case 'J': c_name = "long"; break; |
| 156 | case 'F': c_name = "float"; break; |
| 157 | case 'D': c_name = "double"; break; |
| 158 | case 'V': c_name = "void"; break; |
| 159 | default: |
| 160 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 161 | } |
| 162 | name = String::AllocFromModifiedUtf8(Thread::Current(), c_name); |
| 163 | } else { |
| 164 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 165 | // components. |
| 166 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 167 | descriptor.erase(0, 1); |
| 168 | descriptor.erase(descriptor.size() - 1); |
| 169 | } |
| 170 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
| 171 | name = String::AllocFromModifiedUtf8(Thread::Current(), descriptor.c_str()); |
| 172 | } |
| 173 | SetName(name); |
| 174 | return name; |
| 175 | } |
| 176 | |
| 177 | void Class::DumpClass(std::ostream& os, int flags) const { |
| 178 | if ((flags & kDumpClassFullDetail) == 0) { |
| 179 | os << PrettyClass(this); |
| 180 | if ((flags & kDumpClassClassLoader) != 0) { |
| 181 | os << ' ' << GetClassLoader(); |
| 182 | } |
| 183 | if ((flags & kDumpClassInitialized) != 0) { |
| 184 | os << ' ' << GetStatus(); |
| 185 | } |
| 186 | os << "\n"; |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | Class* super = GetSuperClass(); |
| 191 | ClassHelper kh(this); |
| 192 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
| 193 | << "'" << kh.GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", |
| 194 | os << " objectSize=" << SizeOf() << " " |
| 195 | << "(" << (super != NULL ? super->SizeOf() : -1) << " from super)\n", |
| 196 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 197 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
| 198 | if (super != NULL) { |
| 199 | os << " super='" << PrettyClass(super) << "' (cl=" << super->GetClassLoader() << ")\n"; |
| 200 | } |
| 201 | if (IsArrayClass()) { |
| 202 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 203 | } |
| 204 | if (kh.NumDirectInterfaces() > 0) { |
| 205 | os << " interfaces (" << kh.NumDirectInterfaces() << "):\n"; |
| 206 | for (size_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 207 | Class* interface = kh.GetDirectInterface(i); |
| 208 | const ClassLoader* cl = interface->GetClassLoader(); |
| 209 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
| 210 | } |
| 211 | } |
| 212 | os << " vtable (" << NumVirtualMethods() << " entries, " |
| 213 | << (super != NULL ? super->NumVirtualMethods() : 0) << " in super):\n"; |
| 214 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 215 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetVirtualMethodDuringLinking(i)).c_str()); |
| 216 | } |
| 217 | os << " direct methods (" << NumDirectMethods() << " entries):\n"; |
| 218 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 219 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(GetDirectMethod(i)).c_str()); |
| 220 | } |
| 221 | if (NumStaticFields() > 0) { |
| 222 | os << " static fields (" << NumStaticFields() << " entries):\n"; |
| 223 | if (IsResolved() || IsErroneous()) { |
| 224 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
| 225 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetStaticField(i)).c_str()); |
| 226 | } |
| 227 | } else { |
| 228 | os << " <not yet available>"; |
| 229 | } |
| 230 | } |
| 231 | if (NumInstanceFields() > 0) { |
| 232 | os << " instance fields (" << NumInstanceFields() << " entries):\n"; |
| 233 | if (IsResolved() || IsErroneous()) { |
| 234 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
| 235 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(GetInstanceField(i)).c_str()); |
| 236 | } |
| 237 | } else { |
| 238 | os << " <not yet available>"; |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
| 244 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 245 | // Sanity check that the number of bits set in the reference offset bitmap |
| 246 | // agrees with the number of references |
| 247 | size_t count = 0; |
| 248 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 249 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
| 250 | } |
| 251 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), count); |
| 252 | } |
| 253 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
| 254 | new_reference_offsets, false); |
| 255 | } |
| 256 | |
| 257 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 258 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 259 | // Sanity check that the number of bits set in the reference offset bitmap |
| 260 | // agrees with the number of references |
| 261 | CHECK_EQ((size_t)__builtin_popcount(new_reference_offsets), |
| 262 | NumReferenceStaticFieldsDuringLinking()); |
| 263 | } |
| 264 | SetField32(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
| 265 | new_reference_offsets, false); |
| 266 | } |
| 267 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 268 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
| 269 | size_t i = 0; |
| 270 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 271 | ++i; |
| 272 | } |
| 273 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 274 | descriptor2.find('/', i) != StringPiece::npos) { |
| 275 | return false; |
| 276 | } else { |
| 277 | return true; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | bool Class::IsInSamePackage(const Class* that) const { |
| 282 | const Class* klass1 = this; |
| 283 | const Class* klass2 = that; |
| 284 | if (klass1 == klass2) { |
| 285 | return true; |
| 286 | } |
| 287 | // Class loaders must match. |
| 288 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 289 | return false; |
| 290 | } |
| 291 | // Arrays are in the same package when their element classes are. |
| 292 | while (klass1->IsArrayClass()) { |
| 293 | klass1 = klass1->GetComponentType(); |
| 294 | } |
| 295 | while (klass2->IsArrayClass()) { |
| 296 | klass2 = klass2->GetComponentType(); |
| 297 | } |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 298 | // trivial check again for array types |
| 299 | if (klass1 == klass2) { |
| 300 | return true; |
| 301 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 302 | // Compare the package part of the descriptor string. |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 303 | if (LIKELY(!klass1->IsProxyClass() && !klass2->IsProxyClass())) { |
| 304 | ClassHelper kh(klass1); |
Brian Carlstrom | 857fe96 | 2013-03-26 22:46:15 -0700 | [diff] [blame] | 305 | const DexFile* dex_file1 = &kh.GetDexFile(); |
| 306 | const DexFile::TypeId* type_id1 = &dex_file1->GetTypeId(klass1->GetDexTypeIndex()); |
| 307 | const char* descriptor1 = dex_file1->GetTypeDescriptor(*type_id1); |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 308 | kh.ChangeClass(klass2); |
Brian Carlstrom | 857fe96 | 2013-03-26 22:46:15 -0700 | [diff] [blame] | 309 | const DexFile* dex_file2 = &kh.GetDexFile(); |
| 310 | const DexFile::TypeId* type_id2 = &dex_file2->GetTypeId(klass2->GetDexTypeIndex()); |
| 311 | const char* descriptor2 = dex_file2->GetTypeDescriptor(*type_id2); |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 312 | return IsInSamePackage(descriptor1, descriptor2); |
| 313 | } |
Brian Carlstrom | 857fe96 | 2013-03-26 22:46:15 -0700 | [diff] [blame] | 314 | ClassHelper kh(klass1); |
| 315 | std::string descriptor1(kh.GetDescriptor()); |
| 316 | kh.ChangeClass(klass2); |
| 317 | std::string descriptor2(kh.GetDescriptor()); |
| 318 | return IsInSamePackage(descriptor1, descriptor2); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | bool Class::IsClassClass() const { |
| 322 | Class* java_lang_Class = GetClass()->GetClass(); |
| 323 | return this == java_lang_Class; |
| 324 | } |
| 325 | |
| 326 | bool Class::IsStringClass() const { |
| 327 | return this == String::GetJavaLangString(); |
| 328 | } |
| 329 | |
| 330 | bool Class::IsThrowableClass() const { |
| 331 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
| 332 | } |
| 333 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 334 | bool Class::IsArtFieldClass() const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 335 | Class* java_lang_Class = GetClass(); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 336 | Class* java_lang_reflect_ArtField = java_lang_Class->GetInstanceField(0)->GetClass(); |
| 337 | return this == java_lang_reflect_ArtField; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 340 | bool Class::IsArtMethodClass() const { |
| 341 | return this == ArtMethod::GetJavaLangReflectArtMethod(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 342 | } |
| 343 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 344 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
| 345 | SetFieldObject(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader, false); |
| 346 | } |
| 347 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 348 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 349 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 350 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 351 | if (method != NULL) { |
| 352 | return method; |
| 353 | } |
| 354 | |
| 355 | int32_t iftable_count = GetIfTableCount(); |
| 356 | IfTable* iftable = GetIfTable(); |
| 357 | for (int32_t i = 0; i < iftable_count; i++) { |
| 358 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); |
| 359 | if (method != NULL) { |
| 360 | return method; |
| 361 | } |
| 362 | } |
| 363 | return NULL; |
| 364 | } |
| 365 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 366 | 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] | 367 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 368 | ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 369 | if (method != NULL) { |
| 370 | return method; |
| 371 | } |
| 372 | |
| 373 | int32_t iftable_count = GetIfTableCount(); |
| 374 | IfTable* iftable = GetIfTable(); |
| 375 | for (int32_t i = 0; i < iftable_count; i++) { |
| 376 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 377 | if (method != NULL) { |
| 378 | return method; |
| 379 | } |
| 380 | } |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 385 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 386 | MethodHelper mh; |
| 387 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 388 | ArtMethod* method = GetDirectMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 389 | mh.ChangeMethod(method); |
| 390 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
| 391 | return method; |
| 392 | } |
| 393 | } |
| 394 | return NULL; |
| 395 | } |
| 396 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 397 | 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] | 398 | if (GetDexCache() == dex_cache) { |
| 399 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 400 | ArtMethod* method = GetDirectMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 401 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 402 | return method; |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | return NULL; |
| 407 | } |
| 408 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 409 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 410 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 411 | ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 412 | if (method != NULL) { |
| 413 | return method; |
| 414 | } |
| 415 | } |
| 416 | return NULL; |
| 417 | } |
| 418 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 419 | 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] | 420 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 421 | ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 422 | if (method != NULL) { |
| 423 | return method; |
| 424 | } |
| 425 | } |
| 426 | return NULL; |
| 427 | } |
| 428 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 429 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 430 | const StringPiece& signature) const { |
| 431 | MethodHelper mh; |
| 432 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 433 | ArtMethod* method = GetVirtualMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 434 | mh.ChangeMethod(method); |
| 435 | if (name == mh.GetName() && signature == mh.GetSignature()) { |
| 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 DexCache* dex_cache, uint32_t dex_method_idx) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 443 | if (GetDexCache() == dex_cache) { |
| 444 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 445 | ArtMethod* method = GetVirtualMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 446 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 447 | return method; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | return NULL; |
| 452 | } |
| 453 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 454 | ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) const { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 455 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 456 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 457 | if (method != NULL) { |
| 458 | return method; |
| 459 | } |
| 460 | } |
| 461 | return NULL; |
| 462 | } |
| 463 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 464 | 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] | 465 | for (const Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 466 | ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 467 | if (method != NULL) { |
| 468 | return method; |
| 469 | } |
| 470 | } |
| 471 | return NULL; |
| 472 | } |
| 473 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 474 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 475 | // Is the field in this class? |
| 476 | // Interfaces are not relevant because they can't contain instance fields. |
| 477 | FieldHelper fh; |
| 478 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 479 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 480 | fh.ChangeField(f); |
| 481 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
| 482 | return f; |
| 483 | } |
| 484 | } |
| 485 | return NULL; |
| 486 | } |
| 487 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 488 | ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 489 | if (GetDexCache() == dex_cache) { |
| 490 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 491 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 492 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 493 | return f; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | return NULL; |
| 498 | } |
| 499 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 500 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 501 | // Is the field in this class, or any of its superclasses? |
| 502 | // Interfaces are not relevant because they can't contain instance fields. |
| 503 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 504 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 505 | if (f != NULL) { |
| 506 | return f; |
| 507 | } |
| 508 | } |
| 509 | return NULL; |
| 510 | } |
| 511 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 512 | ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 513 | // Is the field in this class, or any of its superclasses? |
| 514 | // Interfaces are not relevant because they can't contain instance fields. |
| 515 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 516 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 517 | if (f != NULL) { |
| 518 | return f; |
| 519 | } |
| 520 | } |
| 521 | return NULL; |
| 522 | } |
| 523 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 524 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 525 | DCHECK(type != NULL); |
| 526 | FieldHelper fh; |
| 527 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 528 | ArtField* f = GetStaticField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 529 | fh.ChangeField(f); |
| 530 | if (name == fh.GetName() && type == fh.GetTypeDescriptor()) { |
| 531 | return f; |
| 532 | } |
| 533 | } |
| 534 | return NULL; |
| 535 | } |
| 536 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 537 | ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 538 | if (dex_cache == GetDexCache()) { |
| 539 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 540 | ArtField* f = GetStaticField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 541 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 542 | return f; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | return NULL; |
| 547 | } |
| 548 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 549 | ArtField* Class::FindStaticField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 550 | // Is the field in this class (or its interfaces), or any of its |
| 551 | // superclasses (or their interfaces)? |
| 552 | ClassHelper kh; |
| 553 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 554 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 555 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 556 | if (f != NULL) { |
| 557 | return f; |
| 558 | } |
| 559 | // Is this field in any of this class' interfaces? |
| 560 | kh.ChangeClass(k); |
| 561 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 562 | Class* interface = kh.GetDirectInterface(i); |
| 563 | f = interface->FindStaticField(name, type); |
| 564 | if (f != NULL) { |
| 565 | return f; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | return NULL; |
| 570 | } |
| 571 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 572 | ArtField* Class::FindStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 573 | ClassHelper kh; |
| 574 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 575 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 576 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 577 | if (f != NULL) { |
| 578 | return f; |
| 579 | } |
| 580 | // Is this field in any of this class' interfaces? |
| 581 | kh.ChangeClass(k); |
| 582 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 583 | Class* interface = kh.GetDirectInterface(i); |
| 584 | f = interface->FindStaticField(dex_cache, dex_field_idx); |
| 585 | if (f != NULL) { |
| 586 | return f; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | return NULL; |
| 591 | } |
| 592 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 593 | ArtField* Class::FindField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 594 | // Find a field using the JLS field resolution order |
| 595 | ClassHelper kh; |
| 596 | for (Class* k = this; k != NULL; k = k->GetSuperClass()) { |
| 597 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 598 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 599 | if (f != NULL) { |
| 600 | return f; |
| 601 | } |
| 602 | f = k->FindDeclaredStaticField(name, type); |
| 603 | if (f != NULL) { |
| 604 | return f; |
| 605 | } |
| 606 | // Is this field in any of this class' interfaces? |
| 607 | kh.ChangeClass(k); |
| 608 | for (uint32_t i = 0; i < kh.NumDirectInterfaces(); ++i) { |
| 609 | Class* interface = kh.GetDirectInterface(i); |
| 610 | f = interface->FindStaticField(name, type); |
| 611 | if (f != NULL) { |
| 612 | return f; |
| 613 | } |
| 614 | } |
| 615 | } |
| 616 | return NULL; |
| 617 | } |
| 618 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 619 | static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods) |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 620 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 621 | if (methods != NULL) { |
| 622 | for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 623 | mirror::ArtMethod* method = methods->GetWithoutChecks(index); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 624 | DCHECK(method != NULL); |
| 625 | method->SetPreverified(); |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | void Class::SetPreverifiedFlagOnAllMethods() { |
| 631 | DCHECK(IsVerified()); |
| 632 | SetPreverifiedFlagOnMethods(GetDirectMethods()); |
| 633 | SetPreverifiedFlagOnMethods(GetVirtualMethods()); |
| 634 | } |
| 635 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 636 | } // namespace mirror |
| 637 | } // namespace art |