blob: 79c2b91a55cca8ad761a92953b9c2bdc7857064d [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
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070028template <template <class T> class HandleKind>
29mirror::String* MethodHelperT<HandleKind>::GetNameAsString(Thread* self) {
Ian Rogers22d5e732014-07-15 22:23:51 -070030 const DexFile* dex_file = method_->GetDexFile();
31 mirror::ArtMethod* method = method_->GetInterfaceMethodIfProxy();
32 uint32_t dex_method_idx = method->GetDexMethodIndex();
33 const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
34 StackHandleScope<1> hs(self);
35 Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
36 return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
37 dex_cache);
38}
39
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070040template <template <class T> class HandleKind>
41template <template <class T2> class HandleKind2>
42bool MethodHelperT<HandleKind>::HasSameSignatureWithDifferentClassLoaders(
43 MethodHelperT<HandleKind2>* other) {
Ian Rogerse5877a12014-07-16 12:06:35 -070044 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
45 return false;
46 }
47 const DexFile::TypeList* types = method_->GetParameterTypeList();
48 const DexFile::TypeList* other_types = other->method_->GetParameterTypeList();
49 if (types == nullptr) {
50 return (other_types == nullptr) || (other_types->Size() == 0);
51 } else if (UNLIKELY(other_types == nullptr)) {
52 return types->Size() == 0;
53 }
54 uint32_t num_types = types->Size();
55 if (UNLIKELY(num_types != other_types->Size())) {
56 return false;
57 }
58 for (uint32_t i = 0; i < num_types; ++i) {
59 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
60 mirror::Class* other_param_type =
61 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
62 if (UNLIKELY(param_type != other_param_type)) {
63 return false;
64 }
65 }
66 return true;
67}
68
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070069template <template <class T> class HandleKind>
70uint32_t MethodHelperT<HandleKind>::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
Ian Rogers22d5e732014-07-15 22:23:51 -070071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
72 mirror::ArtMethod* method = GetMethod();
73 const DexFile* dexfile = method->GetDexFile();
74 if (dexfile == &other_dexfile) {
75 return method->GetDexMethodIndex();
76 }
77 const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex());
78 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
79 const DexFile::StringId* other_descriptor =
80 other_dexfile.FindStringId(mid_declaring_class_descriptor);
81 if (other_descriptor != nullptr) {
82 const DexFile::TypeId* other_type_id =
83 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
84 if (other_type_id != nullptr) {
85 const char* mid_name = dexfile->GetMethodName(mid);
86 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
87 if (other_name != nullptr) {
88 uint16_t other_return_type_idx;
89 std::vector<uint16_t> other_param_type_idxs;
90 bool success = other_dexfile.CreateTypeList(
91 dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx,
92 &other_param_type_idxs);
93 if (success) {
94 const DexFile::ProtoId* other_sig =
95 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
96 if (other_sig != nullptr) {
97 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
98 *other_type_id, *other_name, *other_sig);
99 if (other_mid != nullptr) {
100 return other_dexfile.GetIndexForMethodId(*other_mid);
101 }
102 }
103 }
104 }
105 }
106 }
107 return DexFile::kDexNoIndex;
108}
109
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700110template <template <typename> class HandleKind>
111uint32_t MethodHelperT<HandleKind>::FindDexMethodIndexInOtherDexFile(
112 const DexFile& other_dexfile, uint32_t name_and_signature_idx)
Ian Rogers22d5e732014-07-15 22:23:51 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
114 mirror::ArtMethod* method = GetMethod();
115 const DexFile* dexfile = method->GetDexFile();
116 const uint32_t dex_method_idx = method->GetDexMethodIndex();
117 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
118 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
119 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
120 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
121 if (dexfile == &other_dexfile) {
122 return dex_method_idx;
123 }
124 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
125 const DexFile::StringId* other_descriptor =
126 other_dexfile.FindStringId(mid_declaring_class_descriptor);
127 if (other_descriptor != nullptr) {
128 const DexFile::TypeId* other_type_id =
129 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
130 if (other_type_id != nullptr) {
131 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
132 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
133 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
134 if (other_mid != nullptr) {
135 return other_dexfile.GetIndexForMethodId(*other_mid);
136 }
137 }
138 }
139 return DexFile::kDexNoIndex;
140}
141
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700142// Instantiate methods.
143template mirror::String* MethodHelperT<Handle>::GetNameAsString(Thread* self);
144
145template mirror::String* MethodHelperT<MutableHandle>::GetNameAsString(Thread* self);
146
147template
148uint32_t MethodHelperT<Handle>::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile);
149template
150uint32_t MethodHelperT<MutableHandle>::FindDexMethodIndexInOtherDexFile(
151 const DexFile& other_dexfile);
152
153template
154uint32_t MethodHelperT<Handle>::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
155 uint32_t name_and_signature_idx);
156template
157uint32_t MethodHelperT<MutableHandle>::FindDexMethodIndexInOtherDexFile(
158 const DexFile& other_dexfile, uint32_t name_and_signature_idx);
159
160template
161bool MethodHelperT<Handle>::HasSameSignatureWithDifferentClassLoaders<Handle>(
162 MethodHelperT<Handle>* other);
163
164template
165bool MethodHelperT<Handle>::HasSameSignatureWithDifferentClassLoaders<MutableHandle>(
166 MethodHelperT<MutableHandle>* other);
167
168template
169bool MethodHelperT<MutableHandle>::HasSameSignatureWithDifferentClassLoaders<Handle>(
170 MethodHelperT<Handle>* other);
171
172template
173bool MethodHelperT<MutableHandle>::HasSameSignatureWithDifferentClassLoaders<MutableHandle>(
174 MethodHelperT<MutableHandle>* other);
175
Ian Rogers22d5e732014-07-15 22:23:51 -0700176} // namespace art