blob: 31f3b8e84ef1a1bd2d87d9e981056cd7d3cd6e7a [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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_METHOD_REFERENCE_H_
18#define ART_RUNTIME_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>
22#include "dex_file.h"
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070023#include "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 }
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070034 const DexFile::MethodId& GetMethodId() const {
35 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.
Mathieu Chartierfc8b4222017-09-17 13:44:24 -070053 const DexFile::MethodId& mid1 = mr1.GetMethodId();
54 const DexFile::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.
66 const DexFile::ProtoId& prid1 = mr1.dex_file->GetProtoId(mid1.proto_idx_);
67 const DexFile::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 DexFile::TypeList* params1 = mr1.dex_file->GetProtoParameters(prid1);
75 size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u;
76 const DexFile::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
Brian Carlstrom51c24672013-07-11 16:00:56 -070089} // namespace art
90
Brian Carlstromfc0e3212013-07-17 14:40:12 -070091#endif // ART_RUNTIME_METHOD_REFERENCE_H_