class_rs/enums/
mod.rs

1use crate::structs::{
2    Annotation, BootstrapMethod, ExceptionTableEntry, InnerClass, LineNumber, LocalVar,
3    LocalVariable, LocalVariableType, MethodParameter, ModuleExports, ModuleOpens, ModuleProvides,
4    ModuleRequires, RecordComponent, StackMapFrame, TypeAnnotation,
5};
6
7mod instructions;
8pub use instructions::Instruction;
9
10#[derive(Debug, Clone, Copy, PartialEq)]
11pub enum AccessFlag {
12    /// - class: Declared abstract; must not be instantiated.
13    /// - inner class: Marked or implicitly abstract in source.
14    /// - method: Declared abstract; no implementation is provided.
15    Abstract,
16    /// - class: Declared as an annotation type.
17    /// - inner class: Declared as an annotation type.
18    Annotation,
19    /// - method: A bridge method, generated by the compiler.
20    Bridge,
21    /// - class: Declared as an enum type.
22    /// - inner class: Declared as an enum type.
23    /// - field: Declared as an element of an enum.
24    Enum,
25    /// - class: Declared final; no subclasses allowed.
26    /// - inner class: Marked final in source.
27    /// - field: Declared final; never directly assigned to after object construction.
28    /// - method: Declared final; must not be overridden
29    /// - formal parameter: Indicates that the formal parameter was declared final.
30    Final,
31    /// - class: Is an interface, not a class.
32    /// - inner class: Was an interface in source.
33    Interface,
34    /// - 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.
35    /// - module: Indicates that this module was implicitly declared.
36    /// - module requires/exports/opens flag: Indicates that this dependence was implicitly declared in the source of the module declaration.
37    Mandated,
38    /// - class: Is a module, not a class or interface.
39    Module,
40    /// - method: Declared native; implemented in a language other than Java.
41    Native,
42    /// - module: Indicates that this module is open.
43    Open,
44    /// - class: Declared public; may be accessed from outside its package.
45    /// - inner class: Marked or implicitly public in source.
46    /// - field, method: Declared public; may be accessed from outside its package.
47    Public,
48    /// - inner class: Marked private in source.
49    /// - field, method: Declared private; accessible only within the defining class.
50    Private,
51    /// - inner class: Marked protected in source.
52    /// - field, method: Declared protected; may be accessed within subclasses.
53    Protected,
54    /// - inner class: Marked or implicitly static in source.
55    /// - field, method: Declared static.
56    Static,
57    /// - 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.
58    StaticPhase,
59    /// - method: Declared strictfp; floating-point mode is FP-strict.
60    Strict,
61    /// - class: Treat superclass methods specially when invoked by the invokespecial instruction.
62    Super,
63    /// - method: Declared synchronized; invocation is wrapped by a monitor use.
64    Synchronized,
65    /// Declared synthetic; not present in the source code.
66    Synthetic,
67    /// - field: Declared transient; not written or read by a persistent object manager.
68    Transient,
69    /// - module requires flag: Indicates that any module which depends on the current module, implicitly declares a dependence on the module indicated by this entry.
70    Transitive,
71    /// - method: Declared with variable number of arguments.
72    VarArgs,
73    /// - field: Declared volatile; cannot be cached.
74    Volatile,
75}
76
77#[derive(Debug, Clone)]
78pub enum Constant {
79    Class {
80        name_index: u16,
81    },
82    Double(f64),
83    Dynamic {
84        bootstrap_method_attr_index: u16,
85        name_and_type_index: u16,
86    },
87    Fieldref {
88        class_index: u16,
89        name_and_type_index: u16,
90    },
91    Float(f32),
92    Integer(i32),
93    InterfaceMethodref {
94        class_index: u16,
95        name_and_type_index: u16,
96    },
97    /// Used for the first entry in the constant pool, and the second entry of doubles and longs.
98    Invalid,
99    InvokeDynamic {
100        bootstrap_method_attr_index: u16,
101        name_and_type_index: u16,
102    },
103    Long(i64),
104    MethodHandle {
105        reference_kind: u8,
106        reference_index: u16,
107    },
108    Methodref {
109        class_index: u16,
110        name_and_type_index: u16,
111    },
112    MethodType {
113        descriptor_index: u16,
114    },
115    Module {
116        name_index: u16,
117    },
118    NameAndType {
119        name_index: u16,
120        descriptor_index: u16,
121    },
122    Package {
123        name_index: u16,
124    },
125    String {
126        string_index: u16,
127    },
128    /// ⚠️ 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.
129    Utf8(String),
130}
131
132impl std::fmt::Display for Constant {
133    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
134        match self {
135            Constant::Class { name_index } => write!(f, "Constant::Class #{name_index}"),
136            Constant::Double(double) => write!(f, "Constant::Double {double}"),
137            Constant::Dynamic {
138                bootstrap_method_attr_index,
139                name_and_type_index,
140            } => write!(
141                f,
142                "Constant::Dynamic #{bootstrap_method_attr_index}, #{name_and_type_index}"
143            ),
144            Constant::Fieldref {
145                class_index,
146                name_and_type_index,
147            } => write!(
148                f,
149                "Constant::Fieldref #{class_index}, #{name_and_type_index}"
150            ),
151            Constant::Float(float) => write!(f, "Constant::Float {float}"),
152            Constant::Integer(int) => write!(f, "Constant::Integer {int}"),
153            Constant::InterfaceMethodref {
154                class_index,
155                name_and_type_index,
156            } => write!(
157                f,
158                "Constant::InterfaceMethodref #{class_index}, #{name_and_type_index}"
159            ),
160            Constant::Invalid => write!(f, "Constant::Invalid"),
161            Constant::InvokeDynamic {
162                bootstrap_method_attr_index,
163                name_and_type_index,
164            } => write!(
165                f,
166                "Constant::InvokeDynamic #{bootstrap_method_attr_index}, #{name_and_type_index}"
167            ),
168            Constant::Long(long) => write!(f, "Constant::Long {long}"),
169            Constant::MethodHandle {
170                reference_kind,
171                reference_index,
172            } => write!(
173                f,
174                "Constant::MethodHandle #{reference_kind}, #{reference_index}"
175            ),
176            Constant::Methodref {
177                class_index,
178                name_and_type_index,
179            } => write!(
180                f,
181                "Constant::Methodref #{class_index}, #{name_and_type_index}"
182            ),
183            Constant::MethodType { descriptor_index } => {
184                write!(f, "Constant::MethodType #{descriptor_index}")
185            }
186            Constant::Module { name_index } => write!(f, "Constant::Module #{name_index}"),
187            Constant::NameAndType {
188                name_index,
189                descriptor_index,
190            } => write!(
191                f,
192                "Constant::NameAndType #{name_index}, #{descriptor_index}"
193            ),
194            Constant::Package { name_index } => write!(f, "Constant::Package #{name_index}"),
195            Constant::String { string_index } => write!(f, "Constant::String #{string_index}"),
196            Constant::Utf8(s) => write!(f, "Constant::Utf8({s})"),
197        }
198    }
199}
200
201#[derive(Debug, Clone)]
202pub enum Attribute {
203    AnnotationDefault(ElementValue),
204    BootstrapMethods(Vec<BootstrapMethod>),
205    Code {
206        code: Vec<Instruction>,
207        max_stack: u16,
208        max_locals: u16,
209        exception_table: Vec<ExceptionTableEntry>,
210        attributes: Vec<Attribute>,
211    },
212    ConstantValue {
213        constantvalue_index: u16,
214    },
215    Deprecated,
216    EnclosingMethod {
217        class_index: u16,
218        method_index: u16,
219    },
220    Exceptions(Vec<u16>),
221    InnerClasses(Vec<InnerClass>),
222    LineNumberTable(Vec<LineNumber>),
223    LocalVariableTable(Vec<LocalVariable>),
224    LocalVariableTypeTable(Vec<LocalVariableType>),
225    MethodParameters(Vec<MethodParameter>),
226    Module {
227        module_name_index: u16,
228        module_flags: Vec<AccessFlag>,
229        module_version_index: u16,
230        requires: Vec<ModuleRequires>,
231        exports: Vec<ModuleExports>,
232        opens: Vec<ModuleOpens>,
233        uses: Vec<u16>,
234        provides: Vec<ModuleProvides>,
235    },
236    ModuleMainClass(u16),
237    ModulePackages(Vec<u16>),
238    NestHost(u16),
239    NestMembers(Vec<u16>),
240    PermittedSubclasses(Vec<u16>),
241    Record(Vec<RecordComponent>),
242    RuntimeInvisibleAnnotations(Vec<Annotation>),
243    RuntimeInvisibleParameterAnnotations(Vec<Vec<Annotation>>),
244    RuntimeInvisibleTypeAnnotations(Vec<TypeAnnotation>),
245    RuntimeVisibleAnnotations(Vec<Annotation>),
246    RuntimeVisibleParameterAnnotations(Vec<Vec<Annotation>>),
247    RuntimeVisibleTypeAnnotations(Vec<TypeAnnotation>),
248    Signature {
249        signature_index: u16,
250    },
251    SourceDebugExtension {
252        debug_extension: Vec<u8>,
253    },
254    SourceFile {
255        sourcefile_index: u16,
256    },
257    StackMapTable(Vec<StackMapFrame>),
258    Synthetic,
259    Unknown {
260        name: String,
261        data: Vec<u8>,
262    },
263}
264
265#[derive(Debug, Clone)]
266pub enum StackMapFrameType {
267    AppendFrame(u8),
268    ChopFrame(u8),
269    FullFrame,
270    SameFrame(u8),
271    SameFrameExtended,
272    SameLocals1StackItemFrame(u8),
273    SameLocals1StackItemFrameExtended,
274}
275
276#[derive(Debug, Clone)]
277pub enum VerificationType {
278    Double,
279    Float,
280    Integer,
281    Long,
282    Null,
283    Object { cpool_index: u16 },
284    Top,
285    Uninitialized { offset: u16 },
286    UninitializedThis,
287}
288
289#[derive(Debug, Clone)]
290pub enum ElementValue {
291    AnnotationValue(Annotation),
292    ArrayValue(Vec<ElementValue>),
293    ClassInfoIndex(u16),
294    ConstValueIndex {
295        tag: u8,
296        const_value_index: u16,
297    },
298    EnumConstValue {
299        type_name_index: u16,
300        const_name_index: u16,
301    },
302}
303
304#[derive(Debug, Clone)]
305pub enum TargetInfo {
306    TypeParameter {
307        target_type: u8,
308        type_parameter_index: u8,
309    },
310    Supertype {
311        supertype_index: u16,
312    },
313    TypeParameterBound {
314        target_type: u8,
315        type_parameter_index: u8,
316        bound_index: u8,
317    },
318    Empty(u8),
319    FormalParameter {
320        formal_parameter_index: u8,
321    },
322    Throws {
323        throws_type_index: u16,
324    },
325    Localvar {
326        target_type: u8,
327        table: Vec<LocalVar>,
328    },
329    Catch {
330        exception_table_index: u16,
331    },
332    Offset {
333        target_type: u8,
334        offset: u16,
335    },
336    TypeArgument {
337        target_type: u8,
338        offset: u16,
339        type_argument_index: u8,
340    },
341}