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" |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 21 | #include "class_linker-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 28 | #include "method.h" |
Ian Rogers | 22d5e73 | 2014-07-15 22:23:51 -0700 | [diff] [blame] | 29 | #include "object_array-inl.h" |
| 30 | #include "object-inl.h" |
| 31 | #include "runtime.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "thread.h" |
| 33 | #include "throwable.h" |
| 34 | #include "utils.h" |
| 35 | #include "well_known_classes.h" |
| 36 | |
| 37 | namespace art { |
| 38 | namespace mirror { |
| 39 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 40 | GcRoot<Class> Class::java_lang_Class_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 41 | |
| 42 | void Class::SetClassClass(Class* java_lang_Class) { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 43 | CHECK(java_lang_Class_.IsNull()) |
| 44 | << java_lang_Class_.Read() |
Hiroshi Yamauchi | 4f1ebc2 | 2014-06-25 14:30:41 -0700 | [diff] [blame] | 45 | << " " << java_lang_Class; |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 46 | CHECK(java_lang_Class != nullptr); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 47 | java_lang_Class->SetClassFlags(mirror::kClassFlagClass); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 48 | java_lang_Class_ = GcRoot<Class>(java_lang_Class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void Class::ResetClass() { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 52 | CHECK(!java_lang_Class_.IsNull()); |
| 53 | java_lang_Class_ = GcRoot<Class>(nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 56 | void Class::VisitRoots(RootVisitor* visitor) { |
| 57 | java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 60 | inline void Class::SetVerifyError(mirror::Object* error) { |
| 61 | CHECK(error != nullptr) << PrettyClass(this); |
| 62 | if (Runtime::Current()->IsActiveTransaction()) { |
| 63 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error); |
| 64 | } else { |
| 65 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, verify_error_), error); |
| 66 | } |
| 67 | } |
| 68 | |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 69 | void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) { |
| 70 | Status old_status = h_this->GetStatus(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 71 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 72 | bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 73 | if (LIKELY(class_linker_initialized)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 74 | if (UNLIKELY(new_status <= old_status && new_status != kStatusError && |
| 75 | new_status != kStatusRetired)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 76 | LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get()) |
| 77 | << " " << old_status << " -> " << new_status; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 78 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 79 | if (new_status >= kStatusResolved || old_status >= kStatusResolved) { |
| 80 | // When classes are being resolved the resolution code should hold the lock. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 81 | CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId()) |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 82 | << "Attempt to change status of class while not holding its lock: " |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 83 | << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 84 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 85 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 86 | if (UNLIKELY(new_status == kStatusError)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 87 | CHECK_NE(h_this->GetStatus(), kStatusError) |
| 88 | << "Attempt to set as erroneous an already erroneous class " |
| 89 | << PrettyClass(h_this.Get()); |
Andreas Gampe | 31decb1 | 2015-08-24 21:09:05 -0700 | [diff] [blame] | 90 | if (VLOG_IS_ON(class_linker)) { |
| 91 | LOG(ERROR) << "Setting " << PrettyDescriptor(h_this.Get()) << " to erroneous."; |
| 92 | if (self->IsExceptionPending()) { |
| 93 | LOG(ERROR) << "Exception: " << self->GetException()->Dump(); |
| 94 | } |
| 95 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 96 | |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 97 | // Remember the current exception. |
| 98 | CHECK(self->GetException() != nullptr); |
| 99 | h_this->SetVerifyError(self->GetException()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | } |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 101 | 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] | 102 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 103 | h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 104 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 105 | h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 106 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 107 | |
| 108 | if (!class_linker_initialized) { |
| 109 | // When the class linker is being initialized its single threaded and by definition there can be |
| 110 | // no waiters. During initialization classes may appear temporary but won't be retired as their |
| 111 | // size was statically computed. |
| 112 | } else { |
| 113 | // Classes that are being resolved or initialized need to notify waiters that the class status |
| 114 | // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 115 | if (h_this->IsTemp()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 116 | // Class is a temporary one, ensure that waiters for resolution get notified of retirement |
| 117 | // so that they can grab the new version of the class from the class linker's table. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 118 | CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 119 | if (new_status == kStatusRetired || new_status == kStatusError) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 120 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 121 | } |
| 122 | } else { |
| 123 | CHECK_NE(new_status, kStatusRetired); |
| 124 | if (old_status >= kStatusResolved || new_status >= kStatusResolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 125 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 128 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 131 | void Class::SetDexCache(DexCache* new_dex_cache) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 132 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 133 | SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 136 | void Class::SetClassSize(uint32_t new_class_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 137 | if (kIsDebugBuild && new_class_size < GetClassSize()) { |
| 138 | DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail); |
| 139 | LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize(); |
| 140 | LOG(FATAL) << " class=" << PrettyTypeOf(this); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 141 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 142 | // Not called within a transaction. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 143 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 147 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 148 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 149 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 150 | String* Class::ComputeName(Handle<Class> h_this) { |
| 151 | String* name = h_this->GetName(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 152 | if (name != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 153 | return name; |
| 154 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 155 | std::string temp; |
| 156 | const char* descriptor = h_this->GetDescriptor(&temp); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 157 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 158 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 159 | // The descriptor indicates that this is the class for |
| 160 | // a primitive type; special-case the return value. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 161 | const char* c_name = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 162 | switch (descriptor[0]) { |
| 163 | case 'Z': c_name = "boolean"; break; |
| 164 | case 'B': c_name = "byte"; break; |
| 165 | case 'C': c_name = "char"; break; |
| 166 | case 'S': c_name = "short"; break; |
| 167 | case 'I': c_name = "int"; break; |
| 168 | case 'J': c_name = "long"; break; |
| 169 | case 'F': c_name = "float"; break; |
| 170 | case 'D': c_name = "double"; break; |
| 171 | case 'V': c_name = "void"; break; |
| 172 | default: |
| 173 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 174 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 175 | name = String::AllocFromModifiedUtf8(self, c_name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 176 | } else { |
| 177 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 178 | // components. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 179 | name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 181 | h_this->SetName(name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 182 | return name; |
| 183 | } |
| 184 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 185 | void Class::DumpClass(std::ostream& os, int flags) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 186 | if ((flags & kDumpClassFullDetail) == 0) { |
| 187 | os << PrettyClass(this); |
| 188 | if ((flags & kDumpClassClassLoader) != 0) { |
| 189 | os << ' ' << GetClassLoader(); |
| 190 | } |
| 191 | if ((flags & kDumpClassInitialized) != 0) { |
| 192 | os << ' ' << GetStatus(); |
| 193 | } |
| 194 | os << "\n"; |
| 195 | return; |
| 196 | } |
| 197 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 198 | Thread* const self = Thread::Current(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 199 | StackHandleScope<2> hs(self); |
| 200 | Handle<mirror::Class> h_this(hs.NewHandle(this)); |
| 201 | Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 202 | auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 203 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 204 | std::string temp; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 205 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 206 | << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 207 | os << " objectSize=" << SizeOf() << " " |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 208 | << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 209 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 210 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 211 | if (h_super.Get() != nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 212 | os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader() |
| 213 | << ")\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 214 | } |
| 215 | if (IsArrayClass()) { |
| 216 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 217 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 218 | const size_t num_direct_interfaces = NumDirectInterfaces(); |
| 219 | if (num_direct_interfaces > 0) { |
| 220 | os << " interfaces (" << num_direct_interfaces << "):\n"; |
| 221 | for (size_t i = 0; i < num_direct_interfaces; ++i) { |
| 222 | Class* interface = GetDirectInterface(self, h_this, i); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 223 | if (interface == nullptr) { |
| 224 | os << StringPrintf(" %2zd: nullptr!\n", i); |
| 225 | } else { |
| 226 | const ClassLoader* cl = interface->GetClassLoader(); |
| 227 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
| 228 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 231 | if (!IsLoaded()) { |
| 232 | os << " class not yet loaded"; |
| 233 | } else { |
| 234 | // After this point, this may have moved due to GetDirectInterface. |
| 235 | os << " vtable (" << h_this->NumVirtualMethods() << " entries, " |
| 236 | << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n"; |
| 237 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 238 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod( |
| 239 | h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 240 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 241 | os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; |
| 242 | for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 243 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod( |
| 244 | h_this->GetDirectMethod(i, image_pointer_size)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 245 | } |
| 246 | if (h_this->NumStaticFields() > 0) { |
| 247 | os << " static fields (" << h_this->NumStaticFields() << " entries):\n"; |
| 248 | if (h_this->IsResolved() || h_this->IsErroneous()) { |
| 249 | for (size_t i = 0; i < h_this->NumStaticFields(); ++i) { |
| 250 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetStaticField(i)).c_str()); |
| 251 | } |
| 252 | } else { |
| 253 | os << " <not yet available>"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 254 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 255 | } |
| 256 | if (h_this->NumInstanceFields() > 0) { |
| 257 | os << " instance fields (" << h_this->NumInstanceFields() << " entries):\n"; |
| 258 | if (h_this->IsResolved() || h_this->IsErroneous()) { |
| 259 | for (size_t i = 0; i < h_this->NumInstanceFields(); ++i) { |
| 260 | os << StringPrintf(" %2zd: %s\n", i, PrettyField(h_this->GetInstanceField(i)).c_str()); |
| 261 | } |
| 262 | } else { |
| 263 | os << " <not yet available>"; |
| 264 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | void Class::SetReferenceInstanceOffsets(uint32_t new_reference_offsets) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 270 | if (kIsDebugBuild && new_reference_offsets != kClassWalkSuper) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 271 | // Sanity check that the number of bits set in the reference offset bitmap |
| 272 | // agrees with the number of references |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 273 | uint32_t count = 0; |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 274 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 275 | count += c->NumReferenceInstanceFieldsDuringLinking(); |
| 276 | } |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 277 | // +1 for the Class in Object. |
| 278 | CHECK_EQ(static_cast<uint32_t>(POPCOUNT(new_reference_offsets)) + 1, count); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 279 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 280 | // Not called within a transaction. |
| 281 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, reference_instance_offsets_), |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 282 | new_reference_offsets); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 283 | } |
| 284 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 285 | bool Class::IsInSamePackage(const StringPiece& descriptor1, const StringPiece& descriptor2) { |
| 286 | size_t i = 0; |
Ian Rogers | 6b604a1 | 2014-09-25 15:35:37 -0700 | [diff] [blame] | 287 | size_t min_length = std::min(descriptor1.size(), descriptor2.size()); |
| 288 | while (i < min_length && descriptor1[i] == descriptor2[i]) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 289 | ++i; |
| 290 | } |
| 291 | if (descriptor1.find('/', i) != StringPiece::npos || |
| 292 | descriptor2.find('/', i) != StringPiece::npos) { |
| 293 | return false; |
| 294 | } else { |
| 295 | return true; |
| 296 | } |
| 297 | } |
| 298 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 299 | bool Class::IsInSamePackage(Class* that) { |
| 300 | Class* klass1 = this; |
| 301 | Class* klass2 = that; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 302 | if (klass1 == klass2) { |
| 303 | return true; |
| 304 | } |
| 305 | // Class loaders must match. |
| 306 | if (klass1->GetClassLoader() != klass2->GetClassLoader()) { |
| 307 | return false; |
| 308 | } |
| 309 | // Arrays are in the same package when their element classes are. |
| 310 | while (klass1->IsArrayClass()) { |
| 311 | klass1 = klass1->GetComponentType(); |
| 312 | } |
| 313 | while (klass2->IsArrayClass()) { |
| 314 | klass2 = klass2->GetComponentType(); |
| 315 | } |
Anwar Ghuloum | 9fa3f20 | 2013-03-26 14:32:54 -0700 | [diff] [blame] | 316 | // trivial check again for array types |
| 317 | if (klass1 == klass2) { |
| 318 | return true; |
| 319 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 320 | // Compare the package part of the descriptor string. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 321 | std::string temp1, temp2; |
| 322 | return IsInSamePackage(klass1->GetDescriptor(&temp1), klass2->GetDescriptor(&temp2)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 323 | } |
| 324 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 325 | bool Class::IsThrowableClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 326 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
| 327 | } |
| 328 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 329 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 330 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 331 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 332 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 333 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 334 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 335 | } |
| 336 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 337 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature, |
| 338 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 339 | // Check the current class before checking the interfaces. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 340 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 341 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 342 | return method; |
| 343 | } |
| 344 | |
| 345 | int32_t iftable_count = GetIfTableCount(); |
| 346 | IfTable* iftable = GetIfTable(); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 347 | for (int32_t i = 0; i < iftable_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 348 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 349 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 350 | return method; |
| 351 | } |
| 352 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 353 | return nullptr; |
| 354 | } |
| 355 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 356 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature, |
| 357 | size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 358 | // Check the current class before checking the interfaces. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 359 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 360 | if (method != nullptr) { |
| 361 | return method; |
| 362 | } |
| 363 | |
| 364 | int32_t iftable_count = GetIfTableCount(); |
| 365 | IfTable* iftable = GetIfTable(); |
| 366 | for (int32_t i = 0; i < iftable_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 367 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 368 | if (method != nullptr) { |
| 369 | return method; |
| 370 | } |
| 371 | } |
| 372 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 373 | } |
| 374 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 375 | ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx, |
| 376 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 377 | // Check the current class before checking the interfaces. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 378 | ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 379 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 380 | return method; |
| 381 | } |
| 382 | |
| 383 | int32_t iftable_count = GetIfTableCount(); |
| 384 | IfTable* iftable = GetIfTable(); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 385 | for (int32_t i = 0; i < iftable_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 386 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod( |
| 387 | dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 388 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 389 | return method; |
| 390 | } |
| 391 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 392 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 395 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature, |
| 396 | size_t pointer_size) { |
| 397 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 398 | if (name == method.GetName() && method.GetSignature() == signature) { |
| 399 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 402 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 405 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature, |
| 406 | size_t pointer_size) { |
| 407 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 408 | if (name == method.GetName() && signature == method.GetSignature()) { |
| 409 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 410 | } |
| 411 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 412 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 413 | } |
| 414 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 415 | ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx, |
| 416 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 417 | if (GetDexCache() == dex_cache) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 418 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 419 | if (method.GetDexMethodIndex() == dex_method_idx) { |
| 420 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 424 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 427 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& signature, |
| 428 | size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 429 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 430 | ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 431 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 432 | return method; |
| 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 438 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature, |
| 439 | size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 440 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 441 | ArtMethod* method = klass->FindDeclaredDirectMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 442 | if (method != nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 443 | return method; |
| 444 | } |
| 445 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 446 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 449 | ArtMethod* Class::FindDirectMethod( |
| 450 | const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 451 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 452 | ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 453 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 454 | return method; |
| 455 | } |
| 456 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 457 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 460 | ArtMethod* Class::FindDeclaredDirectMethodByName(const StringPiece& name, size_t pointer_size) { |
| 461 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 462 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 463 | if (name == np_method->GetName()) { |
| 464 | return &method; |
| 465 | } |
| 466 | } |
| 467 | return nullptr; |
| 468 | } |
| 469 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 470 | // TODO These should maybe be changed to be named FindOwnedVirtualMethod or something similar |
| 471 | // because they do not only find 'declared' methods and will return copied methods. This behavior is |
| 472 | // desired and correct but the naming can lead to confusion because in the java language declared |
| 473 | // excludes interface methods which might be found by this. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 474 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature, |
| 475 | size_t pointer_size) { |
| 476 | for (auto& method : GetVirtualMethods(pointer_size)) { |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 477 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 478 | if (name == np_method->GetName() && np_method->GetSignature() == signature) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 479 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 482 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 485 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature, |
| 486 | size_t pointer_size) { |
| 487 | for (auto& method : GetVirtualMethods(pointer_size)) { |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 488 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 489 | if (name == np_method->GetName() && signature == np_method->GetSignature()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 490 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 493 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 496 | ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx, |
| 497 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 498 | if (GetDexCache() == dex_cache) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 499 | for (auto& method : GetDeclaredVirtualMethods(pointer_size)) { |
| 500 | if (method.GetDexMethodIndex() == dex_method_idx) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 501 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 505 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 506 | } |
| 507 | |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 508 | ArtMethod* Class::FindDeclaredVirtualMethodByName(const StringPiece& name, size_t pointer_size) { |
| 509 | for (auto& method : GetVirtualMethods(pointer_size)) { |
| 510 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 511 | if (name == np_method->GetName()) { |
| 512 | return &method; |
| 513 | } |
| 514 | } |
| 515 | return nullptr; |
| 516 | } |
| 517 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 518 | ArtMethod* Class::FindVirtualMethod( |
| 519 | const StringPiece& name, const StringPiece& signature, size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 520 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 521 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 522 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 523 | return method; |
| 524 | } |
| 525 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 526 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 527 | } |
| 528 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 529 | ArtMethod* Class::FindVirtualMethod( |
| 530 | const StringPiece& name, const Signature& signature, size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 531 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 532 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 533 | if (method != nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 534 | return method; |
| 535 | } |
| 536 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 537 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 540 | ArtMethod* Class::FindVirtualMethod( |
| 541 | const DexCache* dex_cache, uint32_t dex_method_idx, size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 542 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 543 | ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 544 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 545 | return method; |
| 546 | } |
| 547 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 548 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 549 | } |
| 550 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 551 | ArtMethod* Class::FindVirtualMethodForInterfaceSuper(ArtMethod* method, size_t pointer_size) { |
| 552 | DCHECK(method->GetDeclaringClass()->IsInterface()); |
| 553 | DCHECK(IsInterface()) << "Should only be called on a interface class"; |
| 554 | // Check if we have one defined on this interface first. This includes searching copied ones to |
| 555 | // get any conflict methods. Conflict methods are copied into each subtype from the supertype. We |
| 556 | // don't do any indirect method checks here. |
| 557 | for (ArtMethod& iface_method : GetVirtualMethods(pointer_size)) { |
| 558 | if (method->HasSameNameAndSignature(&iface_method)) { |
| 559 | return &iface_method; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | std::vector<ArtMethod*> abstract_methods; |
| 564 | // Search through the IFTable for a working version. We don't need to check for conflicts |
| 565 | // because if there was one it would appear in this classes virtual_methods_ above. |
| 566 | |
| 567 | Thread* self = Thread::Current(); |
| 568 | StackHandleScope<2> hs(self); |
| 569 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(GetIfTable())); |
| 570 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 571 | size_t iftable_count = GetIfTableCount(); |
| 572 | // Find the method. We don't need to check for conflicts because they would have been in the |
| 573 | // copied virtuals of this interface. Order matters, traverse in reverse topological order; most |
| 574 | // subtypiest interfaces get visited first. |
| 575 | for (size_t k = iftable_count; k != 0;) { |
| 576 | k--; |
| 577 | DCHECK_LT(k, iftable->Count()); |
| 578 | iface.Assign(iftable->GetInterface(k)); |
| 579 | // Iterate through every declared method on this interface. Each direct method's name/signature |
| 580 | // is unique so the order of the inner loop doesn't matter. |
| 581 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(pointer_size)) { |
| 582 | ArtMethod* current_method = &method_iter; |
| 583 | if (current_method->HasSameNameAndSignature(method)) { |
| 584 | if (current_method->IsDefault()) { |
| 585 | // Handle JLS soft errors, a default method from another superinterface tree can |
| 586 | // "override" an abstract method(s) from another superinterface tree(s). To do this, |
| 587 | // ignore any [default] method which are dominated by the abstract methods we've seen so |
| 588 | // far. Check if overridden by any in abstract_methods. We do not need to check for |
| 589 | // default_conflicts because we would hit those before we get to this loop. |
| 590 | bool overridden = false; |
| 591 | for (ArtMethod* possible_override : abstract_methods) { |
| 592 | DCHECK(possible_override->HasSameNameAndSignature(current_method)); |
| 593 | if (iface->IsAssignableFrom(possible_override->GetDeclaringClass())) { |
| 594 | overridden = true; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | if (!overridden) { |
| 599 | return current_method; |
| 600 | } |
| 601 | } else { |
| 602 | // Is not default. |
| 603 | // This might override another default method. Just stash it for now. |
| 604 | abstract_methods.push_back(current_method); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | } |
| 609 | // If we reach here we either never found any declaration of the method (in which case |
| 610 | // 'abstract_methods' is empty or we found no non-overriden default methods in which case |
| 611 | // 'abstract_methods' contains a number of abstract implementations of the methods. We choose one |
| 612 | // of these arbitrarily. |
| 613 | return abstract_methods.empty() ? nullptr : abstract_methods[0]; |
| 614 | } |
| 615 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 616 | ArtMethod* Class::FindClassInitializer(size_t pointer_size) { |
| 617 | for (ArtMethod& method : GetDirectMethods(pointer_size)) { |
| 618 | if (method.IsClassInitializer()) { |
| 619 | DCHECK_STREQ(method.GetName(), "<clinit>"); |
| 620 | DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V"); |
| 621 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 624 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 627 | // Custom binary search to avoid double comparisons from std::binary_search. |
| 628 | static ArtField* FindFieldByNameAndType(LengthPrefixedArray<ArtField>* fields, |
| 629 | const StringPiece& name, |
| 630 | const StringPiece& type) |
| 631 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 632 | if (fields == nullptr) { |
| 633 | return nullptr; |
| 634 | } |
| 635 | size_t low = 0; |
Vladimir Marko | 35831e8 | 2015-09-11 11:59:18 +0100 | [diff] [blame] | 636 | size_t high = fields->size(); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 637 | ArtField* ret = nullptr; |
| 638 | while (low < high) { |
| 639 | size_t mid = (low + high) / 2; |
| 640 | ArtField& field = fields->At(mid); |
| 641 | // Fields are sorted by class, then name, then type descriptor. This is verified in dex file |
| 642 | // verifier. There can be multiple fields with the same in the same class name due to proguard. |
| 643 | int result = StringPiece(field.GetName()).Compare(name); |
| 644 | if (result == 0) { |
| 645 | result = StringPiece(field.GetTypeDescriptor()).Compare(type); |
| 646 | } |
| 647 | if (result < 0) { |
| 648 | low = mid + 1; |
| 649 | } else if (result > 0) { |
| 650 | high = mid; |
| 651 | } else { |
| 652 | ret = &field; |
| 653 | break; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 654 | } |
| 655 | } |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 656 | if (kIsDebugBuild) { |
| 657 | ArtField* found = nullptr; |
| 658 | for (ArtField& field : MakeIterationRangeFromLengthPrefixedArray(fields)) { |
| 659 | if (name == field.GetName() && type == field.GetTypeDescriptor()) { |
| 660 | found = &field; |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | CHECK_EQ(found, ret) << "Found " << PrettyField(found) << " vs " << PrettyField(ret); |
| 665 | } |
| 666 | return ret; |
| 667 | } |
| 668 | |
| 669 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
| 670 | // Binary search by name. Interfaces are not relevant because they can't contain instance fields. |
| 671 | return FindFieldByNameAndType(GetIFieldsPtr(), name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 674 | ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 675 | if (GetDexCache() == dex_cache) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 676 | for (ArtField& field : GetIFields()) { |
| 677 | if (field.GetDexFieldIndex() == dex_field_idx) { |
| 678 | return &field; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 682 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 683 | } |
| 684 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 685 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 686 | // Is the field in this class, or any of its superclasses? |
| 687 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 688 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 689 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 690 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 691 | return f; |
| 692 | } |
| 693 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 694 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 697 | ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 698 | // Is the field in this class, or any of its superclasses? |
| 699 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 700 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 701 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 702 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 703 | return f; |
| 704 | } |
| 705 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 706 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 707 | } |
| 708 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 709 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 710 | DCHECK(type != nullptr); |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 711 | return FindFieldByNameAndType(GetSFieldsPtr(), name, type); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 714 | ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 715 | if (dex_cache == GetDexCache()) { |
Mathieu Chartier | e2aa326 | 2015-10-20 18:30:03 -0700 | [diff] [blame] | 716 | for (ArtField& field : GetSFields()) { |
| 717 | if (field.GetDexFieldIndex() == dex_field_idx) { |
| 718 | return &field; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 719 | } |
| 720 | } |
| 721 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 722 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 723 | } |
| 724 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 725 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 726 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 727 | // Is the field in this class (or its interfaces), or any of its |
| 728 | // superclasses (or their interfaces)? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 729 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 730 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 731 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 732 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 733 | return f; |
| 734 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 735 | // Wrap k incase it moves during GetDirectInterface. |
| 736 | StackHandleScope<1> hs(self); |
| 737 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 738 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 739 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 740 | StackHandleScope<1> hs2(self); |
| 741 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 742 | f = FindStaticField(self, interface, name, type); |
| 743 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 744 | return f; |
| 745 | } |
| 746 | } |
| 747 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 748 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 751 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache, |
| 752 | uint32_t dex_field_idx) { |
| 753 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 754 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 755 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 756 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 757 | return f; |
| 758 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 759 | // Wrap k incase it moves during GetDirectInterface. |
| 760 | StackHandleScope<1> hs(self); |
| 761 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 762 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 763 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 764 | StackHandleScope<1> hs2(self); |
| 765 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 766 | f = FindStaticField(self, interface, dex_cache, dex_field_idx); |
| 767 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 768 | return f; |
| 769 | } |
| 770 | } |
| 771 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 772 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 775 | ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 776 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 777 | // Find a field using the JLS field resolution order |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 778 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 779 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 780 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 781 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 782 | return f; |
| 783 | } |
| 784 | f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 785 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 786 | return f; |
| 787 | } |
| 788 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 789 | StackHandleScope<1> hs(self); |
| 790 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
| 791 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 792 | StackHandleScope<1> hs2(self); |
| 793 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 794 | f = interface->FindStaticField(self, interface, name, type); |
| 795 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 796 | return f; |
| 797 | } |
| 798 | } |
| 799 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 800 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 801 | } |
| 802 | |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 803 | void Class::SetSkipAccessChecksFlagOnAllMethods(size_t pointer_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 804 | DCHECK(IsVerified()); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 805 | for (auto& m : GetMethods(pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 806 | if (!m.IsNative() && m.IsInvokable()) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 807 | m.SetSkipAccessChecks(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 808 | } |
| 809 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 810 | } |
| 811 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 812 | const char* Class::GetDescriptor(std::string* storage) { |
| 813 | if (IsPrimitive()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 814 | return Primitive::Descriptor(GetPrimitiveType()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 815 | } else if (IsArrayClass()) { |
| 816 | return GetArrayDescriptor(storage); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 817 | } else if (IsProxyClass()) { |
| 818 | *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 819 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 820 | } else { |
| 821 | const DexFile& dex_file = GetDexFile(); |
| 822 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 823 | return dex_file.GetTypeDescriptor(type_id); |
| 824 | } |
| 825 | } |
| 826 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 827 | const char* Class::GetArrayDescriptor(std::string* storage) { |
| 828 | std::string temp; |
| 829 | const char* elem_desc = GetComponentType()->GetDescriptor(&temp); |
| 830 | *storage = "["; |
| 831 | *storage += elem_desc; |
| 832 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | const DexFile::ClassDef* Class::GetClassDef() { |
| 836 | uint16_t class_def_idx = GetDexClassDefIndex(); |
| 837 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 838 | return nullptr; |
| 839 | } |
| 840 | return &GetDexFile().GetClassDef(class_def_idx); |
| 841 | } |
| 842 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 843 | uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) { |
| 844 | DCHECK(!IsPrimitive()); |
| 845 | DCHECK(!IsArrayClass()); |
| 846 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 847 | } |
| 848 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 849 | mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 850 | uint32_t idx) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 851 | DCHECK(klass.Get() != nullptr); |
| 852 | DCHECK(!klass->IsPrimitive()); |
| 853 | if (klass->IsArrayClass()) { |
| 854 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 855 | if (idx == 0) { |
| 856 | return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;"); |
| 857 | } else { |
| 858 | DCHECK_EQ(1U, idx); |
| 859 | return class_linker->FindSystemClass(self, "Ljava/io/Serializable;"); |
| 860 | } |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 861 | } else if (klass->IsProxyClass()) { |
| 862 | mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 863 | DCHECK(interfaces != nullptr); |
| 864 | return interfaces->Get(idx); |
| 865 | } else { |
| 866 | uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
| 867 | mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx); |
| 868 | if (interface == nullptr) { |
| 869 | interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx, |
| 870 | klass.Get()); |
| 871 | CHECK(interface != nullptr || self->IsExceptionPending()); |
| 872 | } |
| 873 | return interface; |
| 874 | } |
| 875 | } |
| 876 | |
Calin Juravle | 52503d8 | 2015-11-11 16:58:31 +0000 | [diff] [blame] | 877 | mirror::Class* Class::GetCommonSuperClass(Handle<Class> klass) { |
| 878 | DCHECK(klass.Get() != nullptr); |
| 879 | DCHECK(!klass->IsInterface()); |
| 880 | DCHECK(!IsInterface()); |
| 881 | mirror::Class* common_super_class = this; |
| 882 | while (!common_super_class->IsAssignableFrom(klass.Get())) { |
| 883 | common_super_class = common_super_class->GetSuperClass(); |
| 884 | } |
| 885 | DCHECK(common_super_class != nullptr); |
| 886 | return common_super_class; |
| 887 | } |
| 888 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 889 | const char* Class::GetSourceFile() { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 890 | const DexFile& dex_file = GetDexFile(); |
| 891 | const DexFile::ClassDef* dex_class_def = GetClassDef(); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 892 | if (dex_class_def == nullptr) { |
| 893 | // Generated classes have no class def. |
| 894 | return nullptr; |
| 895 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 896 | return dex_file.GetSourceFile(*dex_class_def); |
| 897 | } |
| 898 | |
| 899 | std::string Class::GetLocation() { |
| 900 | mirror::DexCache* dex_cache = GetDexCache(); |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 901 | if (dex_cache != nullptr && !IsProxyClass()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 902 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 903 | } |
| 904 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 905 | return "generated class"; |
| 906 | } |
| 907 | |
| 908 | const DexFile::TypeList* Class::GetInterfaceTypeList() { |
| 909 | const DexFile::ClassDef* class_def = GetClassDef(); |
| 910 | if (class_def == nullptr) { |
| 911 | return nullptr; |
| 912 | } |
| 913 | return GetDexFile().GetInterfacesList(*class_def); |
| 914 | } |
| 915 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 916 | void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize], |
| 917 | size_t pointer_size) { |
| 918 | for (size_t i = 0; i < kImtSize; i++) { |
| 919 | auto method = methods[i]; |
| 920 | DCHECK(method != nullptr); |
Mathieu Chartier | 4edd847 | 2015-06-01 10:47:36 -0700 | [diff] [blame] | 921 | SetEmbeddedImTableEntry(i, method, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 922 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 923 | PointerArray* table = GetVTableDuringLinking(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 924 | CHECK(table != nullptr) << PrettyClass(this); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 925 | const size_t table_length = table->GetLength(); |
| 926 | SetEmbeddedVTableLength(table_length); |
| 927 | for (size_t i = 0; i < table_length; i++) { |
| 928 | SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 929 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 930 | // Keep java.lang.Object class's vtable around for since it's easier |
| 931 | // to be reused by array classes during their linking. |
| 932 | if (!IsObjectClass()) { |
| 933 | SetVTable(nullptr); |
| 934 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 937 | class ReadBarrierOnNativeRootsVisitor { |
| 938 | public: |
| 939 | void operator()(mirror::Object* obj ATTRIBUTE_UNUSED, |
| 940 | MemberOffset offset ATTRIBUTE_UNUSED, |
| 941 | bool is_static ATTRIBUTE_UNUSED) const {} |
| 942 | |
| 943 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 944 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 945 | if (!root->IsNull()) { |
| 946 | VisitRoot(root); |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 951 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 952 | mirror::Object* old_ref = root->AsMirrorPtr(); |
| 953 | mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root); |
| 954 | if (old_ref != new_ref) { |
| 955 | // Update the field atomically. This may fail if mutator updates before us, but it's ok. |
| 956 | auto* atomic_root = |
| 957 | reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root); |
| 958 | atomic_root->CompareExchangeStrongSequentiallyConsistent( |
| 959 | mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref), |
| 960 | mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref)); |
| 961 | } |
| 962 | } |
| 963 | }; |
| 964 | |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 965 | // The pre-fence visitor for Class::CopyOf(). |
| 966 | class CopyClassVisitor { |
| 967 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 968 | CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length, |
| 969 | size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize], |
| 970 | size_t pointer_size) |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 971 | : self_(self), orig_(orig), new_length_(new_length), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 972 | copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) { |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 975 | void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 976 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 977 | StackHandleScope<1> hs(self_); |
| 978 | Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass())); |
| 979 | mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_); |
| 980 | mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 981 | h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 982 | h_new_class_obj->SetClassSize(new_length_); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 983 | // Visit all of the references to make sure there is no from space references in the native |
| 984 | // roots. |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 985 | static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences( |
| 986 | ReadBarrierOnNativeRootsVisitor(), VoidFunctor()); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | private: |
| 990 | Thread* const self_; |
| 991 | Handle<mirror::Class>* const orig_; |
| 992 | const size_t new_length_; |
| 993 | const size_t copy_bytes_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 994 | ArtMethod* const (&imt_)[mirror::Class::kImtSize]; |
| 995 | const size_t pointer_size_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 996 | DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); |
| 997 | }; |
| 998 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 999 | Class* Class::CopyOf(Thread* self, int32_t new_length, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1000 | ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1001 | DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); |
| 1002 | // We may get copied by a compacting GC. |
| 1003 | StackHandleScope<1> hs(self); |
| 1004 | Handle<mirror::Class> h_this(hs.NewHandle(this)); |
| 1005 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1006 | // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() |
| 1007 | // to skip copying the tail part that we will overwrite here. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1008 | CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size); |
| 1009 | mirror::Object* new_class = kMovingClasses ? |
| 1010 | heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) : |
| 1011 | heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1012 | if (UNLIKELY(new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1013 | self->AssertPendingOOMException(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 1014 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1015 | } |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 1016 | return new_class->AsClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
Nicolas Geoffray | 3a09092 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 1019 | bool Class::ProxyDescriptorEquals(const char* match) { |
| 1020 | DCHECK(IsProxyClass()); |
| 1021 | return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match; |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 1022 | } |
| 1023 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1024 | // TODO: Move this to java_lang_Class.cc? |
| 1025 | ArtMethod* Class::GetDeclaredConstructor( |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1026 | Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1027 | for (auto& m : GetDirectMethods(sizeof(void*))) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1028 | // Skip <clinit> which is a static constructor, as well as non constructors. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1029 | if (m.IsStatic() || !m.IsConstructor()) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1030 | continue; |
| 1031 | } |
| 1032 | // May cause thread suspension and exceptions. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1033 | if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) { |
| 1034 | return &m; |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1035 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1036 | if (UNLIKELY(self->IsExceptionPending())) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1037 | return nullptr; |
| 1038 | } |
| 1039 | } |
| 1040 | return nullptr; |
| 1041 | } |
| 1042 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1043 | uint32_t Class::Depth() { |
| 1044 | uint32_t depth = 0; |
| 1045 | for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) { |
| 1046 | depth++; |
| 1047 | } |
| 1048 | return depth; |
| 1049 | } |
| 1050 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1051 | } // namespace mirror |
| 1052 | } // namespace art |