blob: 0412858084b2e951f149da5ed341c3d275354636 [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);
172 if (dex_class_def == NULL) {
173 return NULL;
174 } else {
175 return dex_file.GetSourceFile(*dex_class_def);
176 }
177 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800178
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800179 std::string GetLocation() {
180 return GetDexCache()->GetLocation()->ToModifiedUtf8();
181 }
182
183 const DexFile& GetDexFile() {
184 const DexFile* result = dex_file_;
185 if (result == NULL) {
186 const DexCache* dex_cache = GetDexCache();
187 result = &GetClassLinker()->FindDexFile(dex_cache);
188 dex_file_ = result;
189 }
190 return *result;
191 }
192
193 private:
194 const DexFile::TypeList* GetInterfaceTypeList() {
195 const DexFile::TypeList* result = interface_type_list_;
196 if (result == NULL) {
197 const DexFile::ClassDef* class_def = GetClassDef();
198 if (class_def != NULL) {
199 result = GetDexFile().GetInterfacesList(*class_def);
200 interface_type_list_ = result;
201 }
202 }
203 return result;
204 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800205
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800206 DexCache* GetDexCache() {
207 DexCache* result = dex_cache_;
208 if (result == NULL) {
209 result = klass_->GetDexCache();
210 dex_cache_ = result;
211 }
212 return result;
213 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800214
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800215 ClassLinker* GetClassLinker() {
216 ClassLinker* result = class_linker_;
217 if (result == NULL) {
218 result = Runtime::Current()->GetClassLinker();
219 class_linker_ = result;
220 }
221 return result;
222 }
223
224 const DexFile::ClassDef* class_def_;
225 ClassLinker* class_linker_;
226 DexCache* dex_cache_;
227 const DexFile* dex_file_;
228 const DexFile::TypeList* interface_type_list_;
229 const Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800230 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800231
232 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
233};
234
235class FieldHelper {
236 public:
237 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
238 FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
239 FieldHelper(const Field* f, ClassLinker* l) : class_linker_(l), dex_cache_(NULL), dex_file_(NULL),
240 field_(f) {}
241
242 void ChangeField(const Field* new_f) {
243 DCHECK(new_f != NULL);
244 if (dex_cache_ != NULL) {
245 DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
246 if (new_f_dex_cache != dex_cache_) {
247 dex_cache_ = new_f_dex_cache;
248 dex_file_ = NULL;
249 }
250 }
251 field_ = new_f;
252 }
253 const char* GetName() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800254 uint32_t field_index = field_->GetDexFieldIndex();
255 if (field_index != DexFile::kDexNoIndex) {
256 const DexFile& dex_file = GetDexFile();
257 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
258 } else {
259 // Proxy classes have a single static field called "throws"
260 CHECK(field_->GetDeclaringClass()->IsProxyClass());
261 DCHECK(field_->IsStatic());
262 return "throws";
263 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800264 }
265 String* GetNameAsString() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800266 uint32_t field_index = field_->GetDexFieldIndex();
267 if (field_index != DexFile::kDexNoIndex) {
268 const DexFile& dex_file = GetDexFile();
269 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
270 return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
271 } else {
272 // Proxy classes have a single static field called "throws"
273 CHECK(field_->GetDeclaringClass()->IsProxyClass());
274 DCHECK(field_->IsStatic());
275 return Runtime::Current()->GetInternTable()->InternStrong("throws");
276 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800277 }
278 Class* GetType() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800279 uint32_t field_index = field_->GetDexFieldIndex();
280 if (field_index != DexFile::kDexNoIndex) {
281 const DexFile& dex_file = GetDexFile();
282 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
283 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
284 if (type == NULL) {
285 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
286 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
287 }
288 return type;
289 } else {
290 // Proxy classes have a single static field called "throws" whose type is Class[][]
291 CHECK(field_->GetDeclaringClass()->IsProxyClass());
292 DCHECK(field_->IsStatic());
293 return GetClassLinker()->FindSystemClass("[[Ljava/lang/Class;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800294 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800295 }
296 const char* GetTypeDescriptor() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800297 uint32_t field_index = field_->GetDexFieldIndex();
298 if (field_index != DexFile::kDexNoIndex) {
299 const DexFile& dex_file = GetDexFile();
300 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
301 return dex_file.GetFieldTypeDescriptor(field_id);
302 } else {
303 // Proxy classes have a single static field called "throws" whose type is Class[][]
304 CHECK(field_->GetDeclaringClass()->IsProxyClass());
305 DCHECK(field_->IsStatic());
306 return "[[Ljava/lang/Class;";
307 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800308 }
309 Primitive::Type GetTypeAsPrimitiveType() {
310 return Primitive::GetType(GetTypeDescriptor()[0]);
311 }
312 bool IsPrimitiveType() {
313 Primitive::Type type = GetTypeAsPrimitiveType();
314 return type != Primitive::kPrimNot;
315 }
316 size_t FieldSize() {
317 Primitive::Type type = GetTypeAsPrimitiveType();
318 return Primitive::FieldSize(type);
319 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800320
321 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
322 // If you need it longer, copy it into a std::string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800323 const char* GetDeclaringClassDescriptor() {
324 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800325 if (type_idx != DexFile::kDexNoIndex16) {
326 const DexFile& dex_file = GetDexFile();
327 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
328 } else {
329 // Most likely a proxy class
330 ClassHelper kh(field_->GetDeclaringClass());
331 declaring_class_descriptor_ = kh.GetDescriptor();
332 return declaring_class_descriptor_.c_str();
333 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334 }
335
336 private:
337 DexCache* GetDexCache() {
338 DexCache* result = dex_cache_;
339 if (result == NULL) {
340 result = field_->GetDeclaringClass()->GetDexCache();
341 dex_cache_ = result;
342 }
343 return result;
344 }
345 ClassLinker* GetClassLinker() {
346 ClassLinker* result = class_linker_;
347 if (result == NULL) {
348 result = Runtime::Current()->GetClassLinker();
349 class_linker_ = result;
350 }
351 return result;
352 }
353 const DexFile& GetDexFile() {
354 const DexFile* result = dex_file_;
355 if (result == NULL) {
356 const DexCache* dex_cache = GetDexCache();
357 result = &GetClassLinker()->FindDexFile(dex_cache);
358 dex_file_ = result;
359 }
360 return *result;
361 }
362
363 ClassLinker* class_linker_;
364 DexCache* dex_cache_;
365 const DexFile* dex_file_;
366 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800367 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800368
369 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
370};
371
372class MethodHelper {
373 public:
374 MethodHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL),
375 shorty_(NULL), shorty_len_(0) {}
376 MethodHelper(const Method* m) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL),
377 method_(NULL), shorty_(NULL), shorty_len_(0) {
378 SetMethod(m);
379 }
380 MethodHelper(const Method* m, ClassLinker* l) : class_linker_(l), dex_cache_(NULL),
381 dex_file_(NULL), method_(NULL), shorty_(NULL), shorty_len_(0) {
382 SetMethod(m);
383 }
384
385 void ChangeMethod(Method* new_m) {
386 DCHECK(new_m != NULL);
387 if (dex_cache_ != NULL) {
388 Class* klass = new_m->GetDeclaringClass();
389 if (klass->IsProxyClass()) {
390 dex_cache_ = NULL;
391 dex_file_ = NULL;
392 } else {
393 DexCache* new_m_dex_cache = klass->GetDexCache();
394 if (new_m_dex_cache != dex_cache_) {
395 dex_cache_ = new_m_dex_cache;
396 dex_file_ = NULL;
397 }
398 }
399 }
400 SetMethod(new_m);
401 shorty_ = NULL;
402 }
403 const char* GetName() {
404 const DexFile& dex_file = GetDexFile();
405 return dex_file.GetMethodName(dex_file.GetMethodId(method_->GetDexMethodIndex()));
406 }
407 String* GetNameAsString() {
408 const DexFile& dex_file = GetDexFile();
409 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
410 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
411 }
412 const char* GetShorty() {
413 const char* result = shorty_;
414 if (result == NULL) {
415 const DexFile& dex_file = GetDexFile();
416 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
417 &shorty_len_);
418 shorty_ = result;
419 }
420 return result;
421 }
422 int32_t GetShortyLength() {
423 if (shorty_ == NULL) {
424 GetShorty();
425 }
426 return shorty_len_;
427 }
428 const std::string GetSignature() {
429 const DexFile& dex_file = GetDexFile();
430 return dex_file.GetMethodSignature(dex_file.GetMethodId(method_->GetDexMethodIndex()));
431 }
432 const DexFile::ProtoId& GetPrototype() {
433 const DexFile& dex_file = GetDexFile();
434 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
435 }
436 const DexFile::TypeList* GetParameterTypeList() {
437 const DexFile::ProtoId& proto = GetPrototype();
438 return GetDexFile().GetProtoParameters(proto);
439 }
440 ObjectArray<Class>* GetParameterTypes() {
441 const DexFile::TypeList* params = GetParameterTypeList();
442 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
443 uint32_t num_params = params == NULL ? 0 : params->Size();
444 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
445 for (uint32_t i = 0; i < num_params; i++) {
446 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
447 result->Set(i, param_type);
448 }
449 return result;
450 }
451 Class* GetReturnType() {
452 const DexFile& dex_file = GetDexFile();
453 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
454 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
455 uint16_t return_type_idx = proto_id.return_type_idx_;
456 return GetClassFromTypeIdx(return_type_idx);
457 }
458 const char* GetReturnTypeDescriptor() {
459 const DexFile& dex_file = GetDexFile();
460 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
461 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
462 uint16_t return_type_idx = proto_id.return_type_idx_;
463 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
464 }
465 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
466 const DexFile& dex_file = GetDexFile();
467 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
468 }
469 const char* GetDeclaringClassDescriptor() {
470 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800471 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800472 uint16_t type_idx = klass->GetDexTypeIndex();
473 const DexFile& dex_file = GetDexFile();
474 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
475 }
476 const char* GetDeclaringClassSourceFile() {
477 const char* descriptor = GetDeclaringClassDescriptor();
478 const DexFile& dex_file = GetDexFile();
479 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
480 if (dex_class_def == NULL) {
481 return NULL;
482 } else {
483 return dex_file.GetSourceFile(*dex_class_def);
484 }
485 }
486 bool IsStatic() {
487 return method_->IsStatic();
488 }
489 bool IsClassInitializer() {
490 return IsStatic() && StringPiece(GetName()) == "<clinit>";
491 }
492 size_t NumArgs() {
493 // "1 +" because the first in Args is the receiver.
494 // "- 1" because we don't count the return type.
495 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
496 }
497 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
498 bool IsParamALongOrDouble(size_t param) {
499 CHECK_LT(param, NumArgs());
500 if (IsStatic()) {
501 param++; // 0th argument must skip return value at start of the shorty
502 } else if (param == 0) {
503 return false; // this argument
504 }
505 char ch = GetShorty()[param];
506 return (ch == 'J' || ch == 'D');
507 }
508 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
509 bool IsParamAReference(size_t param) {
510 CHECK_LT(param, NumArgs());
511 if (IsStatic()) {
512 param++; // 0th argument must skip return value at start of the shorty
513 } else if (param == 0) {
514 return true; // this argument
515 }
516 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
517 }
518 bool HasSameNameAndSignature(MethodHelper* other) {
519 StringPiece name(GetName());
520 StringPiece other_name(other->GetName());
521 if (name != other_name) {
522 return false;
523 }
524 if (GetDexCache() == other->GetDexCache()) {
525 const DexFile& dex_file = GetDexFile();
526 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
527 const DexFile::MethodId& other_mid =
528 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
529 return mid.proto_idx_ == other_mid.proto_idx_;
530 }
531 return GetSignature() == other->GetSignature();
532 }
533 const DexFile::CodeItem* GetCodeItem() {
534 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
535 }
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800536 bool IsResolvedTypeIdx(uint16_t type_idx) const {
537 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
538 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800539 Class* GetClassFromTypeIdx(uint16_t type_idx) {
540 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
541 if (type == NULL) {
542 type = GetClassLinker()->ResolveType(type_idx, method_);
543 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
544 }
545 return type;
546 }
547 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
548 const DexFile& dex_file = GetDexFile();
549 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
550 }
551 Class* GetDexCacheResolvedType(uint16_t type_idx) {
552 return GetDexCache()->GetResolvedType(type_idx);
553 }
554 const DexFile& GetDexFile() {
555 const DexFile* result = dex_file_;
556 if (result == NULL) {
557 const DexCache* dex_cache = GetDexCache();
558 result = &GetClassLinker()->FindDexFile(dex_cache);
559 dex_file_ = result;
560 }
561 return *result;
562 }
563 private:
564 // Set the method_ field, for proxy methods looking up the interface method via the resolved
565 // methods table.
566 void SetMethod(const Method* method) {
567 if (method != NULL) {
568 Class* klass = method->GetDeclaringClass();
569 if (klass->IsProxyClass()) {
570 method = method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
571 CHECK(method != NULL);
572 }
573 }
574 method_ = method;
575 }
576 DexCache* GetDexCache() {
577 DexCache* result = dex_cache_;
578 if (result == NULL) {
579 Class* klass = method_->GetDeclaringClass();
580 result = klass->GetDexCache();
581 dex_cache_ = result;
582 }
583 return result;
584 }
585 ClassLinker* GetClassLinker() {
586 ClassLinker* result = class_linker_;
587 if (result == NULL) {
588 result = Runtime::Current()->GetClassLinker();
589 class_linker_ = result;
590 }
591 return result;
592 }
593
594 ClassLinker* class_linker_;
595 DexCache* dex_cache_;
596 const DexFile* dex_file_;
597 const Method* method_;
598 const char* shorty_;
599 int32_t shorty_len_;
600
601 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
602};
603
604} // namespace art
605
606#endif // ART_SRC_OBJECT_UTILS_H_