blob: e2d9b88604b0c2c71098a622cde80aaa688310ab [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();
252 if (field_index != DexFile::kDexNoIndex) {
253 const DexFile& dex_file = GetDexFile();
254 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
255 } else {
256 // Proxy classes have a single static field called "throws"
257 CHECK(field_->GetDeclaringClass()->IsProxyClass());
258 DCHECK(field_->IsStatic());
259 return "throws";
260 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800261 }
262 String* GetNameAsString() {
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 GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
268 } else {
269 // Proxy classes have a single static field called "throws"
270 CHECK(field_->GetDeclaringClass()->IsProxyClass());
271 DCHECK(field_->IsStatic());
272 return Runtime::Current()->GetInternTable()->InternStrong("throws");
273 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800274 }
275 Class* GetType() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800276 uint32_t field_index = field_->GetDexFieldIndex();
277 if (field_index != DexFile::kDexNoIndex) {
278 const DexFile& dex_file = GetDexFile();
279 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
280 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
281 if (type == NULL) {
282 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
283 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
284 }
285 return type;
286 } else {
287 // Proxy classes have a single static field called "throws" whose type is Class[][]
288 CHECK(field_->GetDeclaringClass()->IsProxyClass());
289 DCHECK(field_->IsStatic());
290 return GetClassLinker()->FindSystemClass("[[Ljava/lang/Class;");
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800292 }
293 const char* GetTypeDescriptor() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800294 uint32_t field_index = field_->GetDexFieldIndex();
295 if (field_index != DexFile::kDexNoIndex) {
296 const DexFile& dex_file = GetDexFile();
297 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
298 return dex_file.GetFieldTypeDescriptor(field_id);
299 } else {
300 // Proxy classes have a single static field called "throws" whose type is Class[][]
301 CHECK(field_->GetDeclaringClass()->IsProxyClass());
302 DCHECK(field_->IsStatic());
303 return "[[Ljava/lang/Class;";
304 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800305 }
306 Primitive::Type GetTypeAsPrimitiveType() {
307 return Primitive::GetType(GetTypeDescriptor()[0]);
308 }
309 bool IsPrimitiveType() {
310 Primitive::Type type = GetTypeAsPrimitiveType();
311 return type != Primitive::kPrimNot;
312 }
313 size_t FieldSize() {
314 Primitive::Type type = GetTypeAsPrimitiveType();
315 return Primitive::FieldSize(type);
316 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800317
318 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
319 // If you need it longer, copy it into a std::string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800320 const char* GetDeclaringClassDescriptor() {
321 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800322 if (type_idx != DexFile::kDexNoIndex16) {
323 const DexFile& dex_file = GetDexFile();
324 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
325 } else {
326 // Most likely a proxy class
327 ClassHelper kh(field_->GetDeclaringClass());
328 declaring_class_descriptor_ = kh.GetDescriptor();
329 return declaring_class_descriptor_.c_str();
330 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800331 }
332
333 private:
334 DexCache* GetDexCache() {
335 DexCache* result = dex_cache_;
336 if (result == NULL) {
337 result = field_->GetDeclaringClass()->GetDexCache();
338 dex_cache_ = result;
339 }
340 return result;
341 }
342 ClassLinker* GetClassLinker() {
343 ClassLinker* result = class_linker_;
344 if (result == NULL) {
345 result = Runtime::Current()->GetClassLinker();
346 class_linker_ = result;
347 }
348 return result;
349 }
350 const DexFile& GetDexFile() {
351 const DexFile* result = dex_file_;
352 if (result == NULL) {
353 const DexCache* dex_cache = GetDexCache();
354 result = &GetClassLinker()->FindDexFile(dex_cache);
355 dex_file_ = result;
356 }
357 return *result;
358 }
359
360 ClassLinker* class_linker_;
361 DexCache* dex_cache_;
362 const DexFile* dex_file_;
363 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800364 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800365
366 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
367};
368
369class MethodHelper {
370 public:
371 MethodHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL),
372 shorty_(NULL), shorty_len_(0) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800373 explicit MethodHelper(const Method* m) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800374 method_(NULL), shorty_(NULL), shorty_len_(0) {
375 SetMethod(m);
376 }
377 MethodHelper(const Method* m, ClassLinker* l) : class_linker_(l), dex_cache_(NULL),
378 dex_file_(NULL), method_(NULL), shorty_(NULL), shorty_len_(0) {
379 SetMethod(m);
380 }
381
382 void ChangeMethod(Method* new_m) {
383 DCHECK(new_m != NULL);
384 if (dex_cache_ != NULL) {
385 Class* klass = new_m->GetDeclaringClass();
386 if (klass->IsProxyClass()) {
387 dex_cache_ = NULL;
388 dex_file_ = NULL;
389 } else {
390 DexCache* new_m_dex_cache = klass->GetDexCache();
391 if (new_m_dex_cache != dex_cache_) {
392 dex_cache_ = new_m_dex_cache;
393 dex_file_ = NULL;
394 }
395 }
396 }
397 SetMethod(new_m);
398 shorty_ = NULL;
399 }
400 const char* GetName() {
401 const DexFile& dex_file = GetDexFile();
402 return dex_file.GetMethodName(dex_file.GetMethodId(method_->GetDexMethodIndex()));
403 }
404 String* GetNameAsString() {
405 const DexFile& dex_file = GetDexFile();
406 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
407 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
408 }
409 const char* GetShorty() {
410 const char* result = shorty_;
411 if (result == NULL) {
412 const DexFile& dex_file = GetDexFile();
413 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
414 &shorty_len_);
415 shorty_ = result;
416 }
417 return result;
418 }
419 int32_t GetShortyLength() {
420 if (shorty_ == NULL) {
421 GetShorty();
422 }
423 return shorty_len_;
424 }
425 const std::string GetSignature() {
426 const DexFile& dex_file = GetDexFile();
427 return dex_file.GetMethodSignature(dex_file.GetMethodId(method_->GetDexMethodIndex()));
428 }
429 const DexFile::ProtoId& GetPrototype() {
430 const DexFile& dex_file = GetDexFile();
431 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
432 }
433 const DexFile::TypeList* GetParameterTypeList() {
434 const DexFile::ProtoId& proto = GetPrototype();
435 return GetDexFile().GetProtoParameters(proto);
436 }
437 ObjectArray<Class>* GetParameterTypes() {
438 const DexFile::TypeList* params = GetParameterTypeList();
439 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
440 uint32_t num_params = params == NULL ? 0 : params->Size();
441 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
442 for (uint32_t i = 0; i < num_params; i++) {
443 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
444 result->Set(i, param_type);
445 }
446 return result;
447 }
448 Class* GetReturnType() {
449 const DexFile& dex_file = GetDexFile();
450 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
451 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
452 uint16_t return_type_idx = proto_id.return_type_idx_;
453 return GetClassFromTypeIdx(return_type_idx);
454 }
455 const char* GetReturnTypeDescriptor() {
456 const DexFile& dex_file = GetDexFile();
457 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
458 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
459 uint16_t return_type_idx = proto_id.return_type_idx_;
460 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
461 }
462 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
463 const DexFile& dex_file = GetDexFile();
464 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
465 }
466 const char* GetDeclaringClassDescriptor() {
467 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800468 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469 uint16_t type_idx = klass->GetDexTypeIndex();
470 const DexFile& dex_file = GetDexFile();
471 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
472 }
473 const char* GetDeclaringClassSourceFile() {
474 const char* descriptor = GetDeclaringClassDescriptor();
475 const DexFile& dex_file = GetDexFile();
476 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800477 CHECK(dex_class_def != NULL);
478 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800479 }
480 bool IsStatic() {
481 return method_->IsStatic();
482 }
483 bool IsClassInitializer() {
484 return IsStatic() && StringPiece(GetName()) == "<clinit>";
485 }
486 size_t NumArgs() {
487 // "1 +" because the first in Args is the receiver.
488 // "- 1" because we don't count the return type.
489 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
490 }
491 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
492 bool IsParamALongOrDouble(size_t param) {
493 CHECK_LT(param, NumArgs());
494 if (IsStatic()) {
495 param++; // 0th argument must skip return value at start of the shorty
496 } else if (param == 0) {
497 return false; // this argument
498 }
499 char ch = GetShorty()[param];
500 return (ch == 'J' || ch == 'D');
501 }
502 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
503 bool IsParamAReference(size_t param) {
504 CHECK_LT(param, NumArgs());
505 if (IsStatic()) {
506 param++; // 0th argument must skip return value at start of the shorty
507 } else if (param == 0) {
508 return true; // this argument
509 }
510 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
511 }
512 bool HasSameNameAndSignature(MethodHelper* other) {
513 StringPiece name(GetName());
514 StringPiece other_name(other->GetName());
515 if (name != other_name) {
516 return false;
517 }
518 if (GetDexCache() == other->GetDexCache()) {
519 const DexFile& dex_file = GetDexFile();
520 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
521 const DexFile::MethodId& other_mid =
522 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
523 return mid.proto_idx_ == other_mid.proto_idx_;
524 }
525 return GetSignature() == other->GetSignature();
526 }
527 const DexFile::CodeItem* GetCodeItem() {
528 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
529 }
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800530 bool IsResolvedTypeIdx(uint16_t type_idx) const {
531 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
532 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800533 Class* GetClassFromTypeIdx(uint16_t type_idx) {
534 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
535 if (type == NULL) {
536 type = GetClassLinker()->ResolveType(type_idx, method_);
537 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
538 }
539 return type;
540 }
541 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
542 const DexFile& dex_file = GetDexFile();
543 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
544 }
545 Class* GetDexCacheResolvedType(uint16_t type_idx) {
546 return GetDexCache()->GetResolvedType(type_idx);
547 }
548 const DexFile& GetDexFile() {
549 const DexFile* result = dex_file_;
550 if (result == NULL) {
551 const DexCache* dex_cache = GetDexCache();
552 result = &GetClassLinker()->FindDexFile(dex_cache);
553 dex_file_ = result;
554 }
555 return *result;
556 }
557 private:
558 // Set the method_ field, for proxy methods looking up the interface method via the resolved
559 // methods table.
560 void SetMethod(const Method* method) {
561 if (method != NULL) {
562 Class* klass = method->GetDeclaringClass();
563 if (klass->IsProxyClass()) {
564 method = method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
565 CHECK(method != NULL);
566 }
567 }
568 method_ = method;
569 }
570 DexCache* GetDexCache() {
571 DexCache* result = dex_cache_;
572 if (result == NULL) {
573 Class* klass = method_->GetDeclaringClass();
574 result = klass->GetDexCache();
575 dex_cache_ = result;
576 }
577 return result;
578 }
579 ClassLinker* GetClassLinker() {
580 ClassLinker* result = class_linker_;
581 if (result == NULL) {
582 result = Runtime::Current()->GetClassLinker();
583 class_linker_ = result;
584 }
585 return result;
586 }
587
588 ClassLinker* class_linker_;
589 DexCache* dex_cache_;
590 const DexFile* dex_file_;
591 const Method* method_;
592 const char* shorty_;
593 int32_t shorty_len_;
594
595 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
596};
597
598} // namespace art
599
600#endif // ART_SRC_OBJECT_UTILS_H_