blob: 42937cff71a00abaefcce6dbdd326e827ee57183 [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 Rogers6d4d9fc2011-11-30 16:24:48 -080060 if (klass_->IsArrayClass()) {
Elliott Hughes95572412011-12-13 18:14:20 -080061 std::string result("[");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080062 const Class* saved_klass = klass_;
63 ChangeClass(klass_->GetComponentType());
64 result += GetDescriptor();
65 ChangeClass(saved_klass);
Elliott Hughes91250e02011-12-13 22:30:35 -080066 descriptor_ = result;
67 return descriptor_.c_str();
68 } else if (klass_->IsPrimitive()) {
69 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080070 } else if (klass_->IsProxyClass()) {
Elliott Hughes91250e02011-12-13 22:30:35 -080071 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
72 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080073 } else {
74 const DexFile& dex_file = GetDexFile();
75 const DexFile::TypeId& type_id = dex_file.GetTypeId(klass_->GetDexTypeIndex());
76 return dex_file.GetTypeDescriptor(type_id);
77 }
78 }
Elliott Hughes91250e02011-12-13 22:30:35 -080079
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080080 const DexFile::ClassDef* GetClassDef() {
81 const DexFile::ClassDef* result = class_def_;
82 if (result == NULL) {
83 result = GetDexFile().FindClassDef(GetDescriptor());
84 class_def_ = result;
85 }
86 return result;
87 }
Elliott Hughes91250e02011-12-13 22:30:35 -080088
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080089 uint32_t NumInterfaces() {
90 if (klass_->IsPrimitive()) {
91 return 0;
92 } else if (klass_->IsArrayClass()) {
93 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -080094 } else if (klass_->IsProxyClass()) {
95 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080096 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080097 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
98 if (interfaces == NULL) {
99 return 0;
100 } else {
101 return interfaces->Size();
102 }
103 }
104 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800105
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800106 uint16_t GetInterfaceTypeIdx(uint32_t idx) {
107 DCHECK(!klass_->IsPrimitive());
108 DCHECK(!klass_->IsArrayClass());
109 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
110 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800111
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800112 Class* GetInterface(uint32_t idx) {
113 DCHECK(!klass_->IsPrimitive());
114 if (klass_->IsArrayClass()) {
115 if (idx == 0) {
116 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
117 } else {
118 DCHECK_EQ(1U, idx);
119 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
120 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800121 } else if (klass_->IsProxyClass()) {
122 return klass_->GetIfTable()->Get(idx)->GetInterface();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800123 } else {
124 uint16_t type_idx = GetInterfaceTypeIdx(idx);
125 Class* interface = GetDexCache()->GetResolvedType(type_idx);
126 if (interface == NULL) {
127 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
128 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
129 }
130 return interface;
131 }
132 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800133
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 const char* GetSourceFile() {
Elliott Hughes95572412011-12-13 18:14:20 -0800135 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800136 const DexFile& dex_file = GetDexFile();
137 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
138 if (dex_class_def == NULL) {
139 return NULL;
140 } else {
141 return dex_file.GetSourceFile(*dex_class_def);
142 }
143 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800144
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800145 std::string GetLocation() {
146 return GetDexCache()->GetLocation()->ToModifiedUtf8();
147 }
148
149 const DexFile& GetDexFile() {
150 const DexFile* result = dex_file_;
151 if (result == NULL) {
152 const DexCache* dex_cache = GetDexCache();
153 result = &GetClassLinker()->FindDexFile(dex_cache);
154 dex_file_ = result;
155 }
156 return *result;
157 }
158
159 private:
160 const DexFile::TypeList* GetInterfaceTypeList() {
161 const DexFile::TypeList* result = interface_type_list_;
162 if (result == NULL) {
163 const DexFile::ClassDef* class_def = GetClassDef();
164 if (class_def != NULL) {
165 result = GetDexFile().GetInterfacesList(*class_def);
166 interface_type_list_ = result;
167 }
168 }
169 return result;
170 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800171
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800172 DexCache* GetDexCache() {
173 DexCache* result = dex_cache_;
174 if (result == NULL) {
175 result = klass_->GetDexCache();
176 dex_cache_ = result;
177 }
178 return result;
179 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800180
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800181 ClassLinker* GetClassLinker() {
182 ClassLinker* result = class_linker_;
183 if (result == NULL) {
184 result = Runtime::Current()->GetClassLinker();
185 class_linker_ = result;
186 }
187 return result;
188 }
189
190 const DexFile::ClassDef* class_def_;
191 ClassLinker* class_linker_;
192 DexCache* dex_cache_;
193 const DexFile* dex_file_;
194 const DexFile::TypeList* interface_type_list_;
195 const Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800196 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800197
198 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
199};
200
201class FieldHelper {
202 public:
203 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
204 FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
205 FieldHelper(const Field* f, ClassLinker* l) : class_linker_(l), dex_cache_(NULL), dex_file_(NULL),
206 field_(f) {}
207
208 void ChangeField(const Field* new_f) {
209 DCHECK(new_f != NULL);
210 if (dex_cache_ != NULL) {
211 DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
212 if (new_f_dex_cache != dex_cache_) {
213 dex_cache_ = new_f_dex_cache;
214 dex_file_ = NULL;
215 }
216 }
217 field_ = new_f;
218 }
219 const char* GetName() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800220 uint32_t field_index = field_->GetDexFieldIndex();
221 if (field_index != DexFile::kDexNoIndex) {
222 const DexFile& dex_file = GetDexFile();
223 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
224 } else {
225 // Proxy classes have a single static field called "throws"
226 CHECK(field_->GetDeclaringClass()->IsProxyClass());
227 DCHECK(field_->IsStatic());
228 return "throws";
229 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800230 }
231 String* GetNameAsString() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800232 uint32_t field_index = field_->GetDexFieldIndex();
233 if (field_index != DexFile::kDexNoIndex) {
234 const DexFile& dex_file = GetDexFile();
235 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
236 return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
237 } else {
238 // Proxy classes have a single static field called "throws"
239 CHECK(field_->GetDeclaringClass()->IsProxyClass());
240 DCHECK(field_->IsStatic());
241 return Runtime::Current()->GetInternTable()->InternStrong("throws");
242 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800243 }
244 Class* GetType() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800245 uint32_t field_index = field_->GetDexFieldIndex();
246 if (field_index != DexFile::kDexNoIndex) {
247 const DexFile& dex_file = GetDexFile();
248 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
249 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
250 if (type == NULL) {
251 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
252 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
253 }
254 return type;
255 } else {
256 // Proxy classes have a single static field called "throws" whose type is Class[][]
257 CHECK(field_->GetDeclaringClass()->IsProxyClass());
258 DCHECK(field_->IsStatic());
259 return GetClassLinker()->FindSystemClass("[[Ljava/lang/Class;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800260 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800261 }
262 const char* GetTypeDescriptor() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800263 uint32_t field_index = field_->GetDexFieldIndex();
264 if (field_index != DexFile::kDexNoIndex) {
265 const DexFile& dex_file = GetDexFile();
266 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
267 return dex_file.GetFieldTypeDescriptor(field_id);
268 } else {
269 // Proxy classes have a single static field called "throws" whose type is Class[][]
270 CHECK(field_->GetDeclaringClass()->IsProxyClass());
271 DCHECK(field_->IsStatic());
272 return "[[Ljava/lang/Class;";
273 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 }
275 Primitive::Type GetTypeAsPrimitiveType() {
276 return Primitive::GetType(GetTypeDescriptor()[0]);
277 }
278 bool IsPrimitiveType() {
279 Primitive::Type type = GetTypeAsPrimitiveType();
280 return type != Primitive::kPrimNot;
281 }
282 size_t FieldSize() {
283 Primitive::Type type = GetTypeAsPrimitiveType();
284 return Primitive::FieldSize(type);
285 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800286
287 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
288 // If you need it longer, copy it into a std::string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800289 const char* GetDeclaringClassDescriptor() {
290 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800291 if (type_idx != DexFile::kDexNoIndex16) {
292 const DexFile& dex_file = GetDexFile();
293 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
294 } else {
295 // Most likely a proxy class
296 ClassHelper kh(field_->GetDeclaringClass());
297 declaring_class_descriptor_ = kh.GetDescriptor();
298 return declaring_class_descriptor_.c_str();
299 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800300 }
301
302 private:
303 DexCache* GetDexCache() {
304 DexCache* result = dex_cache_;
305 if (result == NULL) {
306 result = field_->GetDeclaringClass()->GetDexCache();
307 dex_cache_ = result;
308 }
309 return result;
310 }
311 ClassLinker* GetClassLinker() {
312 ClassLinker* result = class_linker_;
313 if (result == NULL) {
314 result = Runtime::Current()->GetClassLinker();
315 class_linker_ = result;
316 }
317 return result;
318 }
319 const DexFile& GetDexFile() {
320 const DexFile* result = dex_file_;
321 if (result == NULL) {
322 const DexCache* dex_cache = GetDexCache();
323 result = &GetClassLinker()->FindDexFile(dex_cache);
324 dex_file_ = result;
325 }
326 return *result;
327 }
328
329 ClassLinker* class_linker_;
330 DexCache* dex_cache_;
331 const DexFile* dex_file_;
332 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800333 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800334
335 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
336};
337
338class MethodHelper {
339 public:
340 MethodHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL),
341 shorty_(NULL), shorty_len_(0) {}
342 MethodHelper(const Method* m) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL),
343 method_(NULL), shorty_(NULL), shorty_len_(0) {
344 SetMethod(m);
345 }
346 MethodHelper(const Method* m, ClassLinker* l) : class_linker_(l), dex_cache_(NULL),
347 dex_file_(NULL), method_(NULL), shorty_(NULL), shorty_len_(0) {
348 SetMethod(m);
349 }
350
351 void ChangeMethod(Method* new_m) {
352 DCHECK(new_m != NULL);
353 if (dex_cache_ != NULL) {
354 Class* klass = new_m->GetDeclaringClass();
355 if (klass->IsProxyClass()) {
356 dex_cache_ = NULL;
357 dex_file_ = NULL;
358 } else {
359 DexCache* new_m_dex_cache = klass->GetDexCache();
360 if (new_m_dex_cache != dex_cache_) {
361 dex_cache_ = new_m_dex_cache;
362 dex_file_ = NULL;
363 }
364 }
365 }
366 SetMethod(new_m);
367 shorty_ = NULL;
368 }
369 const char* GetName() {
370 const DexFile& dex_file = GetDexFile();
371 return dex_file.GetMethodName(dex_file.GetMethodId(method_->GetDexMethodIndex()));
372 }
373 String* GetNameAsString() {
374 const DexFile& dex_file = GetDexFile();
375 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
376 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
377 }
378 const char* GetShorty() {
379 const char* result = shorty_;
380 if (result == NULL) {
381 const DexFile& dex_file = GetDexFile();
382 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
383 &shorty_len_);
384 shorty_ = result;
385 }
386 return result;
387 }
388 int32_t GetShortyLength() {
389 if (shorty_ == NULL) {
390 GetShorty();
391 }
392 return shorty_len_;
393 }
394 const std::string GetSignature() {
395 const DexFile& dex_file = GetDexFile();
396 return dex_file.GetMethodSignature(dex_file.GetMethodId(method_->GetDexMethodIndex()));
397 }
398 const DexFile::ProtoId& GetPrototype() {
399 const DexFile& dex_file = GetDexFile();
400 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
401 }
402 const DexFile::TypeList* GetParameterTypeList() {
403 const DexFile::ProtoId& proto = GetPrototype();
404 return GetDexFile().GetProtoParameters(proto);
405 }
406 ObjectArray<Class>* GetParameterTypes() {
407 const DexFile::TypeList* params = GetParameterTypeList();
408 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
409 uint32_t num_params = params == NULL ? 0 : params->Size();
410 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
411 for (uint32_t i = 0; i < num_params; i++) {
412 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
413 result->Set(i, param_type);
414 }
415 return result;
416 }
417 Class* GetReturnType() {
418 const DexFile& dex_file = GetDexFile();
419 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
420 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
421 uint16_t return_type_idx = proto_id.return_type_idx_;
422 return GetClassFromTypeIdx(return_type_idx);
423 }
424 const char* GetReturnTypeDescriptor() {
425 const DexFile& dex_file = GetDexFile();
426 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
427 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
428 uint16_t return_type_idx = proto_id.return_type_idx_;
429 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
430 }
431 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
432 const DexFile& dex_file = GetDexFile();
433 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
434 }
435 const char* GetDeclaringClassDescriptor() {
436 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800437 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800438 uint16_t type_idx = klass->GetDexTypeIndex();
439 const DexFile& dex_file = GetDexFile();
440 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
441 }
442 const char* GetDeclaringClassSourceFile() {
443 const char* descriptor = GetDeclaringClassDescriptor();
444 const DexFile& dex_file = GetDexFile();
445 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
446 if (dex_class_def == NULL) {
447 return NULL;
448 } else {
449 return dex_file.GetSourceFile(*dex_class_def);
450 }
451 }
452 bool IsStatic() {
453 return method_->IsStatic();
454 }
455 bool IsClassInitializer() {
456 return IsStatic() && StringPiece(GetName()) == "<clinit>";
457 }
458 size_t NumArgs() {
459 // "1 +" because the first in Args is the receiver.
460 // "- 1" because we don't count the return type.
461 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
462 }
463 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
464 bool IsParamALongOrDouble(size_t param) {
465 CHECK_LT(param, NumArgs());
466 if (IsStatic()) {
467 param++; // 0th argument must skip return value at start of the shorty
468 } else if (param == 0) {
469 return false; // this argument
470 }
471 char ch = GetShorty()[param];
472 return (ch == 'J' || ch == 'D');
473 }
474 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
475 bool IsParamAReference(size_t param) {
476 CHECK_LT(param, NumArgs());
477 if (IsStatic()) {
478 param++; // 0th argument must skip return value at start of the shorty
479 } else if (param == 0) {
480 return true; // this argument
481 }
482 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
483 }
484 bool HasSameNameAndSignature(MethodHelper* other) {
485 StringPiece name(GetName());
486 StringPiece other_name(other->GetName());
487 if (name != other_name) {
488 return false;
489 }
490 if (GetDexCache() == other->GetDexCache()) {
491 const DexFile& dex_file = GetDexFile();
492 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
493 const DexFile::MethodId& other_mid =
494 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
495 return mid.proto_idx_ == other_mid.proto_idx_;
496 }
497 return GetSignature() == other->GetSignature();
498 }
499 const DexFile::CodeItem* GetCodeItem() {
500 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
501 }
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800502 bool IsResolvedTypeIdx(uint16_t type_idx) const {
503 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
504 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800505 Class* GetClassFromTypeIdx(uint16_t type_idx) {
506 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
507 if (type == NULL) {
508 type = GetClassLinker()->ResolveType(type_idx, method_);
509 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
510 }
511 return type;
512 }
513 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
514 const DexFile& dex_file = GetDexFile();
515 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
516 }
517 Class* GetDexCacheResolvedType(uint16_t type_idx) {
518 return GetDexCache()->GetResolvedType(type_idx);
519 }
520 const DexFile& GetDexFile() {
521 const DexFile* result = dex_file_;
522 if (result == NULL) {
523 const DexCache* dex_cache = GetDexCache();
524 result = &GetClassLinker()->FindDexFile(dex_cache);
525 dex_file_ = result;
526 }
527 return *result;
528 }
529 private:
530 // Set the method_ field, for proxy methods looking up the interface method via the resolved
531 // methods table.
532 void SetMethod(const Method* method) {
533 if (method != NULL) {
534 Class* klass = method->GetDeclaringClass();
535 if (klass->IsProxyClass()) {
536 method = method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
537 CHECK(method != NULL);
538 }
539 }
540 method_ = method;
541 }
542 DexCache* GetDexCache() {
543 DexCache* result = dex_cache_;
544 if (result == NULL) {
545 Class* klass = method_->GetDeclaringClass();
546 result = klass->GetDexCache();
547 dex_cache_ = result;
548 }
549 return result;
550 }
551 ClassLinker* GetClassLinker() {
552 ClassLinker* result = class_linker_;
553 if (result == NULL) {
554 result = Runtime::Current()->GetClassLinker();
555 class_linker_ = result;
556 }
557 return result;
558 }
559
560 ClassLinker* class_linker_;
561 DexCache* dex_cache_;
562 const DexFile* dex_file_;
563 const Method* method_;
564 const char* shorty_;
565 int32_t shorty_len_;
566
567 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
568};
569
570} // namespace art
571
572#endif // ART_SRC_OBJECT_UTILS_H_