summaryrefslogtreecommitdiff
path: root/libdexfile/dex/method_reference.h
diff options
context:
space:
mode:
author David Sehr <sehr@google.com> 2018-03-19 08:39:26 -0700
committer David Sehr <sehr@google.com> 2018-03-19 11:02:48 -0700
commit312f3b2fd0094c028a7d243b116947a35a745806 (patch)
tree3d7ec049ded98c489098c87250c75e3f711f8290 /libdexfile/dex/method_reference.h
parent0a3d5eb2ff9e70fa5785638da938439835d0337e (diff)
Move some remaining dex utilities
There were several utilities related to building/walking/testing dex files that were not in libdexfile. This change consolidates these. Bug: 22322814 Test: make -j 50 test-art-host Change-Id: Id76e9179d03b8ec7d67f7e0f267121f54f0ec2e0
Diffstat (limited to 'libdexfile/dex/method_reference.h')
-rw-r--r--libdexfile/dex/method_reference.h91
1 files changed, 91 insertions, 0 deletions
diff --git a/libdexfile/dex/method_reference.h b/libdexfile/dex/method_reference.h
new file mode 100644
index 0000000000..266582b059
--- /dev/null
+++ b/libdexfile/dex/method_reference.h
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_
+#define ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_
+
+#include <stdint.h>
+#include <string>
+#include "dex/dex_file.h"
+#include "dex/dex_file_reference.h"
+
+namespace art {
+
+// A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile
+class MethodReference : public DexFileReference {
+ public:
+ MethodReference(const DexFile* file, uint32_t index) : DexFileReference(file, index) {}
+ std::string PrettyMethod(bool with_signature = true) const {
+ return dex_file->PrettyMethod(index, with_signature);
+ }
+ const DexFile::MethodId& GetMethodId() const {
+ return dex_file->GetMethodId(index);
+ }
+};
+
+// Compare the actual referenced method signatures. Used for method reference deduplication.
+struct MethodReferenceValueComparator {
+ bool operator()(MethodReference mr1, MethodReference mr2) const {
+ if (mr1.dex_file == mr2.dex_file) {
+ DCHECK_EQ(mr1.index < mr2.index, SlowCompare(mr1, mr2));
+ return mr1.index < mr2.index;
+ } else {
+ return SlowCompare(mr1, mr2);
+ }
+ }
+
+ bool SlowCompare(MethodReference mr1, MethodReference mr2) const {
+ // The order is the same as for method ids in a single dex file.
+ // Compare the class descriptors first.
+ const DexFile::MethodId& mid1 = mr1.GetMethodId();
+ const DexFile::MethodId& mid2 = mr2.GetMethodId();
+ int descriptor_diff = strcmp(mr1.dex_file->StringByTypeIdx(mid1.class_idx_),
+ mr2.dex_file->StringByTypeIdx(mid2.class_idx_));
+ if (descriptor_diff != 0) {
+ return descriptor_diff < 0;
+ }
+ // Compare names second.
+ int name_diff = strcmp(mr1.dex_file->GetMethodName(mid1), mr2.dex_file->GetMethodName(mid2));
+ if (name_diff != 0) {
+ return name_diff < 0;
+ }
+ // And then compare proto ids, starting with return type comparison.
+ const DexFile::ProtoId& prid1 = mr1.dex_file->GetProtoId(mid1.proto_idx_);
+ const DexFile::ProtoId& prid2 = mr2.dex_file->GetProtoId(mid2.proto_idx_);
+ int return_type_diff = strcmp(mr1.dex_file->StringByTypeIdx(prid1.return_type_idx_),
+ mr2.dex_file->StringByTypeIdx(prid2.return_type_idx_));
+ if (return_type_diff != 0) {
+ return return_type_diff < 0;
+ }
+ // And finishing with lexicographical parameter comparison.
+ const DexFile::TypeList* params1 = mr1.dex_file->GetProtoParameters(prid1);
+ size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u;
+ const DexFile::TypeList* params2 = mr2.dex_file->GetProtoParameters(prid2);
+ size_t param2_size = (params2 != nullptr) ? params2->Size() : 0u;
+ for (size_t i = 0, num = std::min(param1_size, param2_size); i != num; ++i) {
+ int param_diff = strcmp(mr1.dex_file->StringByTypeIdx(params1->GetTypeItem(i).type_idx_),
+ mr2.dex_file->StringByTypeIdx(params2->GetTypeItem(i).type_idx_));
+ if (param_diff != 0) {
+ return param_diff < 0;
+ }
+ }
+ return param1_size < param2_size;
+ }
+};
+
+} // namespace art
+
+#endif // ART_LIBDEXFILE_DEX_METHOD_REFERENCE_H_