blob: 8a7878933470a14046992f66a303e8e4dc3e30ca [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "src/dex_file.h"
4#include "src/heap.h"
5#include "src/globals.h"
6#include "src/logging.h"
7#include "src/object.h"
8#include "src/raw_dex_file.h"
9
10namespace art {
11
Carl Shapiro80d4dde2011-06-28 16:24:07 -070012DexFile* DexFile::OpenFile(const char* filename) {
13 RawDexFile* raw = RawDexFile::OpenFile(filename);
14 return Open(raw);
15}
16
17DexFile* DexFile::OpenBase64(const char* base64) {
18 RawDexFile* raw = RawDexFile::OpenBase64(base64);
19 return Open(raw);
20}
21
22DexFile* DexFile::Open(RawDexFile* raw) {
Carl Shapiro1fb86202011-06-27 17:43:13 -070023 if (raw == NULL) {
24 return NULL;
25 }
26 DexFile* dex_file = new DexFile(raw);
27 dex_file->Init();
28 return dex_file;
29}
30
31DexFile::~DexFile() {
Carl Shapiro1fb86202011-06-27 17:43:13 -070032 delete[] strings_;
33 delete[] classes_;
34 delete[] methods_;
35 delete[] fields_;
36}
37
38void DexFile::Init() {
39 num_strings_ = raw_->NumStringIds();
40 strings_ = new String*[num_strings_];
41
42 num_classes_ = raw_->NumTypeIds();
43 classes_ = new Class*[num_classes_];
44
45 num_methods_ = raw_->NumMethodIds();
46 methods_ = new Method*[num_methods_];
47
48 num_fields_ = raw_->NumFieldIds();
49 fields_ = new Field*[num_fields_];
50}
51
52Class* DexFile::LoadClass(const char* descriptor) {
53 const RawDexFile::ClassDef* class_def = raw_->FindClassDef(descriptor);
54 if (class_def == NULL) {
55 return NULL;
56 } else {
57 return LoadClass(*class_def);
58 }
59}
60
61Class* DexFile::LoadClass(const RawDexFile::ClassDef& class_def) {
62 const byte* class_data = raw_->GetClassData(class_def);
Carl Shapiro3ee755d2011-06-28 12:11:04 -070063 RawDexFile::ClassDataHeader header = raw_->ReadClassDataHeader(&class_data);
Carl Shapiro1fb86202011-06-27 17:43:13 -070064
65 const char* descriptor = raw_->GetClassDescriptor(class_def);
66 CHECK(descriptor != NULL);
67
68 // Allocate storage for the new class object.
69 size_t size = Class::Size(header.static_fields_size_);
70 Class* klass = Heap::AllocClass(size);
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071 CHECK(klass != NULL); // TODO: throw an OOME
Carl Shapiro1fb86202011-06-27 17:43:13 -070072
Carl Shapiro3ee755d2011-06-28 12:11:04 -070073 klass->klass_ = NULL; // TODO
Carl Shapiro1fb86202011-06-27 17:43:13 -070074 klass->descriptor_ = descriptor;
75 klass->access_flags_ = class_def.access_flags_;
76 klass->class_loader_ = NULL; // TODO
77 klass->dex_file_ = this;
78 klass->primitive_type_ = Class::kPrimNot;
Carl Shapiro894d0fa2011-06-30 14:48:49 -070079 klass->status_ = Class::kStatusIdx;
Carl Shapiro1fb86202011-06-27 17:43:13 -070080
81 klass->super_ = reinterpret_cast<Class*>(class_def.superclass_idx_);
Carl Shapiro894d0fa2011-06-30 14:48:49 -070082 klass->super_idx_ = class_def.superclass_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -070083
Carl Shapiro3ee755d2011-06-28 12:11:04 -070084 klass->num_sfields_ = header.static_fields_size_;
85 klass->num_ifields_ = header.instance_fields_size_;
86 klass->num_dmethods_ = header.direct_methods_size_;
87 klass->num_vmethods_ = header.virtual_methods_size_;
88
89 klass->source_file_ = raw_->dexGetSourceFile(class_def);
90
Carl Shapiro1fb86202011-06-27 17:43:13 -070091 // Load class interfaces.
Carl Shapiro3ee755d2011-06-28 12:11:04 -070092 LoadInterfaces(class_def, klass);
Carl Shapiro1fb86202011-06-27 17:43:13 -070093
94 // Load static fields.
Carl Shapiro3ee755d2011-06-28 12:11:04 -070095 if (klass->num_sfields_ != 0) {
96 uint32_t last_idx = 0;
97 for (size_t i = 0; i < klass->num_sfields_; ++i) {
98 RawDexFile::Field raw_field;
99 raw_->dexReadClassDataField(&class_data, &raw_field, &last_idx);
100 LoadField(klass, raw_field, &klass->sfields_[i]);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700101 }
102 }
103
104 // Load instance fields.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700105 if (klass->num_ifields_ != 0) {
106 // TODO: append instance fields to class object
107 klass->ifields_ = new IField[klass->num_ifields_];
108 uint32_t last_idx = 0;
109 for (size_t i = 0; i < klass->num_ifields_; ++i) {
110 RawDexFile::Field raw_field;
111 raw_->dexReadClassDataField(&class_data, &raw_field, &last_idx);
112 LoadField(klass, raw_field, &klass->ifields_[i]);
Carl Shapiro1fb86202011-06-27 17:43:13 -0700113 }
114 }
115
116 // Load direct methods.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700117 if (klass->num_dmethods_ != 0) {
118 // TODO: append direct methods to class object
119 klass->dmethods_ = new Method[klass->num_dmethods_];
120 uint32_t last_idx = 0;
121 for (size_t i = 0; i < klass->num_dmethods_; ++i) {
122 RawDexFile::Method raw_method;
123 raw_->dexReadClassDataMethod(&class_data, &raw_method, &last_idx);
124 LoadMethod(klass, raw_method, &klass->dmethods_[i]);
125 // TODO: register maps
126 }
127 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700128
129 // Load virtual methods.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700130 if (klass->num_vmethods_ != 0) {
131 // TODO: append virtual methods to class object
132 klass->vmethods_ = new Method[klass->num_vmethods_];
133 memset(klass->vmethods_, 0xff, sizeof(Method));
134 uint32_t last_idx = 0;
135 for (size_t i = 0; i < klass->num_vmethods_; ++i) {
136 RawDexFile::Method raw_method;
137 raw_->dexReadClassDataMethod(&class_data, &raw_method, &last_idx);
138 LoadMethod(klass, raw_method, &klass->vmethods_[i]);
139 // TODO: register maps
140 }
141 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700142
143 return klass;
144}
145
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700146void DexFile::LoadInterfaces(const RawDexFile::ClassDef& class_def,
147 Class* klass) {
Carl Shapiro1fb86202011-06-27 17:43:13 -0700148 const RawDexFile::TypeList* list = raw_->GetInterfacesList(class_def);
149 if (list != NULL) {
150 klass->interface_count_ = list->Size();
151 klass->interfaces_ = new Class*[list->Size()];
152 for (size_t i = 0; i < list->Size(); ++i) {
153 const RawDexFile::TypeItem& type_item = list->GetTypeItem(i);
154 klass->interfaces_[i] = reinterpret_cast<Class*>(type_item.type_idx_);
155 }
156 }
157}
158
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700159void DexFile::LoadField(Class* klass, const RawDexFile::Field& src,
160 Field* dst) {
161 const RawDexFile::FieldId& field_id = raw_->GetFieldId(src.field_idx_);
162 dst->klass_ = klass;
163 dst->name_ = raw_->dexStringById(field_id.name_idx_);
164 dst->signature_ = raw_->dexStringByTypeIdx(field_id.type_idx_);
165 dst->access_flags_ = src.access_flags_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700166}
167
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700168void DexFile::LoadMethod(Class* klass, const RawDexFile::Method& src,
Carl Shapiro1fb86202011-06-27 17:43:13 -0700169 Method* dst) {
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700170 const RawDexFile::MethodId& method_id = raw_->GetMethodId(src.method_idx_);
171 dst->klass_ = klass;
172 dst->name_ = raw_->dexStringById(method_id.name_idx_);
173 dst->dex_file_ = this;
174 dst->proto_idx_ = method_id.proto_idx_;
175 dst->shorty_ = raw_->GetShorty(method_id.proto_idx_);
176 dst->access_flags_ = src.access_flags_;
177
178 // TODO: check for finalize method
179
180 const RawDexFile::Code* code_item = raw_->GetCode(src);
181 if (code_item != NULL) {
182 dst->num_registers_ = code_item->registers_size_;
183 dst->num_ins_ = code_item->ins_size_;
184 dst->num_outs_ = code_item->outs_size_;
185 dst->insns_ = code_item->insns_;
186 } else {
187 uint16_t num_args = dst->NumArgRegisters();
188 if (!dst->IsStatic()) {
189 ++num_args;
190 }
191 dst->num_registers_ = dst->num_ins_ + num_args;
192 // TODO: native methods
193 }
Carl Shapiro1fb86202011-06-27 17:43:13 -0700194}
195
Carl Shapiro1fb86202011-06-27 17:43:13 -0700196} // namespace art