blob: f66ac30c53783286b18ff2903d4496777e4c823a [file] [log] [blame]
Brian Carlstrom51c24672013-07-11 16:00:56 -07001/*
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
David Sehr312f3b22018-03-19 08:39:26 -070017#ifndef ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_
18#define ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_
Brian Carlstrom51c24672013-07-11 16:00:56 -070019
Vladimir Markoc7f83202014-01-24 17:55:18 +000020#include <stdint.h>
David Sehr709b0702016-10-13 09:12:37 -070021#include <string>
David Sehr9e734c72018-01-04 17:56:19 -080022#include "dex/dex_file.h"
23#include "dex/dex_file_reference.h"
Vladimir Markoc7f83202014-01-24 17:55:18 +000024
Brian Carlstrom51c24672013-07-11 16:00:56 -070025namespace art {
26
Brian Carlstrom51c24672013-07-11 16:00:56 -070027// A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070028class MethodReference : public DexFileReference {
29 public:
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);
Brian Carlstrom51c24672013-07-11 16:00:56 -070033 }
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080034 const dex::MethodId& GetMethodId() const {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070035 return dex_file->GetMethodId(index);
Brian Carlstrom51c24672013-07-11 16:00:56 -070036 }
37};
38
Vladimir Marko0eb882b2017-05-15 13:39:18 +010039// Compare the actual referenced method signatures. Used for method reference deduplication.
40struct MethodReferenceValueComparator {
41 bool operator()(MethodReference mr1, MethodReference mr2) const {
42 if (mr1.dex_file == mr2.dex_file) {
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070043 DCHECK_EQ(mr1.index < mr2.index, SlowCompare(mr1, mr2));
44 return mr1.index < mr2.index;
Vladimir Marko0eb882b2017-05-15 13:39:18 +010045 } else {
46 return SlowCompare(mr1, mr2);
47 }
48 }
49
50 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.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080053 const dex::MethodId& mid1 = mr1.GetMethodId();
54 const dex::MethodId& mid2 = mr2.GetMethodId();
Vladimir Marko0eb882b2017-05-15 13:39:18 +010055 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.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080066 const dex::ProtoId& prid1 = mr1.dex_file->GetProtoId(mid1.proto_idx_);
67 const dex::ProtoId& prid2 = mr2.dex_file->GetProtoId(mid2.proto_idx_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +010068 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.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080074 const dex::TypeList* params1 = mr1.dex_file->GetProtoParameters(prid1);
Vladimir Marko0eb882b2017-05-15 13:39:18 +010075 size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u;
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080076 const dex::TypeList* params2 = mr2.dex_file->GetProtoParameters(prid2);
Vladimir Marko0eb882b2017-05-15 13:39:18 +010077 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
Brian Carlstrom51c24672013-07-11 16:00:56 -070089} // namespace art
90
David Sehr312f3b22018-03-19 08:39:26 -070091#endif // ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_