blob: dc305d57b73f8795c40e53934a9df7345b596260 [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 const char* GetShorty() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
44 const char* result = shorty_;
45 if (result == nullptr) {
46 result = method_->GetShorty(&shorty_len_);
47 shorty_ = result;
48 }
49 return result;
50 }
51
52 uint32_t GetShortyLength() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
53 if (shorty_ == nullptr) {
54 GetShorty();
55 }
56 return shorty_len_;
57 }
58
59 // Counts the number of references in the parameter list of the corresponding method.
60 // Note: Thus does _not_ include "this" for non-static methods.
61 uint32_t GetNumberOfReferenceArgsWithoutReceiver() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
62 const char* shorty = GetShorty();
63 uint32_t refs = 0;
64 for (uint32_t i = 1; i < shorty_len_ ; ++i) {
65 if (shorty[i] == 'L') {
66 refs++;
67 }
68 }
69
70 return refs;
71 }
72
Ian Rogers22d5e732014-07-15 22:23:51 -070073 size_t NumArgs() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
74 // "1 +" because the first in Args is the receiver.
75 // "- 1" because we don't count the return type.
76 return (method_->IsStatic() ? 0 : 1) + GetShortyLength() - 1;
77 }
78
79 // Get the primitive type associated with the given parameter.
80 Primitive::Type GetParamPrimitiveType(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
81 CHECK_LT(param, NumArgs());
82 if (GetMethod()->IsStatic()) {
83 param++; // 0th argument must skip return value at start of the shorty
84 } else if (param == 0) {
85 return Primitive::kPrimNot;
86 }
87 return Primitive::GetType(GetShorty()[param]);
88 }
89
90 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods.
91 bool IsParamALongOrDouble(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
92 Primitive::Type type = GetParamPrimitiveType(param);
93 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
94 }
95
96 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods.
97 bool IsParamAReference(size_t param) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
98 return GetParamPrimitiveType(param) == Primitive::kPrimNot;
99 }
100
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700101 template <template <class T> class HandleKind2>
102 ALWAYS_INLINE bool HasSameNameAndSignature(MethodHelperT<HandleKind2>* other)
Ian Rogers1ff3c982014-08-12 02:30:58 -0700103 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700104
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700105 template <template <class T> class HandleKind2>
Ian Rogersded66a02014-10-28 18:12:55 -0700106 bool HasSameSignatureWithDifferentClassLoaders(Thread* self, MethodHelperT<HandleKind2>* other)
Ian Rogerse5877a12014-07-16 12:06:35 -0700107 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers22d5e732014-07-15 22:23:51 -0700108
109 mirror::Class* GetClassFromTypeIdx(uint16_t type_idx, bool resolve = true)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
111
Ian Rogers22d5e732014-07-15 22:23:51 -0700112 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile)
113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114
115 // The name_and_signature_idx MUST point to a MethodId with the same name and signature in the
116 // other_dexfile, such as the method index used to resolve this method in the other_dexfile.
117 uint32_t FindDexMethodIndexInOtherDexFile(const DexFile& other_dexfile,
118 uint32_t name_and_signature_idx)
119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
120
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700121 protected:
122 HandleKind<mirror::ArtMethod> method_;
123
124 const char* shorty_;
125 uint32_t shorty_len_;
126
127 private:
128 template <template <class T2> class HandleKind2> friend class MethodHelperT;
129
130 DISALLOW_COPY_AND_ASSIGN(MethodHelperT);
131};
132
133class MethodHelper : public MethodHelperT<Handle> {
134 using MethodHelperT<Handle>::MethodHelperT;
135 private:
136 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
137};
138
139class MutableMethodHelper : public MethodHelperT<MutableHandle> {
140 using MethodHelperT<MutableHandle>::MethodHelperT;
141 public:
142 void ChangeMethod(mirror::ArtMethod* new_m) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
143 DCHECK(new_m != nullptr);
144 SetMethod(new_m);
145 shorty_ = nullptr;
146 }
147
Ian Rogers22d5e732014-07-15 22:23:51 -0700148 private:
149 // Set the method_ field, for proxy methods looking up the interface method via the resolved
150 // methods table.
151 void SetMethod(mirror::ArtMethod* method) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
152 method_.Assign(method);
153 }
154
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700155 DISALLOW_COPY_AND_ASSIGN(MutableMethodHelper);
Ian Rogers22d5e732014-07-15 22:23:51 -0700156};
157
158} // namespace art
159
160#endif // ART_RUNTIME_METHOD_HELPER_H_