blob: 8463672444f2e4012a1ac0846cf4be7c512dd77f [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;
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070016union JValue;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070017
Carl Shapiro1fb86202011-06-27 17:43:13 -070018class DexFile {
19 public:
Carl Shapiro80d4dde2011-06-28 16:24:07 -070020 // Opens a .dex file from the file system. Returns NULL on failure.
21 static DexFile* OpenFile(const char* filename);
22
23 // Opens a .dex file from a base64 encoded array. Returns NULL on
24 // failure.
Carl Shapiroa506cb02011-06-28 22:53:46 -070025 // TODO: move this into the DexFile unit test
Carl Shapiro80d4dde2011-06-28 16:24:07 -070026 static DexFile* OpenBase64(const char* base64);
27
28 // Opens a .dex file from a RawDexFile. Takes ownership of the
29 // RawDexFile.
30 static DexFile* Open(RawDexFile* raw);
Carl Shapiro1fb86202011-06-27 17:43:13 -070031
32 // Close and deallocate.
33 ~DexFile();
34
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035 size_t NumTypes() const {
Carl Shapiro1fb86202011-06-27 17:43:13 -070036 return num_classes_;
37 }
38
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070039 size_t NumMethods() const {
Carl Shapiro1fb86202011-06-27 17:43:13 -070040 return num_methods_;
41 }
42
Carl Shapiro565f5072011-07-10 13:39:43 -070043 bool LoadClass(const char* descriptor, Class* klass);
Carl Shapiro1fb86202011-06-27 17:43:13 -070044
Carl Shapiro565f5072011-07-10 13:39:43 -070045 bool LoadClass(const RawDexFile::ClassDef& class_def, Class* klass);
Carl Shapiro1fb86202011-06-27 17:43:13 -070046
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047 bool HasClass(const char* descriptor) {
48 return raw_->FindClassDef(descriptor) != NULL;
49 }
50
51 RawDexFile* GetRaw() const {
52 return raw_.get();
53 }
54
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070055 String* GetResolvedString(uint32_t string_idx) const {
Carl Shapiro565f5072011-07-10 13:39:43 -070056 CHECK_LT(string_idx, num_strings_);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070057 return strings_[string_idx];
58 }
59
60 void SetResolvedString(String* resolved, uint32_t string_idx) {
Carl Shapiro565f5072011-07-10 13:39:43 -070061 CHECK_LT(string_idx, num_strings_);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070062 strings_[string_idx] = resolved;
63 }
64
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070065 Class* GetResolvedClass(uint32_t class_idx) const {
Carl Shapiro565f5072011-07-10 13:39:43 -070066 CHECK_LT(class_idx, num_classes_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070067 return classes_[class_idx];
68 }
69
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070070 void SetResolvedClass(Class* resolved, uint32_t class_idx) {
Carl Shapiro565f5072011-07-10 13:39:43 -070071 CHECK_LT(class_idx, num_classes_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070072 classes_[class_idx] = resolved;
73 }
74
Carl Shapiro1fb86202011-06-27 17:43:13 -070075 private:
76 DexFile(RawDexFile* raw) : raw_(raw) {};
77
78 void Init();
79
Carl Shapiro3ee755d2011-06-28 12:11:04 -070080 void LoadInterfaces(const RawDexFile::ClassDef& class_def, Class *klass);
Carl Shapiro1fb86202011-06-27 17:43:13 -070081
Carl Shapiro3ee755d2011-06-28 12:11:04 -070082 void LoadField(Class* klass, const RawDexFile::Field& src, Field* dst);
Carl Shapiro1fb86202011-06-27 17:43:13 -070083
Carl Shapiro3ee755d2011-06-28 12:11:04 -070084 void LoadMethod(Class* klass, const RawDexFile::Method& src, Method* dst);
Carl Shapiro1fb86202011-06-27 17:43:13 -070085
86 // Table of contents for interned String objects.
87 String** strings_;
88 size_t num_strings_;
89
90 // Table of contents for Class objects.
91 Class** classes_;
92 size_t num_classes_;
93
94 // Table of contents for methods.
95 Method** methods_;
96 size_t num_methods_;
97
98 // Table of contents for fields.
99 Field** fields_;
100 size_t num_fields_;
101
102 // The size of the DEX file, in bytes.
103 size_t length_;
104
105 // The underlying dex file.
Carl Shapiro7e782482011-06-28 16:30:04 -0700106 scoped_ptr<RawDexFile> raw_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700107
108 DISALLOW_COPY_AND_ASSIGN(DexFile);
109};
110
111} // namespace art
112
113#endif // ART_SRC_DEX_FILE_H_