blob: ceef18621da6cd2074f1b41b123f9e7816cf9886 [file] [log] [blame]
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001/*
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_SRC_OBJECT_UTILS_H_
18#define ART_SRC_OBJECT_UTILS_H_
19
20#include "class_linker.h"
21#include "dex_cache.h"
22#include "dex_file.h"
Ian Rogersc2b44472011-12-14 21:17:17 -080023#include "intern_table.h"
Ian Rogers672f5202012-01-12 18:06:40 -080024#include "monitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080025#include "object.h"
26#include "runtime.h"
Elliott Hughes91250e02011-12-13 22:30:35 -080027#include "UniquePtr.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080028
29#include <string>
30
31namespace art {
32
Ian Rogers672f5202012-01-12 18:06:40 -080033class ObjectLock {
34 public:
35 explicit ObjectLock(Object* object) : self_(Thread::Current()), obj_(object) {
36 CHECK(object != NULL);
37 obj_->MonitorEnter(self_);
38 }
39
40 ~ObjectLock() {
41 obj_->MonitorExit(self_);
42 }
43
44 void Wait() {
45 return Monitor::Wait(self_, obj_, 0, 0, false);
46 }
47
48 void Notify() {
49 obj_->Notify();
50 }
51
52 void NotifyAll() {
53 obj_->NotifyAll();
54 }
55
56 private:
57 Thread* self_;
58 Object* obj_;
59 DISALLOW_COPY_AND_ASSIGN(ObjectLock);
60};
61
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080062class ClassHelper {
63 public:
Elliott Hughes91250e02011-12-13 22:30:35 -080064 ClassHelper(const Class* c = NULL, ClassLinker* l = NULL)
65 : class_def_(NULL),
66 class_linker_(l),
67 dex_cache_(NULL),
68 dex_file_(NULL),
69 interface_type_list_(NULL),
Brian Carlstrome77be402012-03-30 01:08:38 -070070 klass_(NULL) {
71 if (c != NULL) {
72 ChangeClass(c);
73 }
Elliott Hughes91250e02011-12-13 22:30:35 -080074 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080075
76 void ChangeClass(const Class* new_c) {
Brian Carlstrom01e076e2012-03-30 11:54:16 -070077 CHECK(new_c != NULL) << "klass_=" << klass_; // Log what we were changing from if any
78 CHECK(new_c->IsClass()) << "new_c=" << new_c;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080079 if (dex_cache_ != NULL) {
80 DexCache* new_c_dex_cache = new_c->GetDexCache();
81 if (new_c_dex_cache != dex_cache_) {
82 dex_cache_ = new_c_dex_cache;
83 dex_file_ = NULL;
84 }
85 }
86 klass_ = new_c;
87 interface_type_list_ = NULL;
88 class_def_ = NULL;
89 }
90
Elliott Hughes91250e02011-12-13 22:30:35 -080091 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
92 // If you need it longer, copy it into a std::string.
93 const char* GetDescriptor() {
Brian Carlstrom93235f72012-03-29 22:48:15 -070094 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080095 if (UNLIKELY(klass_->IsArrayClass())) {
96 return GetArrayDescriptor();
97 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080098 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -080099 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -0800100 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
101 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800102 } else {
103 const DexFile& dex_file = GetDexFile();
104 const DexFile::TypeId& type_id = dex_file.GetTypeId(klass_->GetDexTypeIndex());
105 return dex_file.GetTypeDescriptor(type_id);
106 }
107 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800108
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800109 const char* GetArrayDescriptor() {
110 std::string result("[");
111 const Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700112 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800113 ChangeClass(klass_->GetComponentType());
114 result += GetDescriptor();
115 ChangeClass(saved_klass);
116 descriptor_ = result;
117 return descriptor_.c_str();
118 }
119
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800120 const DexFile::ClassDef* GetClassDef() {
121 const DexFile::ClassDef* result = class_def_;
122 if (result == NULL) {
123 result = GetDexFile().FindClassDef(GetDescriptor());
124 class_def_ = result;
125 }
126 return result;
127 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800128
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800129 uint32_t NumInterfaces() {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700130 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800131 if (klass_->IsPrimitive()) {
132 return 0;
133 } else if (klass_->IsArrayClass()) {
134 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800135 } else if (klass_->IsProxyClass()) {
136 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800137 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800138 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
139 if (interfaces == NULL) {
140 return 0;
141 } else {
142 return interfaces->Size();
143 }
144 }
145 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800146
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800147 uint16_t GetInterfaceTypeIdx(uint32_t idx) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700148 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800149 DCHECK(!klass_->IsPrimitive());
150 DCHECK(!klass_->IsArrayClass());
151 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
152 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800153
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800154 Class* GetInterface(uint32_t idx) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700155 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800156 DCHECK(!klass_->IsPrimitive());
157 if (klass_->IsArrayClass()) {
158 if (idx == 0) {
159 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
160 } else {
161 DCHECK_EQ(1U, idx);
162 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
163 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800164 } else if (klass_->IsProxyClass()) {
165 return klass_->GetIfTable()->Get(idx)->GetInterface();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800166 } else {
167 uint16_t type_idx = GetInterfaceTypeIdx(idx);
168 Class* interface = GetDexCache()->GetResolvedType(type_idx);
169 if (interface == NULL) {
170 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
171 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
172 }
173 return interface;
174 }
175 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800176
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800177 const char* GetSourceFile() {
Elliott Hughes95572412011-12-13 18:14:20 -0800178 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800179 const DexFile& dex_file = GetDexFile();
180 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800181 CHECK(dex_class_def != NULL);
182 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800183 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800184
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 std::string GetLocation() {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700186 DexCache* dex_cache = GetDexCache();
187 if (dex_cache != NULL && !klass_->IsProxyClass()) {
188 return dex_cache->GetLocation()->ToModifiedUtf8();
189 } else {
190 // Arrays and proxies are generated and have no corresponding dex file location.
191 return "generated class";
192 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800193 }
194
195 const DexFile& GetDexFile() {
196 const DexFile* result = dex_file_;
197 if (result == NULL) {
198 const DexCache* dex_cache = GetDexCache();
199 result = &GetClassLinker()->FindDexFile(dex_cache);
200 dex_file_ = result;
201 }
202 return *result;
203 }
204
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 DexCache* GetDexCache() {
206 DexCache* result = dex_cache_;
207 if (result == NULL) {
208 DCHECK(klass_ != NULL);
209 result = klass_->GetDexCache();
210 dex_cache_ = result;
211 }
212 return result;
213 }
214
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800215 private:
216 const DexFile::TypeList* GetInterfaceTypeList() {
217 const DexFile::TypeList* result = interface_type_list_;
218 if (result == NULL) {
219 const DexFile::ClassDef* class_def = GetClassDef();
220 if (class_def != NULL) {
221 result = GetDexFile().GetInterfacesList(*class_def);
222 interface_type_list_ = result;
223 }
224 }
225 return result;
226 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800227
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800228 ClassLinker* GetClassLinker() {
229 ClassLinker* result = class_linker_;
230 if (result == NULL) {
231 result = Runtime::Current()->GetClassLinker();
232 class_linker_ = result;
233 }
234 return result;
235 }
236
237 const DexFile::ClassDef* class_def_;
238 ClassLinker* class_linker_;
239 DexCache* dex_cache_;
240 const DexFile* dex_file_;
241 const DexFile::TypeList* interface_type_list_;
242 const Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800243 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800244
245 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
246};
247
248class FieldHelper {
249 public:
250 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800251 explicit FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800252 FieldHelper(const Field* f, ClassLinker* l) : class_linker_(l), dex_cache_(NULL), dex_file_(NULL),
253 field_(f) {}
254
255 void ChangeField(const Field* new_f) {
256 DCHECK(new_f != NULL);
257 if (dex_cache_ != NULL) {
258 DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
259 if (new_f_dex_cache != dex_cache_) {
260 dex_cache_ = new_f_dex_cache;
261 dex_file_ = NULL;
262 }
263 }
264 field_ = new_f;
265 }
266 const char* GetName() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800267 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700268 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800269 const DexFile& dex_file = GetDexFile();
270 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
271 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800272 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700273 DCHECK_LT(field_index, 2U);
274 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800275 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 }
277 String* GetNameAsString() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800278 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700279 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800280 const DexFile& dex_file = GetDexFile();
281 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
282 return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
283 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700284 return Runtime::Current()->GetInternTable()->InternStrong(GetName());
Ian Rogersc2b44472011-12-14 21:17:17 -0800285 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800286 }
287 Class* GetType() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800288 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700289 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800290 const DexFile& dex_file = GetDexFile();
291 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
292 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
293 if (type == NULL) {
294 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
295 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
296 }
297 return type;
298 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700299 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800300 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800301 }
302 const char* GetTypeDescriptor() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800303 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700304 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800305 const DexFile& dex_file = GetDexFile();
306 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
307 return dex_file.GetFieldTypeDescriptor(field_id);
308 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800309 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700310 DCHECK_LT(field_index, 2U);
311 // 0 == Class[] interfaces; 1 == Class[][] throws;
312 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800313 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800314 }
315 Primitive::Type GetTypeAsPrimitiveType() {
316 return Primitive::GetType(GetTypeDescriptor()[0]);
317 }
318 bool IsPrimitiveType() {
319 Primitive::Type type = GetTypeAsPrimitiveType();
320 return type != Primitive::kPrimNot;
321 }
322 size_t FieldSize() {
323 Primitive::Type type = GetTypeAsPrimitiveType();
324 return Primitive::FieldSize(type);
325 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800326
327 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
328 // If you need it longer, copy it into a std::string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800329 const char* GetDeclaringClassDescriptor() {
330 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800331 if (type_idx != DexFile::kDexNoIndex16) {
332 const DexFile& dex_file = GetDexFile();
333 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
334 } else {
335 // Most likely a proxy class
336 ClassHelper kh(field_->GetDeclaringClass());
337 declaring_class_descriptor_ = kh.GetDescriptor();
338 return declaring_class_descriptor_.c_str();
339 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800340 }
341
342 private:
343 DexCache* GetDexCache() {
344 DexCache* result = dex_cache_;
345 if (result == NULL) {
346 result = field_->GetDeclaringClass()->GetDexCache();
347 dex_cache_ = result;
348 }
349 return result;
350 }
351 ClassLinker* GetClassLinker() {
352 ClassLinker* result = class_linker_;
353 if (result == NULL) {
354 result = Runtime::Current()->GetClassLinker();
355 class_linker_ = result;
356 }
357 return result;
358 }
359 const DexFile& GetDexFile() {
360 const DexFile* result = dex_file_;
361 if (result == NULL) {
362 const DexCache* dex_cache = GetDexCache();
363 result = &GetClassLinker()->FindDexFile(dex_cache);
364 dex_file_ = result;
365 }
366 return *result;
367 }
368
369 ClassLinker* class_linker_;
370 DexCache* dex_cache_;
371 const DexFile* dex_file_;
372 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800373 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800374
375 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
376};
377
378class MethodHelper {
379 public:
380 MethodHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL),
381 shorty_(NULL), shorty_len_(0) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800382 explicit MethodHelper(const Method* m) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800383 method_(NULL), shorty_(NULL), shorty_len_(0) {
384 SetMethod(m);
385 }
386 MethodHelper(const Method* m, ClassLinker* l) : class_linker_(l), dex_cache_(NULL),
387 dex_file_(NULL), method_(NULL), shorty_(NULL), shorty_len_(0) {
388 SetMethod(m);
389 }
390
391 void ChangeMethod(Method* new_m) {
392 DCHECK(new_m != NULL);
393 if (dex_cache_ != NULL) {
394 Class* klass = new_m->GetDeclaringClass();
395 if (klass->IsProxyClass()) {
396 dex_cache_ = NULL;
397 dex_file_ = NULL;
398 } else {
399 DexCache* new_m_dex_cache = klass->GetDexCache();
400 if (new_m_dex_cache != dex_cache_) {
401 dex_cache_ = new_m_dex_cache;
402 dex_file_ = NULL;
403 }
404 }
405 }
406 SetMethod(new_m);
407 shorty_ = NULL;
408 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700409
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800410 const char* GetName() {
411 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800412 uint32_t dex_method_idx = method_->GetDexMethodIndex();
413 if (dex_method_idx != DexFile::kDexNoIndex16) {
414 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
415 } else {
416 Runtime* runtime = Runtime::Current();
417 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700418 return "<runtime internal resolution method>";
Ian Rogers19846512012-02-24 11:42:47 -0800419 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700420 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800421 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700422 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800423 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700424 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800425 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700426 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800427 }
428 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800429 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700430
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800431 String* GetNameAsString() {
432 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800433 uint32_t dex_method_idx = method_->GetDexMethodIndex();
434 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800435 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
436 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800438 const char* GetShorty() {
439 const char* result = shorty_;
440 if (result == NULL) {
441 const DexFile& dex_file = GetDexFile();
442 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
443 &shorty_len_);
444 shorty_ = result;
445 }
446 return result;
447 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448
Elliott Hughes45651fd2012-02-21 15:48:20 -0800449 uint32_t GetShortyLength() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450 if (shorty_ == NULL) {
451 GetShorty();
452 }
453 return shorty_len_;
454 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800456 const std::string GetSignature() {
457 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800458 uint32_t dex_method_idx = method_->GetDexMethodIndex();
459 if (dex_method_idx != DexFile::kDexNoIndex16) {
460 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
461 } else {
462 return "<no signature>";
463 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800464 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700465
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800466 const DexFile::ProtoId& GetPrototype() {
467 const DexFile& dex_file = GetDexFile();
468 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
469 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700470
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800471 const DexFile::TypeList* GetParameterTypeList() {
472 const DexFile::ProtoId& proto = GetPrototype();
473 return GetDexFile().GetProtoParameters(proto);
474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800476 ObjectArray<Class>* GetParameterTypes() {
477 const DexFile::TypeList* params = GetParameterTypeList();
478 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
479 uint32_t num_params = params == NULL ? 0 : params->Size();
480 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
481 for (uint32_t i = 0; i < num_params; i++) {
482 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
jeffhao441d9122012-03-21 17:29:10 -0700483 if (param_type == NULL) {
484 DCHECK(Thread::Current()->IsExceptionPending());
485 return NULL;
486 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800487 result->Set(i, param_type);
488 }
489 return result;
490 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800492 Class* GetReturnType() {
493 const DexFile& dex_file = GetDexFile();
494 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
495 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
496 uint16_t return_type_idx = proto_id.return_type_idx_;
497 return GetClassFromTypeIdx(return_type_idx);
498 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700499
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800500 const char* GetReturnTypeDescriptor() {
501 const DexFile& dex_file = GetDexFile();
502 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
503 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
504 uint16_t return_type_idx = proto_id.return_type_idx_;
505 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
506 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700507
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800508 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
509 const DexFile& dex_file = GetDexFile();
510 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
511 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700512
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800513 const char* GetDeclaringClassDescriptor() {
514 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800515 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800516 uint16_t type_idx = klass->GetDexTypeIndex();
517 const DexFile& dex_file = GetDexFile();
518 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
519 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700520
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800521 const char* GetDeclaringClassSourceFile() {
522 const char* descriptor = GetDeclaringClassDescriptor();
523 const DexFile& dex_file = GetDexFile();
524 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800525 CHECK(dex_class_def != NULL);
526 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800527 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700528
529 uint32_t GetClassDefIndex() {
530 const char* descriptor = GetDeclaringClassDescriptor();
531 const DexFile& dex_file = GetDexFile();
532 uint32_t index;
533 CHECK(dex_file.FindClassDefIndex(descriptor, index));
534 return index;
535 }
536
537 ClassLoader* GetClassLoader() {
538 return method_->GetDeclaringClass()->GetClassLoader();
539 }
540
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800541 bool IsStatic() {
542 return method_->IsStatic();
543 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700544
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545 bool IsClassInitializer() {
546 return IsStatic() && StringPiece(GetName()) == "<clinit>";
547 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700548
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800549 size_t NumArgs() {
550 // "1 +" because the first in Args is the receiver.
551 // "- 1" because we don't count the return type.
552 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
553 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700554
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800555 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
556 bool IsParamALongOrDouble(size_t param) {
557 CHECK_LT(param, NumArgs());
558 if (IsStatic()) {
559 param++; // 0th argument must skip return value at start of the shorty
560 } else if (param == 0) {
561 return false; // this argument
562 }
563 char ch = GetShorty()[param];
564 return (ch == 'J' || ch == 'D');
565 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700566
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800567 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
568 bool IsParamAReference(size_t param) {
569 CHECK_LT(param, NumArgs());
570 if (IsStatic()) {
571 param++; // 0th argument must skip return value at start of the shorty
572 } else if (param == 0) {
573 return true; // this argument
574 }
575 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
576 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700577
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800578 bool HasSameNameAndSignature(MethodHelper* other) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800579 if (GetDexCache() == other->GetDexCache()) {
580 const DexFile& dex_file = GetDexFile();
581 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
582 const DexFile::MethodId& other_mid =
583 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800584 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800585 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800586 StringPiece name(GetName());
587 StringPiece other_name(other->GetName());
588 return name == other_name && GetSignature() == other->GetSignature();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800589 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700590
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800591 const DexFile::CodeItem* GetCodeItem() {
592 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
593 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700594
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800595 bool IsResolvedTypeIdx(uint16_t type_idx) const {
596 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
597 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700598
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800599 Class* GetClassFromTypeIdx(uint16_t type_idx) {
600 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
601 if (type == NULL) {
602 type = GetClassLinker()->ResolveType(type_idx, method_);
603 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
604 }
605 return type;
606 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700607
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800608 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
609 const DexFile& dex_file = GetDexFile();
610 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
611 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700612
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800613 Class* GetDexCacheResolvedType(uint16_t type_idx) {
614 return GetDexCache()->GetResolvedType(type_idx);
615 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700616
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800617 const DexFile& GetDexFile() {
618 const DexFile* result = dex_file_;
619 if (result == NULL) {
620 const DexCache* dex_cache = GetDexCache();
621 result = &GetClassLinker()->FindDexFile(dex_cache);
622 dex_file_ = result;
623 }
624 return *result;
625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700626
627 DexCache* GetDexCache() {
628 DexCache* result = dex_cache_;
629 if (result == NULL) {
630 Class* klass = method_->GetDeclaringClass();
631 result = klass->GetDexCache();
632 dex_cache_ = result;
633 }
634 return result;
635 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800636 private:
637 // Set the method_ field, for proxy methods looking up the interface method via the resolved
638 // methods table.
639 void SetMethod(const Method* method) {
640 if (method != NULL) {
641 Class* klass = method->GetDeclaringClass();
642 if (klass->IsProxyClass()) {
Ian Rogers19846512012-02-24 11:42:47 -0800643 Method* interface_method =
644 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
645 CHECK(interface_method != NULL);
646 CHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
647 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800648 }
649 }
650 method_ = method;
651 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700652
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800653 ClassLinker* GetClassLinker() {
654 ClassLinker* result = class_linker_;
655 if (result == NULL) {
656 result = Runtime::Current()->GetClassLinker();
657 class_linker_ = result;
658 }
659 return result;
660 }
661
662 ClassLinker* class_linker_;
663 DexCache* dex_cache_;
664 const DexFile* dex_file_;
665 const Method* method_;
666 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800667 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800668
669 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
670};
671
672} // namespace art
673
674#endif // ART_SRC_OBJECT_UTILS_H_