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)) { |
| 474 | if (name == method.GetName() && method.GetSignature() == signature) { |
| 475 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 476 | } |
| 477 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 478 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 481 | ArtMethod* Class::FindDeclaredVirtualMethod(const StringPiece& name, const Signature& signature, |
| 482 | size_t pointer_size) { |
| 483 | for (auto& method : GetVirtualMethods(pointer_size)) { |
| 484 | if (name == method.GetName() && signature == method.GetSignature()) { |
| 485 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 488 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 489 | } |
| 490 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 491 | ArtMethod* Class::FindDeclaredVirtualMethod(const DexCache* dex_cache, uint32_t dex_method_idx, |
| 492 | size_t pointer_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 493 | if (GetDexCache() == dex_cache) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 494 | for (auto& method : GetVirtualMethods(pointer_size)) { |
| 495 | // A miranda method may have a different DexCache and is always created by linking, |
| 496 | // never *declared* in the class. |
| 497 | if (method.GetDexMethodIndex() == dex_method_idx && !method.IsMiranda()) { |
| 498 | return &method; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 502 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 505 | ArtMethod* Class::FindVirtualMethod( |
| 506 | const StringPiece& name, const StringPiece& signature, size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 507 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 508 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 509 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 510 | return method; |
| 511 | } |
| 512 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 513 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 516 | ArtMethod* Class::FindVirtualMethod( |
| 517 | const StringPiece& name, const Signature& signature, size_t pointer_size) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 518 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 519 | ArtMethod* method = klass->FindDeclaredVirtualMethod(name, signature, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 520 | if (method != nullptr) { |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 521 | return method; |
| 522 | } |
| 523 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 524 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 527 | ArtMethod* Class::FindVirtualMethod( |
| 528 | 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] | 529 | for (Class* klass = this; klass != nullptr; klass = klass->GetSuperClass()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 530 | ArtMethod* method = klass->FindDeclaredVirtualMethod(dex_cache, dex_method_idx, pointer_size); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 531 | if (method != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 532 | return method; |
| 533 | } |
| 534 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 535 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 538 | ArtMethod* Class::FindClassInitializer(size_t pointer_size) { |
| 539 | for (ArtMethod& method : GetDirectMethods(pointer_size)) { |
| 540 | if (method.IsClassInitializer()) { |
| 541 | DCHECK_STREQ(method.GetName(), "<clinit>"); |
| 542 | DCHECK_STREQ(method.GetSignature().ToString().c_str(), "()V"); |
| 543 | return &method; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 544 | } |
| 545 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 546 | return nullptr; |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 549 | ArtField* Class::FindDeclaredInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 550 | // Is the field in this class? |
| 551 | // Interfaces are not relevant because they can't contain instance fields. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 552 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 553 | ArtField* f = GetInstanceField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 554 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 555 | return f; |
| 556 | } |
| 557 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 558 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 559 | } |
| 560 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 561 | ArtField* Class::FindDeclaredInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 562 | if (GetDexCache() == dex_cache) { |
| 563 | for (size_t i = 0; i < NumInstanceFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 564 | ArtField* f = GetInstanceField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 565 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 566 | return f; |
| 567 | } |
| 568 | } |
| 569 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 570 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 573 | ArtField* Class::FindInstanceField(const StringPiece& name, const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 574 | // Is the field in this class, or any of its superclasses? |
| 575 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 576 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 577 | ArtField* f = c->FindDeclaredInstanceField(name, type); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 578 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 579 | return f; |
| 580 | } |
| 581 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 582 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 583 | } |
| 584 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 585 | ArtField* Class::FindInstanceField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 586 | // Is the field in this class, or any of its superclasses? |
| 587 | // Interfaces are not relevant because they can't contain instance fields. |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 588 | for (Class* c = this; c != nullptr; c = c->GetSuperClass()) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 589 | ArtField* f = c->FindDeclaredInstanceField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 590 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 591 | return f; |
| 592 | } |
| 593 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 594 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 595 | } |
| 596 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 597 | ArtField* Class::FindDeclaredStaticField(const StringPiece& name, const StringPiece& type) { |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 598 | DCHECK(type != nullptr); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 599 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 600 | ArtField* f = GetStaticField(i); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 601 | if (name == f->GetName() && type == f->GetTypeDescriptor()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 602 | return f; |
| 603 | } |
| 604 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 605 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 608 | ArtField* Class::FindDeclaredStaticField(const DexCache* dex_cache, uint32_t dex_field_idx) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 609 | if (dex_cache == GetDexCache()) { |
| 610 | for (size_t i = 0; i < NumStaticFields(); ++i) { |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 611 | ArtField* f = GetStaticField(i); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 612 | if (f->GetDexFieldIndex() == dex_field_idx) { |
| 613 | return f; |
| 614 | } |
| 615 | } |
| 616 | } |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 617 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 620 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 621 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 622 | // Is the field in this class (or its interfaces), or any of its |
| 623 | // superclasses (or their interfaces)? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 624 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 625 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 626 | ArtField* f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 627 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 628 | return f; |
| 629 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 630 | // Wrap k incase it moves during GetDirectInterface. |
| 631 | StackHandleScope<1> hs(self); |
| 632 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 633 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 634 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 635 | StackHandleScope<1> hs2(self); |
| 636 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 637 | f = FindStaticField(self, interface, name, type); |
| 638 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 639 | return f; |
| 640 | } |
| 641 | } |
| 642 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 643 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 646 | ArtField* Class::FindStaticField(Thread* self, Handle<Class> klass, const DexCache* dex_cache, |
| 647 | uint32_t dex_field_idx) { |
| 648 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 649 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 650 | ArtField* f = k->FindDeclaredStaticField(dex_cache, dex_field_idx); |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 651 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 652 | return f; |
| 653 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 654 | // Wrap k incase it moves during GetDirectInterface. |
| 655 | StackHandleScope<1> hs(self); |
| 656 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 657 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 658 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 659 | StackHandleScope<1> hs2(self); |
| 660 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 661 | f = FindStaticField(self, interface, dex_cache, dex_field_idx); |
| 662 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 663 | return f; |
| 664 | } |
| 665 | } |
| 666 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 667 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 668 | } |
| 669 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 670 | ArtField* Class::FindField(Thread* self, Handle<Class> klass, const StringPiece& name, |
| 671 | const StringPiece& type) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 672 | // Find a field using the JLS field resolution order |
Brian Carlstrom | 004644f | 2014-06-18 08:34:01 -0700 | [diff] [blame] | 673 | for (Class* k = klass.Get(); k != nullptr; k = k->GetSuperClass()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 674 | // Is the field in this class? |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 675 | ArtField* f = k->FindDeclaredInstanceField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 676 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 677 | return f; |
| 678 | } |
| 679 | f = k->FindDeclaredStaticField(name, type); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 680 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 681 | return f; |
| 682 | } |
| 683 | // Is this field in any of this class' interfaces? |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 684 | StackHandleScope<1> hs(self); |
| 685 | HandleWrapper<mirror::Class> h_k(hs.NewHandleWrapper(&k)); |
| 686 | for (uint32_t i = 0; i < h_k->NumDirectInterfaces(); ++i) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 687 | StackHandleScope<1> hs2(self); |
| 688 | Handle<mirror::Class> interface(hs2.NewHandle(GetDirectInterface(self, h_k, i))); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 689 | f = interface->FindStaticField(self, interface, name, type); |
| 690 | if (f != nullptr) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 691 | return f; |
| 692 | } |
| 693 | } |
| 694 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 695 | return nullptr; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 698 | void Class::SetPreverifiedFlagOnAllMethods(size_t pointer_size) { |
| 699 | DCHECK(IsVerified()); |
| 700 | for (auto& m : GetDirectMethods(pointer_size)) { |
| 701 | if (!m.IsNative() && !m.IsAbstract()) { |
| 702 | m.SetPreverified(); |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 703 | } |
| 704 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 705 | for (auto& m : GetVirtualMethods(pointer_size)) { |
| 706 | if (!m.IsNative() && !m.IsAbstract()) { |
| 707 | m.SetPreverified(); |
| 708 | } |
| 709 | } |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 710 | } |
| 711 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 712 | const char* Class::GetDescriptor(std::string* storage) { |
| 713 | if (IsPrimitive()) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 714 | return Primitive::Descriptor(GetPrimitiveType()); |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 715 | } else if (IsArrayClass()) { |
| 716 | return GetArrayDescriptor(storage); |
| 717 | } else if (IsProxyClass()) { |
| 718 | *storage = Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this); |
| 719 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 720 | } else { |
| 721 | const DexFile& dex_file = GetDexFile(); |
| 722 | const DexFile::TypeId& type_id = dex_file.GetTypeId(GetClassDef()->class_idx_); |
| 723 | return dex_file.GetTypeDescriptor(type_id); |
| 724 | } |
| 725 | } |
| 726 | |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 727 | const char* Class::GetArrayDescriptor(std::string* storage) { |
| 728 | std::string temp; |
| 729 | const char* elem_desc = GetComponentType()->GetDescriptor(&temp); |
| 730 | *storage = "["; |
| 731 | *storage += elem_desc; |
| 732 | return storage->c_str(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | const DexFile::ClassDef* Class::GetClassDef() { |
| 736 | uint16_t class_def_idx = GetDexClassDefIndex(); |
| 737 | if (class_def_idx == DexFile::kDexNoIndex16) { |
| 738 | return nullptr; |
| 739 | } |
| 740 | return &GetDexFile().GetClassDef(class_def_idx); |
| 741 | } |
| 742 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 743 | uint16_t Class::GetDirectInterfaceTypeIdx(uint32_t idx) { |
| 744 | DCHECK(!IsPrimitive()); |
| 745 | DCHECK(!IsArrayClass()); |
| 746 | return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_; |
| 747 | } |
| 748 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 749 | mirror::Class* Class::GetDirectInterface(Thread* self, Handle<mirror::Class> klass, |
Mathieu Chartier | bf99f77 | 2014-08-23 16:37:27 -0700 | [diff] [blame] | 750 | uint32_t idx) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 751 | DCHECK(klass.Get() != nullptr); |
| 752 | DCHECK(!klass->IsPrimitive()); |
| 753 | if (klass->IsArrayClass()) { |
| 754 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 755 | if (idx == 0) { |
| 756 | return class_linker->FindSystemClass(self, "Ljava/lang/Cloneable;"); |
| 757 | } else { |
| 758 | DCHECK_EQ(1U, idx); |
| 759 | return class_linker->FindSystemClass(self, "Ljava/io/Serializable;"); |
| 760 | } |
| 761 | } else if (klass->IsProxyClass()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 762 | mirror::ObjectArray<mirror::Class>* interfaces = klass.Get()->GetInterfaces(); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 763 | DCHECK(interfaces != nullptr); |
| 764 | return interfaces->Get(idx); |
| 765 | } else { |
| 766 | uint16_t type_idx = klass->GetDirectInterfaceTypeIdx(idx); |
| 767 | mirror::Class* interface = klass->GetDexCache()->GetResolvedType(type_idx); |
| 768 | if (interface == nullptr) { |
| 769 | interface = Runtime::Current()->GetClassLinker()->ResolveType(klass->GetDexFile(), type_idx, |
| 770 | klass.Get()); |
| 771 | CHECK(interface != nullptr || self->IsExceptionPending()); |
| 772 | } |
| 773 | return interface; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | const char* Class::GetSourceFile() { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 778 | const DexFile& dex_file = GetDexFile(); |
| 779 | const DexFile::ClassDef* dex_class_def = GetClassDef(); |
Sebastien Hertz | 4206eb5 | 2014-06-05 10:15:45 +0200 | [diff] [blame] | 780 | if (dex_class_def == nullptr) { |
| 781 | // Generated classes have no class def. |
| 782 | return nullptr; |
| 783 | } |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 784 | return dex_file.GetSourceFile(*dex_class_def); |
| 785 | } |
| 786 | |
| 787 | std::string Class::GetLocation() { |
| 788 | mirror::DexCache* dex_cache = GetDexCache(); |
| 789 | if (dex_cache != nullptr && !IsProxyClass()) { |
| 790 | return dex_cache->GetLocation()->ToModifiedUtf8(); |
| 791 | } |
| 792 | // Arrays and proxies are generated and have no corresponding dex file location. |
| 793 | return "generated class"; |
| 794 | } |
| 795 | |
| 796 | const DexFile::TypeList* Class::GetInterfaceTypeList() { |
| 797 | const DexFile::ClassDef* class_def = GetClassDef(); |
| 798 | if (class_def == nullptr) { |
| 799 | return nullptr; |
| 800 | } |
| 801 | return GetDexFile().GetInterfacesList(*class_def); |
| 802 | } |
| 803 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 804 | void Class::PopulateEmbeddedImtAndVTable(ArtMethod* const (&methods)[kImtSize], |
| 805 | size_t pointer_size) { |
| 806 | for (size_t i = 0; i < kImtSize; i++) { |
| 807 | auto method = methods[i]; |
| 808 | DCHECK(method != nullptr); |
Mathieu Chartier | 4edd847 | 2015-06-01 10:47:36 -0700 | [diff] [blame^] | 809 | SetEmbeddedImTableEntry(i, method, pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 810 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 811 | PointerArray* table = GetVTableDuringLinking(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 812 | CHECK(table != nullptr) << PrettyClass(this); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 813 | const size_t table_length = table->GetLength(); |
| 814 | SetEmbeddedVTableLength(table_length); |
| 815 | for (size_t i = 0; i < table_length; i++) { |
| 816 | SetEmbeddedVTableEntry(i, table->GetElementPtrSize<ArtMethod*>(i, pointer_size), pointer_size); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 817 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 818 | // Keep java.lang.Object class's vtable around for since it's easier |
| 819 | // to be reused by array classes during their linking. |
| 820 | if (!IsObjectClass()) { |
| 821 | SetVTable(nullptr); |
| 822 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 825 | // The pre-fence visitor for Class::CopyOf(). |
| 826 | class CopyClassVisitor { |
| 827 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 828 | explicit CopyClassVisitor(Thread* self, Handle<mirror::Class>* orig, size_t new_length, |
| 829 | size_t copy_bytes, ArtMethod* const (&imt)[mirror::Class::kImtSize], |
| 830 | size_t pointer_size) |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 831 | : self_(self), orig_(orig), new_length_(new_length), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 832 | copy_bytes_(copy_bytes), imt_(imt), pointer_size_(pointer_size) { |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 835 | void operator()(mirror::Object* obj, size_t usable_size ATTRIBUTE_UNUSED) const |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 836 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 837 | StackHandleScope<1> hs(self_); |
| 838 | Handle<mirror::Class> h_new_class_obj(hs.NewHandle(obj->AsClass())); |
| 839 | mirror::Object::CopyObject(self_, h_new_class_obj.Get(), orig_->Get(), copy_bytes_); |
| 840 | mirror::Class::SetStatus(h_new_class_obj, Class::kStatusResolving, self_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 841 | h_new_class_obj->PopulateEmbeddedImtAndVTable(imt_, pointer_size_); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 842 | h_new_class_obj->SetClassSize(new_length_); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | private: |
| 846 | Thread* const self_; |
| 847 | Handle<mirror::Class>* const orig_; |
| 848 | const size_t new_length_; |
| 849 | const size_t copy_bytes_; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 850 | ArtMethod* const (&imt_)[mirror::Class::kImtSize]; |
| 851 | const size_t pointer_size_; |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 852 | DISALLOW_COPY_AND_ASSIGN(CopyClassVisitor); |
| 853 | }; |
| 854 | |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 855 | Class* Class::CopyOf(Thread* self, int32_t new_length, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 856 | ArtMethod* const (&imt)[mirror::Class::kImtSize], size_t pointer_size) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 857 | DCHECK_GE(new_length, static_cast<int32_t>(sizeof(Class))); |
| 858 | // We may get copied by a compacting GC. |
| 859 | StackHandleScope<1> hs(self); |
| 860 | Handle<mirror::Class> h_this(hs.NewHandle(this)); |
| 861 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 862 | // The num_bytes (3rd param) is sizeof(Class) as opposed to SizeOf() |
| 863 | // to skip copying the tail part that we will overwrite here. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 864 | CopyClassVisitor visitor(self, &h_this, new_length, sizeof(Class), imt, pointer_size); |
| 865 | mirror::Object* new_class = kMovingClasses ? |
| 866 | heap->AllocObject<true>(self, java_lang_Class_.Read(), new_length, visitor) : |
| 867 | heap->AllocNonMovableObject<true>(self, java_lang_Class_.Read(), new_length, visitor); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 868 | if (UNLIKELY(new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 869 | self->AssertPendingOOMException(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 870 | return nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 871 | } |
Hiroshi Yamauchi | 0fbd6e6 | 2014-07-17 16:16:31 -0700 | [diff] [blame] | 872 | return new_class->AsClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Vladimir Marko | 3481ba2 | 2015-04-13 12:22:36 +0100 | [diff] [blame] | 875 | bool Class::ProxyDescriptorEquals(const char* match) { |
| 876 | DCHECK(IsProxyClass()); |
| 877 | return Runtime::Current()->GetClassLinker()->GetDescriptorForProxy(this) == match; |
| 878 | } |
| 879 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 880 | // TODO: Move this to java_lang_Class.cc? |
| 881 | ArtMethod* Class::GetDeclaredConstructor( |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 882 | Thread* self, Handle<mirror::ObjectArray<mirror::Class>> args) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 883 | for (auto& m : GetDirectMethods(sizeof(void*))) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 884 | // Skip <clinit> which is a static constructor, as well as non constructors. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 885 | if (m.IsStatic() || !m.IsConstructor()) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 886 | continue; |
| 887 | } |
| 888 | // May cause thread suspension and exceptions. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 889 | if (m.GetInterfaceMethodIfProxy(sizeof(void*))->EqualParameters(args)) { |
| 890 | return &m; |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 891 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 892 | if (UNLIKELY(self->IsExceptionPending())) { |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 893 | return nullptr; |
| 894 | } |
| 895 | } |
| 896 | return nullptr; |
| 897 | } |
| 898 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 899 | uint32_t Class::Depth() { |
| 900 | uint32_t depth = 0; |
| 901 | for (Class* klass = this; klass->GetSuperClass() != nullptr; klass = klass->GetSuperClass()) { |
| 902 | depth++; |
| 903 | } |
| 904 | return depth; |
| 905 | } |
| 906 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 907 | } // namespace mirror |
| 908 | } // namespace art |