1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use std::collections::HashSet;
use std::io;
use std::io::{Read, Write};
use std::ops::Deref;

use attribute::*;
use class_access::*;
use constant_pool::*;
use field::*;
use method::*;
use parsing;
use writing;

/// A representation of a JVM class file.
///
/// For details on the format and structure of a JVM class file, see the
/// corresponding section of the Java Virtual Machine Specification.
///
/// https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-4.html
#[allow(clippy::vec_box)]
#[derive(Debug)]
#[derive(Eq)]
#[derive(PartialEq)]
pub struct ClassFile {
    pub minor_version: u16,
    pub major_version: u16,
    pub constant_pool: Vec<Box<ConstantPoolEntry>>,
    pub access_flags: HashSet<ClassAccess>,
    pub this_class: u16,
    pub super_class: u16,
    pub interfaces: Vec<u16>,
    pub fields: Vec<Field>,
    pub methods: Vec<Method>,
    pub attributes: Vec<Attribute>,
}

impl ClassFile {
    /// Parses the given class file. Fails if the given file is not a valid
    /// class file.
    ///
    /// ```
    /// # use std::fs::File;
    /// # use jvm_class_file_parser::ClassFile;
    /// #
    /// let mut file = File::open("classes/Dummy.class").unwrap();
    /// let class_file = ClassFile::from_file(&mut file).unwrap();
    /// ```
    pub fn from_file<R: Read>(file: &mut R) -> io::Result<ClassFile> {
        parsing::read_class_file(file)
    }

    pub fn to_file<W: Write>(&self, file: &mut W) -> io::Result<()> {
        writing::write_class_file(file, self)
    }

    /// Returns the name of the class file.
    ///
    /// ```
    /// # use std::fs::File;
    /// # use jvm_class_file_parser::ClassFile;
    /// #
    /// let mut file = File::open("classes/Dummy.class").unwrap();
    /// let class_file = ClassFile::from_file(&mut file).unwrap();
    ///
    /// assert_eq!("Dummy", class_file.get_class_name());
    /// ```
    pub fn get_class_name(&self) -> &str {
        use ConstantPoolEntry::*;

        let class = self.get_constant(self.this_class as usize);

        if let ConstantClass { name_index } = *class.deref() {
            let class_name = self.get_constant(name_index as usize);

            if let ConstantUtf8 { ref string } = *class_name.deref() {
                string
            } else {
                panic!("The \"name_index\" pointed to by \"this_class\" did not point to a ConstantUtf8. Found: {:?}", class_name.deref())
            }
        } else {
            panic!("The \"this_class\" did not point to a ConstantClass. Found: {:?}", class.deref())
        }
    }

    /// Returns the name of the source file that the class file was compiled
    /// from.
    ///
    /// If the class file does not have a `SourceFile` attribute, then a `None`
    /// option is returned.
    ///
    /// ```
    /// # use std::fs::File;
    /// # use jvm_class_file_parser::ClassFile;
    /// #
    /// let mut file = File::open("classes/Dummy.class").unwrap();
    /// let class_file = ClassFile::from_file(&mut file).unwrap();
    ///
    /// assert_eq!(Some("Dummy.java"), class_file.get_source_file_name());
    /// ```
    pub fn get_source_file_name(&self) -> Option<&str> {
        use ConstantPoolEntry::*;

        for attr in self.attributes.iter() {
            let name_constant = self.get_constant(attr.attribute_name_index as usize);

            if let ConstantUtf8 { ref string } = *name_constant.deref() {
                if string == "SourceFile" {
                    if attr.info.len() != 2 {
                        panic!("Incorrectly formatted SourceFile attribute. Expected info length of 2, found: {}", attr.info.len());
                    }

                    let info = [attr.info[0], attr.info[1]];
                    let source_file_index = u16::from_be_bytes(info);
                    let source_constant = self.get_constant(source_file_index as usize);

                    if let ConstantUtf8 { ref string } =
                        *source_constant.deref() { return Some(string) }
                    else {
                        panic!("The \"info\" of the \"SourceFile\" annotation did not point to a ConstantUtf8. Found: {:?}", source_constant.deref());
                    }
                }
            }
        }

        None
    }

    /// Returns a string representation of the specified Utf8 constant.
    ///
    /// ```
    /// # use std::fs::File;
    /// # use jvm_class_file_parser::ClassFile;
    /// #
    /// let mut file = File::open("classes/Dummy.class").unwrap();
    /// let class_file = ClassFile::from_file(&mut file).unwrap();
    ///
    /// assert_eq!("<init>", class_file.get_constant_utf8(4));
    /// ```
    pub fn get_constant_utf8(&self, index: usize) -> &str {
        use ConstantPoolEntry::*;

        let constant_utf8 = self.get_constant(index);

        if let ConstantUtf8 { ref string } = *constant_utf8.deref() {
            string
        } else {
            panic!("Failed to get constant \"#{}\" as a ConstantUtf8. Found: {:?}", index, constant_utf8)
        }
    }

    /// Returns a string representation of the specified class constant.
    ///
    /// ```
    /// # use std::fs::File;
    /// # use jvm_class_file_parser::ClassFile;
    /// #
    /// let mut file = File::open("classes/Dummy.class").unwrap();
    /// let class_file = ClassFile::from_file(&mut file).unwrap();
    ///
    /// assert_eq!("java/lang/Object", class_file.get_constant_class_str(3));
    /// ```
    pub fn get_constant_class_str(&self, index: usize) -> &str {
        use ConstantPoolEntry::*;

        let constant_class = self.get_constant(index);

        if let ConstantClass { name_index } = *constant_class.deref() {
            self.get_constant_utf8(name_index as usize)
        } else {
            panic!("Failed to get constant \"#{}\" as a ConstantClass. Found: {:?}", index, constant_class)
        }
    }

    /// Returns a string representation of the specified name and type
    /// constant.
    ///
    /// ```
    /// # use std::fs::File;
    /// # use jvm_class_file_parser::ClassFile;
    /// #
    /// let mut file = File::open("classes/Dummy.class").unwrap();
    /// let class_file = ClassFile::from_file(&mut file).unwrap();
    ///
    /// assert_eq!(
    ///     "\"<init>\":()V",
    ///     class_file.get_constant_name_and_type_str(10)
    /// );
    /// ```
    pub fn get_constant_name_and_type_str(&self, index: usize) -> String {
        use ConstantPoolEntry::*;

        let constant_nat = self.get_constant(index);

        if let ConstantNameAndType { name_index, descriptor_index } =
                *constant_nat.deref() {
            format!(
                "\"{}\":{}",
                self.get_constant_utf8(name_index as usize),
                self.get_constant_utf8(descriptor_index as usize),
            )
        } else {
            panic!("Failed to get constant \"#{}\" as a ConstantNameAndType. Found: {:?}", index, constant_nat)
        }
    }

    /// Returns the specified constant from the constant pool.
    ///
    /// This method exists in order to encapsulate the fact that the constant
    /// pool indexes start at 1 rather than 0.
    ///
    /// ```
    /// # use std::fs::File;
    /// # use std::ops::Deref;
    /// # use jvm_class_file_parser::ClassFile;
    /// # use jvm_class_file_parser::ConstantPoolEntry::*;
    /// #
    /// let mut file = File::open("classes/Dummy.class").unwrap();
    /// let class_file = ClassFile::from_file(&mut file).unwrap();
    ///
    /// assert_eq!(
    ///     ConstantClass {
    ///         name_index: 11,
    ///     },
    ///     *class_file.get_constant(2).deref()
    /// );
    /// ```
    #[allow(clippy::borrowed_box)]
    pub fn get_constant(&self, index: usize) -> &Box<ConstantPoolEntry> {
        &self.constant_pool[index - 1]
    }
}