1 /* 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-inl.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 26 namespace art { 27 28 mirror::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 39 bool MethodHelper::HasSameSignatureWithDifferentClassLoaders(MethodHelper* other) { 40 if (UNLIKELY(GetReturnType() != other->GetReturnType())) { 41 return false; 42 } 43 const DexFile::TypeList* types = method_->GetParameterTypeList(); 44 const DexFile::TypeList* other_types = other->method_->GetParameterTypeList(); 45 if (types == nullptr) { 46 return (other_types == nullptr) || (other_types->Size() == 0); 47 } else if (UNLIKELY(other_types == nullptr)) { 48 return types->Size() == 0; 49 } 50 uint32_t num_types = types->Size(); 51 if (UNLIKELY(num_types != other_types->Size())) { 52 return false; 53 } 54 for (uint32_t i = 0; i < num_types; ++i) { 55 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_); 56 mirror::Class* other_param_type = 57 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_); 58 if (UNLIKELY(param_type != other_param_type)) { 59 return false; 60 } 61 } 62 return true; 63 } 64 65 uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile) 66 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 67 mirror::ArtMethod* method = GetMethod(); 68 const DexFile* dexfile = method->GetDexFile(); 69 if (dexfile == &other_dexfile) { 70 return method->GetDexMethodIndex(); 71 } 72 const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex()); 73 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); 74 const DexFile::StringId* other_descriptor = 75 other_dexfile.FindStringId(mid_declaring_class_descriptor); 76 if (other_descriptor != nullptr) { 77 const DexFile::TypeId* other_type_id = 78 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); 79 if (other_type_id != nullptr) { 80 const char* mid_name = dexfile->GetMethodName(mid); 81 const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name); 82 if (other_name != nullptr) { 83 uint16_t other_return_type_idx; 84 std::vector<uint16_t> other_param_type_idxs; 85 bool success = other_dexfile.CreateTypeList( 86 dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx, 87 &other_param_type_idxs); 88 if (success) { 89 const DexFile::ProtoId* other_sig = 90 other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs); 91 if (other_sig != nullptr) { 92 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( 93 *other_type_id, *other_name, *other_sig); 94 if (other_mid != nullptr) { 95 return other_dexfile.GetIndexForMethodId(*other_mid); 96 } 97 } 98 } 99 } 100 } 101 } 102 return DexFile::kDexNoIndex; 103 } 104 105 uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile, 106 uint32_t name_and_signature_idx) 107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { 108 mirror::ArtMethod* method = GetMethod(); 109 const DexFile* dexfile = method->GetDexFile(); 110 const uint32_t dex_method_idx = method->GetDexMethodIndex(); 111 const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx); 112 const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx); 113 DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid)); 114 DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid)); 115 if (dexfile == &other_dexfile) { 116 return dex_method_idx; 117 } 118 const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_); 119 const DexFile::StringId* other_descriptor = 120 other_dexfile.FindStringId(mid_declaring_class_descriptor); 121 if (other_descriptor != nullptr) { 122 const DexFile::TypeId* other_type_id = 123 other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor)); 124 if (other_type_id != nullptr) { 125 const DexFile::MethodId* other_mid = other_dexfile.FindMethodId( 126 *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_), 127 other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_)); 128 if (other_mid != nullptr) { 129 return other_dexfile.GetIndexForMethodId(*other_mid); 130 } 131 } 132 } 133 return DexFile::kDexNoIndex; 134 } 135 136 } // namespace art 137