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