blob: a05ebe6df362683ed8c5cae0dfc5c1a49c68a3b6 [file] [log] [blame]
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_OBJECT_UTILS_H_
18#define ART_RUNTIME_OBJECT_UTILS_H_
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080019
Ian Rogers98379392014-02-24 16:53:16 -080020#include "class_linker.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080021#include "dex_file.h"
Ian Rogers672f5202012-01-12 18:06:40 -080022#include "monitor.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field.h"
24#include "mirror/art_method.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class.h"
26#include "mirror/dex_cache.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/iftable.h"
Sebastien Hertz80989a62014-04-01 14:39:44 +020028#include "mirror/proxy.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/string.h"
30
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080031#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070032#include "handle_scope-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033
34#include <string>
35
36namespace art {
37
Mathieu Chartierc528dba2013-11-26 12:00:11 -080038template <typename T>
Ian Rogers672f5202012-01-12 18:06:40 -080039class ObjectLock {
40 public:
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070041 ObjectLock(Thread* self, Handle<T> object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070042 : self_(self), obj_(object) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070043 CHECK(object.Get() != nullptr);
44 obj_->MonitorEnter(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080045 }
46
Ian Rogersb726dcb2012-09-05 08:57:23 -070047 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070048 obj_->MonitorExit(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080049 }
50
Ian Rogers05f30572013-02-20 12:13:11 -080051 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070052 Monitor::Wait(self_, obj_.Get(), 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080053 }
54
Ian Rogersb726dcb2012-09-05 08:57:23 -070055 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070056 obj_->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080057 }
58
Ian Rogersb726dcb2012-09-05 08:57:23 -070059 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070060 obj_->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080061 }
62
63 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064 Thread* const self_;
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070065 Handle<T> const obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080066 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
67};
68
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080069class FieldHelper {
70 public:
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070071 explicit FieldHelper(Handle<mirror::ArtField> f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080072
Ian Rogersef7d42f2014-01-06 12:55:46 -080073 void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 DCHECK(new_f != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070075 field_.Assign(new_f);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080076 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070077
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070078 mirror::ArtField* GetField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
79 return field_.Get();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070081
Ian Rogers50239c72013-06-17 14:53:22 -070082 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -080083 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -070084 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070085 return Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
86 field_->GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080087 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070088 const DexFile* dex_file = field_->GetDexFile();
89 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index);
90 mirror::Class* type = field_->GetDexCache()->GetResolvedType(field_id.type_idx_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 if (resolve && (type == nullptr)) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070092 type = Runtime::Current()->GetClassLinker()->ResolveType(field_id.type_idx_, field_.Get());
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -070094 }
95 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080096 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070097
Ian Rogersc2b44472011-12-14 21:17:17 -080098 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
99 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700100 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700102 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700103 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700104 DCHECK(field_->IsStatic());
105 DCHECK_LT(field_index, 2U);
106 // 0 == Class[] interfaces; 1 == Class[][] throws;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700107 declaring_class_descriptor_ = field_->GetDeclaringClass()->GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800108 return declaring_class_descriptor_.c_str();
109 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700110 const DexFile* dex_file = field_->GetDexFile();
111 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index);
112 return dex_file->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800113 }
114
115 private:
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700116 Handle<mirror::ArtField> field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800117 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800118
119 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
120};
121
122class MethodHelper {
123 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 MethodHelper() : method_(nullptr), shorty_(nullptr), shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700125
Ian Rogersef7d42f2014-01-06 12:55:46 -0800126 explicit MethodHelper(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 : method_(nullptr), shorty_(nullptr), shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 SetMethod(m);
130 }
131
Brian Carlstromea46f952013-07-30 01:26:50 -0700132 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800133 DCHECK(new_m != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 SetMethod(new_m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 shorty_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800136 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700137
Ian Rogers53b8b092014-03-13 23:45:53 -0700138 mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700139 return method_;
140 }
141
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800144 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700145 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800146 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
147 } else {
148 Runtime* runtime = Runtime::Current();
149 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700150 return "<runtime internal resolution method>";
Jeff Hao88474b42013-10-23 16:24:40 -0700151 } else if (method_ == runtime->GetImtConflictMethod()) {
152 return "<runtime internal imt conflict method>";
Ian Rogers19846512012-02-24 11:42:47 -0800153 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700154 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800155 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700156 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800157 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700158 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800159 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700160 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800161 }
162 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800163 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700164
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800165 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800166 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800167 uint32_t dex_method_idx = method_->GetDexMethodIndex();
168 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700169 StackHandleScope<1> hs(Thread::Current());
170 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700171 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700173
Jeff Hao9a916d32013-06-27 18:45:37 -0700174 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800175 const char* result = shorty_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800176 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800177 const DexFile& dex_file = GetDexFile();
178 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
179 &shorty_len_);
180 shorty_ = result;
181 }
182 return result;
183 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700184
Ian Rogersb726dcb2012-09-05 08:57:23 -0700185 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800186 if (shorty_ == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 GetShorty();
188 }
189 return shorty_len_;
190 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700192 // Counts the number of references in the parameter list of the corresponding method.
193 // Note: Thus does _not_ include "this" for non-static methods.
194 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
195 const char* shorty = GetShorty();
196 uint32_t refs = 0;
197 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
198 if (shorty[i] == 'L') {
199 refs++;
200 }
201 }
202
203 return refs;
204 }
205
Ian Rogersd91d6d62013-09-25 20:26:14 -0700206 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800208 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700209 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800210 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
211 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700212 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800213 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800214 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700215
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700216 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 const DexFile& dex_file = GetDexFile();
218 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
219 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700221 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 const DexFile::ProtoId& proto = GetPrototype();
223 return GetDexFile().GetProtoParameters(proto);
224 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700225
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800226 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800227 const DexFile& dex_file = GetDexFile();
228 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
229 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
230 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800231 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800232 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700233
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700234 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800235 const DexFile& dex_file = GetDexFile();
236 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
237 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
238 uint16_t return_type_idx = proto_id.return_type_idx_;
239 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
240 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700241
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700242 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700243 if (dex_pc == DexFile::kDexNoIndex) {
244 return method_->IsNative() ? -2 : -1;
245 } else {
246 const DexFile& dex_file = GetDexFile();
247 return dex_file.GetLineNumFromPC(method_, dex_pc);
248 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800249 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700250
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700251 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800252 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700253 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700254 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700255 return "<runtime method>";
256 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700257 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
258 }
259
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700260 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700261 return method_->GetDeclaringClass()->GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800262 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700263
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700264 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
265 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700266 }
267
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700268 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
269 return GetDexFile().GetClassDef(GetClassDefIndex());
270 }
271
272 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700273 return method_->GetDeclaringClass()->GetClassLoader();
274 }
275
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800276 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 return method_->IsStatic();
278 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700281 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800282 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283
Ian Rogersb726dcb2012-09-05 08:57:23 -0700284 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800285 // "1 +" because the first in Args is the receiver.
286 // "- 1" because we don't count the return type.
287 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
288 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700289
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800290 // Get the primitive type associated with the given parameter.
291 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 CHECK_LT(param, NumArgs());
293 if (IsStatic()) {
294 param++; // 0th argument must skip return value at start of the shorty
295 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800296 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800297 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800298 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800299 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700300
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800301 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
302 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
303 Primitive::Type type = GetParamPrimitiveType(param);
304 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
305 }
306
307 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800309 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800310 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700311
Ian Rogers151f2212014-05-06 11:27:27 -0700312 bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700313 const DexFile& dex_file = GetDexFile();
314 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800315 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800316 const DexFile::MethodId& other_mid =
317 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800318 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700320 const DexFile& other_dex_file = other->GetDexFile();
321 const DexFile::MethodId& other_mid =
322 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700323 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
324 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700325 return false; // Name mismatch.
326 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700327 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800328 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700329
Ian Rogers151f2212014-05-06 11:27:27 -0700330 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
331 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
332 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
333 return false;
334 }
335 const DexFile::TypeList* types = GetParameterTypeList();
336 const DexFile::TypeList* other_types = other->GetParameterTypeList();
337 if (types == nullptr) {
338 return (other_types == nullptr) || (other_types->Size() == 0);
339 } else if (UNLIKELY(other_types == nullptr)) {
340 return types->Size() == 0;
341 }
342 uint32_t num_types = types->Size();
343 if (UNLIKELY(num_types != other_types->Size())) {
344 return false;
345 }
346 for (uint32_t i = 0; i < num_types; ++i) {
347 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
348 mirror::Class* other_param_type =
349 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
350 if (UNLIKELY(param_type != other_param_type)) {
351 return false;
352 }
353 }
354 return true;
355 }
356
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700357 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700358 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800359 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
360 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700362 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700363 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800364 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != nullptr;
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800365 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700366
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800367 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700368 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800369 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800370 if (type == nullptr && resolve) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371 type = GetClassLinker()->ResolveType(type_idx, method_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800372 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 }
374 return type;
375 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700376
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700378 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800379 const DexFile& dex_file = GetDexFile();
380 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
381 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700382
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800383 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700384 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700385 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800386 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700387
Ian Rogersb726dcb2012-09-05 08:57:23 -0700388 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700389 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800390 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700391
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800392 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700393 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700394 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700395
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800396 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
397 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800398 if (UNLIKELY(s == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700399 StackHandleScope<1> hs(Thread::Current());
400 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700401 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700402 }
403 return s;
404 }
405
Ian Rogers83883d72013-10-21 21:07:24 -0700406 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
408 const DexFile& dexfile = GetDexFile();
409 if (&dexfile == &other_dexfile) {
410 return method_->GetDexMethodIndex();
411 }
412 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
413 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
414 const DexFile::StringId* other_descriptor =
415 other_dexfile.FindStringId(mid_declaring_class_descriptor);
416 if (other_descriptor != nullptr) {
417 const DexFile::TypeId* other_type_id =
418 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
419 if (other_type_id != nullptr) {
420 const char* mid_name = dexfile.GetMethodName(mid);
421 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
422 if (other_name != nullptr) {
423 uint16_t other_return_type_idx;
424 std::vector<uint16_t> other_param_type_idxs;
425 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
426 &other_return_type_idx,
427 &other_param_type_idxs);
428 if (success) {
429 const DexFile::ProtoId* other_sig =
430 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
431 if (other_sig != nullptr) {
432 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
433 *other_name,
434 *other_sig);
435 if (other_mid != nullptr) {
436 return other_dexfile.GetIndexForMethodId(*other_mid);
437 }
438 }
439 }
440 }
441 }
442 }
443 return DexFile::kDexNoIndex;
444 }
445
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000446 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
447 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
448 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
449 uint32_t name_and_signature_idx)
450 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
451 const DexFile& dexfile = GetDexFile();
452 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
453 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
454 DCHECK_STREQ(dexfile.GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
455 DCHECK_EQ(dexfile.GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
456 if (&dexfile == &other_dexfile) {
457 return method_->GetDexMethodIndex();
458 }
459 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
460 const DexFile::StringId* other_descriptor =
461 other_dexfile.FindStringId(mid_declaring_class_descriptor);
462 if (other_descriptor != nullptr) {
463 const DexFile::TypeId* other_type_id =
464 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
465 if (other_type_id != nullptr) {
466 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
467 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
468 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
469 if (other_mid != nullptr) {
470 return other_dexfile.GetIndexForMethodId(*other_mid);
471 }
472 }
473 }
474 return DexFile::kDexNoIndex;
475 }
476
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800477 private:
478 // Set the method_ field, for proxy methods looking up the interface method via the resolved
479 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800480 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
481 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700483 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700484 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800485 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800486 DCHECK(interface_method != nullptr);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700487 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800488 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800489 }
490 }
491 method_ = method;
492 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700493
Mathieu Chartier590fee92013-09-13 13:46:47 -0700494 ClassLinker* GetClassLinker() ALWAYS_INLINE {
495 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800496 }
497
Ian Rogersef7d42f2014-01-06 12:55:46 -0800498 mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800499 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800500 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800501
502 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
503};
504
505} // namespace art
506
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700507#endif // ART_RUNTIME_OBJECT_UTILS_H_