1
1
use anyhow:: bail;
2
+ use std:: { iter:: Empty , path:: Path } ;
2
3
3
4
use crate :: {
4
- annotation:: { Annotation , ParameterAnnotations } ,
5
+ annotation:: { Annotation , ElementPairs , ParameterAnnotation , TypeAnnotation } ,
5
6
flags:: { ExportFlags , InnerClassAccessFlags , ModuleFlags , OpenFlags , RequiresFlags } ,
6
7
instruction:: Instruction ,
7
8
type_verification:: VType ,
@@ -19,7 +20,7 @@ pub(crate) mod attr_constants {
19
20
pub ( crate ) const SYNTHETIC : & [ u8 ] = b"Synthetic" ;
20
21
pub ( crate ) const SIGNATURE : & [ u8 ] = b"Signature" ;
21
22
pub ( crate ) const SOURCE_FILE : & [ u8 ] = b"SourceFile" ;
22
- pub ( crate ) const SOURCE_DEBUG_EXTENSION : & [ u8 ] = b"SourceDebugExtension " ;
23
+ pub ( crate ) const SOURCE_DEBUG_EXTENSION : & [ u8 ] = b"SourceDebug, Clone, Eq, PartialEqExtension " ;
23
24
pub ( crate ) const LINE_NUMBER_TABLE : & [ u8 ] = b"LineNumberTable" ;
24
25
pub ( crate ) const LOCAL_VARIABLE_TABLE : & [ u8 ] = b"LocalVariableTable" ;
25
26
pub ( crate ) const LOCAL_VARIABLE_TYPE_TABLE : & [ u8 ] = b"LocalVariableTypeTable" ;
@@ -42,130 +43,160 @@ pub(crate) mod attr_constants {
42
43
pub ( crate ) const RECORD : & [ u8 ] = b"Record" ;
43
44
}
44
45
46
+ // TODO Delete Attribute when everything compiles without it.
47
+ #[ derive( Debug , Clone , Eq , PartialEq , Hash ) ]
48
+ pub struct Attribute ;
45
49
/// Attributes are used in the [`crate::class_file::ClassFile`],
46
50
/// [`crate::field::Field`], [`crate::method::Method`], Code_attribute, and
47
51
/// record_component_info structures of the class file format
48
- #[ derive( Debug ) ]
49
- pub enum Attribute {
50
- ///The [`Attribute::ConstantValue`] attribute is a fixed-length attribute
51
- /// in the attributes table of a field_info structure. A
52
- /// [`Attribute::ConstantValue`] attribute represents the value of a
53
- /// constant expression. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.5
54
- /// https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.29
55
- ConstantValue {
56
- attribute_name_index : u16 ,
57
- attribute_info : Vec < Attribute > ,
58
- } ,
59
- /// The [`Attribute::Code`] attribute is a variable-length attribute in the
60
- /// attributes table of a [`crate::method::Method`] structure. A
61
- /// [`Attribute::Code`] attribute contains the Java Virtual Machine
62
- /// instructions and auxiliary information for a method, including an
63
- /// instance initialization method and a class or interface initialization
64
- /// method. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html#jvms-2.9.1
65
- /// https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html#jvms-2.9.2
66
- Code {
67
- max_stack : u16 ,
68
- max_locals : u16 ,
69
- code : Vec < Instruction > ,
70
- exception_table : Vec < Exception > ,
71
- attributes : Vec < Attribute > ,
72
- } ,
73
- /// The [`Attribute::StackMapTable`] attribute is a variable-length
74
- /// attribute in the attributes table of a [`Attribute::Code`]
75
- /// attribute. A [`Attribute::StackMapTable`] attribute is used during
76
- /// the process of verification by type checking. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.7.3
77
- /// https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.10.1
78
- StackMapTable ( Vec < StackMapFrame > ) ,
79
- Exceptions ( Vec < u16 > ) ,
80
- InnerClasses ( Vec < InnerClassInfo > ) ,
81
- EnclosingMethod {
82
- class_index : u16 ,
83
- method_index : u16 ,
84
- } ,
85
- Synthetic ,
86
- Signature ,
87
- SourceFile ( u16 ) ,
88
- SourceDebugExtension ( Vec < u8 > ) ,
89
- LineNumberTable ( Vec < LineNumberTableEntry > ) ,
90
- LocalVariableTable ( Vec < LocalVarTableEntry > ) ,
91
- LocalVariableTypeTable ( Vec < LocalVarTypeEntry > ) ,
92
- Deprecated ,
93
- RuntimeVisibleAnnotations ( Vec < Annotation > ) ,
94
- RuntimeInvisibleAnnotations ( Vec < Annotation > ) ,
95
- RuntimeVisibleParameterAnnotations ( Vec < ParameterAnnotations > ) ,
96
- RuntimeInvisibleParameterAnnotations {
97
- attribute_name_index : u16 ,
98
- attributes : Vec < Attribute > ,
99
- } ,
100
- RuntimeVisibleTypeAnnotations {
101
- attribute_name_index : u16 ,
102
- attributes : Vec < Attribute > ,
103
- } ,
104
- RuntimeInvisibleTypeAnnotations {
105
- attribute_name_index : u16 ,
106
- attributes : Vec < Attribute > ,
107
- } ,
108
- AnnotationDefault {
109
- attribute_name_index : u16 ,
110
- attributes : Vec < Attribute > ,
111
- } ,
112
- BootstrapMethods ( Vec < BootstrapMethod > ) ,
113
- MethodParameters {
114
- attribute_name_index : u16 ,
115
- attributes : Vec < Attribute > ,
116
- } ,
117
- Module {
118
- module_name_index : u16 ,
119
- module_flags : ModuleFlags ,
120
- module_ver_index : u16 ,
121
- requires : Vec < Requires > ,
122
- exports : Vec < Exports > ,
123
- opens : Vec < Opens > ,
124
- uses : Vec < u16 > ,
125
- provides : Vec < Provides > ,
126
- } ,
127
- ModulePackages ( Vec < u16 > ) ,
128
- ModuleMainClass ( u16 ) ,
129
- NestHost ( u16 ) ,
130
- NestMembers ( Vec < u16 > ) ,
131
- Record ( Vec < RecordComponent > ) ,
132
- PermittedSubclasses ( Vec < u16 > ) ,
133
- Custom ( Vec < u8 > ) ,
52
+
53
+ ///The [`Attribute::ConstantValue`] attribute is a fixed-length attribute
54
+ /// in the attributes table of a field_info structure. A
55
+ /// [`Attribute::ConstantValue`] attribute represents the value of a
56
+ /// constant expression. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.5
57
+ /// https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.29
58
+
59
+ /// The [`Attribute::StackMapTable`] attribute is a variable-length
60
+ /// attribute in the attributes table of a [`Attribute::Code`]
61
+ /// attribute. A [`Attribute::StackMapTable`] attribute is used during
62
+ /// the process of verification by type checking. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.7.3
63
+ /// https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.10.1
64
+
65
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
66
+ pub struct ClassFileAttributes {
67
+ pub source_file : SourceFile ,
68
+ pub inner_classes : InnerClasses ,
69
+ pub enclosing_method : EnclosingMethod ,
70
+ pub source_debug_extension : SourceDebugExtension ,
71
+ pub synthetic : bool ,
72
+ pub signature : String ,
73
+ pub bootstrap_methods : BootstrapMethods ,
74
+ pub module : Module ,
75
+ pub module_packages : ModulePackages ,
76
+ pub module_main_class : ModuleMainClass ,
77
+ pub nest_host : NestHost ,
78
+ pub nest_members : NestMembers ,
79
+ pub record : Record ,
80
+ pub permitted_subclasses : PermittedSubclasses ,
81
+ }
82
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
83
+ pub struct StackMapTable ( pub Vec < StackMapFrame > ) ;
84
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
85
+ pub struct Exceptions ( pub Vec < u16 > ) ;
86
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
87
+ pub struct InnerClasses ( pub Vec < InnerClassInfo > ) ;
88
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
89
+ pub struct EnclosingMethod {
90
+ pub class_index : usize ,
91
+ pub method_index : usize ,
134
92
}
93
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
94
+ pub struct Synthetic ;
95
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
96
+ pub struct Signature ( pub u16 ) ;
97
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
98
+ pub struct SourceFile ( pub usize ) ;
99
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
100
+ pub struct SourceDebugExtension ( pub Vec < u8 > ) ;
101
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
102
+ pub struct LineNumberTable ( pub Vec < LineNumberTableEntry > ) ;
103
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
104
+ pub struct LocalVariableTable ( pub Vec < LocalVarTableEntry > ) ;
105
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
106
+ pub struct LocalVariableTypeTable ( pub Vec < LocalVarTypeEntry > ) ;
107
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
108
+ pub struct Deprecated ;
109
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
110
+ pub struct AnnotationDefault {
111
+ pub attribute_name_index : usize ,
112
+ pub attributes : Vec < u8 > ,
113
+ }
114
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
115
+ pub struct BootstrapMethods ( pub Vec < BootstrapMethod > ) ;
116
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
117
+ pub struct MethodParameters {
118
+ pub attribute_name_index : usize ,
119
+ pub attributes : Vec < Attribute > ,
120
+ }
121
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
122
+ pub struct Module {
123
+ pub module_name_index : usize ,
124
+ pub module_flags : ModuleFlags ,
125
+ pub module_ver_index : usize ,
126
+ pub requires : Vec < Requires > ,
127
+ pub exports : Vec < Exports > ,
128
+ pub opens : Vec < Opens > ,
129
+ pub uses : Vec < usize > ,
130
+ pub provides : Vec < Provides > ,
131
+ }
132
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
133
+ pub struct ModulePackages ( pub Vec < usize > ) ;
134
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
135
+ pub struct ModuleMainClass ( pub usize ) ;
136
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
137
+ pub struct NestHost ( pub usize ) ;
138
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
139
+ pub struct NestMembers ( pub Vec < usize > ) ;
140
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
141
+ pub struct Record ( pub Vec < RecordComponent > ) ;
142
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
143
+ pub struct PermittedSubclasses ( pub Vec < usize > ) ;
144
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
145
+ pub struct Custom ( pub Vec < u8 > ) ;
135
146
136
- #[ derive( Debug ) ]
147
+ /// The [`Attribute::Code`] attribute is a variable-length attribute in the
148
+ /// attributes table of a [`crate::method::Method`] structure. A
149
+ /// [`Attribute::Code`] attribute contains the Java Virtual Machine
150
+ /// instructions and auxiliary information for a method, including an
151
+ /// instance initialization method and a class or interface initialization
152
+ /// method. https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html#jvms-2.9.1
153
+ /// https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-2.html#jvms-2.9.2
154
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
155
+ pub struct Code {
156
+ pub max_stack : u16 ,
157
+ pub max_locals : u16 ,
158
+ pub code : Vec < Instruction > ,
159
+ pub exception_table : Vec < Exception > ,
160
+ pub line_number_table : LineNumberTable ,
161
+ pub local_var_table : LocalVariableTable ,
162
+ pub stack_map_table : StackMapTable ,
163
+ pub local_var_type_table : LocalVariableTypeTable ,
164
+ pub attributes : Vec < u8 > ,
165
+ }
166
+
167
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
137
168
pub struct Exception {
138
169
pub start_pc : u16 ,
139
170
pub end_pc : u16 ,
140
171
pub handler_pc : u16 ,
141
172
pub catch_type : u16 ,
142
173
}
143
174
144
- #[ derive( Debug ) ]
175
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
145
176
pub struct LineNumberTableEntry {
146
177
pub start_pc : u16 ,
147
178
pub line_number : u16 ,
148
179
}
149
180
150
- #[ derive( Debug ) ]
181
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
151
182
pub struct LocalVarTableEntry {
152
183
pub start_pc : u16 ,
153
184
pub len : u16 ,
154
- pub name_index : u16 ,
155
- pub descriptor_index : u16 ,
156
- pub index : u16 ,
185
+ pub name_index : usize ,
186
+ pub descriptor_index : usize ,
187
+ pub index : usize ,
157
188
}
158
189
159
- #[ derive( Debug ) ]
190
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
160
191
pub struct LocalVarTypeEntry {
161
192
pub start_pc : u16 ,
162
193
pub len : u16 ,
163
- pub name_index : u16 ,
164
- pub signature_index : u16 ,
165
- pub index : u16 ,
194
+ pub name_index : usize ,
195
+ pub signature_index : usize ,
196
+ pub index : usize ,
166
197
}
167
198
168
- #[ derive( Debug ) ]
199
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
169
200
pub enum StackMapFrame {
170
201
SameFrame ,
171
202
SameLocals ,
@@ -178,63 +209,80 @@ pub enum StackMapFrame {
178
209
locals : Vec < VType > ,
179
210
stack : Vec < VType > ,
180
211
} ,
212
+ #[ default]
213
+ Invalid ,
181
214
}
182
215
183
- #[ derive( Debug ) ]
216
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
184
217
pub struct InnerClassInfo {
185
- pub inner_class_info_index : u16 ,
186
- pub outer_class_info_index : u16 ,
187
- pub inner_name_index : u16 ,
218
+ pub inner_class_info_index : usize ,
219
+ pub outer_class_info_index : usize ,
220
+ pub inner_name_index : usize ,
188
221
pub inner_class_access_flags : InnerClassAccessFlags ,
189
222
}
190
223
191
- #[ derive( Debug ) ]
224
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
192
225
pub struct BootstrapMethod {
193
226
pub btstr_mthd_ref : u16 ,
194
227
pub btstr_args : Vec < u16 > ,
195
228
}
196
229
197
- #[ derive( Debug ) ]
230
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
198
231
pub struct Requires {
199
- pub requires_index : u16 ,
232
+ pub requires_index : usize ,
200
233
pub requires_flags : RequiresFlags ,
201
- pub requires_ver_index : u16 ,
234
+ pub requires_ver_index : usize ,
202
235
}
203
236
204
- #[ derive( Debug ) ]
237
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
205
238
pub struct Exports {
206
- pub exports_index : u16 ,
239
+ pub exports_index : usize ,
207
240
pub exports_flags : ExportFlags ,
208
- pub exports_to_index : Vec < u16 > ,
241
+ pub exports_to_index : Vec < usize > ,
209
242
}
210
243
211
- #[ derive( Debug ) ]
244
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
212
245
pub struct Opens {
213
- pub opens_index : u16 ,
246
+ pub opens_index : usize ,
214
247
pub opens_flags : OpenFlags ,
215
248
pub opens_to_index : Vec < u16 > ,
216
249
}
217
250
218
- #[ derive( Debug ) ]
251
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
219
252
pub struct Provides {
220
- pub provides_index : u16 ,
221
- pub provides_with_index : Vec < u16 > ,
253
+ pub provides_index : usize ,
254
+ pub provides_with_index : Vec < usize > ,
222
255
}
223
256
224
- #[ derive( Debug ) ]
257
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
225
258
pub struct RecordComponent {
226
- pub name_index : u16 ,
227
- pub descriptor_index : u16 ,
228
- pub attributes : Vec < Attribute > ,
259
+ pub name_index : usize ,
260
+ pub descriptor_index : usize ,
261
+ pub runtime_annotations : Vec < RuntimeAnnotation > ,
229
262
}
230
263
231
264
/// Used to tell parsers if the attributes being parsed are for a
232
265
/// Class | Interface | Record.
233
- #[ derive( Debug ) ]
266
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
234
267
pub enum ClassType {
235
268
Class ,
236
269
Interface ,
237
270
Record ,
271
+ #[ default]
272
+ Invalid ,
273
+ }
274
+
275
+ #[ derive( Debug , Default , Eq , PartialEq , Hash , Clone ) ]
276
+ pub enum RuntimeAnnotation {
277
+ #[ default]
278
+ Invalid ,
279
+ RuntimeVisibleAnnotations ( Vec < Annotation > ) ,
280
+ RuntimeInvisibleAnnotations ( Vec < Annotation > ) ,
281
+ RuntimeVisibleParameterAnnotations ( Vec < ParameterAnnotation > ) ,
282
+ RuntimeInvisibleParameterAnnotations ( Vec < ParameterAnnotation > ) ,
283
+ RuntimeVisibleTypeAnnotations ( Vec < TypeAnnotation > ) ,
284
+ RuntimeInvisibleTypeAnnotations ( Vec < TypeAnnotation > ) ,
285
+ AnnotationDefault ( ElementPairs ) ,
238
286
}
239
287
240
288
impl TryFrom < u8 > for StackMapFrame {
0 commit comments