blob: 14ba7d12546e8745fc59bc5cc6876f7aa8e89ae8 [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
Ian Rogers22d5e732014-07-15 22:23:51 -070075 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
76 // "1 +" because the first in Args is the receiver.
77 // "- 1" because we don't count the return type.
78 return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1;
79 }
80
81 // Get the primitive type associated with the given parameter.
82 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
83 CHECK_LT(param, NumArgs());
84 if (GetMethod()->IsStatic()) {
85 param++; // 0th argument must skip return value at start of the shorty
86 } else if (param == 0) {
87 return Primitive::kPrimNot;
88 }
89 return Primitive::GetType(GetShorty()[param]);
90 }
91
92 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
93 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
94 Primitive::Type type = GetParamPrimitiveType(param);
95 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
96 }
97
98 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
99 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
100 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
101 }
102
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700103 template <template <class T> class HandleKind2>
104 ALWAYS_INLINE bool HasSameNameAndSignature(MethodHelperT<HandleKind2>* other)
Ian Rogers1ff3c982014-08-12 02:30:58 -0700105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700106
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700107 template <template <class T> class HandleKind2>
Ian Rogersded66a02014-10-28 18:12:55 -0700108 bool HasSameSignatureWithDifferentClassLoaders(Thread* self, MethodHelperT<HandleKind2>* other)
Ian Rogerse5877a12014-07-16 12:06:35 -0700109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700110
111 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
112 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
113
Ian Rogers22d5e732014-07-15 22:23:51 -0700114 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
115 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
116
117 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
118 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
119 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
120 uint32_t name_and_signature_idx)
121 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
122
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700123 protected:
124 HandleKind<mirror::ArtMethod> method_;
125
126 const char* shorty_;
127 uint32_t shorty_len_;
128
129 private:
130 template <template <class T2> class HandleKind2> friend class MethodHelperT;
131
132 DISALLOW_COPY_AND_ASSIGN(MethodHelperT);
133};
134
135class MethodHelper : public MethodHelperT<Handle> {
136 using MethodHelperT<Handle>::MethodHelperT;
137 private:
138 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
139};
140
141class MutableMethodHelper : public MethodHelperT<MutableHandle> {
142 using MethodHelperT<MutableHandle>::MethodHelperT;
143 public:
144 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
145 DCHECK(new_m != nullptr);
146 SetMethod(new_m);
147 shorty_ = nullptr;
148 }
149
Ian Rogers22d5e732014-07-15 22:23:51 -0700150 private:
151 // Set the method_ field, for proxy methods looking up the interface method via the resolved
152 // methods table.
153 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
154 method_.Assign(method);
155 }
156
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700157 DISALLOW_COPY_AND_ASSIGN(MutableMethodHelper);
Ian Rogers22d5e732014-07-15 22:23:51 -0700158};
159
160} // namespace art
161
162#endif // ART_RUNTIME_METHOD_HELPER_H_