blob: 664ac897cb8843b150c8b80f532921a31aace645 [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:
Ian Rogersef7d42f2014-01-06 12:55:46 -080071 FieldHelper() : field_(nullptr) {}
72 explicit FieldHelper(mirror::ArtField* f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080073
Ian Rogersef7d42f2014-01-06 12:55:46 -080074 void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
75 DCHECK(new_f != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080076 field_ = new_f;
77 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070078
Ian Rogersb726dcb2012-09-05 08:57:23 -070079 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -080080 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -070081 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -080082 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -070083 DCHECK_LT(field_index, 2U);
84 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -080085 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070086 const DexFile& dex_file = GetDexFile();
87 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080088 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070089
Ian Rogers50239c72013-06-17 14:53:22 -070090 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -080091 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -070092 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers98379392014-02-24 16:53:16 -080093 return GetClassLinker()->FindSystemClass(Thread::Current(), GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080094 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070095 const DexFile& dex_file = GetDexFile();
96 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
97 mirror::Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080098 if (resolve && (type == nullptr)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -070099 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800100 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700101 }
102 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800103 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700104
Ian Rogersb726dcb2012-09-05 08:57:23 -0700105 const char* GetTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800106 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700107 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800108 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700109 DCHECK_LT(field_index, 2U);
110 // 0 == Class[] interfaces; 1 == Class[][] throws;
111 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800112 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700113 const DexFile& dex_file = GetDexFile();
114 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
115 return dex_file.GetFieldTypeDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800116 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700117
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 Primitive::Type GetTypeAsPrimitiveType()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120 return Primitive::GetType(GetTypeDescriptor()[0]);
121 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700122
Ian Rogersb726dcb2012-09-05 08:57:23 -0700123 bool IsPrimitiveType() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800124 Primitive::Type type = GetTypeAsPrimitiveType();
125 return type != Primitive::kPrimNot;
126 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700127
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 size_t FieldSize() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 Primitive::Type type = GetTypeAsPrimitiveType();
130 return Primitive::FieldSize(type);
131 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800132
133 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
134 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700136 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700137 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700138 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700139 DCHECK(field_->IsStatic());
140 DCHECK_LT(field_index, 2U);
141 // 0 == Class[] interfaces; 1 == Class[][] throws;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700142 declaring_class_descriptor_ = field_->GetDeclaringClass()->GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800143 return declaring_class_descriptor_.c_str();
144 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700145 const DexFile& dex_file = GetDexFile();
146 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
147 return dex_file.GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800148 }
149
150 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800151 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700152 return field_->GetDeclaringClass()->GetDexCache();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700154 ClassLinker* GetClassLinker() ALWAYS_INLINE {
155 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800156 }
Ian Rogersb726dcb2012-09-05 08:57:23 -0700157 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700158 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800159 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160 mirror::ArtField* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800161 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800162
163 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
164};
165
166class MethodHelper {
167 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800168 MethodHelper() : method_(nullptr), shorty_(nullptr), shorty_len_(0) {}
Ian Rogersca190662012-06-26 15:45:57 -0700169
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170 explicit MethodHelper(mirror::ArtMethod* m)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700171 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800172 : method_(nullptr), shorty_(nullptr), shorty_len_(0) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800173 SetMethod(m);
174 }
175
Brian Carlstromea46f952013-07-30 01:26:50 -0700176 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800177 DCHECK(new_m != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800178 SetMethod(new_m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800179 shorty_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800180 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700181
Ian Rogers53b8b092014-03-13 23:45:53 -0700182 mirror::ArtMethod* GetMethod() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700183 return method_;
184 }
185
Ian Rogersb726dcb2012-09-05 08:57:23 -0700186 const char* GetName() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800188 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700189 if (LIKELY(dex_method_idx != DexFile::kDexNoIndex)) {
Ian Rogers19846512012-02-24 11:42:47 -0800190 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
191 } else {
192 Runtime* runtime = Runtime::Current();
193 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700194 return "<runtime internal resolution method>";
Jeff Hao88474b42013-10-23 16:24:40 -0700195 } else if (method_ == runtime->GetImtConflictMethod()) {
196 return "<runtime internal imt conflict method>";
Ian Rogers19846512012-02-24 11:42:47 -0800197 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700198 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800199 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700200 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800201 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700202 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800203 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700204 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800205 }
206 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800207 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700208
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 mirror::String* GetNameAsString() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800210 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800211 uint32_t dex_method_idx = method_->GetDexMethodIndex();
212 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700213 StackHandleScope<1> hs(Thread::Current());
214 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700215 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217
Jeff Hao9a916d32013-06-27 18:45:37 -0700218 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800219 const char* result = shorty_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800220 if (result == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800221 const DexFile& dex_file = GetDexFile();
222 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
223 &shorty_len_);
224 shorty_ = result;
225 }
226 return result;
227 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228
Ian Rogersb726dcb2012-09-05 08:57:23 -0700229 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800230 if (shorty_ == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800231 GetShorty();
232 }
233 return shorty_len_;
234 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700235
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700236 // Counts the number of references in the parameter list of the corresponding method.
237 // Note: Thus does _not_ include "this" for non-static methods.
238 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
239 const char* shorty = GetShorty();
240 uint32_t refs = 0;
241 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
242 if (shorty[i] == 'L') {
243 refs++;
244 }
245 }
246
247 return refs;
248 }
249
Ian Rogersd91d6d62013-09-25 20:26:14 -0700250 const Signature GetSignature() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800251 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800252 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700253 if (dex_method_idx != DexFile::kDexNoIndex) {
Ian Rogers19846512012-02-24 11:42:47 -0800254 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
255 } else {
Ian Rogersd91d6d62013-09-25 20:26:14 -0700256 return Signature::NoSignature();
Ian Rogers19846512012-02-24 11:42:47 -0800257 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800258 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700260 const DexFile::ProtoId& GetPrototype() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800261 const DexFile& dex_file = GetDexFile();
262 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
263 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700264
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700265 const DexFile::TypeList* GetParameterTypeList() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 const DexFile::ProtoId& proto = GetPrototype();
267 return GetDexFile().GetProtoParameters(proto);
268 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700269
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800270 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800271 const DexFile& dex_file = GetDexFile();
272 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
273 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
274 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800275 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700277
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700278 const char* GetReturnTypeDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800279 const DexFile& dex_file = GetDexFile();
280 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
281 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
282 uint16_t return_type_idx = proto_id.return_type_idx_;
283 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
284 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700285
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700286 int32_t GetLineNumFromDexPC(uint32_t dex_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700287 if (dex_pc == DexFile::kDexNoIndex) {
288 return method_->IsNative() ? -2 : -1;
289 } else {
290 const DexFile& dex_file = GetDexFile();
291 return dex_file.GetLineNumFromPC(method_, dex_pc);
292 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800293 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700294
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700295 const char* GetDeclaringClassDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800296 const DexFile& dex_file = GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700297 uint32_t dex_method_idx = method_->GetDexMethodIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700298 if (UNLIKELY(dex_method_idx == DexFile::kDexNoIndex)) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700299 return "<runtime method>";
300 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700301 return dex_file.GetMethodDeclaringClassDescriptor(dex_file.GetMethodId(dex_method_idx));
302 }
303
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700304 const char* GetDeclaringClassSourceFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierf8322842014-05-16 10:59:25 -0700305 return method_->GetDeclaringClass()->GetSourceFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800306 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700307
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700308 uint16_t GetClassDefIndex() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
309 return method_->GetDeclaringClass()->GetDexClassDefIndex();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700310 }
311
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700312 const DexFile::ClassDef& GetClassDef() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
313 return GetDexFile().GetClassDef(GetClassDefIndex());
314 }
315
316 mirror::ClassLoader* GetClassLoader() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700317 return method_->GetDeclaringClass()->GetClassLoader();
318 }
319
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800320 bool IsStatic() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800321 return method_->IsStatic();
322 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700323
Ian Rogersb726dcb2012-09-05 08:57:23 -0700324 bool IsClassInitializer() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers241b5de2013-10-09 17:58:57 -0700325 return method_->IsConstructor() && IsStatic();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800326 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700327
Ian Rogersb726dcb2012-09-05 08:57:23 -0700328 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800329 // "1 +" because the first in Args is the receiver.
330 // "- 1" because we don't count the return type.
331 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
332 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800334 // Get the primitive type associated with the given parameter.
335 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800336 CHECK_LT(param, NumArgs());
337 if (IsStatic()) {
338 param++; // 0th argument must skip return value at start of the shorty
339 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800340 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800341 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800342 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800343 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700344
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800345 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
346 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
347 Primitive::Type type = GetParamPrimitiveType(param);
348 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
349 }
350
351 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700352 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800353 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800354 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700355
Ian Rogers151f2212014-05-06 11:27:27 -0700356 bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700357 const DexFile& dex_file = GetDexFile();
358 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800359 if (GetDexCache() == other->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800360 const DexFile::MethodId& other_mid =
361 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800362 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800363 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700364 const DexFile& other_dex_file = other->GetDexFile();
365 const DexFile::MethodId& other_mid =
366 other_dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogersdfb325e2013-10-30 01:00:44 -0700367 if (!DexFileStringEquals(&dex_file, mid.name_idx_,
368 &other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700369 return false; // Name mismatch.
370 }
Ian Rogersd91d6d62013-09-25 20:26:14 -0700371 return dex_file.GetMethodSignature(mid) == other_dex_file.GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700373
Ian Rogers151f2212014-05-06 11:27:27 -0700374 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
375 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
376 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
377 return false;
378 }
379 const DexFile::TypeList* types = GetParameterTypeList();
380 const DexFile::TypeList* other_types = other->GetParameterTypeList();
381 if (types == nullptr) {
382 return (other_types == nullptr) || (other_types->Size() == 0);
383 } else if (UNLIKELY(other_types == nullptr)) {
384 return types->Size() == 0;
385 }
386 uint32_t num_types = types->Size();
387 if (UNLIKELY(num_types != other_types->Size())) {
388 return false;
389 }
390 for (uint32_t i = 0; i < num_types; ++i) {
391 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
392 mirror::Class* other_param_type =
393 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
394 if (UNLIKELY(param_type != other_param_type)) {
395 return false;
396 }
397 }
398 return true;
399 }
400
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700401 const DexFile::CodeItem* GetCodeItem()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700402 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800403 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
404 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700405
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700406 bool IsResolvedTypeIdx(uint16_t type_idx) const
Ian Rogersb726dcb2012-09-05 08:57:23 -0700407 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800408 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != nullptr;
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800409 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700410
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800411 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700412 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800413 mirror::Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800414 if (type == nullptr && resolve) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800415 type = GetClassLinker()->ResolveType(type_idx, method_);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800416 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800417 }
418 return type;
419 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700421 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800423 const DexFile& dex_file = GetDexFile();
424 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
425 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700426
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800427 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700428 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700429 return method_->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800430 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700431
Ian Rogersb726dcb2012-09-05 08:57:23 -0700432 const DexFile& GetDexFile() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700433 return *GetDexCache()->GetDexFile();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800434 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700435
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800436 mirror::DexCache* GetDexCache() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700437 return method_->GetDeclaringClass()->GetDexCache();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700438 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700439
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800440 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
441 mirror::String* s = method_->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800442 if (UNLIKELY(s == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700443 StackHandleScope<1> hs(Thread::Current());
444 Handle<mirror::DexCache> dex_cache(hs.NewHandle(GetDexCache()));
Mathieu Chartier590fee92013-09-13 13:46:47 -0700445 s = GetClassLinker()->ResolveString(GetDexFile(), string_idx, dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700446 }
447 return s;
448 }
449
Ian Rogers83883d72013-10-21 21:07:24 -0700450 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
451 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
452 const DexFile& dexfile = GetDexFile();
453 if (&dexfile == &other_dexfile) {
454 return method_->GetDexMethodIndex();
455 }
456 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
457 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
458 const DexFile::StringId* other_descriptor =
459 other_dexfile.FindStringId(mid_declaring_class_descriptor);
460 if (other_descriptor != nullptr) {
461 const DexFile::TypeId* other_type_id =
462 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
463 if (other_type_id != nullptr) {
464 const char* mid_name = dexfile.GetMethodName(mid);
465 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
466 if (other_name != nullptr) {
467 uint16_t other_return_type_idx;
468 std::vector<uint16_t> other_param_type_idxs;
469 bool success = other_dexfile.CreateTypeList(dexfile.GetMethodSignature(mid).ToString(),
470 &other_return_type_idx,
471 &other_param_type_idxs);
472 if (success) {
473 const DexFile::ProtoId* other_sig =
474 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
475 if (other_sig != nullptr) {
476 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(*other_type_id,
477 *other_name,
478 *other_sig);
479 if (other_mid != nullptr) {
480 return other_dexfile.GetIndexForMethodId(*other_mid);
481 }
482 }
483 }
484 }
485 }
486 }
487 return DexFile::kDexNoIndex;
488 }
489
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000490 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
491 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
492 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
493 uint32_t name_and_signature_idx)
494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
495 const DexFile& dexfile = GetDexFile();
496 const DexFile::MethodId& mid = dexfile.GetMethodId(method_->GetDexMethodIndex());
497 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
498 DCHECK_STREQ(dexfile.GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
499 DCHECK_EQ(dexfile.GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
500 if (&dexfile == &other_dexfile) {
501 return method_->GetDexMethodIndex();
502 }
503 const char* mid_declaring_class_descriptor = dexfile.StringByTypeIdx(mid.class_idx_);
504 const DexFile::StringId* other_descriptor =
505 other_dexfile.FindStringId(mid_declaring_class_descriptor);
506 if (other_descriptor != nullptr) {
507 const DexFile::TypeId* other_type_id =
508 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
509 if (other_type_id != nullptr) {
510 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
511 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
512 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
513 if (other_mid != nullptr) {
514 return other_dexfile.GetIndexForMethodId(*other_mid);
515 }
516 }
517 }
518 return DexFile::kDexNoIndex;
519 }
520
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 private:
522 // Set the method_ field, for proxy methods looking up the interface method via the resolved
523 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800524 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
525 if (method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800526 mirror::Class* klass = method->GetDeclaringClass();
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700527 if (UNLIKELY(klass->IsProxyClass())) {
Brian Carlstromea46f952013-07-30 01:26:50 -0700528 mirror::ArtMethod* interface_method =
Ian Rogers19846512012-02-24 11:42:47 -0800529 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
Ian Rogersef7d42f2014-01-06 12:55:46 -0800530 DCHECK(interface_method != nullptr);
Ian Rogers4f6ad8a2013-03-18 15:27:28 -0700531 DCHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
Ian Rogers19846512012-02-24 11:42:47 -0800532 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800533 }
534 }
535 method_ = method;
536 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700537
Mathieu Chartier590fee92013-09-13 13:46:47 -0700538 ClassLinker* GetClassLinker() ALWAYS_INLINE {
539 return Runtime::Current()->GetClassLinker();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800540 }
541
Ian Rogersef7d42f2014-01-06 12:55:46 -0800542 mirror::ArtMethod* method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800543 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800544 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545
546 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
547};
548
549} // namespace art
550
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700551#endif // ART_RUNTIME_OBJECT_UTILS_H_