blob: ae5101a5fd994183243dcd29506349cf6efceb3e [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
43 Class* LoadClass(const char* descriptor);
44
45 Class* LoadClass(const RawDexFile::ClassDef& class_def);
46
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 {
56 return strings_[string_idx];
57 }
58
59 void SetResolvedString(String* resolved, uint32_t string_idx) {
60 strings_[string_idx] = resolved;
61 }
62
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070063 Class* GetResolvedClass(uint32_t class_idx) const {
64 return classes_[class_idx];
65 }
66
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070067 void SetResolvedClass(Class* resolved, uint32_t class_idx) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070068 classes_[class_idx] = resolved;
69 }
70
Carl Shapiro1fb86202011-06-27 17:43:13 -070071 private:
72 DexFile(RawDexFile* raw) : raw_(raw) {};
73
74 void Init();
75
Carl Shapiro3ee755d2011-06-28 12:11:04 -070076 void LoadInterfaces(const RawDexFile::ClassDef& class_def, Class *klass);
Carl Shapiro1fb86202011-06-27 17:43:13 -070077
Carl Shapiro3ee755d2011-06-28 12:11:04 -070078 void LoadField(Class* klass, const RawDexFile::Field& src, Field* dst);
Carl Shapiro1fb86202011-06-27 17:43:13 -070079
Carl Shapiro3ee755d2011-06-28 12:11:04 -070080 void LoadMethod(Class* klass, const RawDexFile::Method& src, Method* dst);
Carl Shapiro1fb86202011-06-27 17:43:13 -070081
82 // Table of contents for interned String objects.
83 String** strings_;
84 size_t num_strings_;
85
86 // Table of contents for Class objects.
87 Class** classes_;
88 size_t num_classes_;
89
90 // Table of contents for methods.
91 Method** methods_;
92 size_t num_methods_;
93
94 // Table of contents for fields.
95 Field** fields_;
96 size_t num_fields_;
97
98 // The size of the DEX file, in bytes.
99 size_t length_;
100
101 // The underlying dex file.
Carl Shapiro7e782482011-06-28 16:30:04 -0700102 scoped_ptr<RawDexFile> raw_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700103
104 DISALLOW_COPY_AND_ASSIGN(DexFile);
105};
106
107} // namespace art
108
109#endif // ART_SRC_DEX_FILE_H_