summaryrefslogtreecommitdiffstats
path: root/runtime/method_helper.cc
blob: 4b1b1daa9e1d8da87f11b6ed99c067e09246d233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (C) 2011 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.
 */

#include "method_helper.h"

#include "class_linker.h"
#include "dex_file-inl.h"
#include "handle_scope-inl.h"
#include "mirror/art_method-inl.h"
#include "mirror/dex_cache.h"
#include "runtime.h"

namespace art {

mirror::String* MethodHelper::GetNameAsString(Thread* self) {
  const DexFile* dex_file = method_->GetDexFile();
  mirror::ArtMethod* method = method_->GetInterfaceMethodIfProxy();
  uint32_t dex_method_idx = method->GetDexMethodIndex();
  const DexFile::MethodId& method_id = dex_file->GetMethodId(dex_method_idx);
  StackHandleScope<1> hs(self);
  Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
  return Runtime::Current()->GetClassLinker()->ResolveString(*dex_file, method_id.name_idx_,
                                                             dex_cache);
}

bool MethodHelper::HasSameNameAndSignature(MethodHelper* other) {
  const DexFile* dex_file = method_->GetDexFile();
  const DexFile::MethodId& mid = dex_file->GetMethodId(GetMethod()->GetDexMethodIndex());
  if (method_->GetDexCache() == other->method_->GetDexCache()) {
    const DexFile::MethodId& other_mid =
        dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
    return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
  }
  const DexFile* other_dex_file = other->method_->GetDexFile();
  const DexFile::MethodId& other_mid =
      other_dex_file->GetMethodId(other->GetMethod()->GetDexMethodIndex());
  if (!DexFileStringEquals(dex_file, mid.name_idx_, other_dex_file, other_mid.name_idx_)) {
    return false;  // Name mismatch.
  }
  return dex_file->GetMethodSignature(mid) == other_dex_file->GetMethodSignature(other_mid);
}

uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
  mirror::ArtMethod* method = GetMethod();
  const DexFile* dexfile = method->GetDexFile();
  if (dexfile == &other_dexfile) {
    return method->GetDexMethodIndex();
  }
  const DexFile::MethodId& mid = dexfile->GetMethodId(method->GetDexMethodIndex());
  const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
  const DexFile::StringId* other_descriptor =
      other_dexfile.FindStringId(mid_declaring_class_descriptor);
  if (other_descriptor != nullptr) {
    const DexFile::TypeId* other_type_id =
        other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
    if (other_type_id != nullptr) {
      const char* mid_name = dexfile->GetMethodName(mid);
      const DexFile::StringId* other_name = other_dexfile.FindStringId(mid_name);
      if (other_name != nullptr) {
        uint16_t other_return_type_idx;
        std::vector<uint16_t> other_param_type_idxs;
        bool success = other_dexfile.CreateTypeList(
            dexfile->GetMethodSignature(mid).ToString(), &other_return_type_idx,
            &other_param_type_idxs);
        if (success) {
          const DexFile::ProtoId* other_sig =
              other_dexfile.FindProtoId(other_return_type_idx, other_param_type_idxs);
          if (other_sig != nullptr) {
            const  DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
                *other_type_id, *other_name, *other_sig);
            if (other_mid != nullptr) {
              return other_dexfile.GetIndexForMethodId(*other_mid);
            }
          }
        }
      }
    }
  }
  return DexFile::kDexNoIndex;
}

uint32_t MethodHelper::FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
                                                        uint32_t name_and_signature_idx)
    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
  mirror::ArtMethod* method = GetMethod();
  const DexFile* dexfile = method->GetDexFile();
  const uint32_t dex_method_idx = method->GetDexMethodIndex();
  const DexFile::MethodId& mid = dexfile->GetMethodId(dex_method_idx);
  const DexFile::MethodId& name_and_sig_mid = other_dexfile.GetMethodId(name_and_signature_idx);
  DCHECK_STREQ(dexfile->GetMethodName(mid), other_dexfile.GetMethodName(name_and_sig_mid));
  DCHECK_EQ(dexfile->GetMethodSignature(mid), other_dexfile.GetMethodSignature(name_and_sig_mid));
  if (dexfile == &other_dexfile) {
    return dex_method_idx;
  }
  const char* mid_declaring_class_descriptor = dexfile->StringByTypeIdx(mid.class_idx_);
  const DexFile::StringId* other_descriptor =
      other_dexfile.FindStringId(mid_declaring_class_descriptor);
  if (other_descriptor != nullptr) {
    const DexFile::TypeId* other_type_id =
        other_dexfile.FindTypeId(other_dexfile.GetIndexForStringId(*other_descriptor));
    if (other_type_id != nullptr) {
      const DexFile::MethodId* other_mid = other_dexfile.FindMethodId(
          *other_type_id, other_dexfile.GetStringId(name_and_sig_mid.name_idx_),
          other_dexfile.GetProtoId(name_and_sig_mid.proto_idx_));
      if (other_mid != nullptr) {
        return other_dexfile.GetIndexForMethodId(*other_mid);
      }
    }
  }
  return DexFile::kDexNoIndex;
}

}  // namespace art