blob: cbad7b08e8517b1d93b9aa2d37f1bdbe9e06a351 [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),
70 klass_(c) {
71 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080072
73 void ChangeClass(const Class* new_c) {
74 DCHECK(new_c != NULL);
75 if (dex_cache_ != NULL) {
76 DexCache* new_c_dex_cache = new_c->GetDexCache();
77 if (new_c_dex_cache != dex_cache_) {
78 dex_cache_ = new_c_dex_cache;
79 dex_file_ = NULL;
80 }
81 }
82 klass_ = new_c;
83 interface_type_list_ = NULL;
84 class_def_ = NULL;
85 }
86
Elliott Hughes91250e02011-12-13 22:30:35 -080087 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
88 // If you need it longer, copy it into a std::string.
89 const char* GetDescriptor() {
Ian Rogersa68a1cb2012-01-10 10:34:36 -080090 if (UNLIKELY(klass_->IsArrayClass())) {
91 return GetArrayDescriptor();
92 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080093 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -080094 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080095 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
96 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080097 } else {
98 const DexFile& dex_file = GetDexFile();
99 const DexFile::TypeId& type_id = dex_file.GetTypeId(klass_->GetDexTypeIndex());
100 return dex_file.GetTypeDescriptor(type_id);
101 }
102 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800103
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800104 const char* GetArrayDescriptor() {
105 std::string result("[");
106 const Class* saved_klass = klass_;
107 ChangeClass(klass_->GetComponentType());
108 result += GetDescriptor();
109 ChangeClass(saved_klass);
110 descriptor_ = result;
111 return descriptor_.c_str();
112 }
113
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800114 const DexFile::ClassDef* GetClassDef() {
115 const DexFile::ClassDef* result = class_def_;
116 if (result == NULL) {
117 result = GetDexFile().FindClassDef(GetDescriptor());
118 class_def_ = result;
119 }
120 return result;
121 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800122
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800123 uint32_t NumInterfaces() {
124 if (klass_->IsPrimitive()) {
125 return 0;
126 } else if (klass_->IsArrayClass()) {
127 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800128 } else if (klass_->IsProxyClass()) {
129 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800130 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800131 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
132 if (interfaces == NULL) {
133 return 0;
134 } else {
135 return interfaces->Size();
136 }
137 }
138 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800139
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 uint16_t GetInterfaceTypeIdx(uint32_t idx) {
141 DCHECK(!klass_->IsPrimitive());
142 DCHECK(!klass_->IsArrayClass());
143 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
144 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800145
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800146 Class* GetInterface(uint32_t idx) {
147 DCHECK(!klass_->IsPrimitive());
148 if (klass_->IsArrayClass()) {
149 if (idx == 0) {
150 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
151 } else {
152 DCHECK_EQ(1U, idx);
153 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
154 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800155 } else if (klass_->IsProxyClass()) {
156 return klass_->GetIfTable()->Get(idx)->GetInterface();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800157 } else {
158 uint16_t type_idx = GetInterfaceTypeIdx(idx);
159 Class* interface = GetDexCache()->GetResolvedType(type_idx);
160 if (interface == NULL) {
161 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
162 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
163 }
164 return interface;
165 }
166 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800167
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800168 const char* GetSourceFile() {
Elliott Hughes95572412011-12-13 18:14:20 -0800169 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800170 const DexFile& dex_file = GetDexFile();
171 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800172 CHECK(dex_class_def != NULL);
173 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800174 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800175
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800176 std::string GetLocation() {
177 return GetDexCache()->GetLocation()->ToModifiedUtf8();
178 }
179
180 const DexFile& GetDexFile() {
181 const DexFile* result = dex_file_;
182 if (result == NULL) {
183 const DexCache* dex_cache = GetDexCache();
184 result = &GetClassLinker()->FindDexFile(dex_cache);
185 dex_file_ = result;
186 }
187 return *result;
188 }
189
190 private:
191 const DexFile::TypeList* GetInterfaceTypeList() {
192 const DexFile::TypeList* result = interface_type_list_;
193 if (result == NULL) {
194 const DexFile::ClassDef* class_def = GetClassDef();
195 if (class_def != NULL) {
196 result = GetDexFile().GetInterfacesList(*class_def);
197 interface_type_list_ = result;
198 }
199 }
200 return result;
201 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800202
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800203 DexCache* GetDexCache() {
204 DexCache* result = dex_cache_;
205 if (result == NULL) {
206 result = klass_->GetDexCache();
207 dex_cache_ = result;
208 }
209 return result;
210 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800211
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212 ClassLinker* GetClassLinker() {
213 ClassLinker* result = class_linker_;
214 if (result == NULL) {
215 result = Runtime::Current()->GetClassLinker();
216 class_linker_ = result;
217 }
218 return result;
219 }
220
221 const DexFile::ClassDef* class_def_;
222 ClassLinker* class_linker_;
223 DexCache* dex_cache_;
224 const DexFile* dex_file_;
225 const DexFile::TypeList* interface_type_list_;
226 const Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800227 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800228
229 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
230};
231
232class FieldHelper {
233 public:
234 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800235 explicit FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800236 FieldHelper(const Field* f, ClassLinker* l) : class_linker_(l), dex_cache_(NULL), dex_file_(NULL),
237 field_(f) {}
238
239 void ChangeField(const Field* new_f) {
240 DCHECK(new_f != NULL);
241 if (dex_cache_ != NULL) {
242 DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
243 if (new_f_dex_cache != dex_cache_) {
244 dex_cache_ = new_f_dex_cache;
245 dex_file_ = NULL;
246 }
247 }
248 field_ = new_f;
249 }
250 const char* GetName() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800251 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700252 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800253 const DexFile& dex_file = GetDexFile();
254 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
255 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800256 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700257 DCHECK_LT(field_index, 2U);
258 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800259 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 }
261 String* GetNameAsString() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800262 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700263 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800264 const DexFile& dex_file = GetDexFile();
265 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
266 return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
267 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700268 return Runtime::Current()->GetInternTable()->InternStrong(GetName());
Ian Rogersc2b44472011-12-14 21:17:17 -0800269 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800270 }
271 Class* GetType() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800272 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700273 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800274 const DexFile& dex_file = GetDexFile();
275 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
276 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
277 if (type == NULL) {
278 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
279 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
280 }
281 return type;
282 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700283 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800284 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800285 }
286 const char* GetTypeDescriptor() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800287 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700288 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800289 const DexFile& dex_file = GetDexFile();
290 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
291 return dex_file.GetFieldTypeDescriptor(field_id);
292 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800293 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700294 DCHECK_LT(field_index, 2U);
295 // 0 == Class[] interfaces; 1 == Class[][] throws;
296 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800297 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800298 }
299 Primitive::Type GetTypeAsPrimitiveType() {
300 return Primitive::GetType(GetTypeDescriptor()[0]);
301 }
302 bool IsPrimitiveType() {
303 Primitive::Type type = GetTypeAsPrimitiveType();
304 return type != Primitive::kPrimNot;
305 }
306 size_t FieldSize() {
307 Primitive::Type type = GetTypeAsPrimitiveType();
308 return Primitive::FieldSize(type);
309 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800310
311 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
312 // If you need it longer, copy it into a std::string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800313 const char* GetDeclaringClassDescriptor() {
314 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800315 if (type_idx != DexFile::kDexNoIndex16) {
316 const DexFile& dex_file = GetDexFile();
317 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
318 } else {
319 // Most likely a proxy class
320 ClassHelper kh(field_->GetDeclaringClass());
321 declaring_class_descriptor_ = kh.GetDescriptor();
322 return declaring_class_descriptor_.c_str();
323 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800324 }
325
326 private:
327 DexCache* GetDexCache() {
328 DexCache* result = dex_cache_;
329 if (result == NULL) {
330 result = field_->GetDeclaringClass()->GetDexCache();
331 dex_cache_ = result;
332 }
333 return result;
334 }
335 ClassLinker* GetClassLinker() {
336 ClassLinker* result = class_linker_;
337 if (result == NULL) {
338 result = Runtime::Current()->GetClassLinker();
339 class_linker_ = result;
340 }
341 return result;
342 }
343 const DexFile& GetDexFile() {
344 const DexFile* result = dex_file_;
345 if (result == NULL) {
346 const DexCache* dex_cache = GetDexCache();
347 result = &GetClassLinker()->FindDexFile(dex_cache);
348 dex_file_ = result;
349 }
350 return *result;
351 }
352
353 ClassLinker* class_linker_;
354 DexCache* dex_cache_;
355 const DexFile* dex_file_;
356 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800357 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800358
359 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
360};
361
362class MethodHelper {
363 public:
364 MethodHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL),
365 shorty_(NULL), shorty_len_(0) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800366 explicit MethodHelper(const Method* m) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800367 method_(NULL), shorty_(NULL), shorty_len_(0) {
368 SetMethod(m);
369 }
370 MethodHelper(const Method* m, ClassLinker* l) : class_linker_(l), dex_cache_(NULL),
371 dex_file_(NULL), method_(NULL), shorty_(NULL), shorty_len_(0) {
372 SetMethod(m);
373 }
374
375 void ChangeMethod(Method* new_m) {
376 DCHECK(new_m != NULL);
377 if (dex_cache_ != NULL) {
378 Class* klass = new_m->GetDeclaringClass();
379 if (klass->IsProxyClass()) {
380 dex_cache_ = NULL;
381 dex_file_ = NULL;
382 } else {
383 DexCache* new_m_dex_cache = klass->GetDexCache();
384 if (new_m_dex_cache != dex_cache_) {
385 dex_cache_ = new_m_dex_cache;
386 dex_file_ = NULL;
387 }
388 }
389 }
390 SetMethod(new_m);
391 shorty_ = NULL;
392 }
393 const char* GetName() {
394 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800395 uint32_t dex_method_idx = method_->GetDexMethodIndex();
396 if (dex_method_idx != DexFile::kDexNoIndex16) {
397 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
398 } else {
399 Runtime* runtime = Runtime::Current();
400 if (method_ == runtime->GetResolutionMethod()) {
401 return "<VM internal resolution method>";
402 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
403 return "<VM internal callee-save all registers method>";
404 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
405 return "<VM internal callee-save reference registers method>";
406 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
407 return "<VM internal callee-save reference and argument registers method>";
408 } else {
409 return "<unknown VM internal method>";
410 }
411 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800412 }
413 String* GetNameAsString() {
414 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800415 uint32_t dex_method_idx = method_->GetDexMethodIndex();
416 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800417 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
418 }
419 const char* GetShorty() {
420 const char* result = shorty_;
421 if (result == NULL) {
422 const DexFile& dex_file = GetDexFile();
423 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
424 &shorty_len_);
425 shorty_ = result;
426 }
427 return result;
428 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800429 uint32_t GetShortyLength() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800430 if (shorty_ == NULL) {
431 GetShorty();
432 }
433 return shorty_len_;
434 }
435 const std::string GetSignature() {
436 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800437 uint32_t dex_method_idx = method_->GetDexMethodIndex();
438 if (dex_method_idx != DexFile::kDexNoIndex16) {
439 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
440 } else {
441 return "<no signature>";
442 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800443 }
444 const DexFile::ProtoId& GetPrototype() {
445 const DexFile& dex_file = GetDexFile();
446 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
447 }
448 const DexFile::TypeList* GetParameterTypeList() {
449 const DexFile::ProtoId& proto = GetPrototype();
450 return GetDexFile().GetProtoParameters(proto);
451 }
452 ObjectArray<Class>* GetParameterTypes() {
453 const DexFile::TypeList* params = GetParameterTypeList();
454 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
455 uint32_t num_params = params == NULL ? 0 : params->Size();
456 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
457 for (uint32_t i = 0; i < num_params; i++) {
458 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
459 result->Set(i, param_type);
460 }
461 return result;
462 }
463 Class* GetReturnType() {
464 const DexFile& dex_file = GetDexFile();
465 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
466 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
467 uint16_t return_type_idx = proto_id.return_type_idx_;
468 return GetClassFromTypeIdx(return_type_idx);
469 }
470 const char* GetReturnTypeDescriptor() {
471 const DexFile& dex_file = GetDexFile();
472 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
473 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
474 uint16_t return_type_idx = proto_id.return_type_idx_;
475 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
476 }
477 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
478 const DexFile& dex_file = GetDexFile();
479 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
480 }
481 const char* GetDeclaringClassDescriptor() {
482 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800483 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800484 uint16_t type_idx = klass->GetDexTypeIndex();
485 const DexFile& dex_file = GetDexFile();
486 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
487 }
488 const char* GetDeclaringClassSourceFile() {
489 const char* descriptor = GetDeclaringClassDescriptor();
490 const DexFile& dex_file = GetDexFile();
491 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800492 CHECK(dex_class_def != NULL);
493 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 }
495 bool IsStatic() {
496 return method_->IsStatic();
497 }
498 bool IsClassInitializer() {
499 return IsStatic() && StringPiece(GetName()) == "<clinit>";
500 }
501 size_t NumArgs() {
502 // "1 +" because the first in Args is the receiver.
503 // "- 1" because we don't count the return type.
504 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
505 }
506 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
507 bool IsParamALongOrDouble(size_t param) {
508 CHECK_LT(param, NumArgs());
509 if (IsStatic()) {
510 param++; // 0th argument must skip return value at start of the shorty
511 } else if (param == 0) {
512 return false; // this argument
513 }
514 char ch = GetShorty()[param];
515 return (ch == 'J' || ch == 'D');
516 }
517 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
518 bool IsParamAReference(size_t param) {
519 CHECK_LT(param, NumArgs());
520 if (IsStatic()) {
521 param++; // 0th argument must skip return value at start of the shorty
522 } else if (param == 0) {
523 return true; // this argument
524 }
525 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
526 }
527 bool HasSameNameAndSignature(MethodHelper* other) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800528 if (GetDexCache() == other->GetDexCache()) {
529 const DexFile& dex_file = GetDexFile();
530 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
531 const DexFile::MethodId& other_mid =
532 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800533 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800534 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800535 StringPiece name(GetName());
536 StringPiece other_name(other->GetName());
537 return name == other_name && GetSignature() == other->GetSignature();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800538 }
539 const DexFile::CodeItem* GetCodeItem() {
540 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
541 }
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800542 bool IsResolvedTypeIdx(uint16_t type_idx) const {
543 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
544 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545 Class* GetClassFromTypeIdx(uint16_t type_idx) {
546 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
547 if (type == NULL) {
548 type = GetClassLinker()->ResolveType(type_idx, method_);
549 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
550 }
551 return type;
552 }
553 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
554 const DexFile& dex_file = GetDexFile();
555 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
556 }
557 Class* GetDexCacheResolvedType(uint16_t type_idx) {
558 return GetDexCache()->GetResolvedType(type_idx);
559 }
560 const DexFile& GetDexFile() {
561 const DexFile* result = dex_file_;
562 if (result == NULL) {
563 const DexCache* dex_cache = GetDexCache();
564 result = &GetClassLinker()->FindDexFile(dex_cache);
565 dex_file_ = result;
566 }
567 return *result;
568 }
569 private:
570 // Set the method_ field, for proxy methods looking up the interface method via the resolved
571 // methods table.
572 void SetMethod(const Method* method) {
573 if (method != NULL) {
574 Class* klass = method->GetDeclaringClass();
575 if (klass->IsProxyClass()) {
Ian Rogers19846512012-02-24 11:42:47 -0800576 Method* interface_method =
577 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
578 CHECK(interface_method != NULL);
579 CHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
580 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800581 }
582 }
583 method_ = method;
584 }
585 DexCache* GetDexCache() {
586 DexCache* result = dex_cache_;
587 if (result == NULL) {
588 Class* klass = method_->GetDeclaringClass();
589 result = klass->GetDexCache();
590 dex_cache_ = result;
591 }
592 return result;
593 }
594 ClassLinker* GetClassLinker() {
595 ClassLinker* result = class_linker_;
596 if (result == NULL) {
597 result = Runtime::Current()->GetClassLinker();
598 class_linker_ = result;
599 }
600 return result;
601 }
602
603 ClassLinker* class_linker_;
604 DexCache* dex_cache_;
605 const DexFile* dex_file_;
606 const Method* method_;
607 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800608 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800609
610 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
611};
612
613} // namespace art
614
615#endif // ART_SRC_OBJECT_UTILS_H_