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