blob: b9d8a24a01e4a201af9fb5ac6c8b53ff82a9bf4e [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();
Ian Rogers19846512012-02-24 11:42:47 -0800402 uint32_t dex_method_idx = method_->GetDexMethodIndex();
403 if (dex_method_idx != DexFile::kDexNoIndex16) {
404 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
405 } else {
406 Runtime* runtime = Runtime::Current();
407 if (method_ == runtime->GetResolutionMethod()) {
408 return "<VM internal resolution method>";
409 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
410 return "<VM internal callee-save all registers method>";
411 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
412 return "<VM internal callee-save reference registers method>";
413 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
414 return "<VM internal callee-save reference and argument registers method>";
415 } else {
416 return "<unknown VM internal method>";
417 }
418 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800419 }
420 String* GetNameAsString() {
421 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800422 uint32_t dex_method_idx = method_->GetDexMethodIndex();
423 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800424 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
425 }
426 const char* GetShorty() {
427 const char* result = shorty_;
428 if (result == NULL) {
429 const DexFile& dex_file = GetDexFile();
430 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
431 &shorty_len_);
432 shorty_ = result;
433 }
434 return result;
435 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800436 uint32_t GetShortyLength() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800437 if (shorty_ == NULL) {
438 GetShorty();
439 }
440 return shorty_len_;
441 }
442 const std::string GetSignature() {
443 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800444 uint32_t dex_method_idx = method_->GetDexMethodIndex();
445 if (dex_method_idx != DexFile::kDexNoIndex16) {
446 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
447 } else {
448 return "<no signature>";
449 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800450 }
451 const DexFile::ProtoId& GetPrototype() {
452 const DexFile& dex_file = GetDexFile();
453 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
454 }
455 const DexFile::TypeList* GetParameterTypeList() {
456 const DexFile::ProtoId& proto = GetPrototype();
457 return GetDexFile().GetProtoParameters(proto);
458 }
459 ObjectArray<Class>* GetParameterTypes() {
460 const DexFile::TypeList* params = GetParameterTypeList();
461 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
462 uint32_t num_params = params == NULL ? 0 : params->Size();
463 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
464 for (uint32_t i = 0; i < num_params; i++) {
465 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
466 result->Set(i, param_type);
467 }
468 return result;
469 }
470 Class* GetReturnType() {
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 GetClassFromTypeIdx(return_type_idx);
476 }
477 const char* GetReturnTypeDescriptor() {
478 const DexFile& dex_file = GetDexFile();
479 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
480 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
481 uint16_t return_type_idx = proto_id.return_type_idx_;
482 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
483 }
484 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
485 const DexFile& dex_file = GetDexFile();
486 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
487 }
488 const char* GetDeclaringClassDescriptor() {
489 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800490 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800491 uint16_t type_idx = klass->GetDexTypeIndex();
492 const DexFile& dex_file = GetDexFile();
493 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
494 }
495 const char* GetDeclaringClassSourceFile() {
496 const char* descriptor = GetDeclaringClassDescriptor();
497 const DexFile& dex_file = GetDexFile();
498 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800499 CHECK(dex_class_def != NULL);
500 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800501 }
502 bool IsStatic() {
503 return method_->IsStatic();
504 }
505 bool IsClassInitializer() {
506 return IsStatic() && StringPiece(GetName()) == "<clinit>";
507 }
508 size_t NumArgs() {
509 // "1 +" because the first in Args is the receiver.
510 // "- 1" because we don't count the return type.
511 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
512 }
513 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
514 bool IsParamALongOrDouble(size_t param) {
515 CHECK_LT(param, NumArgs());
516 if (IsStatic()) {
517 param++; // 0th argument must skip return value at start of the shorty
518 } else if (param == 0) {
519 return false; // this argument
520 }
521 char ch = GetShorty()[param];
522 return (ch == 'J' || ch == 'D');
523 }
524 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
525 bool IsParamAReference(size_t param) {
526 CHECK_LT(param, NumArgs());
527 if (IsStatic()) {
528 param++; // 0th argument must skip return value at start of the shorty
529 } else if (param == 0) {
530 return true; // this argument
531 }
532 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
533 }
534 bool HasSameNameAndSignature(MethodHelper* other) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800535 if (GetDexCache() == other->GetDexCache()) {
536 const DexFile& dex_file = GetDexFile();
537 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
538 const DexFile::MethodId& other_mid =
539 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800540 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800541 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800542 StringPiece name(GetName());
543 StringPiece other_name(other->GetName());
544 return name == other_name && GetSignature() == other->GetSignature();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800545 }
546 const DexFile::CodeItem* GetCodeItem() {
547 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
548 }
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800549 bool IsResolvedTypeIdx(uint16_t type_idx) const {
550 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
551 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800552 Class* GetClassFromTypeIdx(uint16_t type_idx) {
553 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
554 if (type == NULL) {
555 type = GetClassLinker()->ResolveType(type_idx, method_);
556 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
557 }
558 return type;
559 }
560 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
561 const DexFile& dex_file = GetDexFile();
562 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
563 }
564 Class* GetDexCacheResolvedType(uint16_t type_idx) {
565 return GetDexCache()->GetResolvedType(type_idx);
566 }
567 const DexFile& GetDexFile() {
568 const DexFile* result = dex_file_;
569 if (result == NULL) {
570 const DexCache* dex_cache = GetDexCache();
571 result = &GetClassLinker()->FindDexFile(dex_cache);
572 dex_file_ = result;
573 }
574 return *result;
575 }
576 private:
577 // Set the method_ field, for proxy methods looking up the interface method via the resolved
578 // methods table.
579 void SetMethod(const Method* method) {
580 if (method != NULL) {
581 Class* klass = method->GetDeclaringClass();
582 if (klass->IsProxyClass()) {
Ian Rogers19846512012-02-24 11:42:47 -0800583 Method* interface_method =
584 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
585 CHECK(interface_method != NULL);
586 CHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
587 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800588 }
589 }
590 method_ = method;
591 }
592 DexCache* GetDexCache() {
593 DexCache* result = dex_cache_;
594 if (result == NULL) {
595 Class* klass = method_->GetDeclaringClass();
596 result = klass->GetDexCache();
597 dex_cache_ = result;
598 }
599 return result;
600 }
601 ClassLinker* GetClassLinker() {
602 ClassLinker* result = class_linker_;
603 if (result == NULL) {
604 result = Runtime::Current()->GetClassLinker();
605 class_linker_ = result;
606 }
607 return result;
608 }
609
610 ClassLinker* class_linker_;
611 DexCache* dex_cache_;
612 const DexFile* dex_file_;
613 const Method* method_;
614 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800615 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800616
617 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
618};
619
620} // namespace art
621
622#endif // ART_SRC_OBJECT_UTILS_H_