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 | } |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 120 | 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] | 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) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 281 | if (kIsDebugBuild && (new_reference_offsets != kClassWalkSuper)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 282 | // Sanity check that the number of bits set in the reference offset bitmap |
| 283 | // agrees with the number of references |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 284 | uint32_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 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 288 | // +1 for the Class in Object. |
| 289 | CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 290 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 291 | // Not called within a transaction. |
| 292 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 293 | new_reference_offsets); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 294 | } |
| 295 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 296 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
| 297 | size_t i = 0; |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 298 | size_t min_length = std::min(descriptor1.size(), descriptor2.size()); |
| 299 | while (i < min_length && descriptor1[i] == descriptor2[i]) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 300 | ++i; |
| 301 | } |
| 302 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 303 | descriptor2.find('/', i) != StringPiece::npos) { |
| 304 | return false; |
| 305 | } else { |
| 306 | return true; |
| 307 | } |
| 308 | } |
| 309 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 310 | bool Class::IsInSamePackage(Class* that) { |
| 311 | Class* klass1 = this; |
| 312 | Class* klass2 = that; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 313 | if (klass1 == klass2) { |
| 314 | return true; |
| 315 | } |
| 316 | // Class loaders must match. |
| 317 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 318 | return false; |
| 319 | } |
| 320 | // Arrays are in the same package when their element classes are. |
| 321 | while (klass1->IsArrayClass()) { |
| 322 | klass1 = klass1->GetComponentType(); |
| 323 | } |
| 324 | while (klass2->IsArrayClass()) { |
| 325 | klass2 = klass2->GetComponentType(); |
| 326 | } |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 327 | // trivial check again for array types |
| 328 | if (klass1 == klass2) { |
| 329 | return true; |
| 330 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 331 | // Compare the package part of the descriptor string. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 332 | std::string temp1, temp2; |
| 333 | return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 336 | bool Class::IsStringClass() const { |
| 337 | return this == String::GetJavaLangString(); |
| 338 | } |
| 339 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 340 | bool Class::IsThrowableClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 341 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
| 342 | } |
| 343 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 344 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 345 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 346 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 347 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 348 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 349 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 352 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 353 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 354 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 355 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 356 | return method; |
| 357 | } |
| 358 | |
| 359 | int32_t iftable_count = GetIfTableCount(); |
| 360 | IfTable* iftable = GetIfTable(); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 361 | for (int32_t i = 0; i < iftable_count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 362 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 363 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 364 | return method; |
| 365 | } |
| 366 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 367 | return nullptr; |
| 368 | } |
| 369 | |
| 370 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature) { |
| 371 | // Check the current class before checking the interfaces. |
| 372 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature); |
| 373 | if (method != nullptr) { |
| 374 | return method; |
| 375 | } |
| 376 | |
| 377 | int32_t iftable_count = GetIfTableCount(); |
| 378 | IfTable* iftable = GetIfTable(); |
| 379 | for (int32_t i = 0; i < iftable_count; ++i) { |
| 380 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature); |
| 381 | if (method != nullptr) { |
| 382 | return method; |
| 383 | } |
| 384 | } |
| 385 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 388 | ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 389 | // Check the current class before checking the interfaces. |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 390 | ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 391 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 392 | return method; |
| 393 | } |
| 394 | |
| 395 | int32_t iftable_count = GetIfTableCount(); |
| 396 | IfTable* iftable = GetIfTable(); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 397 | for (int32_t i = 0; i < iftable_count; ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 398 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 399 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 400 | return method; |
| 401 | } |
| 402 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 403 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 406 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 407 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 408 | ArtMethod* method = GetDirectMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 409 | if (name == method->GetName() && method->GetSignature() == signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 410 | return method; |
| 411 | } |
| 412 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 413 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 416 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 417 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 418 | ArtMethod* method = GetDirectMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 419 | if (name == method->GetName() && signature == method->GetSignature()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 420 | return method; |
| 421 | } |
| 422 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 423 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 426 | ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 427 | if (GetDexCache() == dex_cache) { |
| 428 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 429 | ArtMethod* method = GetDirectMethod(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 430 | if (method->GetDexMethodIndex() == dex_method_idx) { |
| 431 | return method; |
| 432 | } |
| 433 | } |
| 434 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 435 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 436 | } |
| 437 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 438 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 439 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 440 | ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 441 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 442 | return method; |
| 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 Signature& signature) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 449 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -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 | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 452 | return method; |
| 453 | } |
| 454 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 455 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 458 | ArtMethod* Class::FindDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 459 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 460 | ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 461 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 462 | return method; |
| 463 | } |
| 464 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 465 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 468 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 469 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
| 470 | ArtMethod* method = GetVirtualMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 471 | if (name == method->GetName() && method->GetSignature() == signature) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 472 | return method; |
| 473 | } |
| 474 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 475 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 478 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 479 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 480 | ArtMethod* method = GetVirtualMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 481 | if (name == method->GetName() && signature == method->GetSignature()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 482 | return method; |
| 483 | } |
| 484 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 485 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 486 | } |
| 487 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 488 | ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 489 | if (GetDexCache() == dex_cache) { |
| 490 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 491 | ArtMethod* method = GetVirtualMethod(i); |
Brian Carlstrom | f322c4c | 2014-10-31 00:01:54 -0700 | [diff] [blame] | 492 | if (method->GetDexMethodIndex() == dex_method_idx && |
Andreas Gampe | 2b0fa5b | 2014-10-31 18:12:30 -0700 | [diff] [blame] | 493 | // A miranda method may have a different DexCache and is always created by linking, |
| 494 | // never *declared* in the class. |
| 495 | !method->IsMiranda()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 496 | return method; |
| 497 | } |
| 498 | } |
| 499 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 500 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 503 | ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const StringPiece& signature) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 504 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 505 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 506 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 507 | return method; |
| 508 | } |
| 509 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 510 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 511 | } |
| 512 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 513 | ArtMethod* Class::FindVirtualMethod(const StringPiece& name, const Signature& signature) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 514 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 515 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 516 | if (method != nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 517 | return method; |
| 518 | } |
| 519 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 520 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 523 | ArtMethod* Class::FindVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 524 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 525 | ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 526 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 527 | return method; |
| 528 | } |
| 529 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 530 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 533 | ArtMethod* Class::FindClassInitializer() { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 534 | for (size_t i = 0; i < NumDirectMethods(); ++i) { |
| 535 | ArtMethod* method = GetDirectMethod(i); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 536 | if (method->IsClassInitializer()) { |
| 537 | DCHECK_STREQ(method->GetName(), "<clinit>"); |
| 538 | DCHECK_STREQ(method->GetSignature().ToString().c_str(), "()V"); |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 539 | return method; |
| 540 | } |
| 541 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 542 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 545 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 546 | // Is the field in this class? |
| 547 | // Interfaces are not relevant because they can't contain instance fields. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 548 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 549 | ArtField* f = GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 550 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 551 | return f; |
| 552 | } |
| 553 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 554 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 555 | } |
| 556 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 557 | ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 558 | if (GetDexCache() == dex_cache) { |
| 559 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 560 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 561 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 562 | return f; |
| 563 | } |
| 564 | } |
| 565 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 566 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 569 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 570 | // Is the field in this class, or any of its superclasses? |
| 571 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 572 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 573 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 574 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 575 | return f; |
| 576 | } |
| 577 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 578 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 581 | ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 582 | // Is the field in this class, or any of its superclasses? |
| 583 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 584 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 585 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 586 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 587 | return f; |
| 588 | } |
| 589 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 590 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 591 | } |
| 592 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 593 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 594 | DCHECK(type != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 595 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 596 | ArtField* f = GetStaticField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 597 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 598 | return f; |
| 599 | } |
| 600 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 601 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 602 | } |
| 603 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 604 | ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 605 | if (dex_cache == GetDexCache()) { |
| 606 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 607 | ArtField* f = GetStaticField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 608 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 609 | return f; |
| 610 | } |
| 611 | } |
| 612 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 613 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 614 | } |
| 615 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 616 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 617 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 618 | // Is the field in this class (or its interfaces), or any of its |
| 619 | // superclasses (or their interfaces)? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 620 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 621 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 622 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 623 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 624 | return f; |
| 625 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 626 | // Wrap k incase it moves during GetDirectInterface. |
| 627 | StackHandleScope<1> hs(self); |
| 628 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 629 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 630 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame^] | 631 | StackHandleScope<1> hs2(self); |
| 632 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 633 | f = FindStaticField(self, interface, name, type); |
| 634 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 635 | return f; |
| 636 | } |
| 637 | } |
| 638 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 639 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 642 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache, |
| 643 | uint32_t dex_field_idx) { |
| 644 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 645 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 646 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 647 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 648 | return f; |
| 649 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 650 | // Wrap k incase it moves during GetDirectInterface. |
| 651 | StackHandleScope<1> hs(self); |
| 652 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 653 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 654 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame^] | 655 | StackHandleScope<1> hs2(self); |
| 656 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 657 | f = FindStaticField(self, interface, dex_cache, dex_field_idx); |
| 658 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 659 | return f; |
| 660 | } |
| 661 | } |
| 662 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 663 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 666 | ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 667 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 668 | // Find a field using the JLS field resolution order |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 669 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 670 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 671 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 672 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 673 | return f; |
| 674 | } |
| 675 | f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 676 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 677 | return f; |
| 678 | } |
| 679 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 680 | StackHandleScope<1> hs(self); |
| 681 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
| 682 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame^] | 683 | StackHandleScope<1> hs2(self); |
| 684 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 685 | f = interface->FindStaticField(self, interface, name, type); |
| 686 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 687 | return f; |
| 688 | } |
| 689 | } |
| 690 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 691 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 694 | static void SetPreverifiedFlagOnMethods(mirror::ObjectArray<mirror::ArtMethod>* methods) |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 695 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 696 | if (methods != nullptr) { |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 697 | for (int32_t index = 0, end = methods->GetLength(); index < end; ++index) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 698 | mirror::ArtMethod* method = methods->GetWithoutChecks(index); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 699 | DCHECK(method != nullptr); |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 700 | if (!method->IsNative() && !method->IsAbstract()) { |
| 701 | method->SetPreverified(); |
| 702 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | void Class::SetPreverifiedFlagOnAllMethods() { |
| 708 | DCHECK(IsVerified()); |
| 709 | SetPreverifiedFlagOnMethods(GetDirectMethods()); |
| 710 | SetPreverifiedFlagOnMethods(GetVirtualMethods()); |
| 711 | } |
| 712 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 713 | const char* Class::GetDescriptor(std::string* storage) { |
| 714 | if (IsPrimitive()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 715 | return Primitive::Descriptor(GetPrimitiveType()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 716 | } else if (IsArrayClass()) { |
| 717 | return GetArrayDescriptor(storage); |
| 718 | } else if (IsProxyClass()) { |
| 719 | *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); |
| 720 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 721 | } else { |
| 722 | const DexFile& dex_file = GetDexFile(); |
| 723 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 724 | return dex_file.GetTypeDescriptor(type_id); |
| 725 | } |
| 726 | } |
| 727 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 728 | const char* Class::GetArrayDescriptor(std::string* storage) { |
| 729 | std::string temp; |
| 730 | const char* elem_desc = GetComponentType()->GetDescriptor(&temp); |
| 731 | *storage = "["; |
| 732 | *storage += elem_desc; |
| 733 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | const DexFile::ClassDef* Class::GetClassDef() { |
| 737 | uint16_t class_def_idx = GetDexClassDefIndex(); |
| 738 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 739 | return nullptr; |
| 740 | } |
| 741 | return &GetDexFile().GetClassDef(class_def_idx); |
| 742 | } |
| 743 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 744 | uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) { |
| 745 | DCHECK(!IsPrimitive()); |
| 746 | DCHECK(!IsArrayClass()); |
| 747 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 748 | } |
| 749 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 750 | mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 751 | uint32_t idx) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 752 | DCHECK(klass.Get() != nullptr); |
| 753 | DCHECK(!klass->IsPrimitive()); |
| 754 | if (klass->IsArrayClass()) { |
| 755 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 756 | if (idx == 0) { |
| 757 | return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;"); |
| 758 | } else { |
| 759 | DCHECK_EQ(1U, idx); |
| 760 | return class_linker->FindSystemClass(self, "Ljava/io/Serializable;"); |
| 761 | } |
| 762 | } else if (klass->IsProxyClass()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 763 | mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 764 | DCHECK(interfaces != nullptr); |
| 765 | return interfaces->Get(idx); |
| 766 | } else { |
| 767 | uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
| 768 | mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx); |
| 769 | if (interface == nullptr) { |
| 770 | interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx, |
| 771 | klass.Get()); |
| 772 | CHECK(interface != nullptr || self->IsExceptionPending()); |
| 773 | } |
| 774 | return interface; |
| 775 | } |
| 776 | } |
| 777 | |
| 778 | const char* Class::GetSourceFile() { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 779 | const DexFile& dex_file = GetDexFile(); |
| 780 | const DexFile::ClassDef* dex_class_def = GetClassDef(); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 781 | if (dex_class_def == nullptr) { |
| 782 | // Generated classes have no class def. |
| 783 | return nullptr; |
| 784 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 785 | return dex_file.GetSourceFile(*dex_class_def); |
| 786 | } |
| 787 | |
| 788 | std::string Class::GetLocation() { |
| 789 | mirror::DexCache* dex_cache = GetDexCache(); |
| 790 | if (dex_cache != nullptr && !IsProxyClass()) { |
| 791 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 792 | } |
| 793 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 794 | return "generated class"; |
| 795 | } |
| 796 | |
| 797 | const DexFile::TypeList* Class::GetInterfaceTypeList() { |
| 798 | const DexFile::ClassDef* class_def = GetClassDef(); |
| 799 | if (class_def == nullptr) { |
| 800 | return nullptr; |
| 801 | } |
| 802 | return GetDexFile().GetInterfacesList(*class_def); |
| 803 | } |
| 804 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 805 | void Class::PopulateEmbeddedImtAndVTable(StackHandleScope<kImtSize>* imt_handle_scope) { |
| 806 | for (uint32_t i = 0; i < kImtSize; i++) { |
| 807 | // Replace null with conflict. |
| 808 | mirror::Object* obj = imt_handle_scope->GetReference(i); |
| 809 | DCHECK(obj != nullptr); |
| 810 | SetEmbeddedImTableEntry(i, obj->AsArtMethod()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 813 | ObjectArray<ArtMethod>* table = GetVTableDuringLinking(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 814 | CHECK(table != nullptr) << PrettyClass(this); |
| 815 | SetEmbeddedVTableLength(table->GetLength()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 816 | for (int32_t i = 0; i < table->GetLength(); i++) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 817 | SetEmbeddedVTableEntry(i, table->GetWithoutChecks(i)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 818 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 819 | |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 820 | // Keep java.lang.Object class's vtable around for since it's easier |
| 821 | // to be reused by array classes during their linking. |
| 822 | if (!IsObjectClass()) { |
| 823 | SetVTable(nullptr); |
| 824 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 827 | // The pre-fence visitor for Class::CopyOf(). |
| 828 | class CopyClassVisitor { |
| 829 | public: |
| 830 | explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 831 | size_t new_length, size_t copy_bytes, |
| 832 | StackHandleScope<mirror::Class::kImtSize>* imt_handle_scope) |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 833 | : self_(self), orig_(orig), new_length_(new_length), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 834 | copy_bytes_(copy_bytes), imt_handle_scope_(imt_handle_scope) { |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | void operator()(Object* obj, size_t usable_size) const |
| 838 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 839 | UNUSED(usable_size); |
| 840 | mirror::Class* new_class_obj = obj->AsClass(); |
| 841 | mirror::Object::CopyObject(self_, new_class_obj, orig_->Get(), copy_bytes_); |
| 842 | new_class_obj->SetStatus(Class::kStatusResolving, self_); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 843 | new_class_obj->PopulateEmbeddedImtAndVTable(imt_handle_scope_); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 844 | new_class_obj->SetClassSize(new_length_); |
| 845 | } |
| 846 | |
| 847 | private: |
| 848 | Thread* const self_; |
| 849 | Handle<mirror::Class>* const orig_; |
| 850 | const size_t new_length_; |
| 851 | const size_t copy_bytes_; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 852 | StackHandleScope<mirror::Class::kImtSize>* const imt_handle_scope_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 853 | DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); |
| 854 | }; |
| 855 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 856 | Class* Class::CopyOf(Thread* self, int32_t new_length, |
| 857 | StackHandleScope<kImtSize>* imt_handle_scope) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 858 | DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); |
| 859 | // We may get copied by a compacting GC. |
| 860 | StackHandleScope<1> hs(self); |
| 861 | Handle<mirror::Class> h_this(hs.NewHandle(this)); |
| 862 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 863 | // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() |
| 864 | // to skip copying the tail part that we will overwrite here. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 865 | CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt_handle_scope); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 866 | mirror::Object* new_class = |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 867 | kMovingClasses |
| 868 | ? heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) |
| 869 | : heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 870 | if (UNLIKELY(new_class == nullptr)) { |
| 871 | CHECK(self->IsExceptionPending()); // Expect an OOME. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 872 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 873 | } |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 874 | return new_class->AsClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 877 | } // namespace mirror |
| 878 | } // namespace art |