1 /* 2 * Copyright (C) 2013 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 #ifndef ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_ 18 #define ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_ 19 20 #include <stdint.h> 21 #include <string> 22 #include "dex/dex_file.h" 23 #include "dex/dex_file_reference.h" 24 25 namespace art { 26 27 // A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile 28 class MethodReference : public DexFileReference { 29 public: MethodReference(const DexFile * file,uint32_t index)30 MethodReference(const DexFile* file, uint32_t index) : DexFileReference(file, index) {} 31 std::string PrettyMethod(bool with_signature = true) const { 32 return dex_file->PrettyMethod(index, with_signature); 33 } GetMethodId()34 const dex::MethodId& GetMethodId() const { 35 return dex_file->GetMethodId(index); 36 } 37 }; 38 39 // Compare the actual referenced method signatures. Used for method reference deduplication. 40 struct MethodReferenceValueComparator { operatorMethodReferenceValueComparator41 bool operator()(MethodReference mr1, MethodReference mr2) const { 42 if (mr1.dex_file == mr2.dex_file) { 43 DCHECK_EQ(mr1.index < mr2.index, SlowCompare(mr1, mr2)); 44 return mr1.index < mr2.index; 45 } else { 46 return SlowCompare(mr1, mr2); 47 } 48 } 49 SlowCompareMethodReferenceValueComparator50 bool SlowCompare(MethodReference mr1, MethodReference mr2) const { 51 // The order is the same as for method ids in a single dex file. 52 // Compare the class descriptors first. 53 const dex::MethodId& mid1 = mr1.GetMethodId(); 54 const dex::MethodId& mid2 = mr2.GetMethodId(); 55 int descriptor_diff = strcmp(mr1.dex_file->StringByTypeIdx(mid1.class_idx_), 56 mr2.dex_file->StringByTypeIdx(mid2.class_idx_)); 57 if (descriptor_diff != 0) { 58 return descriptor_diff < 0; 59 } 60 // Compare names second. 61 int name_diff = strcmp(mr1.dex_file->GetMethodName(mid1), mr2.dex_file->GetMethodName(mid2)); 62 if (name_diff != 0) { 63 return name_diff < 0; 64 } 65 // And then compare proto ids, starting with return type comparison. 66 const dex::ProtoId& prid1 = mr1.dex_file->GetProtoId(mid1.proto_idx_); 67 const dex::ProtoId& prid2 = mr2.dex_file->GetProtoId(mid2.proto_idx_); 68 int return_type_diff = strcmp(mr1.dex_file->StringByTypeIdx(prid1.return_type_idx_), 69 mr2.dex_file->StringByTypeIdx(prid2.return_type_idx_)); 70 if (return_type_diff != 0) { 71 return return_type_diff < 0; 72 } 73 // And finishing with lexicographical parameter comparison. 74 const dex::TypeList* params1 = mr1.dex_file->GetProtoParameters(prid1); 75 size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u; 76 const dex::TypeList* params2 = mr2.dex_file->GetProtoParameters(prid2); 77 size_t param2_size = (params2 != nullptr) ? params2->Size() : 0u; 78 for (size_t i = 0, num = std::min(param1_size, param2_size); i != num; ++i) { 79 int param_diff = strcmp(mr1.dex_file->StringByTypeIdx(params1->GetTypeItem(i).type_idx_), 80 mr2.dex_file->StringByTypeIdx(params2->GetTypeItem(i).type_idx_)); 81 if (param_diff != 0) { 82 return param_diff < 0; 83 } 84 } 85 return param1_size < param2_size; 86 } 87 }; 88 89 } // namespace art 90 91 #endif // ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_ 92