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" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 31 | #include "handle_scope-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 53 | void Class::VisitRoots(RootCallback* callback, void* arg) { |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 54 | if (java_lang_Class_ != nullptr) { |
Mathieu Chartier | 815873e | 2014-02-13 18:02:13 -0800 | [diff] [blame] | 55 | callback(reinterpret_cast<mirror::Object**>(&java_lang_Class_), arg, 0, kRootStickyClass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 59 | void Class::SetStatus(Status new_status, Thread* self) { |
| 60 | Status old_status = GetStatus(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 61 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 62 | bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 63 | if (LIKELY(class_linker_initialized)) { |
| 64 | if (UNLIKELY(new_status <= old_status && new_status != kStatusError)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 65 | LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(this) << " " |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 66 | << old_status << " -> " << new_status; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 67 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 68 | if (new_status >= kStatusResolved || old_status >= kStatusResolved) { |
| 69 | // When classes are being resolved the resolution code should hold the lock. |
Ian Rogers | d9c4fc9 | 2013-10-01 19:45:43 -0700 | [diff] [blame] | 70 | CHECK_EQ(GetLockOwnerThreadId(), self->GetThreadId()) |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 71 | << "Attempt to change status of class while not holding its lock: " |
| 72 | << PrettyClass(this) << " " << old_status << " -> " << new_status; |
| 73 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 75 | if (UNLIKELY(new_status == kStatusError)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 76 | CHECK_NE(GetStatus(), kStatusError) |
| 77 | << "Attempt to set as erroneous an already erroneous class " << PrettyClass(this); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 78 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 79 | // Stash current exception. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 80 | StackHandleScope<3> hs(self); |
| 81 | ThrowLocation old_throw_location; |
| 82 | Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException(&old_throw_location))); |
| 83 | CHECK(old_exception.Get() != nullptr); |
| 84 | Handle<mirror::Object> old_throw_this_object(hs.NewHandle(old_throw_location.GetThis())); |
| 85 | Handle<mirror::ArtMethod> old_throw_method(hs.NewHandle(old_throw_location.GetMethod())); |
| 86 | uint32_t old_throw_dex_pc = old_throw_location.GetDexPc(); |
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(); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 91 | Class* eiie_class = class_linker->FindSystemClass(self, |
| 92 | "Ljava/lang/ExceptionInInitializerError;"); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 93 | CHECK(!self->IsExceptionPending()); |
| 94 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 95 | // Only verification errors, not initialization problems, should set a verify error. |
| 96 | // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that case. |
| 97 | Class* exception_class = old_exception->GetClass(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | if (!eiie_class->IsAssignableFrom(exception_class)) { |
| 99 | SetVerifyErrorClass(exception_class); |
| 100 | } |
| 101 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 102 | // Restore exception. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 103 | ThrowLocation gc_safe_throw_location(old_throw_this_object.Get(), old_throw_method.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 104 | old_throw_dex_pc); |
| 105 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 106 | self->SetException(gc_safe_throw_location, old_exception.Get()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | } |
Ian Rogers | 03dbc04 | 2014-06-02 14:24:56 -0700 | [diff] [blame] | 108 | COMPILE_ASSERT(sizeof(Status) == sizeof(uint32_t), size_of_status_not_uint32); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 109 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | 03dbc04 | 2014-06-02 14:24:56 -0700 | [diff] [blame] | 110 | SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 111 | } else { |
Ian Rogers | 03dbc04 | 2014-06-02 14:24:56 -0700 | [diff] [blame] | 112 | SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 113 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 114 | // Classes that are being resolved or initialized need to notify waiters that the class status |
| 115 | // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. |
| 116 | if ((old_status >= kStatusResolved || new_status >= kStatusResolved) && |
| 117 | class_linker_initialized) { |
| 118 | NotifyAll(self); |
| 119 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | void Class::SetDexCache(DexCache* new_dex_cache) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 123 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 126 | void Class::SetClassSize(uint32_t new_class_size) { |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 127 | if (kIsDebugBuild && (new_class_size < GetClassSize())) { |
| 128 | DumpClass(LOG(ERROR), kDumpClassFullDetail); |
| 129 | CHECK_GE(new_class_size, GetClassSize()) << " class=" << PrettyTypeOf(this); |
| 130 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 131 | // Not called within a transaction. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 132 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 139 | String* Class::ComputeName(Handle<Class> h_this) { |
| 140 | String* name = h_this->GetName(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 141 | if (name != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 142 | return name; |
| 143 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 144 | std::string descriptor(h_this->GetDescriptor()); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 145 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 146 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 147 | // The descriptor indicates that this is the class for |
| 148 | // a primitive type; special-case the return value. |
| 149 | const char* c_name = NULL; |
| 150 | switch (descriptor[0]) { |
| 151 | case 'Z': c_name = "boolean"; break; |
| 152 | case 'B': c_name = "byte"; break; |
| 153 | case 'C': c_name = "char"; break; |
| 154 | case 'S': c_name = "short"; break; |
| 155 | case 'I': c_name = "int"; break; |
| 156 | case 'J': c_name = "long"; break; |
| 157 | case 'F': c_name = "float"; break; |
| 158 | case 'D': c_name = "double"; break; |
| 159 | case 'V': c_name = "void"; break; |
| 160 | default: |
| 161 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 162 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 163 | name = String::AllocFromModifiedUtf8(self, c_name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 164 | } else { |
| 165 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 166 | // components. |
| 167 | if (descriptor.size() > 2 && descriptor[0] == 'L' && descriptor[descriptor.size() - 1] == ';') { |
| 168 | descriptor.erase(0, 1); |
| 169 | descriptor.erase(descriptor.size() - 1); |
| 170 | } |
| 171 | std::replace(descriptor.begin(), descriptor.end(), '/', '.'); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 172 | name = String::AllocFromModifiedUtf8(self, descriptor.c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 173 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 174 | h_this->SetName(name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 175 | return name; |
| 176 | } |
| 177 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 178 | void Class::DumpClass(std::ostream& os, int flags) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 179 | if ((flags & kDumpClassFullDetail) == 0) { |
| 180 | os << PrettyClass(this); |
| 181 | if ((flags & kDumpClassClassLoader) != 0) { |
| 182 | os << ' ' << GetClassLoader(); |
| 183 | } |
| 184 | if ((flags & kDumpClassInitialized) != 0) { |
| 185 | os << ' ' << GetStatus(); |
| 186 | } |
| 187 | os << "\n"; |
| 188 | return; |
| 189 | } |
| 190 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 191 | Thread* self = Thread::Current(); |
| 192 | StackHandleScope<2> hs(self); |
| 193 | Handle<mirror::Class> h_this(hs.NewHandle(this)); |
| 194 | Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass())); |
| 195 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 196 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 197 | << "'" << GetDescriptor() << "' cl=" << GetClassLoader() << " -----\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 198 | os << " objectSize=" << SizeOf() << " " |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 199 | << "(" << (h_super.Get() != NULL ? h_super->SizeOf() : -1) << " from super)\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 200 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 201 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 202 | if (h_super.Get() != NULL) { |
| 203 | os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader() |
| 204 | << ")\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 205 | } |
| 206 | if (IsArrayClass()) { |
| 207 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 208 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 209 | const size_t num_direct_interfaces = NumDirectInterfaces(); |
| 210 | if (num_direct_interfaces > 0) { |
| 211 | os << " interfaces (" << num_direct_interfaces << "):\n"; |
| 212 | for (size_t i = 0; i < num_direct_interfaces; ++i) { |
| 213 | Class* interface = GetDirectInterface(self, h_this, i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 214 | const ClassLoader* cl = interface->GetClassLoader(); |
| 215 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
| 216 | } |
| 217 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 218 | // After this point, this may have moved due to GetDirectInterface. |
| 219 | os << " vtable (" << h_this->NumVirtualMethods() << " entries, " |
| 220 | << (h_super.Get() != NULL ? h_super->NumVirtualMethods() : 0) << " in super):\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 221 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 222 | os << StringPrintf(" %2zd: %s\n", i, |
| 223 | PrettyMethod(h_this->GetVirtualMethodDuringLinking(i)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 224 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 225 | os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; |
| 226 | for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { |
| 227 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod(h_this->GetDirectMethod(i)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 228 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 229 | if (h_this->NumStaticFields() > 0) { |
| 230 | os << " static fields (" << h_this->NumStaticFields() << " entries):\n"; |
| 231 | if (h_this->IsResolved() || h_this->IsErroneous()) { |
| 232 | for (size_t i = 0; i < h_this->NumStaticFields(); ++i) { |
| 233 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 234 | } |
| 235 | } else { |
| 236 | os << " <not yet available>"; |
| 237 | } |
| 238 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 239 | if (h_this->NumInstanceFields() > 0) { |
| 240 | os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n"; |
| 241 | if (h_this->IsResolved() || h_this->IsErroneous()) { |
| 242 | for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) { |
| 243 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 244 | } |
| 245 | } else { |
| 246 | os << " <not yet available>"; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void Class::SetReferenceInstanceOffsets(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 | size_t count = 0; |
| 256 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
| 257 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
| 258 | } |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 259 | CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), count); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 260 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 261 | // Not called within a transaction. |
| 262 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 263 | new_reference_offsets); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void Class::SetReferenceStaticOffsets(uint32_t new_reference_offsets) { |
| 267 | if (new_reference_offsets != CLASS_WALK_SUPER) { |
| 268 | // Sanity check that the number of bits set in the reference offset bitmap |
| 269 | // agrees with the number of references |
Vladimir Marko | 8194963 | 2014-05-02 11:53:22 +0100 | [diff] [blame] | 270 | CHECK_EQ((size_t)POPCOUNT(new_reference_offsets), |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 271 | NumReferenceStaticFieldsDuringLinking()); |
| 272 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 273 | // Not called within a transaction. |
| 274 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_static_offsets_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 275 | new_reference_offsets); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 278 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
| 279 | size_t i = 0; |
| 280 | while (descriptor1[i] != '\0' && descriptor1[i] == descriptor2[i]) { |
| 281 | ++i; |
| 282 | } |
| 283 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 284 | descriptor2.find('/', i) != StringPiece::npos) { |
| 285 | return false; |
| 286 | } else { |
| 287 | return true; |
| 288 | } |
| 289 | } |
| 290 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 291 | bool Class::IsInSamePackage(Class* that) { |
| 292 | Class* klass1 = this; |
| 293 | Class* klass2 = that; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 294 | if (klass1 == klass2) { |
| 295 | return true; |
| 296 | } |
| 297 | // Class loaders must match. |
| 298 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 299 | return false; |
| 300 | } |
| 301 | // Arrays are in the same package when their element classes are. |
| 302 | while (klass1->IsArrayClass()) { |
| 303 | klass1 = klass1->GetComponentType(); |
| 304 | } |
| 305 | while (klass2->IsArrayClass()) { |
| 306 | klass2 = klass2->GetComponentType(); |
| 307 | } |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 308 | // trivial check again for array types |
| 309 | if (klass1 == klass2) { |
| 310 | return true; |
| 311 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 312 | // Compare the package part of the descriptor string. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 313 | return IsInSamePackage(klass1->GetDescriptor().c_str(), klass2->GetDescriptor().c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 314 | } |
| 315 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 316 | bool Class::IsStringClass() const { |
| 317 | return this == String::GetJavaLangString(); |
| 318 | } |
| 319 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 320 | bool Class::IsThrowableClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 321 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
| 322 | } |
| 323 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 324 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 325 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 326 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 327 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 328 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 329 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 332 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 333 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 334 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 335 | if (method != NULL) { |
| 336 | return method; |
| 337 | } |
| 338 | |
| 339 | int32_t iftable_count = GetIfTableCount(); |
| 340 | IfTable* iftable = GetIfTable(); |
| 341 | for (int32_t i = 0; i < iftable_count; i++) { |
| 342 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); |
| 343 | if (method != NULL) { |
| 344 | return method; |
| 345 | } |
| 346 | } |
| 347 | return NULL; |
| 348 | } |
| 349 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 350 | ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 351 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 352 | ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 353 | if (method != NULL) { |
| 354 | return method; |
| 355 | } |
| 356 | |
| 357 | int32_t iftable_count = GetIfTableCount(); |
| 358 | IfTable* iftable = GetIfTable(); |
| 359 | for (int32_t i = 0; i < iftable_count; i++) { |
| 360 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
| 361 | if (method != NULL) { |
| 362 | return method; |
| 363 | } |
| 364 | } |
| 365 | return NULL; |
| 366 | } |
| 367 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 368 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 369 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 370 | ArtMethod* method = GetDirectMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 371 | if (name == method->GetName() && method->GetSignature() == signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 372 | return method; |
| 373 | } |
| 374 | } |
| 375 | return NULL; |
| 376 | } |
| 377 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 378 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 379 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 380 | ArtMethod* method = GetDirectMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 381 | if (name == method->GetName() && signature == method->GetSignature()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 382 | return method; |
| 383 | } |
| 384 | } |
| 385 | return NULL; |
| 386 | } |
| 387 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 388 | ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
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 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 400 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) { |
| 401 | for (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 | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 410 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature) { |
| 411 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 412 | ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); |
| 413 | if (method != NULL) { |
| 414 | return method; |
| 415 | } |
| 416 | } |
| 417 | return NULL; |
| 418 | } |
| 419 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 420 | ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
| 421 | for (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 | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 430 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 431 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 432 | ArtMethod* method = GetVirtualMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 433 | if (name == method->GetName() && method->GetSignature() == signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 434 | return method; |
| 435 | } |
| 436 | } |
| 437 | return NULL; |
| 438 | } |
| 439 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 440 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 441 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 442 | ArtMethod* method = GetVirtualMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 443 | if (name == method->GetName() && signature == method->GetSignature()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 444 | return method; |
| 445 | } |
| 446 | } |
| 447 | return NULL; |
| 448 | } |
| 449 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 450 | ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 451 | if (GetDexCache() == dex_cache) { |
| 452 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 453 | ArtMethod* method = GetVirtualMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 454 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 455 | return method; |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | return NULL; |
| 460 | } |
| 461 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 462 | ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) { |
| 463 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 464 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 465 | if (method != NULL) { |
| 466 | return method; |
| 467 | } |
| 468 | } |
| 469 | return NULL; |
| 470 | } |
| 471 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 472 | ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) { |
| 473 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 474 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
| 475 | if (method != NULL) { |
| 476 | return method; |
| 477 | } |
| 478 | } |
| 479 | return NULL; |
| 480 | } |
| 481 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 482 | ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
| 483 | for (Class* klass = this; klass != NULL; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 484 | ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 485 | if (method != NULL) { |
| 486 | return method; |
| 487 | } |
| 488 | } |
| 489 | return NULL; |
| 490 | } |
| 491 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 492 | ArtMethod* Class::FindClassInitializer() { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 493 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 494 | ArtMethod* method = GetDirectMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 495 | if (method->IsClassInitializer()) { |
| 496 | DCHECK_STREQ(method->GetName(), "<clinit>"); |
| 497 | DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V"); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 498 | return method; |
| 499 | } |
| 500 | } |
| 501 | return NULL; |
| 502 | } |
| 503 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 504 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 505 | // Is the field in this class? |
| 506 | // Interfaces are not relevant because they can't contain instance fields. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 507 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 508 | ArtField* f = GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 509 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 517 | if (GetDexCache() == dex_cache) { |
| 518 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 519 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 520 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 521 | return f; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | return NULL; |
| 526 | } |
| 527 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 528 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 529 | // Is the field in this class, or any of its superclasses? |
| 530 | // Interfaces are not relevant because they can't contain instance fields. |
| 531 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 532 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 533 | if (f != NULL) { |
| 534 | return f; |
| 535 | } |
| 536 | } |
| 537 | return NULL; |
| 538 | } |
| 539 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 540 | ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 541 | // Is the field in this class, or any of its superclasses? |
| 542 | // Interfaces are not relevant because they can't contain instance fields. |
| 543 | for (Class* c = this; c != NULL; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 544 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 545 | if (f != NULL) { |
| 546 | return f; |
| 547 | } |
| 548 | } |
| 549 | return NULL; |
| 550 | } |
| 551 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 552 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 553 | DCHECK(type != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 554 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 555 | ArtField* f = GetStaticField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 556 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 557 | return f; |
| 558 | } |
| 559 | } |
| 560 | return NULL; |
| 561 | } |
| 562 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 563 | ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 564 | if (dex_cache == GetDexCache()) { |
| 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 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 568 | return f; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | return NULL; |
| 573 | } |
| 574 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 575 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 576 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 577 | // Is the field in this class (or its interfaces), or any of its |
| 578 | // superclasses (or their interfaces)? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 579 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 580 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 581 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 582 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 583 | return f; |
| 584 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 585 | // Wrap k incase it moves during GetDirectInterface. |
| 586 | StackHandleScope<1> hs(self); |
| 587 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 588 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 589 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
| 590 | StackHandleScope<1> hs(self); |
| 591 | Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i))); |
| 592 | f = FindStaticField(self, interface, name, type); |
| 593 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 594 | return f; |
| 595 | } |
| 596 | } |
| 597 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 598 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 601 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache, |
| 602 | uint32_t dex_field_idx) { |
| 603 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 604 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 605 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 606 | if (f != NULL) { |
| 607 | return f; |
| 608 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 609 | // Wrap k incase it moves during GetDirectInterface. |
| 610 | StackHandleScope<1> hs(self); |
| 611 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 612 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 613 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
| 614 | StackHandleScope<1> hs(self); |
| 615 | Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i))); |
| 616 | f = FindStaticField(self, interface, dex_cache, dex_field_idx); |
| 617 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 618 | return f; |
| 619 | } |
| 620 | } |
| 621 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 622 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 623 | } |
| 624 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 625 | ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 626 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 627 | // Find a field using the JLS field resolution order |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 628 | for (Class* k = klass.Get(); k != NULL; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 629 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 630 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 631 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 632 | return f; |
| 633 | } |
| 634 | f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 635 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 636 | return f; |
| 637 | } |
| 638 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 639 | StackHandleScope<1> hs(self); |
| 640 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
| 641 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
| 642 | StackHandleScope<1> hs(self); |
| 643 | Handle<mirror::Class> interface(hs.NewHandle(GetDirectInterface(self, h_k, i))); |
| 644 | f = interface->FindStaticField(self, interface, name, type); |
| 645 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 646 | return f; |
| 647 | } |
| 648 | } |
| 649 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 650 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 653 | static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods) |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 654 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 655 | if (methods != NULL) { |
| 656 | for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 657 | mirror::ArtMethod* method = methods->GetWithoutChecks(index); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 658 | DCHECK(method != NULL); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 659 | if (!method->IsNative() && !method->IsAbstract()) { |
| 660 | method->SetPreverified(); |
| 661 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | void Class::SetPreverifiedFlagOnAllMethods() { |
| 667 | DCHECK(IsVerified()); |
| 668 | SetPreverifiedFlagOnMethods(GetDirectMethods()); |
| 669 | SetPreverifiedFlagOnMethods(GetVirtualMethods()); |
| 670 | } |
| 671 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 672 | std::string Class::GetDescriptor() { |
| 673 | if (UNLIKELY(IsArrayClass())) { |
| 674 | return GetArrayDescriptor(); |
| 675 | } else if (UNLIKELY(IsPrimitive())) { |
| 676 | return Primitive::Descriptor(GetPrimitiveType()); |
| 677 | } else if (UNLIKELY(IsProxyClass())) { |
| 678 | return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); |
| 679 | } else { |
| 680 | const DexFile& dex_file = GetDexFile(); |
| 681 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 682 | return dex_file.GetTypeDescriptor(type_id); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | std::string Class::GetArrayDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 687 | return "[" + GetComponentType()->GetDescriptor(); |
| 688 | } |
| 689 | |
| 690 | const DexFile::ClassDef* Class::GetClassDef() { |
| 691 | uint16_t class_def_idx = GetDexClassDefIndex(); |
| 692 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 693 | return nullptr; |
| 694 | } |
| 695 | return &GetDexFile().GetClassDef(class_def_idx); |
| 696 | } |
| 697 | |
| 698 | uint32_t Class::NumDirectInterfaces() { |
| 699 | if (IsPrimitive()) { |
| 700 | return 0; |
| 701 | } else if (IsArrayClass()) { |
| 702 | return 2; |
| 703 | } else if (IsProxyClass()) { |
| 704 | mirror::SynthesizedProxyClass* proxy_class= |
| 705 | reinterpret_cast<mirror::SynthesizedProxyClass*>(this); |
| 706 | mirror::ObjectArray<mirror::Class>* interfaces = proxy_class->GetInterfaces(); |
| 707 | return interfaces != nullptr ? interfaces->GetLength() : 0; |
| 708 | } else { |
| 709 | const DexFile::TypeList* interfaces = GetInterfaceTypeList(); |
| 710 | if (interfaces == nullptr) { |
| 711 | return 0; |
| 712 | } else { |
| 713 | return interfaces->Size(); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) { |
| 719 | DCHECK(!IsPrimitive()); |
| 720 | DCHECK(!IsArrayClass()); |
| 721 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 722 | } |
| 723 | |
| 724 | mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, uint32_t idx) { |
| 725 | DCHECK(klass.Get() != nullptr); |
| 726 | DCHECK(!klass->IsPrimitive()); |
| 727 | if (klass->IsArrayClass()) { |
| 728 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 729 | if (idx == 0) { |
| 730 | return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;"); |
| 731 | } else { |
| 732 | DCHECK_EQ(1U, idx); |
| 733 | return class_linker->FindSystemClass(self, "Ljava/io/Serializable;"); |
| 734 | } |
| 735 | } else if (klass->IsProxyClass()) { |
| 736 | mirror::SynthesizedProxyClass* proxy_class = |
| 737 | reinterpret_cast<mirror::SynthesizedProxyClass*>(klass.Get()); |
| 738 | mirror::ObjectArray<mirror::Class>* interfaces = proxy_class->GetInterfaces(); |
| 739 | DCHECK(interfaces != nullptr); |
| 740 | return interfaces->Get(idx); |
| 741 | } else { |
| 742 | uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
| 743 | mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx); |
| 744 | if (interface == nullptr) { |
| 745 | interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx, |
| 746 | klass.Get()); |
| 747 | CHECK(interface != nullptr || self->IsExceptionPending()); |
| 748 | } |
| 749 | return interface; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | const char* Class::GetSourceFile() { |
| 754 | std::string descriptor(GetDescriptor()); |
| 755 | const DexFile& dex_file = GetDexFile(); |
| 756 | const DexFile::ClassDef* dex_class_def = GetClassDef(); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 757 | if (dex_class_def == nullptr) { |
| 758 | // Generated classes have no class def. |
| 759 | return nullptr; |
| 760 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 761 | return dex_file.GetSourceFile(*dex_class_def); |
| 762 | } |
| 763 | |
| 764 | std::string Class::GetLocation() { |
| 765 | mirror::DexCache* dex_cache = GetDexCache(); |
| 766 | if (dex_cache != nullptr && !IsProxyClass()) { |
| 767 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 768 | } |
| 769 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 770 | return "generated class"; |
| 771 | } |
| 772 | |
| 773 | const DexFile::TypeList* Class::GetInterfaceTypeList() { |
| 774 | const DexFile::ClassDef* class_def = GetClassDef(); |
| 775 | if (class_def == nullptr) { |
| 776 | return nullptr; |
| 777 | } |
| 778 | return GetDexFile().GetInterfacesList(*class_def); |
| 779 | } |
| 780 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 781 | } // namespace mirror |
| 782 | } // namespace art |