rjvm/bytecode/
attributes.rs

1use crate::bytecode::flags::InnerClassAccessFlags;
2use crate::bytecode::pool::ConstantPoolIndex;
3
4#[derive(Debug)]
5pub enum Attribute {
6    ConstantValue(ConstantValueInfo),
7    Code(CodeInfo),
8    StackMapTable(StackMapTableInfo),
9    Exceptions(ExceptionsInfo),
10    InnerClasses(InnerClassesInfo),
11    EnclosingMethod(EnclosingMethodInfo),
12    Synthetic(SyntheticInfo),
13    Signature(SignatureInfo),
14    SourceFile(SourceFileInfo),
15    SourceDebugExtension(SourceDebugExtensionInfo),
16    LineNumberTable(LineNumberTableInfo),
17    LocalVariableTable(LocalVariableTableInfo),
18    LocalVariableTypeTable(LocalVariableTypeTableInfo),
19    Deprecated(DeprecatedInfo),
20    RuntimeVisibleAnnotations(RuntimeVisibleAnnotationsInfo),
21    RuntimeInvisibleAnnotations(RuntimeInvisibleAnnotationsInfo),
22    RuntimeVisibleParameterAnnotations(RuntimeVisibleParameterAnnotationsInfo),
23    RuntimeInvisibleParameterAnnotations(RuntimeInvisibleParameterAnnotationsInfo),
24    RuntimeVisibleTypeAnnotations(RuntimeVisibleTypeAnnotationsInfo),
25    RuntimeInvisibleTypeAnnotations(RuntimeInvisibleTypeAnnotationsInfo),
26    AnnotationDefault(AnnotationDefaultInfo),
27    BootstrapMethods(BootstrapMethodsInfo),
28    MethodParameters(MethodParametersInfo),
29    Module(ModuleInfo),
30    ModulePackages(ModulePackagesInfo),
31    ModuleMainClass(ModuleMainClassInfo),
32    NestHost(NestHostInfo),
33    NestMembers(NestMembersInfo),
34    Record(RecordInfo),
35    PermittedSubtypes(PermittedSubtypesInfo),
36}
37
38impl std::fmt::Display for Attribute {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        match self {
41            Self::ConstantValue(_) => write!(f, "ConstantValue"),
42            Self::Code(_) => write!(f, "Code"),
43            Self::StackMapTable(_) => write!(f, "StackMapTable"),
44            Self::Exceptions(_) => write!(f, "Exceptions"),
45            Self::InnerClasses(_) => write!(f, "InnerClasses"),
46            Self::EnclosingMethod(_) => write!(f, "EnclosingMethod"),
47            Self::Synthetic(_) => write!(f, "Synthetic"),
48            Self::Signature(_) => write!(f, "Signature"),
49            Self::SourceFile(_) => write!(f, "SourceFile"),
50            Self::SourceDebugExtension(_) => write!(f, "SourceDebugExtension"),
51            Self::LineNumberTable(_) => write!(f, "LineNumberTable"),
52            Self::LocalVariableTable(_) => write!(f, "LocalVariableTable"),
53            Self::LocalVariableTypeTable(_) => write!(f, "LocalVariableTypeTable"),
54            Self::Deprecated(_) => write!(f, "Deprecated"),
55            Self::RuntimeVisibleAnnotations(_) => write!(f, "RuntimeVisibleAnnotations"),
56            Self::RuntimeInvisibleAnnotations(_) => write!(f, "RuntimeInvisibleAnnotations"),
57            Self::RuntimeVisibleParameterAnnotations(_) => {
58                write!(f, "RuntimeVisibleParameterAnnotations")
59            }
60            Self::RuntimeInvisibleParameterAnnotations(_) => {
61                write!(f, "RuntimeInvisibleParameterAnnotations")
62            }
63            Self::RuntimeVisibleTypeAnnotations(_) => write!(f, "RuntimeVisibleTypeAnnotations"),
64            Self::RuntimeInvisibleTypeAnnotations(_) => {
65                write!(f, "RuntimeInvisibleTypeAnnotations")
66            }
67            Self::AnnotationDefault(_) => write!(f, "AnnotationDefault"),
68            Self::BootstrapMethods(_) => write!(f, "BootstrapMethods"),
69            Self::MethodParameters(_) => write!(f, "MethodParameters"),
70            Self::Module(_) => write!(f, "Module"),
71            Self::ModulePackages(_) => write!(f, "ModulePackages"),
72            Self::ModuleMainClass(_) => write!(f, "ModuleMainClass"),
73            Self::NestHost(_) => write!(f, "NestHost"),
74            Self::NestMembers(_) => write!(f, "NestMembers"),
75            Self::Record(_) => write!(f, "Record"),
76            Self::PermittedSubtypes(_) => write!(f, "PermittedSubtypes"),
77        }
78    }
79}
80
81#[derive(Debug, Clone, PartialEq)]
82pub struct ExceptionTableEntry {
83    pub start_pc: u16,
84    pub end_pc: u16,
85    pub handler_pc: u16,
86    pub catch_type: ConstantPoolIndex,
87}
88
89#[derive(Debug, Clone, PartialEq)]
90pub enum VerificationTypeInfo {
91    Top,
92    Integer,
93    Float,
94    Double,
95    Long,
96    Null,
97    UninitializedThis,
98    Object {
99        /// An index into the constant pool for the class of the object
100        class: ConstantPoolIndex,
101    },
102    Uninitialized {
103        /// Offset into associated code array of a new instruction
104        /// that created the object being stored here.
105        offset: u16,
106    },
107}
108
109#[derive(Debug, Clone, PartialEq)]
110pub struct LineNumberTableEntry {
111    pub start_pc: u16,
112    pub line_number: u16,
113}
114
115#[derive(Debug, Clone, PartialEq)]
116pub struct LocalVariableTableEntry {
117    pub start_pc: u16,
118    pub length: u16,
119    pub name_index: ConstantPoolIndex,
120    pub descriptor_index: ConstantPoolIndex,
121    pub index: ConstantPoolIndex,
122}
123
124#[derive(Debug, Clone, PartialEq)]
125pub struct LocalVariableTypeTableEntry {
126    pub start_pc: u16,
127    pub length: u16,
128    pub name_index: ConstantPoolIndex,
129    pub signature_index: ConstantPoolIndex,
130    pub index: ConstantPoolIndex,
131}
132
133#[derive(Debug, Clone, PartialEq)]
134pub struct Annotation {
135    pub type_index: ConstantPoolIndex,
136    pub num_element_value_pairs: u16,
137    pub element_value_pairs: Vec<ElementValuePair>,
138}
139
140#[derive(Debug, Clone, PartialEq)]
141pub struct ElementValuePair {
142    pub element_name_index: ConstantPoolIndex,
143    pub value: ElementValue,
144}
145
146#[derive(Debug, Clone, PartialEq)]
147pub enum ElementTag {
148    Byte,
149    Char,
150    Double,
151    Float,
152    Int,
153    Long,
154    Short,
155    Boolean,
156    String,
157    Enum {
158        type_name_index: ConstantPoolIndex,
159        const_name_index: ConstantPoolIndex,
160    },
161    Class,
162    AnnotationType,
163    Array {
164        num_values: u16,
165        values: Vec<ElementValue>,
166    },
167}
168
169#[derive(Debug, Clone, PartialEq)]
170pub enum ElementValue {
171    ConstValueIndex(ConstantPoolIndex),
172    EnumConstValue {
173        type_name_index: ConstantPoolIndex,
174        const_name_index: ConstantPoolIndex,
175    },
176    ClassInfoIndex(ConstantPoolIndex),
177    Annotation(Annotation),
178    Array {
179        num_values: u16,
180        values: Vec<ElementValue>,
181    },
182}
183
184#[derive(Debug, Clone, PartialEq)]
185pub struct ParameterAnnotation {
186    pub num_annotations: u16,
187    pub annotations: Vec<Annotation>,
188}
189
190#[derive(Debug, Clone, PartialEq)]
191pub struct TypeAnnotation {
192    pub target_type: u8,
193    pub target_info: TypeAnnotationTargetInfo,
194    pub target_path: TypePath,
195    pub type_index: ConstantPoolIndex,
196    pub num_element_value_pairs: u16,
197    pub element_value_pairs: Vec<ElementValuePair>,
198}
199
200#[derive(Debug, Clone, PartialEq)]
201pub struct TypeAnnotationTargetInfo {
202    pub target_info: TypeAnnotationTargetInfoType,
203}
204
205#[derive(Debug, Clone, PartialEq)]
206pub enum TypeAnnotationTargetInfoType {
207    TypeParameter {
208        type_parameter_index: ConstantPoolIndex,
209    },
210    SuperType {
211        super_type_index: ConstantPoolIndex,
212    },
213    TypeParameterBound {
214        type_parameter_index: ConstantPoolIndex,
215        bound_index: ConstantPoolIndex,
216    },
217    Empty,
218    FormalParameter {
219        formal_parameter_index: ConstantPoolIndex,
220    },
221    Throws {
222        throws_type_index: ConstantPoolIndex,
223    },
224    LocalVar {
225        table: Vec<LocalVarTargetTableEntry>,
226    },
227    Catch {
228        exception_table_index: ConstantPoolIndex,
229    },
230    Offset {
231        offset: u16,
232    },
233    TypeArgument {
234        offset: u16,
235        type_argument_index: ConstantPoolIndex,
236    },
237}
238
239#[derive(Debug, Clone, PartialEq)]
240pub struct LocalVarTargetTableEntry {
241    pub start_pc: u16,
242    pub length: u16,
243    pub index: ConstantPoolIndex,
244}
245
246#[derive(Debug, Clone, PartialEq)]
247pub struct TypePath {
248    pub path_length: u8,
249    pub path: Vec<TypePathEntry>,
250}
251
252#[derive(Debug, Clone, PartialEq)]
253pub struct TypePathEntry {
254    pub type_path_kind: u8,
255    pub type_argument_index: ConstantPoolIndex,
256}
257
258#[derive(Debug, Clone, PartialEq)]
259pub enum StackMapFrame {
260    SameFrame {
261        frame_type: u8,
262    },
263    SameLocals1StackItemFrame {
264        frame_type: u8,
265        stack: VerificationTypeInfo,
266    },
267    SameLocals1StackItemFrameExtended {
268        frame_type: u8,
269        offset_delta: u16,
270        stack: VerificationTypeInfo,
271    },
272    ChopFrame {
273        frame_type: u8,
274        offset_delta: u16,
275    },
276    SameFrameExtended {
277        frame_type: u8,
278        offset_delta: u16,
279    },
280    AppendFrame {
281        frame_type: u8,
282        offset_delta: u16,
283        locals: Vec<VerificationTypeInfo>,
284    },
285    FullFrame {
286        frame_type: u8,
287        offset_delta: u16,
288        number_of_locals: u16,
289        locals: Vec<VerificationTypeInfo>,
290        number_of_stack_items: u16,
291        stack: Vec<VerificationTypeInfo>,
292    },
293}
294
295#[derive(Debug, Clone, PartialEq)]
296pub struct InnerClass {
297    pub inner_class_info_index: ConstantPoolIndex,
298    pub outer_class_info_index: ConstantPoolIndex,
299    pub inner_name_index: ConstantPoolIndex,
300    pub inner_class_access_flags: InnerClassAccessFlags,
301}
302
303#[derive(Debug, Clone, PartialEq)]
304pub struct BootstrapMethod {
305    pub bootstrap_method_ref: ConstantPoolIndex,
306    pub num_bootstrap_arguments: u16,
307    pub bootstrap_arguments: Vec<ConstantPoolIndex>,
308}
309
310#[derive(Debug)]
311pub struct RecordComponent {
312    pub name_index: ConstantPoolIndex,
313    pub descriptor_index: ConstantPoolIndex,
314    pub attributes_count: u16,
315    pub attributes: Vec<Attribute>,
316}
317
318#[derive(Debug, Clone, PartialEq)]
319pub struct MethodParameter {
320    pub name_index: ConstantPoolIndex,
321    pub access_flags: u16,
322}
323
324#[derive(Debug, Clone, PartialEq)]
325pub struct Requires {
326    pub requires_index: ConstantPoolIndex,
327    pub requires_flags: u16,
328    pub requires_version_index: ConstantPoolIndex,
329}
330
331#[derive(Debug, Clone, PartialEq)]
332pub struct Exports {
333    pub exports_index: ConstantPoolIndex,
334    pub exports_flags: u16,
335    pub exports_to_count: u16,
336    pub exports_to_index: Vec<ConstantPoolIndex>,
337}
338
339#[derive(Debug, Clone, PartialEq)]
340pub struct Opens {
341    pub opens_index: ConstantPoolIndex,
342    pub opens_flags: u16,
343    pub opens_to_count: u16,
344    pub opens_to_index: Vec<ConstantPoolIndex>,
345}
346
347#[derive(Debug, Clone, PartialEq)]
348pub struct Provides {
349    pub provides_index: ConstantPoolIndex,
350    pub provides_with_count: u16,
351    pub provides_with_index: Vec<ConstantPoolIndex>,
352}
353
354#[derive(Debug)]
355pub struct ConstantValueInfo {
356    pub attribute_name_index: ConstantPoolIndex,
357    pub attribute_length: u32,
358    pub constantvalue_index: ConstantPoolIndex,
359}
360
361#[derive(Debug)]
362pub struct CodeInfo {
363    pub attribute_name_index: ConstantPoolIndex,
364    pub attribute_length: u32,
365    pub max_stack: u16,
366    pub max_locals: u16,
367    pub code_length: u32,
368    pub code: Vec<u8>,
369    pub exception_table_length: u16,
370    pub exception_table: Vec<ExceptionTableEntry>,
371    pub attributes_count: u16,
372    pub attributes: Vec<Attribute>,
373}
374
375#[derive(Debug)]
376pub struct StackMapTableInfo {
377    pub attribute_name_index: ConstantPoolIndex,
378    pub attribute_length: u32,
379    pub number_of_entries: u16,
380    pub entries: Vec<StackMapFrame>,
381}
382
383#[derive(Debug)]
384pub struct ExceptionsInfo {
385    pub attribute_name_index: ConstantPoolIndex,
386    pub attribute_length: u32,
387    pub number_of_exceptions: u16,
388    pub exception_index_table: Vec<ConstantPoolIndex>,
389}
390
391#[derive(Debug)]
392pub struct InnerClassesInfo {
393    pub attribute_name_index: ConstantPoolIndex,
394    pub attribute_length: u32,
395    pub number_of_classes: u16,
396    pub classes: Vec<InnerClass>,
397}
398
399#[derive(Debug)]
400pub struct EnclosingMethodInfo {
401    pub attribute_name_index: ConstantPoolIndex,
402    pub attribute_length: u32,
403    pub class_index: ConstantPoolIndex,
404    pub method_index: ConstantPoolIndex,
405}
406
407#[derive(Debug)]
408pub struct SyntheticInfo {
409    pub attribute_name_index: ConstantPoolIndex,
410    pub attribute_length: u32,
411}
412
413#[derive(Debug)]
414pub struct SignatureInfo {
415    pub attribute_name_index: ConstantPoolIndex,
416    pub attribute_length: u32,
417    pub signature_index: ConstantPoolIndex,
418}
419
420#[derive(Debug)]
421pub struct SourceFileInfo {
422    pub attribute_name_index: ConstantPoolIndex,
423    pub attribute_length: u32,
424    pub sourcefile_index: ConstantPoolIndex,
425}
426
427#[derive(Debug)]
428pub struct SourceDebugExtensionInfo {
429    pub attribute_name_index: ConstantPoolIndex,
430    pub attribute_length: u32,
431    pub debug_extension: Vec<u8>,
432}
433
434#[derive(Debug)]
435pub struct LineNumberTableInfo {
436    pub attribute_name_index: ConstantPoolIndex,
437    pub attribute_length: u32,
438    pub line_number_table_length: u16,
439    pub line_number_table: Vec<LineNumberTableEntry>,
440}
441
442#[derive(Debug)]
443pub struct LocalVariableTableInfo {
444    pub attribute_name_index: ConstantPoolIndex,
445    pub attribute_length: u32,
446    pub local_variable_table_length: u16,
447    pub local_variable_table: Vec<LocalVariableTableEntry>,
448}
449
450#[derive(Debug)]
451pub struct LocalVariableTypeTableInfo {
452    pub attribute_name_index: ConstantPoolIndex,
453    pub attribute_length: u32,
454    pub local_variable_type_table_length: u16,
455    pub local_variable_type_table: Vec<LocalVariableTypeTableEntry>,
456}
457
458#[derive(Debug)]
459pub struct DeprecatedInfo {
460    pub attribute_name_index: ConstantPoolIndex,
461    pub attribute_length: u32,
462}
463
464#[derive(Debug)]
465pub struct RuntimeVisibleAnnotationsInfo {
466    pub attribute_name_index: ConstantPoolIndex,
467    pub attribute_length: u32,
468    pub num_annotations: u16,
469    pub annotations: Vec<Annotation>,
470}
471
472#[derive(Debug)]
473pub struct RuntimeInvisibleAnnotationsInfo {
474    pub attribute_name_index: ConstantPoolIndex,
475    pub attribute_length: u32,
476    pub num_annotations: u16,
477    pub annotations: Vec<Annotation>,
478}
479
480#[derive(Debug)]
481pub struct RuntimeVisibleParameterAnnotationsInfo {
482    pub attribute_name_index: ConstantPoolIndex,
483    pub attribute_length: u32,
484    pub num_parameters: u8,
485    pub parameter_annotations: Vec<ParameterAnnotation>,
486}
487
488#[derive(Debug)]
489pub struct RuntimeInvisibleParameterAnnotationsInfo {
490    pub attribute_name_index: ConstantPoolIndex,
491    pub attribute_length: u32,
492    pub num_parameters: u8,
493    pub parameter_annotations: Vec<ParameterAnnotation>,
494}
495
496#[derive(Debug)]
497pub struct RuntimeVisibleTypeAnnotationsInfo {
498    pub attribute_name_index: ConstantPoolIndex,
499    pub attribute_length: u32,
500    pub num_annotations: u16,
501    pub annotations: Vec<TypeAnnotation>,
502}
503
504#[derive(Debug)]
505pub struct RuntimeInvisibleTypeAnnotationsInfo {
506    pub attribute_name_index: ConstantPoolIndex,
507    pub attribute_length: u32,
508    pub num_annotations: u16,
509    pub annotations: Vec<TypeAnnotation>,
510}
511
512#[derive(Debug)]
513pub struct AnnotationDefaultInfo {
514    pub attribute_name_index: ConstantPoolIndex,
515    pub attribute_length: u32,
516    pub default_value: ElementValue,
517}
518
519#[derive(Debug)]
520pub struct BootstrapMethodsInfo {
521    pub attribute_name_index: ConstantPoolIndex,
522    pub attribute_length: u32,
523    pub num_bootstrap_methods: u16,
524    pub bootstrap_methods: Vec<BootstrapMethod>,
525}
526
527#[derive(Debug)]
528pub struct MethodParametersInfo {
529    pub attribute_name_index: ConstantPoolIndex,
530    pub attribute_length: u32,
531    pub parameters_count: u8,
532    pub parameters: Vec<MethodParameter>,
533}
534
535#[derive(Debug)]
536pub struct ModuleInfo {
537    pub attribute_name_index: ConstantPoolIndex,
538    pub attribute_length: u32,
539    pub module_name_index: ConstantPoolIndex,
540    pub module_flags: u16,
541    pub module_version_index: ConstantPoolIndex,
542    pub requires_count: u16,
543    pub requires: Vec<Requires>,
544    pub exports_count: u16,
545    pub exports: Vec<Exports>,
546    pub opens_count: u16,
547    pub opens: Vec<Opens>,
548    pub uses_count: u16,
549    pub uses_index: Vec<ConstantPoolIndex>,
550    pub provides_count: u16,
551    pub provides: Vec<Provides>,
552}
553
554#[derive(Debug)]
555pub struct ModulePackagesInfo {
556    pub attribute_name_index: ConstantPoolIndex,
557    pub attribute_length: u32,
558    pub package_count: u16,
559    pub package_index: Vec<ConstantPoolIndex>,
560}
561
562#[derive(Debug)]
563pub struct ModuleMainClassInfo {
564    pub attribute_name_index: ConstantPoolIndex,
565    pub attribute_length: u32,
566    pub main_class_index: ConstantPoolIndex,
567}
568
569#[derive(Debug)]
570pub struct NestHostInfo {
571    pub attribute_name_index: ConstantPoolIndex,
572    pub attribute_length: u32,
573    pub host_class_index: ConstantPoolIndex,
574}
575
576#[derive(Debug)]
577pub struct NestMembersInfo {
578    pub attribute_name_index: ConstantPoolIndex,
579    pub attribute_length: u32,
580    pub number_of_classes: u16,
581    pub classes: Vec<ConstantPoolIndex>,
582}
583
584#[derive(Debug)]
585pub struct RecordInfo {
586    pub attribute_name_index: ConstantPoolIndex,
587    pub attribute_length: u32,
588    pub component_count: u16,
589    pub components: Vec<Attribute>,
590}
591
592#[derive(Debug)]
593pub struct PermittedSubtypesInfo {
594    pub attribute_name_index: ConstantPoolIndex,
595    pub attribute_length: u32,
596    pub number_of_classes: u16,
597    pub classes: Vec<String>,
598}