blob: 1bd2f9020c618f6e2d91ed0cb89778be85f81484 [file] [log] [blame]
Ian Rogers22d5e732014-07-15 22:23:51 -07001/*
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
Ian Rogerse5877a12014-07-16 12:06:35 -070017#include "method_helper-inl.h"
Ian Rogers22d5e732014-07-15 22:23:51 -070018
19#include "class_linker.h"
20#include "dex_file-inl.h"
21#include "handle_scope-inl.h"
22#include "mirror/art_method-inl.h"
23#include "mirror/dex_cache.h"
24#include "runtime.h"
25
26namespace art {
27
28mirror::String* MethodHelper::GetNameAsString(Thread* self) {
29 const DexFile* dex_file = method_->GetDexFile();
30 mirror::ArtMethod* method = method_->GetInterfaceMethodIfProxy();
31 uint32_t dex_method_idx = method->GetDexMethodIndex();
32 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
33 StackHandleScope<1> hs(self);
34 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
35 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
36 dex_cache);
37}
38
39bool MethodHelper::HasSameNameAndSignature(MethodHelper* other) {
40 const DexFile* dex_file = method_->GetDexFile();
41 const DexFile::MethodId& mid = dex_file->GetMethodId(GetMethod()->GetDexMethodIndex());
42 if (method_->GetDexCache() == other->method_->GetDexCache()) {
43 const DexFile::MethodId& other_mid =
44 dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
45 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
46 }
47 const DexFile* other_dex_file = other->method_->GetDexFile();
48 const DexFile::MethodId& other_mid =
49 other_dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
50 if (!DexFileStringEquals(dex_file, mid.name_idx_, other_dex_file, other_mid.name_idx_)) {
51 return false; // Name mismatch.
52 }
53 return dex_file->GetMethodSignature(mid) == other_dex_file->GetMethodSignature(other_mid);
54}
55
Ian Rogerse5877a12014-07-16 12:06:35 -070056bool MethodHelper::HasSameSignatureWithDifferentClassLoaders(MethodHelper* other) {
57 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
58 return false;
59 }
60 const DexFile::TypeList* types = method_->GetParameterTypeList();
61 const DexFile::TypeList* other_types = other->method_->GetParameterTypeList();
62 if (types == nullptr) {
63 return (other_types == nullptr) || (other_types->Size() == 0);
64 } else if (UNLIKELY(other_types == nullptr)) {
65 return types->Size() == 0;
66 }
67 uint32_t num_types = types->Size();
68 if (UNLIKELY(num_types != other_types->Size())) {
69 return false;
70 }
71 for (uint32_t i = 0; i < num_types; ++i) {
72 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
73 mirror::Class* other_param_type =
74 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
75 if (UNLIKELY(param_type != other_param_type)) {
76 return false;
77 }
78 }
79 return true;
80}
81
Ian Rogers22d5e732014-07-15 22:23:51 -070082uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
83 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
84 mirror::ArtMethod* method = GetMethod();
85 const DexFile* dexfile = method->GetDexFile();
86 if (dexfile == &other_dexfile) {
87 return method->GetDexMethodIndex();
88 }
89 const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex());
90 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
91 const DexFile::StringId* other_descriptor =
92 other_dexfile.FindStringId(mid_declaring_class_descriptor);
93 if (other_descriptor != nullptr) {
94 const DexFile::TypeId* other_type_id =
95 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
96 if (other_type_id != nullptr) {
97 const char* mid_name = dexfile->GetMethodName(mid);
98 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
99 if (other_name != nullptr) {
100 uint16_t other_return_type_idx;
101 std::vector<uint16_t> other_param_type_idxs;
102 bool success = other_dexfile.CreateTypeList(
103 dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx,
104 &other_param_type_idxs);
105 if (success) {
106 const DexFile::ProtoId* other_sig =
107 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
108 if (other_sig != nullptr) {
109 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
110 *other_type_id, *other_name, *other_sig);
111 if (other_mid != nullptr) {
112 return other_dexfile.GetIndexForMethodId(*other_mid);
113 }
114 }
115 }
116 }
117 }
118 }
119 return DexFile::kDexNoIndex;
120}
121
122uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
123 uint32_t name_and_signature_idx)
124 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
125 mirror::ArtMethod* method = GetMethod();
126 const DexFile* dexfile = method->GetDexFile();
127 const uint32_t dex_method_idx = method->GetDexMethodIndex();
128 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
129 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
130 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
131 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
132 if (dexfile == &other_dexfile) {
133 return dex_method_idx;
134 }
135 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
136 const DexFile::StringId* other_descriptor =
137 other_dexfile.FindStringId(mid_declaring_class_descriptor);
138 if (other_descriptor != nullptr) {
139 const DexFile::TypeId* other_type_id =
140 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
141 if (other_type_id != nullptr) {
142 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
143 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
144 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
145 if (other_mid != nullptr) {
146 return other_dexfile.GetIndexForMethodId(*other_mid);
147 }
148 }
149 }
150 return DexFile::kDexNoIndex;
151}
152
153} // namespace art