blob: 8150456eb980e04c8ee2e8aae815faf688ccc04b [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
Hiroshi Yamauchi41369d22014-08-19 13:10:36 -070044 // GetMethod() != Get() for proxy methods.
45 mirror::ArtMethod* Get() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
46 return method_.Get();
47 }
48
Ian Rogers22d5e732014-07-15 22:23:51 -070049 mirror::String* GetNameAsString(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
50
51 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
52 const char* result = shorty_;
53 if (result == nullptr) {
54 result = method_->GetShorty(&shorty_len_);
55 shorty_ = result;
56 }
57 return result;
58 }
59
60 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
61 if (shorty_ == nullptr) {
62 GetShorty();
63 }
64 return shorty_len_;
65 }
66
67 // Counts the number of references in the parameter list of the corresponding method.
68 // Note: Thus does _not_ include "this" for non-static methods.
69 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
70 const char* shorty = GetShorty();
71 uint32_t refs = 0;
72 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
73 if (shorty[i] == 'L') {
74 refs++;
75 }
76 }
77
78 return refs;
79 }
80
81 // May cause thread suspension due to GetClassFromTypeIdx calling ResolveType this caused a large
82 // number of bugs at call sites.
Ian Rogerse5877a12014-07-16 12:06:35 -070083 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -070084
85 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
86 // "1 +" because the first in Args is the receiver.
87 // "- 1" because we don't count the return type.
88 return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1;
89 }
90
91 // Get the primitive type associated with the given parameter.
92 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
93 CHECK_LT(param, NumArgs());
94 if (GetMethod()->IsStatic()) {
95 param++; // 0th argument must skip return value at start of the shorty
96 } else if (param == 0) {
97 return Primitive::kPrimNot;
98 }
99 return Primitive::GetType(GetShorty()[param]);
100 }
101
102 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
103 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
104 Primitive::Type type = GetParamPrimitiveType(param);
105 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
106 }
107
108 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
109 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
110 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
111 }
112
Ian Rogers1ff3c982014-08-12 02:30:58 -0700113 ALWAYS_INLINE bool HasSameNameAndSignature(MethodHelper* other)
114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700115
116 bool HasSameSignatureWithDifferentClassLoaders(MethodHelper* other)
Ian Rogerse5877a12014-07-16 12:06:35 -0700117 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700118
119 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
120 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
121
122 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
123
124 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126
127 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
128 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
129 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
130 uint32_t name_and_signature_idx)
131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132
133 private:
134 // Set the method_ field, for proxy methods looking up the interface method via the resolved
135 // methods table.
136 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
137 method_.Assign(method);
138 }
139
140 Handle<mirror::ArtMethod> method_;
141 const char* shorty_;
142 uint32_t shorty_len_;
143
144 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
145};
146
147} // namespace art
148
149#endif // ART_RUNTIME_METHOD_HELPER_H_