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); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 47 | java_lang_Class_ = GcRoot<Class>(java_lang_Class); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void Class::ResetClass() { |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 51 | CHECK(!java_lang_Class_.IsNull()); |
| 52 | java_lang_Class_ = GcRoot<Class>(nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 55 | void Class::VisitRoots(RootVisitor* visitor) { |
| 56 | java_lang_Class_.VisitRootIfNonNull(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 59 | void Class::SetStatus(Handle<Class> h_this, Status new_status, Thread* self) { |
| 60 | Status old_status = h_this->GetStatus(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 61 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 62 | bool class_linker_initialized = class_linker != nullptr && class_linker->IsInitialized(); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 63 | if (LIKELY(class_linker_initialized)) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 64 | if (UNLIKELY(new_status <= old_status && new_status != kStatusError && |
| 65 | new_status != kStatusRetired)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 66 | LOG(FATAL) << "Unexpected change back of class status for " << PrettyClass(h_this.Get()) |
| 67 | << " " << old_status << " -> " << new_status; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 68 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 69 | if (new_status >= kStatusResolved || old_status >= kStatusResolved) { |
| 70 | // When classes are being resolved the resolution code should hold the lock. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 71 | CHECK_EQ(h_this->GetLockOwnerThreadId(), self->GetThreadId()) |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 72 | << "Attempt to change status of class while not holding its lock: " |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 73 | << PrettyClass(h_this.Get()) << " " << old_status << " -> " << new_status; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 74 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 75 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 76 | if (UNLIKELY(new_status == kStatusError)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 77 | CHECK_NE(h_this->GetStatus(), kStatusError) |
| 78 | << "Attempt to set as erroneous an already erroneous class " |
| 79 | << PrettyClass(h_this.Get()); |
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. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 82 | StackHandleScope<1> hs(self); |
| 83 | Handle<mirror::Throwable> old_exception(hs.NewHandle(self->GetException())); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 84 | CHECK(old_exception.Get() != nullptr); |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 85 | Class* eiie_class; |
| 86 | // Do't attempt to use FindClass if we have an OOM error since this can try to do more |
| 87 | // allocations and may cause infinite loops. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 88 | bool throw_eiie = (old_exception.Get() == nullptr); |
| 89 | if (!throw_eiie) { |
| 90 | std::string temp; |
| 91 | const char* old_exception_descriptor = old_exception->GetClass()->GetDescriptor(&temp); |
| 92 | throw_eiie = (strcmp(old_exception_descriptor, "Ljava/lang/OutOfMemoryError;") != 0); |
| 93 | } |
| 94 | if (throw_eiie) { |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 95 | // Clear exception to call FindSystemClass. |
| 96 | self->ClearException(); |
| 97 | eiie_class = Runtime::Current()->GetClassLinker()->FindSystemClass( |
| 98 | self, "Ljava/lang/ExceptionInInitializerError;"); |
| 99 | CHECK(!self->IsExceptionPending()); |
| 100 | // Only verification errors, not initialization problems, should set a verify error. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 101 | // This is to ensure that ThrowEarlierClassFailure will throw NoClassDefFoundError in that |
| 102 | // case. |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 103 | Class* exception_class = old_exception->GetClass(); |
| 104 | if (!eiie_class->IsAssignableFrom(exception_class)) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 105 | h_this->SetVerifyErrorClass(exception_class); |
Mathieu Chartier | fd22d5b | 2014-07-14 10:16:05 -0700 | [diff] [blame] | 106 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 109 | // Restore exception. |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 110 | self->SetException(old_exception.Get()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | } |
Andreas Gampe | 575e78c | 2014-11-03 23:41:03 -0800 | [diff] [blame] | 112 | 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] | 113 | if (Runtime::Current()->IsActiveTransaction()) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 114 | h_this->SetField32Volatile<true>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 115 | } else { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 116 | h_this->SetField32Volatile<false>(OFFSET_OF_OBJECT_MEMBER(Class, status_), new_status); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 117 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 118 | |
| 119 | if (!class_linker_initialized) { |
| 120 | // When the class linker is being initialized its single threaded and by definition there can be |
| 121 | // no waiters. During initialization classes may appear temporary but won't be retired as their |
| 122 | // size was statically computed. |
| 123 | } else { |
| 124 | // Classes that are being resolved or initialized need to notify waiters that the class status |
| 125 | // changed. See ClassLinker::EnsureResolved and ClassLinker::WaitForInitializeClass. |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 126 | if (h_this->IsTemp()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 127 | // Class is a temporary one, ensure that waiters for resolution get notified of retirement |
| 128 | // 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] | 129 | CHECK_LT(new_status, kStatusResolved) << PrettyDescriptor(h_this.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 130 | if (new_status == kStatusRetired || new_status == kStatusError) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 131 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 132 | } |
| 133 | } else { |
| 134 | CHECK_NE(new_status, kStatusRetired); |
| 135 | if (old_status >= kStatusResolved || new_status >= kStatusResolved) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 136 | h_this->NotifyAll(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 139 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 142 | void Class::SetDexCache(DexCache* new_dex_cache) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 143 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, dex_cache_), new_dex_cache); |
Mathieu Chartier | 91a6dc4 | 2014-12-01 10:31:15 -0800 | [diff] [blame] | 144 | SetDexCacheStrings(new_dex_cache != nullptr ? new_dex_cache->GetStrings() : nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 147 | void Class::SetClassSize(uint32_t new_class_size) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 148 | if (kIsDebugBuild && new_class_size < GetClassSize()) { |
| 149 | DumpClass(LOG(INTERNAL_FATAL), kDumpClassFullDetail); |
| 150 | LOG(INTERNAL_FATAL) << new_class_size << " vs " << GetClassSize(); |
| 151 | LOG(FATAL) << " class=" << PrettyTypeOf(this); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 152 | } |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 153 | // Not called within a transaction. |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 154 | SetField32<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_size_), new_class_size); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Return the class' name. The exact format is bizarre, but it's the specified behavior for |
| 158 | // Class.getName: keywords for primitive types, regular "[I" form for primitive arrays (so "int" |
| 159 | // but "[I"), and arrays of reference types written between "L" and ";" but with dots rather than |
| 160 | // slashes (so "java.lang.String" but "[Ljava.lang.String;"). Madness. |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 161 | String* Class::ComputeName(Handle<Class> h_this) { |
| 162 | String* name = h_this->GetName(); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 163 | if (name != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 164 | return name; |
| 165 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 166 | std::string temp; |
| 167 | const char* descriptor = h_this->GetDescriptor(&temp); |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 168 | Thread* self = Thread::Current(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | if ((descriptor[0] != 'L') && (descriptor[0] != '[')) { |
| 170 | // The descriptor indicates that this is the class for |
| 171 | // a primitive type; special-case the return value. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 172 | const char* c_name = nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 173 | switch (descriptor[0]) { |
| 174 | case 'Z': c_name = "boolean"; break; |
| 175 | case 'B': c_name = "byte"; break; |
| 176 | case 'C': c_name = "char"; break; |
| 177 | case 'S': c_name = "short"; break; |
| 178 | case 'I': c_name = "int"; break; |
| 179 | case 'J': c_name = "long"; break; |
| 180 | case 'F': c_name = "float"; break; |
| 181 | case 'D': c_name = "double"; break; |
| 182 | case 'V': c_name = "void"; break; |
| 183 | default: |
| 184 | LOG(FATAL) << "Unknown primitive type: " << PrintableChar(descriptor[0]); |
| 185 | } |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 186 | name = String::AllocFromModifiedUtf8(self, c_name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 187 | } else { |
| 188 | // Convert the UTF-8 name to a java.lang.String. The name must use '.' to separate package |
| 189 | // components. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 190 | name = String::AllocFromModifiedUtf8(self, DescriptorToDot(descriptor).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 191 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 192 | h_this->SetName(name); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 193 | return name; |
| 194 | } |
| 195 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 196 | void Class::DumpClass(std::ostream& os, int flags) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 197 | if ((flags & kDumpClassFullDetail) == 0) { |
| 198 | os << PrettyClass(this); |
| 199 | if ((flags & kDumpClassClassLoader) != 0) { |
| 200 | os << ' ' << GetClassLoader(); |
| 201 | } |
| 202 | if ((flags & kDumpClassInitialized) != 0) { |
| 203 | os << ' ' << GetStatus(); |
| 204 | } |
| 205 | os << "\n"; |
| 206 | return; |
| 207 | } |
| 208 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 209 | Thread* const self = Thread::Current(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 210 | StackHandleScope<2> hs(self); |
| 211 | Handle<mirror::Class> h_this(hs.NewHandle(this)); |
| 212 | Handle<mirror::Class> h_super(hs.NewHandle(GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 213 | auto image_pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 214 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 215 | std::string temp; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 216 | os << "----- " << (IsInterface() ? "interface" : "class") << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 217 | << "'" << GetDescriptor(&temp) << "' cl=" << GetClassLoader() << " -----\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 218 | os << " objectSize=" << SizeOf() << " " |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 219 | << "(" << (h_super.Get() != nullptr ? h_super->SizeOf() : -1) << " from super)\n", |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 220 | os << StringPrintf(" access=0x%04x.%04x\n", |
| 221 | GetAccessFlags() >> 16, GetAccessFlags() & kAccJavaFlagsMask); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 222 | if (h_super.Get() != nullptr) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 223 | os << " super='" << PrettyClass(h_super.Get()) << "' (cl=" << h_super->GetClassLoader() |
| 224 | << ")\n"; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 225 | } |
| 226 | if (IsArrayClass()) { |
| 227 | os << " componentType=" << PrettyClass(GetComponentType()) << "\n"; |
| 228 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 229 | const size_t num_direct_interfaces = NumDirectInterfaces(); |
| 230 | if (num_direct_interfaces > 0) { |
| 231 | os << " interfaces (" << num_direct_interfaces << "):\n"; |
| 232 | for (size_t i = 0; i < num_direct_interfaces; ++i) { |
| 233 | Class* interface = GetDirectInterface(self, h_this, i); |
Andreas Gampe | 16f149c | 2015-03-23 10:10:20 -0700 | [diff] [blame] | 234 | if (interface == nullptr) { |
| 235 | os << StringPrintf(" %2zd: nullptr!\n", i); |
| 236 | } else { |
| 237 | const ClassLoader* cl = interface->GetClassLoader(); |
| 238 | os << StringPrintf(" %2zd: %s (cl=%p)\n", i, PrettyClass(interface).c_str(), cl); |
| 239 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 240 | } |
| 241 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 242 | if (!IsLoaded()) { |
| 243 | os << " class not yet loaded"; |
| 244 | } else { |
| 245 | // After this point, this may have moved due to GetDirectInterface. |
| 246 | os << " vtable (" << h_this->NumVirtualMethods() << " entries, " |
| 247 | << (h_super.Get() != nullptr ? h_super->NumVirtualMethods() : 0) << " in super):\n"; |
| 248 | for (size_t i = 0; i < NumVirtualMethods(); ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 249 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod( |
| 250 | h_this->GetVirtualMethodDuringLinking(i, image_pointer_size)).c_str()); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 251 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 252 | os << " direct methods (" << h_this->NumDirectMethods() << " entries):\n"; |
| 253 | for (size_t i = 0; i < h_this->NumDirectMethods(); ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 254 | os << StringPrintf(" %2zd: %s\n", i, PrettyMethod( |
| 255 | h_this->GetDirectMethod(i, image_pointer_size)).c_str()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 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) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -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 | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 336 | bool Class::IsThrowableClass() { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 337 | return WellKnownClasses::ToClass(WellKnownClasses::java_lang_Throwable)->IsAssignableFrom(this); |
| 338 | } |
| 339 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 340 | void Class::SetClassLoader(ClassLoader* new_class_loader) { |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 341 | if (Runtime::Current()->IsActiveTransaction()) { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 342 | SetFieldObject<true>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 343 | } else { |
Ian Rogers | b0fa5dc | 2014-04-28 16:47:08 -0700 | [diff] [blame] | 344 | SetFieldObject<false>(OFFSET_OF_OBJECT_MEMBER(Class, class_loader_), new_class_loader); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 345 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 348 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const StringPiece& signature, |
| 349 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 350 | // Check the current class before checking the interfaces. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 351 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 352 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 353 | return method; |
| 354 | } |
| 355 | |
| 356 | int32_t iftable_count = GetIfTableCount(); |
| 357 | IfTable* iftable = GetIfTable(); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 358 | for (int32_t i = 0; i < iftable_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 359 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 360 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 361 | return method; |
| 362 | } |
| 363 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 364 | return nullptr; |
| 365 | } |
| 366 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 367 | ArtMethod* Class::FindInterfaceMethod(const StringPiece& name, const Signature& signature, |
| 368 | size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 369 | // Check the current class before checking the interfaces. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 370 | ArtMethod* method = FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 371 | if (method != nullptr) { |
| 372 | return method; |
| 373 | } |
| 374 | |
| 375 | int32_t iftable_count = GetIfTableCount(); |
| 376 | IfTable* iftable = GetIfTable(); |
| 377 | for (int32_t i = 0; i < iftable_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 378 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 379 | if (method != nullptr) { |
| 380 | return method; |
| 381 | } |
| 382 | } |
| 383 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 386 | ArtMethod* Class::FindInterfaceMethod(const DexCache* dex_cache, uint32_t dex_method_idx, |
| 387 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 388 | // Check the current class before checking the interfaces. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 389 | ArtMethod* method = FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 390 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 391 | return method; |
| 392 | } |
| 393 | |
| 394 | int32_t iftable_count = GetIfTableCount(); |
| 395 | IfTable* iftable = GetIfTable(); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 396 | for (int32_t i = 0; i < iftable_count; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 397 | method = iftable->GetInterface(i)->FindDeclaredVirtualMethod( |
| 398 | dex_cache, dex_method_idx, pointer_size); |
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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 406 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const StringPiece& signature, |
| 407 | size_t pointer_size) { |
| 408 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 409 | if (name == method.GetName() && method.GetSignature() == signature) { |
| 410 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 416 | ArtMethod* Class::FindDeclaredDirectMethod(const StringPiece& name, const Signature& signature, |
| 417 | size_t pointer_size) { |
| 418 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 419 | if (name == method.GetName() && signature == method.GetSignature()) { |
| 420 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 426 | ArtMethod* Class::FindDeclaredDirectMethod(const DexCache* dex_cache, uint32_t dex_method_idx, |
| 427 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 428 | if (GetDexCache() == dex_cache) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 429 | for (auto& method : GetDirectMethods(pointer_size)) { |
| 430 | if (method.GetDexMethodIndex() == dex_method_idx) { |
| 431 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 438 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const StringPiece& 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 | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 443 | return method; |
| 444 | } |
| 445 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 446 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 449 | ArtMethod* Class::FindDirectMethod(const StringPiece& name, const Signature& signature, |
| 450 | 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(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 453 | if (method != nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 454 | return method; |
| 455 | } |
| 456 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 457 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 460 | ArtMethod* Class::FindDirectMethod( |
| 461 | 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] | 462 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 463 | ArtMethod* method = klass->FindDeclaredDirectMethod(dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 464 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 465 | return method; |
| 466 | } |
| 467 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 468 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 469 | } |
| 470 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 471 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const StringPiece& signature, |
| 472 | size_t pointer_size) { |
| 473 | for (auto& method : GetVirtualMethods(pointer_size)) { |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 474 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 475 | if (name == np_method->GetName() && np_method->GetSignature() == signature) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 476 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 479 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 482 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature, |
| 483 | size_t pointer_size) { |
| 484 | for (auto& method : GetVirtualMethods(pointer_size)) { |
Mathieu Chartier | 72156e2 | 2015-07-10 18:26:41 -0700 | [diff] [blame] | 485 | ArtMethod* const np_method = method.GetInterfaceMethodIfProxy(pointer_size); |
| 486 | if (name == np_method->GetName() && signature == np_method->GetSignature()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 487 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 488 | } |
| 489 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 490 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 493 | ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx, |
| 494 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 495 | if (GetDexCache() == dex_cache) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 496 | for (auto& method : GetVirtualMethods(pointer_size)) { |
| 497 | // A miranda method may have a different DexCache and is always created by linking, |
| 498 | // never *declared* in the class. |
| 499 | if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) { |
| 500 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 501 | } |
| 502 | } |
| 503 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 504 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 507 | ArtMethod* Class::FindVirtualMethod( |
| 508 | const StringPiece& name, const StringPiece& signature, size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 509 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 510 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 511 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 512 | return method; |
| 513 | } |
| 514 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 515 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 518 | ArtMethod* Class::FindVirtualMethod( |
| 519 | const StringPiece& name, const Signature& 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 | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 523 | return method; |
| 524 | } |
| 525 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 526 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 529 | ArtMethod* Class::FindVirtualMethod( |
| 530 | 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] | 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(dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 533 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 534 | return method; |
| 535 | } |
| 536 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 537 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 538 | } |
| 539 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 540 | ArtMethod* Class::FindClassInitializer(size_t pointer_size) { |
| 541 | for (ArtMethod& method : GetDirectMethods(pointer_size)) { |
| 542 | if (method.IsClassInitializer()) { |
| 543 | DCHECK_STREQ(method.GetName(), "<clinit>"); |
| 544 | DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V"); |
| 545 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 548 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 551 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 552 | // Is the field in this class? |
| 553 | // Interfaces are not relevant because they can't contain instance fields. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 554 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 555 | ArtField* f = GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 556 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 557 | return f; |
| 558 | } |
| 559 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 560 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 563 | ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 564 | if (GetDexCache() == dex_cache) { |
| 565 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 566 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 567 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 568 | return f; |
| 569 | } |
| 570 | } |
| 571 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 572 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 573 | } |
| 574 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 575 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 576 | // Is the field in this class, or any of its superclasses? |
| 577 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 578 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 579 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 580 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 581 | return f; |
| 582 | } |
| 583 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 584 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 585 | } |
| 586 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 587 | ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 588 | // Is the field in this class, or any of its superclasses? |
| 589 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 590 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 591 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 592 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 593 | return f; |
| 594 | } |
| 595 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 596 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 599 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 600 | DCHECK(type != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 601 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 602 | ArtField* f = GetStaticField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 603 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 604 | return f; |
| 605 | } |
| 606 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 607 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 608 | } |
| 609 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 610 | ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 611 | if (dex_cache == GetDexCache()) { |
| 612 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 613 | ArtField* f = GetStaticField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 614 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 615 | return f; |
| 616 | } |
| 617 | } |
| 618 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 619 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 622 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 623 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 624 | // Is the field in this class (or its interfaces), or any of its |
| 625 | // superclasses (or their interfaces)? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 626 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 627 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 628 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 629 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 630 | return f; |
| 631 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 632 | // Wrap k incase it moves during GetDirectInterface. |
| 633 | StackHandleScope<1> hs(self); |
| 634 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 635 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 636 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 637 | StackHandleScope<1> hs2(self); |
| 638 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 639 | f = FindStaticField(self, interface, name, type); |
| 640 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 641 | return f; |
| 642 | } |
| 643 | } |
| 644 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 645 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 648 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache, |
| 649 | uint32_t dex_field_idx) { |
| 650 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 651 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 652 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 653 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 654 | return f; |
| 655 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 656 | // Wrap k incase it moves during GetDirectInterface. |
| 657 | StackHandleScope<1> hs(self); |
| 658 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 659 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 660 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 661 | StackHandleScope<1> hs2(self); |
| 662 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 663 | f = FindStaticField(self, interface, dex_cache, dex_field_idx); |
| 664 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 665 | return f; |
| 666 | } |
| 667 | } |
| 668 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 669 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 672 | ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 673 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 674 | // Find a field using the JLS field resolution order |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 675 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 676 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 677 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 678 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 679 | return f; |
| 680 | } |
| 681 | f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 682 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 683 | return f; |
| 684 | } |
| 685 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 686 | StackHandleScope<1> hs(self); |
| 687 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
| 688 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 689 | StackHandleScope<1> hs2(self); |
| 690 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 691 | f = interface->FindStaticField(self, interface, name, type); |
| 692 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 693 | return f; |
| 694 | } |
| 695 | } |
| 696 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 697 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 698 | } |
| 699 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 700 | void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) { |
| 701 | DCHECK(IsVerified()); |
| 702 | for (auto& m : GetDirectMethods(pointer_size)) { |
| 703 | if (!m.IsNative() && !m.IsAbstract()) { |
| 704 | m.SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 705 | } |
| 706 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 707 | for (auto& m : GetVirtualMethods(pointer_size)) { |
| 708 | if (!m.IsNative() && !m.IsAbstract()) { |
| 709 | m.SetPreverified(); |
| 710 | } |
| 711 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 714 | const char* Class::GetDescriptor(std::string* storage) { |
| 715 | if (IsPrimitive()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 716 | return Primitive::Descriptor(GetPrimitiveType()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 717 | } else if (IsArrayClass()) { |
| 718 | return GetArrayDescriptor(storage); |
| 719 | } else if (IsProxyClass()) { |
| 720 | *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); |
| 721 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 722 | } else { |
| 723 | const DexFile& dex_file = GetDexFile(); |
| 724 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 725 | return dex_file.GetTypeDescriptor(type_id); |
| 726 | } |
| 727 | } |
| 728 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 729 | const char* Class::GetArrayDescriptor(std::string* storage) { |
| 730 | std::string temp; |
| 731 | const char* elem_desc = GetComponentType()->GetDescriptor(&temp); |
| 732 | *storage = "["; |
| 733 | *storage += elem_desc; |
| 734 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | const DexFile::ClassDef* Class::GetClassDef() { |
| 738 | uint16_t class_def_idx = GetDexClassDefIndex(); |
| 739 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 740 | return nullptr; |
| 741 | } |
| 742 | return &GetDexFile().GetClassDef(class_def_idx); |
| 743 | } |
| 744 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 745 | uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) { |
| 746 | DCHECK(!IsPrimitive()); |
| 747 | DCHECK(!IsArrayClass()); |
| 748 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 749 | } |
| 750 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 751 | mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 752 | uint32_t idx) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 753 | DCHECK(klass.Get() != nullptr); |
| 754 | DCHECK(!klass->IsPrimitive()); |
| 755 | if (klass->IsArrayClass()) { |
| 756 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 757 | if (idx == 0) { |
| 758 | return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;"); |
| 759 | } else { |
| 760 | DCHECK_EQ(1U, idx); |
| 761 | return class_linker->FindSystemClass(self, "Ljava/io/Serializable;"); |
| 762 | } |
| 763 | } else if (klass->IsProxyClass()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 764 | mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 765 | DCHECK(interfaces != nullptr); |
| 766 | return interfaces->Get(idx); |
| 767 | } else { |
| 768 | uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
| 769 | mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx); |
| 770 | if (interface == nullptr) { |
| 771 | interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx, |
| 772 | klass.Get()); |
| 773 | CHECK(interface != nullptr || self->IsExceptionPending()); |
| 774 | } |
| 775 | return interface; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | const char* Class::GetSourceFile() { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 780 | const DexFile& dex_file = GetDexFile(); |
| 781 | const DexFile::ClassDef* dex_class_def = GetClassDef(); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 782 | if (dex_class_def == nullptr) { |
| 783 | // Generated classes have no class def. |
| 784 | return nullptr; |
| 785 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 786 | return dex_file.GetSourceFile(*dex_class_def); |
| 787 | } |
| 788 | |
| 789 | std::string Class::GetLocation() { |
| 790 | mirror::DexCache* dex_cache = GetDexCache(); |
| 791 | if (dex_cache != nullptr && !IsProxyClass()) { |
| 792 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 793 | } |
| 794 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 795 | return "generated class"; |
| 796 | } |
| 797 | |
| 798 | const DexFile::TypeList* Class::GetInterfaceTypeList() { |
| 799 | const DexFile::ClassDef* class_def = GetClassDef(); |
| 800 | if (class_def == nullptr) { |
| 801 | return nullptr; |
| 802 | } |
| 803 | return GetDexFile().GetInterfacesList(*class_def); |
| 804 | } |
| 805 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 806 | void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize], |
| 807 | size_t pointer_size) { |
| 808 | for (size_t i = 0; i < kImtSize; i++) { |
| 809 | auto method = methods[i]; |
| 810 | DCHECK(method != nullptr); |
Mathieu Chartier | 4edd847 | 2015-06-01 10:47:36 -0700 | [diff] [blame] | 811 | SetEmbeddedImTableEntry(i, method, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 812 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 813 | PointerArray* table = GetVTableDuringLinking(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 814 | CHECK(table != nullptr) << PrettyClass(this); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 815 | const size_t table_length = table->GetLength(); |
| 816 | SetEmbeddedVTableLength(table_length); |
| 817 | for (size_t i = 0; i < table_length; i++) { |
| 818 | SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -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 | |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 827 | class ReadBarrierOnNativeRootsVisitor { |
| 828 | public: |
| 829 | void operator()(mirror::Object* obj ATTRIBUTE_UNUSED, |
| 830 | MemberOffset offset ATTRIBUTE_UNUSED, |
| 831 | bool is_static ATTRIBUTE_UNUSED) const {} |
| 832 | |
| 833 | void VisitRootIfNonNull(mirror::CompressedReference<mirror::Object>* root) const |
| 834 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 835 | if (!root->IsNull()) { |
| 836 | VisitRoot(root); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 841 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 842 | mirror::Object* old_ref = root->AsMirrorPtr(); |
| 843 | mirror::Object* new_ref = ReadBarrier::BarrierForRoot(root); |
| 844 | if (old_ref != new_ref) { |
| 845 | // Update the field atomically. This may fail if mutator updates before us, but it's ok. |
| 846 | auto* atomic_root = |
| 847 | reinterpret_cast<Atomic<mirror::CompressedReference<mirror::Object>>*>(root); |
| 848 | atomic_root->CompareExchangeStrongSequentiallyConsistent( |
| 849 | mirror::CompressedReference<mirror::Object>::FromMirrorPtr(old_ref), |
| 850 | mirror::CompressedReference<mirror::Object>::FromMirrorPtr(new_ref)); |
| 851 | } |
| 852 | } |
| 853 | }; |
| 854 | |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 855 | // The pre-fence visitor for Class::CopyOf(). |
| 856 | class CopyClassVisitor { |
| 857 | public: |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 858 | CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length, |
| 859 | size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize], |
| 860 | size_t pointer_size) |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 861 | : self_(self), orig_(orig), new_length_(new_length), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 862 | copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) { |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 865 | void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 866 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 867 | StackHandleScope<1> hs(self_); |
| 868 | Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass())); |
| 869 | mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_); |
| 870 | mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 871 | h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 872 | h_new_class_obj->SetClassSize(new_length_); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 873 | // Visit all of the references to make sure there is no from space references in the native |
| 874 | // roots. |
Mathieu Chartier | 059ef3d | 2015-08-18 13:54:21 -0700 | [diff] [blame] | 875 | static_cast<mirror::Object*>(h_new_class_obj.Get())->VisitReferences( |
| 876 | ReadBarrierOnNativeRootsVisitor(), VoidFunctor()); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | private: |
| 880 | Thread* const self_; |
| 881 | Handle<mirror::Class>* const orig_; |
| 882 | const size_t new_length_; |
| 883 | const size_t copy_bytes_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 884 | ArtMethod* const (&imt_)[mirror::Class::kImtSize]; |
| 885 | const size_t pointer_size_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 886 | DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); |
| 887 | }; |
| 888 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 889 | Class* Class::CopyOf(Thread* self, int32_t new_length, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 890 | ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 891 | DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); |
| 892 | // We may get copied by a compacting GC. |
| 893 | StackHandleScope<1> hs(self); |
| 894 | Handle<mirror::Class> h_this(hs.NewHandle(this)); |
| 895 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 896 | // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() |
| 897 | // to skip copying the tail part that we will overwrite here. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 898 | CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size); |
| 899 | mirror::Object* new_class = kMovingClasses ? |
| 900 | heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) : |
| 901 | heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 902 | if (UNLIKELY(new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 903 | self->AssertPendingOOMException(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 904 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 905 | } |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 906 | return new_class->AsClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 909 | bool Class::ProxyDescriptorEquals(const char* match) { |
| 910 | DCHECK(IsProxyClass()); |
| 911 | return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match; |
| 912 | } |
| 913 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 914 | // TODO: Move this to java_lang_Class.cc? |
| 915 | ArtMethod* Class::GetDeclaredConstructor( |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 916 | Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 917 | for (auto& m : GetDirectMethods(sizeof(void*))) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 918 | // Skip <clinit> which is a static constructor, as well as non constructors. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 919 | if (m.IsStatic() || !m.IsConstructor()) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 920 | continue; |
| 921 | } |
| 922 | // May cause thread suspension and exceptions. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 923 | if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) { |
| 924 | return &m; |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 925 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 926 | if (UNLIKELY(self->IsExceptionPending())) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 927 | return nullptr; |
| 928 | } |
| 929 | } |
| 930 | return nullptr; |
| 931 | } |
| 932 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 933 | uint32_t Class::Depth() { |
| 934 | uint32_t depth = 0; |
| 935 | for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) { |
| 936 | depth++; |
| 937 | } |
| 938 | return depth; |
| 939 | } |
| 940 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 941 | } // namespace mirror |
| 942 | } // namespace art |