class_rs/enums/
mod.rs

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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
use crate::structs::{
    Annotation, BootstrapMethod, InnerClass, LineNumber, LocalVar, LocalVariable,
    LocalVariableType, MethodParameter, ModuleExports, ModuleOpens, ModuleProvides, ModuleRequires,
    RecordComponent, StackMapFrame, TypeAnnotation,
};

mod instructions;
pub use instructions::Instruction;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AccessFlag {
    /// - class: Declared abstract; must not be instantiated.
    /// - inner class: Marked or implicitly abstract in source.
    /// - method: Declared abstract; no implementation is provided.
    Abstract,
    /// - class: Declared as an annotation type.
    /// - inner class: Declared as an annotation type.
    Annotation,
    /// - method: A bridge method, generated by the compiler.
    Bridge,
    /// - class: Declared as an enum type.
    /// - inner class: Declared as an enum type.
    /// - field: Declared as an element of an enum.
    Enum,
    /// - class: Declared final; no subclasses allowed.
    /// - inner class: Marked final in source.
    /// - field: Declared final; never directly assigned to after object construction.
    /// - method: Declared final; must not be overridden
    /// - formal parameter: Indicates that the formal parameter was declared final.
    Final,
    /// - class: Is an interface, not a class.
    /// - inner class: Was an interface in source.
    Interface,
    /// - formal parameter: Indicates that the formal parameter was implicitly declared in source code, according to the specification of the language in which the source code was written.
    /// - module: Indicates that this module was implicitly declared.
    /// - module requires/exports/opens flag: Indicates that this dependence was implicitly declared in the source of the module declaration.
    Mandated,
    /// - class: Is a module, not a class or interface.
    Module,
    /// - method: Declared native; implemented in a language other than Java.
    Native,
    /// - module: Indicates that this module is open.
    Open,
    /// - class: Declared public; may be accessed from outside its package.
    /// - inner class: Marked or implicitly public in source.
    /// - field, method: Declared public; may be accessed from outside its package.
    Public,
    /// - inner class: Marked private in source.
    /// - field, method: Declared private; accessible only within the defining class.
    Private,
    /// - inner class: Marked protected in source.
    /// - field, method: Declared protected; may be accessed within subclasses.
    Protected,
    /// - inner class: Marked or implicitly static in source.
    /// - field, method: Declared static.
    Static,
    /// - module requires flag: Indicates that this dependence is mandatory in the static phase, i.e., at compile time, but is optional in the dynamic phase, i.e., at run time.
    StaticPhase,
    /// - method: Declared strictfp; floating-point mode is FP-strict.
    Strict,
    /// - class: Treat superclass methods specially when invoked by the invokespecial instruction.
    Super,
    /// - method: Declared synchronized; invocation is wrapped by a monitor use.
    Synchronized,
    /// Declared synthetic; not present in the source code.
    Synthetic,
    /// - field: Declared transient; not written or read by a persistent object manager.
    Transient,
    /// - module requires flag: Indicates that any module which depends on the current module, implicitly declares a dependence on the module indicated by this entry.
    Transitive,
    /// - method: Declared with variable number of arguments.
    VarArgs,
    /// - field: Declared volatile; cannot be cached.
    Volatile,
}

#[derive(Debug)]
pub enum Constant {
    Class {
        name_index: u16,
    },
    Double(f64),
    Dynamic {
        bootstrap_method_attr_index: u16,
        name_and_type_index: u16,
    },
    Fieldref {
        class_index: u16,
        name_and_type_index: u16,
    },
    Float(f32),
    Integer(i32),
    InterfaceMethodref {
        class_index: u16,
        name_and_type_index: u16,
    },
    /// Used for the first entry in the constant pool, and the second entry of doubles and longs.
    Invalid,
    InvokeDynamic {
        bootstrap_method_attr_index: u16,
        name_and_type_index: u16,
    },
    Long(i64),
    MethodHandle {
        reference_kind: u8,
        reference_index: u16,
    },
    Methodref {
        class_index: u16,
        name_and_type_index: u16,
    },
    MethodType {
        descriptor_index: u16,
    },
    Module {
        name_index: u16,
    },
    NameAndType {
        name_index: u16,
        descriptor_index: u16,
    },
    Package {
        name_index: u16,
    },
    String {
        string_index: u16,
    },
    /// ⚠️ It is using Rust's String type and not the JVM's modified UTF-8. If you have a string that makes that crate panic, open an issue.
    Utf8(String),
}

impl std::fmt::Display for Constant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            Constant::Class { name_index } => write!(f, "Constant::Class #{name_index}"),
            Constant::Double(double) => write!(f, "Constant::Double {double}"),
            Constant::Dynamic {
                bootstrap_method_attr_index,
                name_and_type_index,
            } => write!(
                f,
                "Constant::Dynamic #{bootstrap_method_attr_index}, #{name_and_type_index}"
            ),
            Constant::Fieldref {
                class_index,
                name_and_type_index,
            } => write!(
                f,
                "Constant::Fieldref #{class_index}, #{name_and_type_index}"
            ),
            Constant::Float(float) => write!(f, "Constant::Float {float}"),
            Constant::Integer(int) => write!(f, "Constant::Integer {int}"),
            Constant::InterfaceMethodref {
                class_index,
                name_and_type_index,
            } => write!(
                f,
                "Constant::InterfaceMethodref #{class_index}, #{name_and_type_index}"
            ),
            Constant::Invalid => write!(f, "Constant::Invalid"),
            Constant::InvokeDynamic {
                bootstrap_method_attr_index,
                name_and_type_index,
            } => write!(
                f,
                "Constant::InvokeDynamic #{bootstrap_method_attr_index}, #{name_and_type_index}"
            ),
            Constant::Long(long) => write!(f, "Constant::Long {long}"),
            Constant::MethodHandle {
                reference_kind,
                reference_index,
            } => write!(
                f,
                "Constant::MethodHandle #{reference_kind}, #{reference_index}"
            ),
            Constant::Methodref {
                class_index,
                name_and_type_index,
            } => write!(
                f,
                "Constant::Methodref #{class_index}, #{name_and_type_index}"
            ),
            Constant::MethodType { descriptor_index } => {
                write!(f, "Constant::MethodType #{descriptor_index}")
            }
            Constant::Module { name_index } => write!(f, "Constant::Module #{name_index}"),
            Constant::NameAndType {
                name_index,
                descriptor_index,
            } => write!(
                f,
                "Constant::NameAndType #{name_index}, #{descriptor_index}"
            ),
            Constant::Package { name_index } => write!(f, "Constant::Package #{name_index}"),
            Constant::String { string_index } => write!(f, "Constant::String #{string_index}"),
            Constant::Utf8(s) => write!(f, "Constant::Utf8({s})"),
        }
    }
}

#[derive(Debug)]
pub enum Attribute {
    AnnotationDefault(ElementValue),
    BootstrapMethods(Vec<BootstrapMethod>),
    Code {
        code: Vec<Instruction>,
        max_stack: u16,
        max_locals: u16,
        attributes: Vec<Attribute>,
    },
    ConstantValue {
        constantvalue_index: u16,
    },
    Deprecated,
    EnclosingMethod {
        class_index: u16,
        method_index: u16,
    },
    Exceptions(Vec<u16>),
    InnerClasses(Vec<InnerClass>),
    LineNumberTable(Vec<LineNumber>),
    LocalVariableTable(Vec<LocalVariable>),
    LocalVariableTypeTable(Vec<LocalVariableType>),
    MethodParameters(Vec<MethodParameter>),
    Module {
        module_name_index: u16,
        module_flags: Vec<AccessFlag>,
        module_version_index: u16,
        requires: Vec<ModuleRequires>,
        exports: Vec<ModuleExports>,
        opens: Vec<ModuleOpens>,
        uses: Vec<u16>,
        provides: Vec<ModuleProvides>,
    },
    ModuleMainClass(u16),
    ModulePackages(Vec<u16>),
    NestHost(u16),
    NestMembers(Vec<u16>),
    PermittedSubclasses(Vec<u16>),
    Record(Vec<RecordComponent>),
    RuntimeInvisibleAnnotations(Vec<Annotation>),
    RuntimeInvisibleParameterAnnotations(Vec<Vec<Annotation>>),
    RuntimeInvisibleTypeAnnotations(Vec<TypeAnnotation>),
    RuntimeVisibleAnnotations(Vec<Annotation>),
    RuntimeVisibleParameterAnnotations(Vec<Vec<Annotation>>),
    RuntimeVisibleTypeAnnotations(Vec<TypeAnnotation>),
    Signature {
        signature_index: u16,
    },
    SourceDebugExtension {
        debug_extension: Vec<u8>,
    },
    SourceFile {
        sourcefile_index: u16,
    },
    StackMapTable(Vec<StackMapFrame>),
    Synthetic,
    Unknown {
        name: String,
        data: Vec<u8>,
    },
}

#[derive(Debug)]
pub enum StackMapFrameType {
    AppendFrame(u8),
    ChopFrame(u8),
    FullFrame,
    SameFrame(u8),
    SameFrameExtended,
    SameLocals1StackItemFrame(u8),
    SameLocals1StackItemFrameExtended,
}

#[derive(Debug)]
pub enum VerificationType {
    Double,
    Float,
    Integer,
    Long,
    Null,
    Object { cpool_index: u16 },
    Top,
    Uninitialized { offset: u16 },
    UninitializedThis,
}

#[derive(Debug)]
pub enum ElementValue {
    AnnotationValue(Annotation),
    ArrayValue(Vec<ElementValue>),
    ClassInfoIndex(u16),
    ConstValueIndex {
        tag: u8,
        const_value_index: u16,
    },
    EnumConstValue {
        type_name_index: u16,
        const_name_index: u16,
    },
}

#[derive(Debug)]
pub enum TargetInfo {
    TypeParameter {
        target_type: u8,
        type_parameter_index: u8,
    },
    Supertype {
        supertype_index: u16,
    },
    TypeParameterBound {
        target_type: u8,
        type_parameter_index: u8,
        bound_index: u8,
    },
    Empty(u8),
    FormalParameter {
        formal_parameter_index: u8,
    },
    Throws {
        throws_type_index: u16,
    },
    Localvar {
        target_type: u8,
        table: Vec<LocalVar>,
    },
    Catch {
        exception_table_index: u16,
    },
    Offset {
        target_type: u8,
        offset: u16,
    },
    TypeArgument {
        target_type: u8,
        offset: u16,
        type_argument_index: u8,
    },
}