xref: /aosp_15_r20/art/libdexfile/dex/method_reference.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2013 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <stdint.h>
21*795d594fSAndroid Build Coastguard Worker #include <string>
22*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file.h"
23*795d594fSAndroid Build Coastguard Worker #include "dex/dex_file_reference.h"
24*795d594fSAndroid Build Coastguard Worker #include "dex/proto_reference.h"
25*795d594fSAndroid Build Coastguard Worker 
26*795d594fSAndroid Build Coastguard Worker namespace art {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker // A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile
29*795d594fSAndroid Build Coastguard Worker class MethodReference : public DexFileReference {
30*795d594fSAndroid Build Coastguard Worker  public:
MethodReference(const DexFile * file,uint32_t index)31*795d594fSAndroid Build Coastguard Worker   MethodReference(const DexFile* file, uint32_t index) : DexFileReference(file, index) {}
32*795d594fSAndroid Build Coastguard Worker   std::string PrettyMethod(bool with_signature = true) const {
33*795d594fSAndroid Build Coastguard Worker     return dex_file->PrettyMethod(index, with_signature);
34*795d594fSAndroid Build Coastguard Worker   }
GetMethodId()35*795d594fSAndroid Build Coastguard Worker   const dex::MethodId& GetMethodId() const {
36*795d594fSAndroid Build Coastguard Worker     return dex_file->GetMethodId(index);
37*795d594fSAndroid Build Coastguard Worker   }
GetProtoReference()38*795d594fSAndroid Build Coastguard Worker   const art::ProtoReference GetProtoReference() const {
39*795d594fSAndroid Build Coastguard Worker     return ProtoReference(dex_file, GetMethodId().proto_idx_);
40*795d594fSAndroid Build Coastguard Worker   }
41*795d594fSAndroid Build Coastguard Worker };
42*795d594fSAndroid Build Coastguard Worker 
43*795d594fSAndroid Build Coastguard Worker // Compare the actual referenced method signatures. Used for method reference deduplication.
44*795d594fSAndroid Build Coastguard Worker struct MethodReferenceValueComparator {
operatorMethodReferenceValueComparator45*795d594fSAndroid Build Coastguard Worker   bool operator()(MethodReference mr1, MethodReference mr2) const {
46*795d594fSAndroid Build Coastguard Worker     if (mr1.dex_file == mr2.dex_file) {
47*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(mr1.index < mr2.index, SlowCompare(mr1, mr2));
48*795d594fSAndroid Build Coastguard Worker       return mr1.index < mr2.index;
49*795d594fSAndroid Build Coastguard Worker     } else {
50*795d594fSAndroid Build Coastguard Worker       return SlowCompare(mr1, mr2);
51*795d594fSAndroid Build Coastguard Worker     }
52*795d594fSAndroid Build Coastguard Worker   }
53*795d594fSAndroid Build Coastguard Worker 
SlowCompareMethodReferenceValueComparator54*795d594fSAndroid Build Coastguard Worker   bool SlowCompare(MethodReference mr1, MethodReference mr2) const {
55*795d594fSAndroid Build Coastguard Worker     // The order is the same as for method ids in a single dex file.
56*795d594fSAndroid Build Coastguard Worker     // Compare the class descriptors first.
57*795d594fSAndroid Build Coastguard Worker     const dex::MethodId& mid1 = mr1.GetMethodId();
58*795d594fSAndroid Build Coastguard Worker     const dex::MethodId& mid2 = mr2.GetMethodId();
59*795d594fSAndroid Build Coastguard Worker     int descriptor_diff = DexFile::CompareDescriptors(
60*795d594fSAndroid Build Coastguard Worker         mr1.dex_file->GetTypeDescriptorView(mid1.class_idx_),
61*795d594fSAndroid Build Coastguard Worker         mr2.dex_file->GetTypeDescriptorView(mid2.class_idx_));
62*795d594fSAndroid Build Coastguard Worker     if (descriptor_diff != 0) {
63*795d594fSAndroid Build Coastguard Worker       return descriptor_diff < 0;
64*795d594fSAndroid Build Coastguard Worker     }
65*795d594fSAndroid Build Coastguard Worker     // Compare names second.
66*795d594fSAndroid Build Coastguard Worker     int name_diff = DexFile::CompareMemberNames(mr1.dex_file->GetMethodNameView(mid1),
67*795d594fSAndroid Build Coastguard Worker                                                 mr2.dex_file->GetMethodNameView(mid2));
68*795d594fSAndroid Build Coastguard Worker     if (name_diff != 0) {
69*795d594fSAndroid Build Coastguard Worker       return name_diff < 0;
70*795d594fSAndroid Build Coastguard Worker     }
71*795d594fSAndroid Build Coastguard Worker     // Then compare protos.
72*795d594fSAndroid Build Coastguard Worker     return ProtoReferenceValueComparator()(mr1.GetProtoReference(), mr2.GetProtoReference());
73*795d594fSAndroid Build Coastguard Worker   }
74*795d594fSAndroid Build Coastguard Worker };
75*795d594fSAndroid Build Coastguard Worker 
76*795d594fSAndroid Build Coastguard Worker }  // namespace art
77*795d594fSAndroid Build Coastguard Worker 
78*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_
79