blob: 28ce8f34ac53b26234806219e919d665c97fb908 [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:
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700124 explicit MethodHelper(Handle<mirror::ArtMethod> m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
125 : method_(m), shorty_(nullptr), shorty_len_(0) {
126 SetMethod(m.Get());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127 }
128
Brian Carlstromea46f952013-07-30 01:26:50 -0700129 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 DCHECK(new_m != nullptr);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800131 SetMethod(new_m);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 shorty_ = nullptr;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800133 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700134
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700135 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
136 return method_->GetInterfaceMethodIfProxy();
Ian Rogers848871b2013-08-05 10:56:33 -0700137 }
138
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700139 mirror::String* GetNameAsString(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
140 const DexFile* dex_file = method_->GetDexFile();
141 mirror::ArtMethod* method = method_->GetInterfaceMethodIfProxy();
142 uint32_t dex_method_idx = method->GetDexMethodIndex();
143 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
144 StackHandleScope<1> hs(self);
145 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
146 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
147 dex_cache);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800148 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700149
Jeff Hao9a916d32013-06-27 18:45:37 -0700150 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800151 const char* result = shorty_;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152 if (result == nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700153 result = method_->GetShorty(&shorty_len_);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800154 shorty_ = result;
155 }
156 return result;
157 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158
Ian Rogersb726dcb2012-09-05 08:57:23 -0700159 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800160 if (shorty_ == nullptr) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800161 GetShorty();
162 }
163 return shorty_len_;
164 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700165
Andreas Gampe36fea8d2014-03-10 13:37:40 -0700166 // Counts the number of references in the parameter list of the corresponding method.
167 // Note: Thus does _not_ include "this" for non-static methods.
168 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
169 const char* shorty = GetShorty();
170 uint32_t refs = 0;
171 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
172 if (shorty[i] == 'L') {
173 refs++;
174 }
175 }
176
177 return refs;
178 }
179
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700180 // May cause thread suspension due to GetClassFromTypeIdx calling ResolveType this caused a large
181 // number of bugs at call sites.
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800182 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700183 mirror::ArtMethod* method = GetMethod();
184 const DexFile* dex_file = method->GetDexFile();
185 const DexFile::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex());
186 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800187 uint16_t return_type_idx = proto_id.return_type_idx_;
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800188 return GetClassFromTypeIdx(return_type_idx, resolve);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800189 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700190
Ian Rogersb726dcb2012-09-05 08:57:23 -0700191 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800192 // "1 +" because the first in Args is the receiver.
193 // "- 1" because we don't count the return type.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700194 return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800195 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700196
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800197 // Get the primitive type associated with the given parameter.
198 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800199 CHECK_LT(param, NumArgs());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700200 if (GetMethod()->IsStatic()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201 param++; // 0th argument must skip return value at start of the shorty
202 } else if (param == 0) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800203 return Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800204 }
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800205 return Primitive::GetType(GetShorty()[param]);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800208 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
209 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
210 Primitive::Type type = GetParamPrimitiveType(param);
211 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
212 }
213
214 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700215 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800216 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800217 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700218
Ian Rogers151f2212014-05-06 11:27:27 -0700219 bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700220 const DexFile* dex_file = method_->GetDexFile();
221 const DexFile::MethodId& mid = dex_file->GetMethodId(GetMethod()->GetDexMethodIndex());
222 if (method_->GetDexCache() == other->method_->GetDexCache()) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800223 const DexFile::MethodId& other_mid =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700224 dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800225 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800226 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700227 const DexFile* other_dex_file = other->method_->GetDexFile();
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700228 const DexFile::MethodId& other_mid =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700229 other_dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
230 if (!DexFileStringEquals(dex_file, mid.name_idx_, other_dex_file, other_mid.name_idx_)) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700231 return false; // Name mismatch.
232 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700233 return dex_file->GetMethodSignature(mid) == other_dex_file->GetMethodSignature(other_mid);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700235
Ian Rogers151f2212014-05-06 11:27:27 -0700236 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
237 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
238 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
239 return false;
240 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700241 const DexFile::TypeList* types = method_->GetParameterTypeList();
242 const DexFile::TypeList* other_types = other->method_->GetParameterTypeList();
Ian Rogers151f2212014-05-06 11:27:27 -0700243 if (types == nullptr) {
244 return (other_types == nullptr) || (other_types->Size() == 0);
245 } else if (UNLIKELY(other_types == nullptr)) {
246 return types->Size() == 0;
247 }
248 uint32_t num_types = types->Size();
249 if (UNLIKELY(num_types != other_types->Size())) {
250 return false;
251 }
252 for (uint32_t i = 0; i < num_types; ++i) {
253 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
254 mirror::Class* other_param_type =
255 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
256 if (UNLIKELY(param_type != other_param_type)) {
257 return false;
258 }
259 }
260 return true;
261 }
262
Mathieu Chartiereae2fb22014-01-14 14:31:25 -0800263 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700264 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700265 mirror::ArtMethod* method = GetMethod();
266 mirror::Class* type = method->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800267 if (type == nullptr && resolve) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700268 type = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800269 CHECK(type != nullptr || Thread::Current()->IsExceptionPending());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 }
271 return type;
272 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700273
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800274 mirror::Class* GetDexCacheResolvedType(uint16_t type_idx)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700276 return GetMethod()->GetDexCacheResolvedTypes()->Get(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700277 }
Elliott Hughesa21039c2012-06-21 12:09:25 -0700278
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700280 mirror::ArtMethod* method = GetMethod();
281 mirror::String* s = method->GetDexCacheStrings()->Get(string_idx);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800282 if (UNLIKELY(s == nullptr)) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700283 StackHandleScope<1> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700284 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
285 s = Runtime::Current()->GetClassLinker()->ResolveString(*method->GetDexFile(), string_idx,
286 dex_cache);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700287 }
288 return s;
289 }
290
Ian Rogers83883d72013-10-21 21:07:24 -0700291 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
292 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700293 mirror::ArtMethod* method = GetMethod();
294 const DexFile* dexfile = method->GetDexFile();
295 if (dexfile == &other_dexfile) {
296 return method->GetDexMethodIndex();
Ian Rogers83883d72013-10-21 21:07:24 -0700297 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700298 const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex());
299 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Ian Rogers83883d72013-10-21 21:07:24 -0700300 const DexFile::StringId* other_descriptor =
301 other_dexfile.FindStringId(mid_declaring_class_descriptor);
302 if (other_descriptor != nullptr) {
303 const DexFile::TypeId* other_type_id =
304 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
305 if (other_type_id != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700306 const char* mid_name = dexfile->GetMethodName(mid);
Ian Rogers83883d72013-10-21 21:07:24 -0700307 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
308 if (other_name != nullptr) {
309 uint16_t other_return_type_idx;
310 std::vector<uint16_t> other_param_type_idxs;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700311 bool success = other_dexfile.CreateTypeList(
312 dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx,
313 &other_param_type_idxs);
Ian Rogers83883d72013-10-21 21:07:24 -0700314 if (success) {
315 const DexFile::ProtoId* other_sig =
316 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
317 if (other_sig != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700318 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
319 *other_type_id, *other_name, *other_sig);
Ian Rogers83883d72013-10-21 21:07:24 -0700320 if (other_mid != nullptr) {
321 return other_dexfile.GetIndexForMethodId(*other_mid);
322 }
323 }
324 }
325 }
326 }
327 }
328 return DexFile::kDexNoIndex;
329 }
330
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000331 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
332 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
333 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
334 uint32_t name_and_signature_idx)
335 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700336 mirror::ArtMethod* method = GetMethod();
337 const DexFile* dexfile = method->GetDexFile();
338 const uint32_t dex_method_idx = method->GetDexMethodIndex();
339 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000340 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700341 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
342 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
343 if (dexfile == &other_dexfile) {
344 return dex_method_idx;
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000345 }
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700346 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
Vladimir Markobbcc0c02014-02-03 14:08:42 +0000347 const DexFile::StringId* other_descriptor =
348 other_dexfile.FindStringId(mid_declaring_class_descriptor);
349 if (other_descriptor != nullptr) {
350 const DexFile::TypeId* other_type_id =
351 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
352 if (other_type_id != nullptr) {
353 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
354 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
355 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
356 if (other_mid != nullptr) {
357 return other_dexfile.GetIndexForMethodId(*other_mid);
358 }
359 }
360 }
361 return DexFile::kDexNoIndex;
362 }
363
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800364 private:
365 // Set the method_ field, for proxy methods looking up the interface method via the resolved
366 // methods table.
Ian Rogersef7d42f2014-01-06 12:55:46 -0800367 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700368 method_.Assign(method);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800369 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700371 Handle<mirror::ArtMethod> method_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800372 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800373 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800374
375 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
376};
377
378} // namespace art
379
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700380#endif // ART_RUNTIME_OBJECT_UTILS_H_