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