blob: a89baafed755ae152f311a43ba7b2d5635b4edc4 [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
12DexFile* DexFile::Open(const char* filename) {
13 RawDexFile* raw = RawDexFile::Open(filename);
14 if (raw == NULL) {
15 return NULL;
16 }
17 DexFile* dex_file = new DexFile(raw);
18 dex_file->Init();
19 return dex_file;
20}
21
22DexFile::~DexFile() {
23 delete raw_;
24 delete[] strings_;
25 delete[] classes_;
26 delete[] methods_;
27 delete[] fields_;
28}
29
30void DexFile::Init() {
31 num_strings_ = raw_->NumStringIds();
32 strings_ = new String*[num_strings_];
33
34 num_classes_ = raw_->NumTypeIds();
35 classes_ = new Class*[num_classes_];
36
37 num_methods_ = raw_->NumMethodIds();
38 methods_ = new Method*[num_methods_];
39
40 num_fields_ = raw_->NumFieldIds();
41 fields_ = new Field*[num_fields_];
42}
43
44Class* DexFile::LoadClass(const char* descriptor) {
45 const RawDexFile::ClassDef* class_def = raw_->FindClassDef(descriptor);
46 if (class_def == NULL) {
47 return NULL;
48 } else {
49 return LoadClass(*class_def);
50 }
51}
52
53Class* DexFile::LoadClass(const RawDexFile::ClassDef& class_def) {
54 const byte* class_data = raw_->GetClassData(class_def);
55 RawDexFile::ClassDataHeader header;
56 raw_->DecodeClassDataHeader(&header, class_data);
57
58 const char* descriptor = raw_->GetClassDescriptor(class_def);
59 CHECK(descriptor != NULL);
60
61 // Allocate storage for the new class object.
62 size_t size = Class::Size(header.static_fields_size_);
63 Class* klass = Heap::AllocClass(size);
64 CHECK(klass != NULL);
65
66 //klass->klass_ = NULL; // TODO
67 klass->descriptor_ = descriptor;
68 klass->access_flags_ = class_def.access_flags_;
69 klass->class_loader_ = NULL; // TODO
70 klass->dex_file_ = this;
71 klass->primitive_type_ = Class::kPrimNot;
72 klass->status_ = Class::kClassIdx;
73
74 klass->super_ = reinterpret_cast<Class*>(class_def.superclass_idx_);
75
76 // Load class interfaces.
77 LoadClassInterfaces(class_def, klass);
78
79 // Load static fields.
80 if (header.static_fields_size_ != 0) {
81 for (size_t i = 0; i < header.static_fields_size_; ++i) {
82 // TODO
83 }
84 }
85
86 // Load instance fields.
87 if (header.instance_fields_size_ != 0) {
88 for (size_t i = 0; i < header.instance_fields_size_; ++i) {
89 // TODO
90 }
91 }
92
93 // Load direct methods.
94
95 // Load virtual methods.
96
97 return klass;
98}
99
100void DexFile::LoadClassInterfaces(const RawDexFile::ClassDef& class_def,
101 Class* klass) {
102 const RawDexFile::TypeList* list = raw_->GetInterfacesList(class_def);
103 if (list != NULL) {
104 klass->interface_count_ = list->Size();
105 klass->interfaces_ = new Class*[list->Size()];
106 for (size_t i = 0; i < list->Size(); ++i) {
107 const RawDexFile::TypeItem& type_item = list->GetTypeItem(i);
108 klass->interfaces_[i] = reinterpret_cast<Class*>(type_item.type_idx_);
109 }
110 }
111}
112
113void DexFile::LoadSFields(Class* klass, const RawDexFile::Field* src,
114 Field* dst) {
115
116}
117
118void DexFile::LoadIFields(Class* klass, const RawDexFile::Field* src,
119 Field* dst) {
120}
121
122void DexFile::LoadMethod(Class* klass, const RawDexFile::Method* src,
123 Method* dst) {
124}
125
126
127
128} // namespace art