Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 1 | // 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 Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 8 | #include "src/raw_dex_file.h" |
| 9 | |
| 10 | namespace art { |
| 11 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 12 | class Class; |
| 13 | class Field; |
| 14 | class Method; |
| 15 | class String; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame^] | 16 | union JValue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 17 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 18 | class DexFile { |
| 19 | public: |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 20 | // 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 Shapiro | a506cb0 | 2011-06-28 22:53:46 -0700 | [diff] [blame] | 25 | // TODO: move this into the DexFile unit test |
Carl Shapiro | 80d4dde | 2011-06-28 16:24:07 -0700 | [diff] [blame] | 26 | 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 Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 31 | |
| 32 | // Close and deallocate. |
| 33 | ~DexFile(); |
| 34 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 35 | size_t NumTypes() const { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 36 | return num_classes_; |
| 37 | } |
| 38 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 39 | size_t NumMethods() const { |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 40 | return num_methods_; |
| 41 | } |
| 42 | |
| 43 | Class* LoadClass(const char* descriptor); |
| 44 | |
| 45 | Class* LoadClass(const RawDexFile::ClassDef& class_def); |
| 46 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 47 | bool HasClass(const char* descriptor) { |
| 48 | return raw_->FindClassDef(descriptor) != NULL; |
| 49 | } |
| 50 | |
| 51 | RawDexFile* GetRaw() const { |
| 52 | return raw_.get(); |
| 53 | } |
| 54 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame^] | 55 | 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 Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 63 | Class* GetResolvedClass(uint32_t class_idx) const { |
| 64 | return classes_[class_idx]; |
| 65 | } |
| 66 | |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame^] | 67 | void SetResolvedClass(Class* resolved, uint32_t class_idx) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 68 | classes_[class_idx] = resolved; |
| 69 | } |
| 70 | |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 71 | private: |
| 72 | DexFile(RawDexFile* raw) : raw_(raw) {}; |
| 73 | |
| 74 | void Init(); |
| 75 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 76 | void LoadInterfaces(const RawDexFile::ClassDef& class_def, Class *klass); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 77 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 78 | void LoadField(Class* klass, const RawDexFile::Field& src, Field* dst); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 79 | |
Carl Shapiro | 3ee755d | 2011-06-28 12:11:04 -0700 | [diff] [blame] | 80 | void LoadMethod(Class* klass, const RawDexFile::Method& src, Method* dst); |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 81 | |
| 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 Shapiro | 7e78248 | 2011-06-28 16:30:04 -0700 | [diff] [blame] | 102 | scoped_ptr<RawDexFile> raw_; |
Carl Shapiro | 1fb8620 | 2011-06-27 17:43:13 -0700 | [diff] [blame] | 103 | |
| 104 | DISALLOW_COPY_AND_ASSIGN(DexFile); |
| 105 | }; |
| 106 | |
| 107 | } // namespace art |
| 108 | |
| 109 | #endif // ART_SRC_DEX_FILE_H_ |