blob: f9aca10e6500aad5e2e7734c3f907b8e09e1e735 [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 Rogers6d4d9fc2011-11-30 16:24:48 -080024#include "object.h"
25#include "runtime.h"
Elliott Hughes91250e02011-12-13 22:30:35 -080026#include "UniquePtr.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080027
28#include <string>
29
30namespace art {
31
32class ClassHelper {
33 public:
Elliott Hughes91250e02011-12-13 22:30:35 -080034 ClassHelper(const Class* c = NULL, ClassLinker* l = NULL)
35 : class_def_(NULL),
36 class_linker_(l),
37 dex_cache_(NULL),
38 dex_file_(NULL),
39 interface_type_list_(NULL),
40 klass_(c) {
41 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080042
43 void ChangeClass(const Class* new_c) {
44 DCHECK(new_c != NULL);
45 if (dex_cache_ != NULL) {
46 DexCache* new_c_dex_cache = new_c->GetDexCache();
47 if (new_c_dex_cache != dex_cache_) {
48 dex_cache_ = new_c_dex_cache;
49 dex_file_ = NULL;
50 }
51 }
52 klass_ = new_c;
53 interface_type_list_ = NULL;
54 class_def_ = NULL;
55 }
56
Elliott Hughes91250e02011-12-13 22:30:35 -080057 // The returned const char* is only guaranteed to be valid for the lifetime of the ClassHelper.
58 // If you need it longer, copy it into a std::string.
59 const char* GetDescriptor() {
Ian Rogersa68a1cb2012-01-10 10:34:36 -080060 if (UNLIKELY(klass_->IsArrayClass())) {
61 return GetArrayDescriptor();
62 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080063 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -080064 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080065 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
66 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080067 } else {
68 const DexFile& dex_file = GetDexFile();
69 const DexFile::TypeId& type_id = dex_file.GetTypeId(klass_->GetDexTypeIndex());
70 return dex_file.GetTypeDescriptor(type_id);
71 }
72 }
Elliott Hughes91250e02011-12-13 22:30:35 -080073
Ian Rogersa68a1cb2012-01-10 10:34:36 -080074 const char* GetArrayDescriptor() {
75 std::string result("[");
76 const Class* saved_klass = klass_;
77 ChangeClass(klass_->GetComponentType());
78 result += GetDescriptor();
79 ChangeClass(saved_klass);
80 descriptor_ = result;
81 return descriptor_.c_str();
82 }
83
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080084 const DexFile::ClassDef* GetClassDef() {
85 const DexFile::ClassDef* result = class_def_;
86 if (result == NULL) {
87 result = GetDexFile().FindClassDef(GetDescriptor());
88 class_def_ = result;
89 }
90 return result;
91 }
Elliott Hughes91250e02011-12-13 22:30:35 -080092
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080093 uint32_t NumInterfaces() {
94 if (klass_->IsPrimitive()) {
95 return 0;
96 } else if (klass_->IsArrayClass()) {
97 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -080098 } else if (klass_->IsProxyClass()) {
99 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800100 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800101 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
102 if (interfaces == NULL) {
103 return 0;
104 } else {
105 return interfaces->Size();
106 }
107 }
108 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800109
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800110 uint16_t GetInterfaceTypeIdx(uint32_t idx) {
111 DCHECK(!klass_->IsPrimitive());
112 DCHECK(!klass_->IsArrayClass());
113 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
114 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800115
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800116 Class* GetInterface(uint32_t idx) {
117 DCHECK(!klass_->IsPrimitive());
118 if (klass_->IsArrayClass()) {
119 if (idx == 0) {
120 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
121 } else {
122 DCHECK_EQ(1U, idx);
123 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
124 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800125 } else if (klass_->IsProxyClass()) {
126 return klass_->GetIfTable()->Get(idx)->GetInterface();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127 } else {
128 uint16_t type_idx = GetInterfaceTypeIdx(idx);
129 Class* interface = GetDexCache()->GetResolvedType(type_idx);
130 if (interface == NULL) {
131 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
132 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
133 }
134 return interface;
135 }
136 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800137
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800138 const char* GetSourceFile() {
Elliott Hughes95572412011-12-13 18:14:20 -0800139 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800140 const DexFile& dex_file = GetDexFile();
141 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
142 if (dex_class_def == NULL) {
143 return NULL;
144 } else {
145 return dex_file.GetSourceFile(*dex_class_def);
146 }
147 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800148
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800149 std::string GetLocation() {
150 return GetDexCache()->GetLocation()->ToModifiedUtf8();
151 }
152
153 const DexFile& GetDexFile() {
154 const DexFile* result = dex_file_;
155 if (result == NULL) {
156 const DexCache* dex_cache = GetDexCache();
157 result = &GetClassLinker()->FindDexFile(dex_cache);
158 dex_file_ = result;
159 }
160 return *result;
161 }
162
163 private:
164 const DexFile::TypeList* GetInterfaceTypeList() {
165 const DexFile::TypeList* result = interface_type_list_;
166 if (result == NULL) {
167 const DexFile::ClassDef* class_def = GetClassDef();
168 if (class_def != NULL) {
169 result = GetDexFile().GetInterfacesList(*class_def);
170 interface_type_list_ = result;
171 }
172 }
173 return result;
174 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800175
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800176 DexCache* GetDexCache() {
177 DexCache* result = dex_cache_;
178 if (result == NULL) {
179 result = klass_->GetDexCache();
180 dex_cache_ = result;
181 }
182 return result;
183 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800184
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800185 ClassLinker* GetClassLinker() {
186 ClassLinker* result = class_linker_;
187 if (result == NULL) {
188 result = Runtime::Current()->GetClassLinker();
189 class_linker_ = result;
190 }
191 return result;
192 }
193
194 const DexFile::ClassDef* class_def_;
195 ClassLinker* class_linker_;
196 DexCache* dex_cache_;
197 const DexFile* dex_file_;
198 const DexFile::TypeList* interface_type_list_;
199 const Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800200 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800201
202 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
203};
204
205class FieldHelper {
206 public:
207 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
208 FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
209 FieldHelper(const Field* f, ClassLinker* l) : class_linker_(l), dex_cache_(NULL), dex_file_(NULL),
210 field_(f) {}
211
212 void ChangeField(const Field* new_f) {
213 DCHECK(new_f != NULL);
214 if (dex_cache_ != NULL) {
215 DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
216 if (new_f_dex_cache != dex_cache_) {
217 dex_cache_ = new_f_dex_cache;
218 dex_file_ = NULL;
219 }
220 }
221 field_ = new_f;
222 }
223 const char* GetName() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800224 uint32_t field_index = field_->GetDexFieldIndex();
225 if (field_index != DexFile::kDexNoIndex) {
226 const DexFile& dex_file = GetDexFile();
227 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
228 } else {
229 // Proxy classes have a single static field called "throws"
230 CHECK(field_->GetDeclaringClass()->IsProxyClass());
231 DCHECK(field_->IsStatic());
232 return "throws";
233 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234 }
235 String* GetNameAsString() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800236 uint32_t field_index = field_->GetDexFieldIndex();
237 if (field_index != DexFile::kDexNoIndex) {
238 const DexFile& dex_file = GetDexFile();
239 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
240 return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
241 } else {
242 // Proxy classes have a single static field called "throws"
243 CHECK(field_->GetDeclaringClass()->IsProxyClass());
244 DCHECK(field_->IsStatic());
245 return Runtime::Current()->GetInternTable()->InternStrong("throws");
246 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800247 }
248 Class* GetType() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800249 uint32_t field_index = field_->GetDexFieldIndex();
250 if (field_index != DexFile::kDexNoIndex) {
251 const DexFile& dex_file = GetDexFile();
252 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
253 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
254 if (type == NULL) {
255 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
256 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
257 }
258 return type;
259 } else {
260 // Proxy classes have a single static field called "throws" whose type is Class[][]
261 CHECK(field_->GetDeclaringClass()->IsProxyClass());
262 DCHECK(field_->IsStatic());
263 return GetClassLinker()->FindSystemClass("[[Ljava/lang/Class;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800264 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800265 }
266 const char* GetTypeDescriptor() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800267 uint32_t field_index = field_->GetDexFieldIndex();
268 if (field_index != DexFile::kDexNoIndex) {
269 const DexFile& dex_file = GetDexFile();
270 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
271 return dex_file.GetFieldTypeDescriptor(field_id);
272 } else {
273 // Proxy classes have a single static field called "throws" whose type is Class[][]
274 CHECK(field_->GetDeclaringClass()->IsProxyClass());
275 DCHECK(field_->IsStatic());
276 return "[[Ljava/lang/Class;";
277 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800278 }
279 Primitive::Type GetTypeAsPrimitiveType() {
280 return Primitive::GetType(GetTypeDescriptor()[0]);
281 }
282 bool IsPrimitiveType() {
283 Primitive::Type type = GetTypeAsPrimitiveType();
284 return type != Primitive::kPrimNot;
285 }
286 size_t FieldSize() {
287 Primitive::Type type = GetTypeAsPrimitiveType();
288 return Primitive::FieldSize(type);
289 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800290
291 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
292 // If you need it longer, copy it into a std::string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800293 const char* GetDeclaringClassDescriptor() {
294 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800295 if (type_idx != DexFile::kDexNoIndex16) {
296 const DexFile& dex_file = GetDexFile();
297 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
298 } else {
299 // Most likely a proxy class
300 ClassHelper kh(field_->GetDeclaringClass());
301 declaring_class_descriptor_ = kh.GetDescriptor();
302 return declaring_class_descriptor_.c_str();
303 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 }
305
306 private:
307 DexCache* GetDexCache() {
308 DexCache* result = dex_cache_;
309 if (result == NULL) {
310 result = field_->GetDeclaringClass()->GetDexCache();
311 dex_cache_ = result;
312 }
313 return result;
314 }
315 ClassLinker* GetClassLinker() {
316 ClassLinker* result = class_linker_;
317 if (result == NULL) {
318 result = Runtime::Current()->GetClassLinker();
319 class_linker_ = result;
320 }
321 return result;
322 }
323 const DexFile& GetDexFile() {
324 const DexFile* result = dex_file_;
325 if (result == NULL) {
326 const DexCache* dex_cache = GetDexCache();
327 result = &GetClassLinker()->FindDexFile(dex_cache);
328 dex_file_ = result;
329 }
330 return *result;
331 }
332
333 ClassLinker* class_linker_;
334 DexCache* dex_cache_;
335 const DexFile* dex_file_;
336 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800337 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800338
339 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
340};
341
342class MethodHelper {
343 public:
344 MethodHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL),
345 shorty_(NULL), shorty_len_(0) {}
346 MethodHelper(const Method* m) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL),
347 method_(NULL), shorty_(NULL), shorty_len_(0) {
348 SetMethod(m);
349 }
350 MethodHelper(const Method* m, ClassLinker* l) : class_linker_(l), dex_cache_(NULL),
351 dex_file_(NULL), method_(NULL), shorty_(NULL), shorty_len_(0) {
352 SetMethod(m);
353 }
354
355 void ChangeMethod(Method* new_m) {
356 DCHECK(new_m != NULL);
357 if (dex_cache_ != NULL) {
358 Class* klass = new_m->GetDeclaringClass();
359 if (klass->IsProxyClass()) {
360 dex_cache_ = NULL;
361 dex_file_ = NULL;
362 } else {
363 DexCache* new_m_dex_cache = klass->GetDexCache();
364 if (new_m_dex_cache != dex_cache_) {
365 dex_cache_ = new_m_dex_cache;
366 dex_file_ = NULL;
367 }
368 }
369 }
370 SetMethod(new_m);
371 shorty_ = NULL;
372 }
373 const char* GetName() {
374 const DexFile& dex_file = GetDexFile();
375 return dex_file.GetMethodName(dex_file.GetMethodId(method_->GetDexMethodIndex()));
376 }
377 String* GetNameAsString() {
378 const DexFile& dex_file = GetDexFile();
379 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
380 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
381 }
382 const char* GetShorty() {
383 const char* result = shorty_;
384 if (result == NULL) {
385 const DexFile& dex_file = GetDexFile();
386 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
387 &shorty_len_);
388 shorty_ = result;
389 }
390 return result;
391 }
392 int32_t GetShortyLength() {
393 if (shorty_ == NULL) {
394 GetShorty();
395 }
396 return shorty_len_;
397 }
398 const std::string GetSignature() {
399 const DexFile& dex_file = GetDexFile();
400 return dex_file.GetMethodSignature(dex_file.GetMethodId(method_->GetDexMethodIndex()));
401 }
402 const DexFile::ProtoId& GetPrototype() {
403 const DexFile& dex_file = GetDexFile();
404 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
405 }
406 const DexFile::TypeList* GetParameterTypeList() {
407 const DexFile::ProtoId& proto = GetPrototype();
408 return GetDexFile().GetProtoParameters(proto);
409 }
410 ObjectArray<Class>* GetParameterTypes() {
411 const DexFile::TypeList* params = GetParameterTypeList();
412 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
413 uint32_t num_params = params == NULL ? 0 : params->Size();
414 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
415 for (uint32_t i = 0; i < num_params; i++) {
416 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
417 result->Set(i, param_type);
418 }
419 return result;
420 }
421 Class* GetReturnType() {
422 const DexFile& dex_file = GetDexFile();
423 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
424 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
425 uint16_t return_type_idx = proto_id.return_type_idx_;
426 return GetClassFromTypeIdx(return_type_idx);
427 }
428 const char* GetReturnTypeDescriptor() {
429 const DexFile& dex_file = GetDexFile();
430 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
431 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
432 uint16_t return_type_idx = proto_id.return_type_idx_;
433 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
434 }
435 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
436 const DexFile& dex_file = GetDexFile();
437 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
438 }
439 const char* GetDeclaringClassDescriptor() {
440 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800441 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800442 uint16_t type_idx = klass->GetDexTypeIndex();
443 const DexFile& dex_file = GetDexFile();
444 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
445 }
446 const char* GetDeclaringClassSourceFile() {
447 const char* descriptor = GetDeclaringClassDescriptor();
448 const DexFile& dex_file = GetDexFile();
449 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
450 if (dex_class_def == NULL) {
451 return NULL;
452 } else {
453 return dex_file.GetSourceFile(*dex_class_def);
454 }
455 }
456 bool IsStatic() {
457 return method_->IsStatic();
458 }
459 bool IsClassInitializer() {
460 return IsStatic() && StringPiece(GetName()) == "<clinit>";
461 }
462 size_t NumArgs() {
463 // "1 +" because the first in Args is the receiver.
464 // "- 1" because we don't count the return type.
465 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
466 }
467 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
468 bool IsParamALongOrDouble(size_t param) {
469 CHECK_LT(param, NumArgs());
470 if (IsStatic()) {
471 param++; // 0th argument must skip return value at start of the shorty
472 } else if (param == 0) {
473 return false; // this argument
474 }
475 char ch = GetShorty()[param];
476 return (ch == 'J' || ch == 'D');
477 }
478 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
479 bool IsParamAReference(size_t param) {
480 CHECK_LT(param, NumArgs());
481 if (IsStatic()) {
482 param++; // 0th argument must skip return value at start of the shorty
483 } else if (param == 0) {
484 return true; // this argument
485 }
486 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
487 }
488 bool HasSameNameAndSignature(MethodHelper* other) {
489 StringPiece name(GetName());
490 StringPiece other_name(other->GetName());
491 if (name != other_name) {
492 return false;
493 }
494 if (GetDexCache() == other->GetDexCache()) {
495 const DexFile& dex_file = GetDexFile();
496 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
497 const DexFile::MethodId& other_mid =
498 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
499 return mid.proto_idx_ == other_mid.proto_idx_;
500 }
501 return GetSignature() == other->GetSignature();
502 }
503 const DexFile::CodeItem* GetCodeItem() {
504 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
505 }
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800506 bool IsResolvedTypeIdx(uint16_t type_idx) const {
507 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
508 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800509 Class* GetClassFromTypeIdx(uint16_t type_idx) {
510 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
511 if (type == NULL) {
512 type = GetClassLinker()->ResolveType(type_idx, method_);
513 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
514 }
515 return type;
516 }
517 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
518 const DexFile& dex_file = GetDexFile();
519 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
520 }
521 Class* GetDexCacheResolvedType(uint16_t type_idx) {
522 return GetDexCache()->GetResolvedType(type_idx);
523 }
524 const DexFile& GetDexFile() {
525 const DexFile* result = dex_file_;
526 if (result == NULL) {
527 const DexCache* dex_cache = GetDexCache();
528 result = &GetClassLinker()->FindDexFile(dex_cache);
529 dex_file_ = result;
530 }
531 return *result;
532 }
533 private:
534 // Set the method_ field, for proxy methods looking up the interface method via the resolved
535 // methods table.
536 void SetMethod(const Method* method) {
537 if (method != NULL) {
538 Class* klass = method->GetDeclaringClass();
539 if (klass->IsProxyClass()) {
540 method = method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
541 CHECK(method != NULL);
542 }
543 }
544 method_ = method;
545 }
546 DexCache* GetDexCache() {
547 DexCache* result = dex_cache_;
548 if (result == NULL) {
549 Class* klass = method_->GetDeclaringClass();
550 result = klass->GetDexCache();
551 dex_cache_ = result;
552 }
553 return result;
554 }
555 ClassLinker* GetClassLinker() {
556 ClassLinker* result = class_linker_;
557 if (result == NULL) {
558 result = Runtime::Current()->GetClassLinker();
559 class_linker_ = result;
560 }
561 return result;
562 }
563
564 ClassLinker* class_linker_;
565 DexCache* dex_cache_;
566 const DexFile* dex_file_;
567 const Method* method_;
568 const char* shorty_;
569 int32_t shorty_len_;
570
571 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
572};
573
574} // namespace art
575
576#endif // ART_SRC_OBJECT_UTILS_H_