blob: fe364d386794423f94d757fc87164e67b7bfaa44 [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
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070027template <template <class T> class HandleKind>
28class MethodHelperT {
Ian Rogers22d5e732014-07-15 22:23:51 -070029 public:
Andreas Gampe5a4b8a22014-09-11 08:30:08 -070030 explicit MethodHelperT(HandleKind<mirror::ArtMethod> m)
31 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) : method_(m), shorty_(nullptr), shorty_len_(0) {
Ian Rogers22d5e732014-07-15 22:23:51 -070032 }
33
34 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
35 return method_->GetInterfaceMethodIfProxy();
36 }
37
Hiroshi Yamauchi41369d22014-08-19 13:10:36 -070038 // GetMethod() != Get() for proxy methods.
39 mirror::ArtMethod* Get() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
40 return method_.Get();
41 }
42
Ian Rogers22d5e732014-07-15 22:23:51 -070043 mirror::String* GetNameAsString(Thread* self) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
44
45 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
46 const char* result = shorty_;
47 if (result == nullptr) {
48 result = method_->GetShorty(&shorty_len_);
49 shorty_ = result;
50 }
51 return result;
52 }
53
54 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
55 if (shorty_ == nullptr) {
56 GetShorty();
57 }
58 return shorty_len_;
59 }
60
61 // Counts the number of references in the parameter list of the corresponding method.
62 // Note: Thus does _not_ include "this" for non-static methods.
63 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
64 const char* shorty = GetShorty();
65 uint32_t refs = 0;
66 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
67 if (shorty[i] == 'L') {
68 refs++;
69 }
70 }
71
72 return refs;
73 }
74
75 // May cause thread suspension due to GetClassFromTypeIdx calling ResolveType this caused a large
76 // number of bugs at call sites.
Ian Rogerse5877a12014-07-16 12:06:35 -070077 mirror::Class* GetReturnType(bool resolve = true) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -070078
79 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
80 // "1 +" because the first in Args is the receiver.
81 // "- 1" because we don't count the return type.
82 return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1;
83 }
84
85 // Get the primitive type associated with the given parameter.
86 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
87 CHECK_LT(param, NumArgs());
88 if (GetMethod()->IsStatic()) {
89 param++; // 0th argument must skip return value at start of the shorty
90 } else if (param == 0) {
91 return Primitive::kPrimNot;
92 }
93 return Primitive::GetType(GetShorty()[param]);
94 }
95
96 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
97 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98 Primitive::Type type = GetParamPrimitiveType(param);
99 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
100 }
101
102 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
103 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
104 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
105 }
106
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700107 template <template <class T> class HandleKind2>
108 ALWAYS_INLINE bool HasSameNameAndSignature(MethodHelperT<HandleKind2>* other)
Ian Rogers1ff3c982014-08-12 02:30:58 -0700109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700110
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700111 template <template <class T> class HandleKind2>
112 bool HasSameSignatureWithDifferentClassLoaders(MethodHelperT<HandleKind2>* other)
Ian Rogerse5877a12014-07-16 12:06:35 -0700113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700114
115 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
117
118 mirror::String* ResolveString(uint32_t string_idx) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
119
120 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
123 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
124 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
125 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
126 uint32_t name_and_signature_idx)
127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
128
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700129 protected:
130 HandleKind<mirror::ArtMethod> method_;
131
132 const char* shorty_;
133 uint32_t shorty_len_;
134
135 private:
136 template <template <class T2> class HandleKind2> friend class MethodHelperT;
137
138 DISALLOW_COPY_AND_ASSIGN(MethodHelperT);
139};
140
141class MethodHelper : public MethodHelperT<Handle> {
142 using MethodHelperT<Handle>::MethodHelperT;
143 private:
144 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
145};
146
147class MutableMethodHelper : public MethodHelperT<MutableHandle> {
148 using MethodHelperT<MutableHandle>::MethodHelperT;
149 public:
150 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
151 DCHECK(new_m != nullptr);
152 SetMethod(new_m);
153 shorty_ = nullptr;
154 }
155
Ian Rogers22d5e732014-07-15 22:23:51 -0700156 private:
157 // Set the method_ field, for proxy methods looking up the interface method via the resolved
158 // methods table.
159 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
160 method_.Assign(method);
161 }
162
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700163 DISALLOW_COPY_AND_ASSIGN(MutableMethodHelper);
Ian Rogers22d5e732014-07-15 22:23:51 -0700164};
165
166} // namespace art
167
168#endif // ART_RUNTIME_METHOD_HELPER_H_