blob: cb963cc99b3822c40fb45d8ae50bb09dd72622b7 [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) {
Brian Carlstrom93235f72012-03-29 22:48:15 -070074 CHECK(new_c != NULL) << "klass_=" << HexDump(klass_, sizeof(Class), true);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080075 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() {
Brian Carlstrom93235f72012-03-29 22:48:15 -070090 CHECK(klass_ != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -080091 if (UNLIKELY(klass_->IsArrayClass())) {
92 return GetArrayDescriptor();
93 } else if (UNLIKELY(klass_->IsPrimitive())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080094 return Primitive::Descriptor(klass_->GetPrimitiveType());
Ian Rogersa68a1cb2012-01-10 10:34:36 -080095 } else if (UNLIKELY(klass_->IsProxyClass())) {
Elliott Hughes91250e02011-12-13 22:30:35 -080096 descriptor_ = GetClassLinker()->GetDescriptorForProxy(klass_);
97 return descriptor_.c_str();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080098 } else {
99 const DexFile& dex_file = GetDexFile();
100 const DexFile::TypeId& type_id = dex_file.GetTypeId(klass_->GetDexTypeIndex());
101 return dex_file.GetTypeDescriptor(type_id);
102 }
103 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800104
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800105 const char* GetArrayDescriptor() {
106 std::string result("[");
107 const Class* saved_klass = klass_;
Brian Carlstrom93235f72012-03-29 22:48:15 -0700108 CHECK(saved_klass != NULL);
Ian Rogersa68a1cb2012-01-10 10:34:36 -0800109 ChangeClass(klass_->GetComponentType());
110 result += GetDescriptor();
111 ChangeClass(saved_klass);
112 descriptor_ = result;
113 return descriptor_.c_str();
114 }
115
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800116 const DexFile::ClassDef* GetClassDef() {
117 const DexFile::ClassDef* result = class_def_;
118 if (result == NULL) {
119 result = GetDexFile().FindClassDef(GetDescriptor());
120 class_def_ = result;
121 }
122 return result;
123 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800124
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800125 uint32_t NumInterfaces() {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700126 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800127 if (klass_->IsPrimitive()) {
128 return 0;
129 } else if (klass_->IsArrayClass()) {
130 return 2;
Ian Rogersc2b44472011-12-14 21:17:17 -0800131 } else if (klass_->IsProxyClass()) {
132 return klass_->GetIfTable()->GetLength();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800133 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800134 const DexFile::TypeList* interfaces = GetInterfaceTypeList();
135 if (interfaces == NULL) {
136 return 0;
137 } else {
138 return interfaces->Size();
139 }
140 }
141 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800142
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800143 uint16_t GetInterfaceTypeIdx(uint32_t idx) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700144 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800145 DCHECK(!klass_->IsPrimitive());
146 DCHECK(!klass_->IsArrayClass());
147 return GetInterfaceTypeList()->GetTypeItem(idx).type_idx_;
148 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800149
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800150 Class* GetInterface(uint32_t idx) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700151 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800152 DCHECK(!klass_->IsPrimitive());
153 if (klass_->IsArrayClass()) {
154 if (idx == 0) {
155 return GetClassLinker()->FindSystemClass("Ljava/lang/Cloneable;");
156 } else {
157 DCHECK_EQ(1U, idx);
158 return GetClassLinker()->FindSystemClass("Ljava/io/Serializable;");
159 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800160 } else if (klass_->IsProxyClass()) {
161 return klass_->GetIfTable()->Get(idx)->GetInterface();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800162 } else {
163 uint16_t type_idx = GetInterfaceTypeIdx(idx);
164 Class* interface = GetDexCache()->GetResolvedType(type_idx);
165 if (interface == NULL) {
166 interface = GetClassLinker()->ResolveType(GetDexFile(), type_idx, klass_);
167 CHECK(interface != NULL || Thread::Current()->IsExceptionPending());
168 }
169 return interface;
170 }
171 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800172
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800173 const char* GetSourceFile() {
Elliott Hughes95572412011-12-13 18:14:20 -0800174 std::string descriptor(GetDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800175 const DexFile& dex_file = GetDexFile();
176 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800177 CHECK(dex_class_def != NULL);
178 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800179 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800180
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800181 std::string GetLocation() {
182 return GetDexCache()->GetLocation()->ToModifiedUtf8();
183 }
184
185 const DexFile& GetDexFile() {
186 const DexFile* result = dex_file_;
187 if (result == NULL) {
188 const DexCache* dex_cache = GetDexCache();
189 result = &GetClassLinker()->FindDexFile(dex_cache);
190 dex_file_ = result;
191 }
192 return *result;
193 }
194
195 private:
196 const DexFile::TypeList* GetInterfaceTypeList() {
197 const DexFile::TypeList* result = interface_type_list_;
198 if (result == NULL) {
199 const DexFile::ClassDef* class_def = GetClassDef();
200 if (class_def != NULL) {
201 result = GetDexFile().GetInterfacesList(*class_def);
202 interface_type_list_ = result;
203 }
204 }
205 return result;
206 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800207
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800208 DexCache* GetDexCache() {
209 DexCache* result = dex_cache_;
210 if (result == NULL) {
Brian Carlstrom93235f72012-03-29 22:48:15 -0700211 DCHECK(klass_ != NULL);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800212 result = klass_->GetDexCache();
213 dex_cache_ = result;
214 }
215 return result;
216 }
Elliott Hughes91250e02011-12-13 22:30:35 -0800217
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800218 ClassLinker* GetClassLinker() {
219 ClassLinker* result = class_linker_;
220 if (result == NULL) {
221 result = Runtime::Current()->GetClassLinker();
222 class_linker_ = result;
223 }
224 return result;
225 }
226
227 const DexFile::ClassDef* class_def_;
228 ClassLinker* class_linker_;
229 DexCache* dex_cache_;
230 const DexFile* dex_file_;
231 const DexFile::TypeList* interface_type_list_;
232 const Class* klass_;
Elliott Hughes91250e02011-12-13 22:30:35 -0800233 std::string descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800234
235 DISALLOW_COPY_AND_ASSIGN(ClassHelper);
236};
237
238class FieldHelper {
239 public:
240 FieldHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(NULL) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800241 explicit FieldHelper(const Field* f) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), field_(f) {}
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800242 FieldHelper(const Field* f, ClassLinker* l) : class_linker_(l), dex_cache_(NULL), dex_file_(NULL),
243 field_(f) {}
244
245 void ChangeField(const Field* new_f) {
246 DCHECK(new_f != NULL);
247 if (dex_cache_ != NULL) {
248 DexCache* new_f_dex_cache = new_f->GetDeclaringClass()->GetDexCache();
249 if (new_f_dex_cache != dex_cache_) {
250 dex_cache_ = new_f_dex_cache;
251 dex_file_ = NULL;
252 }
253 }
254 field_ = new_f;
255 }
256 const char* GetName() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800257 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700258 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800259 const DexFile& dex_file = GetDexFile();
260 return dex_file.GetFieldName(dex_file.GetFieldId(field_index));
261 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800262 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700263 DCHECK_LT(field_index, 2U);
264 return field_index == 0 ? "interfaces" : "throws";
Ian Rogersc2b44472011-12-14 21:17:17 -0800265 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800266 }
267 String* GetNameAsString() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800268 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700269 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800270 const DexFile& dex_file = GetDexFile();
271 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
272 return GetClassLinker()->ResolveString(dex_file, field_id.name_idx_, GetDexCache());
273 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700274 return Runtime::Current()->GetInternTable()->InternStrong(GetName());
Ian Rogersc2b44472011-12-14 21:17:17 -0800275 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800276 }
277 Class* GetType() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800278 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700279 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800280 const DexFile& dex_file = GetDexFile();
281 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
282 Class* type = GetDexCache()->GetResolvedType(field_id.type_idx_);
283 if (type == NULL) {
284 type = GetClassLinker()->ResolveType(field_id.type_idx_, field_);
285 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
286 }
287 return type;
288 } else {
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700289 return GetClassLinker()->FindSystemClass(GetTypeDescriptor());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800290 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800291 }
292 const char* GetTypeDescriptor() {
Ian Rogersc2b44472011-12-14 21:17:17 -0800293 uint32_t field_index = field_->GetDexFieldIndex();
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700294 if (!field_->GetDeclaringClass()->IsProxyClass()) {
Ian Rogersc2b44472011-12-14 21:17:17 -0800295 const DexFile& dex_file = GetDexFile();
296 const DexFile::FieldId& field_id = dex_file.GetFieldId(field_index);
297 return dex_file.GetFieldTypeDescriptor(field_id);
298 } else {
Ian Rogersc2b44472011-12-14 21:17:17 -0800299 DCHECK(field_->IsStatic());
Elliott Hughes2ed52c42012-03-21 16:56:56 -0700300 DCHECK_LT(field_index, 2U);
301 // 0 == Class[] interfaces; 1 == Class[][] throws;
302 return field_index == 0 ? "[Ljava/lang/Class;" : "[[Ljava/lang/Class;";
Ian Rogersc2b44472011-12-14 21:17:17 -0800303 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800304 }
305 Primitive::Type GetTypeAsPrimitiveType() {
306 return Primitive::GetType(GetTypeDescriptor()[0]);
307 }
308 bool IsPrimitiveType() {
309 Primitive::Type type = GetTypeAsPrimitiveType();
310 return type != Primitive::kPrimNot;
311 }
312 size_t FieldSize() {
313 Primitive::Type type = GetTypeAsPrimitiveType();
314 return Primitive::FieldSize(type);
315 }
Ian Rogersc2b44472011-12-14 21:17:17 -0800316
317 // The returned const char* is only guaranteed to be valid for the lifetime of the FieldHelper.
318 // If you need it longer, copy it into a std::string.
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800319 const char* GetDeclaringClassDescriptor() {
320 uint16_t type_idx = field_->GetDeclaringClass()->GetDexTypeIndex();
Ian Rogersc2b44472011-12-14 21:17:17 -0800321 if (type_idx != DexFile::kDexNoIndex16) {
322 const DexFile& dex_file = GetDexFile();
323 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
324 } else {
325 // Most likely a proxy class
326 ClassHelper kh(field_->GetDeclaringClass());
327 declaring_class_descriptor_ = kh.GetDescriptor();
328 return declaring_class_descriptor_.c_str();
329 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800330 }
331
332 private:
333 DexCache* GetDexCache() {
334 DexCache* result = dex_cache_;
335 if (result == NULL) {
336 result = field_->GetDeclaringClass()->GetDexCache();
337 dex_cache_ = result;
338 }
339 return result;
340 }
341 ClassLinker* GetClassLinker() {
342 ClassLinker* result = class_linker_;
343 if (result == NULL) {
344 result = Runtime::Current()->GetClassLinker();
345 class_linker_ = result;
346 }
347 return result;
348 }
349 const DexFile& GetDexFile() {
350 const DexFile* result = dex_file_;
351 if (result == NULL) {
352 const DexCache* dex_cache = GetDexCache();
353 result = &GetClassLinker()->FindDexFile(dex_cache);
354 dex_file_ = result;
355 }
356 return *result;
357 }
358
359 ClassLinker* class_linker_;
360 DexCache* dex_cache_;
361 const DexFile* dex_file_;
362 const Field* field_;
Ian Rogersc2b44472011-12-14 21:17:17 -0800363 std::string declaring_class_descriptor_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800364
365 DISALLOW_COPY_AND_ASSIGN(FieldHelper);
366};
367
368class MethodHelper {
369 public:
370 MethodHelper() : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL), method_(NULL),
371 shorty_(NULL), shorty_len_(0) {}
Elliott Hughesff17f1f2012-01-24 18:12:29 -0800372 explicit MethodHelper(const Method* m) : class_linker_(NULL), dex_cache_(NULL), dex_file_(NULL),
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800373 method_(NULL), shorty_(NULL), shorty_len_(0) {
374 SetMethod(m);
375 }
376 MethodHelper(const Method* m, ClassLinker* l) : class_linker_(l), dex_cache_(NULL),
377 dex_file_(NULL), method_(NULL), shorty_(NULL), shorty_len_(0) {
378 SetMethod(m);
379 }
380
381 void ChangeMethod(Method* new_m) {
382 DCHECK(new_m != NULL);
383 if (dex_cache_ != NULL) {
384 Class* klass = new_m->GetDeclaringClass();
385 if (klass->IsProxyClass()) {
386 dex_cache_ = NULL;
387 dex_file_ = NULL;
388 } else {
389 DexCache* new_m_dex_cache = klass->GetDexCache();
390 if (new_m_dex_cache != dex_cache_) {
391 dex_cache_ = new_m_dex_cache;
392 dex_file_ = NULL;
393 }
394 }
395 }
396 SetMethod(new_m);
397 shorty_ = NULL;
398 }
399 const char* GetName() {
400 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800401 uint32_t dex_method_idx = method_->GetDexMethodIndex();
402 if (dex_method_idx != DexFile::kDexNoIndex16) {
403 return dex_file.GetMethodName(dex_file.GetMethodId(dex_method_idx));
404 } else {
405 Runtime* runtime = Runtime::Current();
406 if (method_ == runtime->GetResolutionMethod()) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700407 return "<runtime internal resolution method>";
Ian Rogers19846512012-02-24 11:42:47 -0800408 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kSaveAll)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700409 return "<runtime internal callee-save all registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800410 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsOnly)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700411 return "<runtime internal callee-save reference registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800412 } else if (method_ == runtime->GetCalleeSaveMethod(Runtime::kRefsAndArgs)) {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700413 return "<runtime internal callee-save reference and argument registers method>";
Ian Rogers19846512012-02-24 11:42:47 -0800414 } else {
Elliott Hughes81ff3182012-03-23 20:35:56 -0700415 return "<unknown runtime internal method>";
Ian Rogers19846512012-02-24 11:42:47 -0800416 }
417 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800418 }
419 String* GetNameAsString() {
420 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800421 uint32_t dex_method_idx = method_->GetDexMethodIndex();
422 const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800423 return GetClassLinker()->ResolveString(dex_file, method_id.name_idx_, GetDexCache());
424 }
425 const char* GetShorty() {
426 const char* result = shorty_;
427 if (result == NULL) {
428 const DexFile& dex_file = GetDexFile();
429 result = dex_file.GetMethodShorty(dex_file.GetMethodId(method_->GetDexMethodIndex()),
430 &shorty_len_);
431 shorty_ = result;
432 }
433 return result;
434 }
Elliott Hughes45651fd2012-02-21 15:48:20 -0800435 uint32_t GetShortyLength() {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800436 if (shorty_ == NULL) {
437 GetShorty();
438 }
439 return shorty_len_;
440 }
441 const std::string GetSignature() {
442 const DexFile& dex_file = GetDexFile();
Ian Rogers19846512012-02-24 11:42:47 -0800443 uint32_t dex_method_idx = method_->GetDexMethodIndex();
444 if (dex_method_idx != DexFile::kDexNoIndex16) {
445 return dex_file.GetMethodSignature(dex_file.GetMethodId(dex_method_idx));
446 } else {
447 return "<no signature>";
448 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800449 }
450 const DexFile::ProtoId& GetPrototype() {
451 const DexFile& dex_file = GetDexFile();
452 return dex_file.GetMethodPrototype(dex_file.GetMethodId(method_->GetDexMethodIndex()));
453 }
454 const DexFile::TypeList* GetParameterTypeList() {
455 const DexFile::ProtoId& proto = GetPrototype();
456 return GetDexFile().GetProtoParameters(proto);
457 }
458 ObjectArray<Class>* GetParameterTypes() {
459 const DexFile::TypeList* params = GetParameterTypeList();
460 Class* array_class = GetClassLinker()->FindSystemClass("[Ljava/lang/Class;");
461 uint32_t num_params = params == NULL ? 0 : params->Size();
462 ObjectArray<Class>* result = ObjectArray<Class>::Alloc(array_class, num_params);
463 for (uint32_t i = 0; i < num_params; i++) {
464 Class* param_type = GetClassFromTypeIdx(params->GetTypeItem(i).type_idx_);
jeffhao441d9122012-03-21 17:29:10 -0700465 if (param_type == NULL) {
466 DCHECK(Thread::Current()->IsExceptionPending());
467 return NULL;
468 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800469 result->Set(i, param_type);
470 }
471 return result;
472 }
473 Class* GetReturnType() {
474 const DexFile& dex_file = GetDexFile();
475 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
476 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
477 uint16_t return_type_idx = proto_id.return_type_idx_;
478 return GetClassFromTypeIdx(return_type_idx);
479 }
480 const char* GetReturnTypeDescriptor() {
481 const DexFile& dex_file = GetDexFile();
482 const DexFile::MethodId& method_id = dex_file.GetMethodId(method_->GetDexMethodIndex());
483 const DexFile::ProtoId& proto_id = dex_file.GetMethodPrototype(method_id);
484 uint16_t return_type_idx = proto_id.return_type_idx_;
485 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(return_type_idx));
486 }
487 int32_t GetLineNumFromNativePC(uintptr_t raw_pc) {
488 const DexFile& dex_file = GetDexFile();
489 return dex_file.GetLineNumFromPC(method_, method_->ToDexPC(raw_pc));
490 }
491 const char* GetDeclaringClassDescriptor() {
492 Class* klass = method_->GetDeclaringClass();
Ian Rogersc2b44472011-12-14 21:17:17 -0800493 DCHECK(!klass->IsProxyClass());
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800494 uint16_t type_idx = klass->GetDexTypeIndex();
495 const DexFile& dex_file = GetDexFile();
496 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
497 }
498 const char* GetDeclaringClassSourceFile() {
499 const char* descriptor = GetDeclaringClassDescriptor();
500 const DexFile& dex_file = GetDexFile();
501 const DexFile::ClassDef* dex_class_def = dex_file.FindClassDef(descriptor);
Elliott Hughes12c51e32012-01-17 20:25:05 -0800502 CHECK(dex_class_def != NULL);
503 return dex_file.GetSourceFile(*dex_class_def);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800504 }
505 bool IsStatic() {
506 return method_->IsStatic();
507 }
508 bool IsClassInitializer() {
509 return IsStatic() && StringPiece(GetName()) == "<clinit>";
510 }
511 size_t NumArgs() {
512 // "1 +" because the first in Args is the receiver.
513 // "- 1" because we don't count the return type.
514 return (IsStatic() ? 0 : 1) + GetShortyLength() - 1;
515 }
516 // Is the specified parameter a long or double, where parameter 0 is 'this' for instance methods
517 bool IsParamALongOrDouble(size_t param) {
518 CHECK_LT(param, NumArgs());
519 if (IsStatic()) {
520 param++; // 0th argument must skip return value at start of the shorty
521 } else if (param == 0) {
522 return false; // this argument
523 }
524 char ch = GetShorty()[param];
525 return (ch == 'J' || ch == 'D');
526 }
527 // Is the specified parameter a reference, where parameter 0 is 'this' for instance methods
528 bool IsParamAReference(size_t param) {
529 CHECK_LT(param, NumArgs());
530 if (IsStatic()) {
531 param++; // 0th argument must skip return value at start of the shorty
532 } else if (param == 0) {
533 return true; // this argument
534 }
535 return GetShorty()[param] == 'L'; // An array also has a shorty character of 'L' (not '[')
536 }
537 bool HasSameNameAndSignature(MethodHelper* other) {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800538 if (GetDexCache() == other->GetDexCache()) {
539 const DexFile& dex_file = GetDexFile();
540 const DexFile::MethodId& mid = dex_file.GetMethodId(method_->GetDexMethodIndex());
541 const DexFile::MethodId& other_mid =
542 dex_file.GetMethodId(other->method_->GetDexMethodIndex());
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800543 return mid.name_idx_ == other_mid.name_idx_ && mid.proto_idx_ == other_mid.proto_idx_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800544 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -0800545 StringPiece name(GetName());
546 StringPiece other_name(other->GetName());
547 return name == other_name && GetSignature() == other->GetSignature();
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800548 }
549 const DexFile::CodeItem* GetCodeItem() {
550 return GetDexFile().GetCodeItem(method_->GetCodeItemOffset());
551 }
Ian Rogers6f1dfe42011-12-08 17:28:34 -0800552 bool IsResolvedTypeIdx(uint16_t type_idx) const {
553 return method_->GetDexCacheResolvedTypes()->Get(type_idx) != NULL;
554 }
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800555 Class* GetClassFromTypeIdx(uint16_t type_idx) {
556 Class* type = method_->GetDexCacheResolvedTypes()->Get(type_idx);
557 if (type == NULL) {
558 type = GetClassLinker()->ResolveType(type_idx, method_);
559 CHECK(type != NULL || Thread::Current()->IsExceptionPending());
560 }
561 return type;
562 }
563 const char* GetTypeDescriptorFromTypeIdx(uint16_t type_idx) {
564 const DexFile& dex_file = GetDexFile();
565 return dex_file.GetTypeDescriptor(dex_file.GetTypeId(type_idx));
566 }
567 Class* GetDexCacheResolvedType(uint16_t type_idx) {
568 return GetDexCache()->GetResolvedType(type_idx);
569 }
570 const DexFile& GetDexFile() {
571 const DexFile* result = dex_file_;
572 if (result == NULL) {
573 const DexCache* dex_cache = GetDexCache();
574 result = &GetClassLinker()->FindDexFile(dex_cache);
575 dex_file_ = result;
576 }
577 return *result;
578 }
579 private:
580 // Set the method_ field, for proxy methods looking up the interface method via the resolved
581 // methods table.
582 void SetMethod(const Method* method) {
583 if (method != NULL) {
584 Class* klass = method->GetDeclaringClass();
585 if (klass->IsProxyClass()) {
Ian Rogers19846512012-02-24 11:42:47 -0800586 Method* interface_method =
587 method->GetDexCacheResolvedMethods()->Get(method->GetDexMethodIndex());
588 CHECK(interface_method != NULL);
589 CHECK(interface_method == GetClassLinker()->FindMethodForProxy(klass, method));
590 method = interface_method;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800591 }
592 }
593 method_ = method;
594 }
595 DexCache* GetDexCache() {
596 DexCache* result = dex_cache_;
597 if (result == NULL) {
598 Class* klass = method_->GetDeclaringClass();
599 result = klass->GetDexCache();
600 dex_cache_ = result;
601 }
602 return result;
603 }
604 ClassLinker* GetClassLinker() {
605 ClassLinker* result = class_linker_;
606 if (result == NULL) {
607 result = Runtime::Current()->GetClassLinker();
608 class_linker_ = result;
609 }
610 return result;
611 }
612
613 ClassLinker* class_linker_;
614 DexCache* dex_cache_;
615 const DexFile* dex_file_;
616 const Method* method_;
617 const char* shorty_;
Elliott Hughes45651fd2012-02-21 15:48:20 -0800618 uint32_t shorty_len_;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800619
620 DISALLOW_COPY_AND_ASSIGN(MethodHelper);
621};
622
623} // namespace art
624
625#endif // ART_SRC_OBJECT_UTILS_H_