blob: 07650c2e9049acfa65630eeed177454aa6b2efc3 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_DEX_FILE_H_
4#define ART_SRC_DEX_FILE_H_
5
6#include "src/globals.h"
7#include "src/macros.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07008#include "src/raw_dex_file.h"
9
10namespace art {
11
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070012class Class;
13class Field;
14class Method;
15class String;
16
Carl Shapiro1fb86202011-06-27 17:43:13 -070017class DexFile {
18 public:
Carl Shapiro80d4dde2011-06-28 16:24:07 -070019 // Opens a .dex file from the file system. Returns NULL on failure.
20 static DexFile* OpenFile(const char* filename);
21
22 // Opens a .dex file from a base64 encoded array. Returns NULL on
23 // failure.
Carl Shapiroa506cb02011-06-28 22:53:46 -070024 // TODO: move this into the DexFile unit test
Carl Shapiro80d4dde2011-06-28 16:24:07 -070025 static DexFile* OpenBase64(const char* base64);
26
27 // Opens a .dex file from a RawDexFile. Takes ownership of the
28 // RawDexFile.
29 static DexFile* Open(RawDexFile* raw);
Carl Shapiro1fb86202011-06-27 17:43:13 -070030
31 // Close and deallocate.
32 ~DexFile();
33
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070034 size_t NumTypes() const {
Carl Shapiro1fb86202011-06-27 17:43:13 -070035 return num_classes_;
36 }
37
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070038 size_t NumMethods() const {
Carl Shapiro1fb86202011-06-27 17:43:13 -070039 return num_methods_;
40 }
41
42 Class* LoadClass(const char* descriptor);
43
44 Class* LoadClass(const RawDexFile::ClassDef& class_def);
45
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046 bool HasClass(const char* descriptor) {
47 return raw_->FindClassDef(descriptor) != NULL;
48 }
49
50 RawDexFile* GetRaw() const {
51 return raw_.get();
52 }
53
54 Class* GetResolvedClass(uint32_t class_idx) const {
55 return classes_[class_idx];
56 }
57
58 void SetResolvedClass(uint32_t class_idx, Class* resolved) {
59 classes_[class_idx] = resolved;
60 }
61
Carl Shapiro1fb86202011-06-27 17:43:13 -070062 private:
63 DexFile(RawDexFile* raw) : raw_(raw) {};
64
65 void Init();
66
Carl Shapiro3ee755d2011-06-28 12:11:04 -070067 void LoadInterfaces(const RawDexFile::ClassDef& class_def, Class *klass);
Carl Shapiro1fb86202011-06-27 17:43:13 -070068
Carl Shapiro3ee755d2011-06-28 12:11:04 -070069 void LoadField(Class* klass, const RawDexFile::Field& src, Field* dst);
Carl Shapiro1fb86202011-06-27 17:43:13 -070070
Carl Shapiro3ee755d2011-06-28 12:11:04 -070071 void LoadMethod(Class* klass, const RawDexFile::Method& src, Method* dst);
Carl Shapiro1fb86202011-06-27 17:43:13 -070072
73 // Table of contents for interned String objects.
74 String** strings_;
75 size_t num_strings_;
76
77 // Table of contents for Class objects.
78 Class** classes_;
79 size_t num_classes_;
80
81 // Table of contents for methods.
82 Method** methods_;
83 size_t num_methods_;
84
85 // Table of contents for fields.
86 Field** fields_;
87 size_t num_fields_;
88
89 // The size of the DEX file, in bytes.
90 size_t length_;
91
92 // The underlying dex file.
Carl Shapiro7e782482011-06-28 16:30:04 -070093 scoped_ptr<RawDexFile> raw_;
Carl Shapiro1fb86202011-06-27 17:43:13 -070094
95 DISALLOW_COPY_AND_ASSIGN(DexFile);
96};
97
98} // namespace art
99
100#endif // ART_SRC_DEX_FILE_H_