blob: 911d3ea00b94bf1ffdcd8ed85dd83ca66d054b35 [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
Brian Carlstrom6cc18452011-07-18 15:10:33 -070043 bool HasClass(const StringPiece& descriptor) {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044 return raw_->FindClassDef(descriptor) != NULL;
45 }
46
47 RawDexFile* GetRaw() const {
48 return raw_.get();
49 }
50
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070051 String* GetResolvedString(uint32_t string_idx) const {
Carl Shapiro565f5072011-07-10 13:39:43 -070052 CHECK_LT(string_idx, num_strings_);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070053 return strings_[string_idx];
54 }
55
56 void SetResolvedString(String* resolved, uint32_t string_idx) {
Carl Shapiro565f5072011-07-10 13:39:43 -070057 CHECK_LT(string_idx, num_strings_);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070058 strings_[string_idx] = resolved;
59 }
60
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070061 Class* GetResolvedClass(uint32_t class_idx) const {
Carl Shapiro565f5072011-07-10 13:39:43 -070062 CHECK_LT(class_idx, num_classes_);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070063 return classes_[class_idx];
64 }
65
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070066 void SetResolvedClass(Class* resolved, uint32_t class_idx) {
Carl Shapiro565f5072011-07-10 13:39:43 -070067 CHECK_LT(class_idx, num_classes_);
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 Shapiro1fb86202011-06-27 17:43:13 -070076 // Table of contents for interned String objects.
77 String** strings_;
78 size_t num_strings_;
79
80 // Table of contents for Class objects.
81 Class** classes_;
82 size_t num_classes_;
83
84 // Table of contents for methods.
85 Method** methods_;
86 size_t num_methods_;
87
88 // Table of contents for fields.
89 Field** fields_;
90 size_t num_fields_;
91
92 // The size of the DEX file, in bytes.
93 size_t length_;
94
95 // The underlying dex file.
Carl Shapiro7e782482011-06-28 16:30:04 -070096 scoped_ptr<RawDexFile> raw_;
Carl Shapiro1fb86202011-06-27 17:43:13 -070097
98 DISALLOW_COPY_AND_ASSIGN(DexFile);
99};
100
101} // namespace art
102
103#endif // ART_SRC_DEX_FILE_H_