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