blob: 012695e4d10ca565168f9027221646dcfcb4e316 [file] [log] [blame]
Ian Rogers22d5e732014-07-15 22:23:51 -07001/*
2 * Copyright (C) 2011 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
17#ifndef ART_RUNTIME_METHOD_HELPER_H_
18#define ART_RUNTIME_METHOD_HELPER_H_
19
20#include "base/macros.h"
21#include "handle.h"
22#include "mirror/art_method.h"
23#include "primitive.h"
24
25namespace art {
26
27class MethodHelper {
28 public:
29 explicit MethodHelper(Handle<mirror::ArtMethod> m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
30 : method_(m), shorty_(nullptr), shorty_len_(0) {
31 SetMethod(m.Get());
32 }
33
34 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
35 DCHECK(new_m != nullptr);
36 SetMethod(new_m);
37 shorty_ = nullptr;
38 }
39
40 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
41 return method_->GetInterfaceMethodIfProxy();
42 }
43
44 mirror::String* GetNameAsString(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
45
46 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
47 const char* result = shorty_;
48 if (result == nullptr) {
49 result = method_->GetShorty(&shorty_len_);
50 shorty_ = result;
51 }
52 return result;
53 }
54
55 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
56 if (shorty_ == nullptr) {
57 GetShorty();
58 }
59 return shorty_len_;
60 }
61
62 // Counts the number of references in the parameter list of the corresponding method.
63 // Note: Thus does _not_ include "this" for non-static methods.
64 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
65 const char* shorty = GetShorty();
66 uint32_t refs = 0;
67 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
68 if (shorty[i] == 'L') {
69 refs++;
70 }
71 }
72
73 return refs;
74 }
75
76 // May cause thread suspension due to GetClassFromTypeIdx calling ResolveType this caused a large
77 // number of bugs at call sites.
78 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
79 mirror::ArtMethod* method = GetMethod();
80 const DexFile* dex_file = method->GetDexFile();
81 const DexFile::MethodId& method_id = dex_file->GetMethodId(method->GetDexMethodIndex());
82 const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id);
83 uint16_t return_type_idx = proto_id.return_type_idx_;
84 return GetClassFromTypeIdx(return_type_idx, resolve);
85 }
86
87 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
88 // "1 +" because the first in Args is the receiver.
89 // "- 1" because we don't count the return type.
90 return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1;
91 }
92
93 // Get the primitive type associated with the given parameter.
94 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
95 CHECK_LT(param, NumArgs());
96 if (GetMethod()->IsStatic()) {
97 param++; // 0th argument must skip return value at start of the shorty
98 } else if (param == 0) {
99 return Primitive::kPrimNot;
100 }
101 return Primitive::GetType(GetShorty()[param]);
102 }
103
104 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
105 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
106 Primitive::Type type = GetParamPrimitiveType(param);
107 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
108 }
109
110 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
111 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
112 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
113 }
114
115 bool HasSameNameAndSignature(MethodHelper* other) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
118 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
119 if (UNLIKELY(GetReturnType() != other->GetReturnType())) {
120 return false;
121 }
122 const DexFile::TypeList* types = method_->GetParameterTypeList();
123 const DexFile::TypeList* other_types = other->method_->GetParameterTypeList();
124 if (types == nullptr) {
125 return (other_types == nullptr) || (other_types->Size() == 0);
126 } else if (UNLIKELY(other_types == nullptr)) {
127 return types->Size() == 0;
128 }
129 uint32_t num_types = types->Size();
130 if (UNLIKELY(num_types != other_types->Size())) {
131 return false;
132 }
133 for (uint32_t i = 0; i < num_types; ++i) {
134 mirror::Class* param_type = GetClassFromTypeIdx(types->GetTypeItem(i).type_idx_);
135 mirror::Class* other_param_type =
136 other->GetClassFromTypeIdx(other_types->GetTypeItem(i).type_idx_);
137 if (UNLIKELY(param_type != other_param_type)) {
138 return false;
139 }
140 }
141 return true;
142 }
143
144 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
145 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
146
147 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
148
149 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
150 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
151
152 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
153 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
154 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
155 uint32_t name_and_signature_idx)
156 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
157
158 private:
159 // Set the method_ field, for proxy methods looking up the interface method via the resolved
160 // methods table.
161 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
162 method_.Assign(method);
163 }
164
165 Handle<mirror::ArtMethod> method_;
166 const char* shorty_;
167 uint32_t shorty_len_;
168
169 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
170};
171
172} // namespace art
173
174#endif // ART_RUNTIME_METHOD_HELPER_H_