blob: 4b1b1daa9e1d8da87f11b6ed99c067e09246d233 [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
17#include "method_helper.h"
18
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
56uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
57 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
58 mirror::ArtMethod* method = GetMethod();
59 const DexFile* dexfile = method->GetDexFile();
60 if (dexfile == &other_dexfile) {
61 return method->GetDexMethodIndex();
62 }
63 const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex());
64 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
65 const DexFile::StringId* other_descriptor =
66 other_dexfile.FindStringId(mid_declaring_class_descriptor);
67 if (other_descriptor != nullptr) {
68 const DexFile::TypeId* other_type_id =
69 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
70 if (other_type_id != nullptr) {
71 const char* mid_name = dexfile->GetMethodName(mid);
72 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
73 if (other_name != nullptr) {
74 uint16_t other_return_type_idx;
75 std::vector<uint16_t> other_param_type_idxs;
76 bool success = other_dexfile.CreateTypeList(
77 dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx,
78 &other_param_type_idxs);
79 if (success) {
80 const DexFile::ProtoId* other_sig =
81 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
82 if (other_sig != nullptr) {
83 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
84 *other_type_id, *other_name, *other_sig);
85 if (other_mid != nullptr) {
86 return other_dexfile.GetIndexForMethodId(*other_mid);
87 }
88 }
89 }
90 }
91 }
92 }
93 return DexFile::kDexNoIndex;
94}
95
96uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
97 uint32_t name_and_signature_idx)
98 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
99 mirror::ArtMethod* method = GetMethod();
100 const DexFile* dexfile = method->GetDexFile();
101 const uint32_t dex_method_idx = method->GetDexMethodIndex();
102 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
103 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
104 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
105 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
106 if (dexfile == &other_dexfile) {
107 return dex_method_idx;
108 }
109 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
110 const DexFile::StringId* other_descriptor =
111 other_dexfile.FindStringId(mid_declaring_class_descriptor);
112 if (other_descriptor != nullptr) {
113 const DexFile::TypeId* other_type_id =
114 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
115 if (other_type_id != nullptr) {
116 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
117 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
118 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
119 if (other_mid != nullptr) {
120 return other_dexfile.GetIndexForMethodId(*other_mid);
121 }
122 }
123 }
124 return DexFile::kDexNoIndex;
125}
126
127} // namespace art