blob: 4379b4ad2a8374cbbd6d377745b33ab49f4dd9e8 [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"
28#include "mirror/string.h"
29
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "runtime.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070031#include "handle_scope-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080032
33#include <string>
34
35namespace art {
36
Mathieu Chartierc528dba2013-11-26 12:00:11 -080037template <typename T>
Ian Rogers672f5202012-01-12 18:06:40 -080038class ObjectLock {
39 public:
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070040 ObjectLock(Thread* self, Handle<T> object) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogers1f539342012-10-03 21:09:42 -070041 : self_(self), obj_(object) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070042 CHECK(object.Get() != nullptr);
43 obj_->MonitorEnter(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080044 }
45
Ian Rogersb726dcb2012-09-05 08:57:23 -070046 ~ObjectLock() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070047 obj_->MonitorExit(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080048 }
49
Ian Rogers05f30572013-02-20 12:13:11 -080050 void WaitIgnoringInterrupts() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070051 Monitor::Wait(self_, obj_.Get(), 0, 0, false, kWaiting);
Ian Rogers672f5202012-01-12 18:06:40 -080052 }
53
Ian Rogersb726dcb2012-09-05 08:57:23 -070054 void Notify() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070055 obj_->Notify(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080056 }
57
Ian Rogersb726dcb2012-09-05 08:57:23 -070058 void NotifyAll() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070059 obj_->NotifyAll(self_);
Ian Rogers672f5202012-01-12 18:06:40 -080060 }
61
62 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063 Thread* const self_;
Mathieu Chartierdb2633c2014-05-16 09:59:29 -070064 Handle<T> const obj_;
Ian Rogers672f5202012-01-12 18:06:40 -080065 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
66};
67
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080068class FieldHelper {
69 public:
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070070 explicit FieldHelper(Handle<mirror::ArtField> f) : field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080071
Ian Rogersef7d42f2014-01-06 12:55:46 -080072 void ChangeField(mirror::ArtField* new_f) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
73 DCHECK(new_f != nullptr);
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070074 field_.Assign(new_f);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080075 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070076
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070077 mirror::ArtField* GetField() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
78 return field_.Get();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080079 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070080
Ian Rogers50239c72013-06-17 14:53:22 -070081 mirror::Class* GetType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersc2b44472011-12-14 21:17:17 -080082 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -070083 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070084 return Runtime::Current()->GetClassLinker()->FindSystemClass(Thread::Current(),
85 field_->GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080086 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070087 const DexFile* dex_file = field_->GetDexFile();
88 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index);
89 mirror::Class* type = field_->GetDexCache()->GetResolvedType(field_id.type_idx_);
Ian Rogersef7d42f2014-01-06 12:55:46 -080090 if (resolve && (type == nullptr)) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -070091 type = Runtime::Current()->GetClassLinker()->ResolveType(field_id.type_idx_, field_.Get());
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogersfc0e94b2013-09-23 23:51:32 -070093 }
94 return type;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080095 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -070096
Ian Rogersc2b44472011-12-14 21:17:17 -080097 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
98 // If you need it longer, copy it into a std::string.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 const char* GetDeclaringClassDescriptor()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700101 uint32_t field_index = field_->GetDexFieldIndex();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700102 if (UNLIKELY(field_->GetDeclaringClass()->IsProxyClass())) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700103 DCHECK(field_->IsStatic());
104 DCHECK_LT(field_index, 2U);
105 // 0 == Class[] interfaces; 1 == Class[][] throws;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700106 declaring_class_descriptor_ = field_->GetDeclaringClass()->GetDescriptor();
Ian Rogersc2b44472011-12-14 21:17:17 -0800107 return declaring_class_descriptor_.c_str();
108 }
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700109 const DexFile* dex_file = field_->GetDexFile();
110 const DexFile::FieldId& field_id = dex_file->GetFieldId(field_index);
111 return dex_file->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800112 }
113
114 private:
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -0700115 Handle<mirror::ArtField> field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800116 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800117
118 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
119};
120
121class MethodHelper {
122 public:
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700123 explicit MethodHelper(Handle<mirror::ArtMethod> m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
124 : method_(m), shorty_(nullptr), shorty_len_(0) {
125 SetMethod(m.Get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800126 }
127
Brian Carlstromea46f952013-07-30 01:26:50 -0700128 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 DCHECK(new_m != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 SetMethod(new_m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800131 shorty_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800132 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700133
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700134 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
135 return method_->GetInterfaceMethodIfProxy();
Ian Rogers848871b2013-08-05 10:56:33 -0700136 }
137
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700138 mirror::String* GetNameAsString(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
139 const DexFile* dex_file = method_->GetDexFile();
140 mirror::ArtMethod* method = method_->GetInterfaceMethodIfProxy();
141 uint32_t dex_method_idx = method->GetDexMethodIndex();
142 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
143 StackHandleScope<1> hs(self);
144 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
145 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
146 dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800147 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700148
Jeff Hao9a916d32013-06-27 18:45:37 -0700149 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800150 const char* result = shorty_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800151 if (result == nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700152 result = method_->GetShorty(&shorty_len_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800153 shorty_ = result;
154 }
155 return result;
156 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700157
Ian Rogersb726dcb2012-09-05 08:57:23 -0700158 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800159 if (shorty_ == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800160 GetShorty();
161 }
162 return shorty_len_;
163 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700164
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700165 // Counts the number of references in the parameter list of the corresponding method.
166 // Note: Thus does _not_ include "this" for non-static methods.
167 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
168 const char* shorty = GetShorty();
169 uint32_t refs = 0;
170 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
171 if (shorty[i] == 'L') {
172 refs++;
173 }
174 }
175
176 return refs;
177 }
178
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700179 // May cause thread suspension due to GetClassFromTypeIdx calling ResolveType this caused a large
180 // number of bugs at call sites.
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800181 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700182 mirror::ArtMethod* method = GetMethod();
183 const DexFile* dex_file = method->GetDexFile();
184 const DexFile::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex());
185 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800186 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800187 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800188 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700189
Ian Rogersb726dcb2012-09-05 08:57:23 -0700190 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800191 // "1 +" because the first in Args is the receiver.
192 // "- 1" because we don't count the return type.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700193 return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800194 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700195
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800196 // Get the primitive type associated with the given parameter.
197 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800198 CHECK_LT(param, NumArgs());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700199 if (GetMethod()->IsStatic()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800200 param++; // 0th argument must skip return value at start of the shorty
201 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800202 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800204 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800205 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700206
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800207 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
208 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
209 Primitive::Type type = GetParamPrimitiveType(param);
210 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
211 }
212
213 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700214 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800215 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800216 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217
Ian Rogers151f2212014-05-06 11:27:27 -0700218 bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700219 const DexFile* dex_file = method_->GetDexFile();
220 const DexFile::MethodId& mid = dex_file->GetMethodId(GetMethod()->GetDexMethodIndex());
221 if (method_->GetDexCache() == other->method_->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800222 const DexFile::MethodId& other_mid =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700223 dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800224 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800225 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700226 const DexFile* other_dex_file = other->method_->GetDexFile();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700227 const DexFile::MethodId& other_mid =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700228 other_dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
229 if (!DexFileStringEquals(dex_file, mid.name_idx_, other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700230 return false; // Name mismatch.
231 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700232 return dex_file->GetMethodSignature(mid) == other_dex_file->GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800233 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700234
Ian Rogers151f2212014-05-06 11:27:27 -0700235 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
236 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
237 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
238 return false;
239 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700240 const DexFile::TypeList* types = method_->GetParameterTypeList();
241 const DexFile::TypeList* other_types = other->method_->GetParameterTypeList();
Ian Rogers151f2212014-05-06 11:27:27 -0700242 if (types == nullptr) {
243 return (other_types == nullptr) || (other_types->Size() == 0);
244 } else if (UNLIKELY(other_types == nullptr)) {
245 return types->Size() == 0;
246 }
247 uint32_t num_types = types->Size();
248 if (UNLIKELY(num_types != other_types->Size())) {
249 return false;
250 }
251 for (uint32_t i = 0; i < num_types; ++i) {
252 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
253 mirror::Class* other_param_type =
254 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
255 if (UNLIKELY(param_type != other_param_type)) {
256 return false;
257 }
258 }
259 return true;
260 }
261
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800262 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700263 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700264 mirror::ArtMethod* method = GetMethod();
265 mirror::Class* type = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800266 if (type == nullptr && resolve) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700267 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800268 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800269 }
270 return type;
271 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700272
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700274 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700275 return GetMethod()->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700276 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700277
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800278 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700279 mirror::ArtMethod* method = GetMethod();
280 mirror::String* s = method->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800281 if (UNLIKELY(s == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700282 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700283 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
284 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
285 dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700286 }
287 return s;
288 }
289
Ian Rogers83883d72013-10-21 21:07:24 -0700290 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
291 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700292 mirror::ArtMethod* method = GetMethod();
293 const DexFile* dexfile = method->GetDexFile();
294 if (dexfile == &other_dexfile) {
295 return method->GetDexMethodIndex();
Ian Rogers83883d72013-10-21 21:07:24 -0700296 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700297 const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex());
298 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Ian Rogers83883d72013-10-21 21:07:24 -0700299 const DexFile::StringId* other_descriptor =
300 other_dexfile.FindStringId(mid_declaring_class_descriptor);
301 if (other_descriptor != nullptr) {
302 const DexFile::TypeId* other_type_id =
303 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
304 if (other_type_id != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700305 const char* mid_name = dexfile->GetMethodName(mid);
Ian Rogers83883d72013-10-21 21:07:24 -0700306 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
307 if (other_name != nullptr) {
308 uint16_t other_return_type_idx;
309 std::vector<uint16_t> other_param_type_idxs;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700310 bool success = other_dexfile.CreateTypeList(
311 dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx,
312 &other_param_type_idxs);
Ian Rogers83883d72013-10-21 21:07:24 -0700313 if (success) {
314 const DexFile::ProtoId* other_sig =
315 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
316 if (other_sig != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700317 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
318 *other_type_id, *other_name, *other_sig);
Ian Rogers83883d72013-10-21 21:07:24 -0700319 if (other_mid != nullptr) {
320 return other_dexfile.GetIndexForMethodId(*other_mid);
321 }
322 }
323 }
324 }
325 }
326 }
327 return DexFile::kDexNoIndex;
328 }
329
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000330 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
331 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
332 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
333 uint32_t name_and_signature_idx)
334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700335 mirror::ArtMethod* method = GetMethod();
336 const DexFile* dexfile = method->GetDexFile();
337 const uint32_t dex_method_idx = method->GetDexMethodIndex();
338 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000339 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700340 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
341 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
342 if (dexfile == &other_dexfile) {
343 return dex_method_idx;
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000344 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700345 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000346 const DexFile::StringId* other_descriptor =
347 other_dexfile.FindStringId(mid_declaring_class_descriptor);
348 if (other_descriptor != nullptr) {
349 const DexFile::TypeId* other_type_id =
350 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
351 if (other_type_id != nullptr) {
352 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
353 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
354 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
355 if (other_mid != nullptr) {
356 return other_dexfile.GetIndexForMethodId(*other_mid);
357 }
358 }
359 }
360 return DexFile::kDexNoIndex;
361 }
362
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800363 private:
364 // Set the method_ field, for proxy methods looking up the interface method via the resolved
365 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800366 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700367 method_.Assign(method);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800368 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700369
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700370 Handle<mirror::ArtMethod> method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800371 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800372 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373
374 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
375};
376
377} // namespace art
378
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700379#endif // ART_RUNTIME_OBJECT_UTILS_H_