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
//! # Attributes Module
//!
//! This module provides definitions and parsing capabilities for the various attributes
//! that can appear in Java class files, as specified by the Java Virtual Machine (JVM)
//! Specification. Attributes are a fundamental part of the class file format, offering
//! extensible metadata about classes, fields, methods, and code.
//!
//! ## Overview
//!
//! Attributes are used to store information that is not part of the basic structure of
//! a class file element but is essential for its correct interpretation, execution,
//! or for tools like debuggers and compilers. Each attribute has a name, which is an
//! index into the constant pool (pointing to a `CONSTANT_Utf8_info` structure), and
//! a sequence of bytes representing its value. The structure and meaning of these bytes
//! are specific to the attribute type.
//!
//! This module defines Rust structs corresponding to these attributes, facilitating
//! their representation and manipulation.
//!
//! ## Common Attributes
//!
//! Some of the well-known attributes include:
//! - `Code`: Contains the JVM bytecode for a method.
//! - `LineNumberTable`: Maps bytecode offsets to source file line numbers for debugging.
//! - `SourceFile`: Specifies the original source file name.
//! - `ConstantValue`: Indicates the constant value for a static final field.
//! - `Exceptions`: Lists the checked exceptions a method may throw.
//! - `InnerClasses`: Describes nested classes.
//! - `RuntimeVisibleAnnotations`: Stores annotations that are visible at runtime.
//! - `BootstrapMethods`: Used by `invokedynamic` instructions.
//!
//! For a comprehensive list and detailed descriptions, refer to the
//! [JVM Specification, Chapter 4: The class File Format](https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.7).
/// Represents a Java annotation, a form of metadata that can be added to Java code elements.
/// Annotations are used by the compiler, runtime, or other tools.
/// Defines the various kinds of elements that can appear within a Java annotation's value.
/// Represents a name-value pair within a Java annotation, forming its members.
/// Defines the structure for representing array type information, often used in signatures or annotations.
/// The core `Attribute` structure, serving as a base for all specific class file attributes.
/// It typically holds the attribute name index and its raw byte data.
/// Represents entries in the `BootstrapMethods` attribute, used for `invokedynamic` instructions.
/// Defines entries in an exception table, part of the `Code` attribute, for try-catch blocks.
/// Represents `exports` declarations in a `Module` attribute, specifying packages exported by a Java module.
/// Defines access flags (e.g., `ACC_SYNTHETIC`, `ACC_MANDATED`) for module exports.
/// Contains information about inner classes and their relationship to any outer class, part of the `InnerClasses` attribute.
/// Represents individual JVM bytecode instructions within the `Code` attribute of a method.
/// Maps bytecode offsets to source code line numbers, part of the `LineNumberTable` attribute, crucial for debugging.
/// Provides information about local variables within a method's scope, part of the `LocalVariableTable` attribute.
/// Represents targeting information for local variables in type annotations (e.g., `RuntimeVisibleTypeAnnotations`).
/// Provides type information (signatures) for local variables, part of the `LocalVariableTypeTable` attribute.
/// Defines the maximum number of local variables (including parameters) for a method, part of the `Code` attribute.
/// Defines the maximum operand stack size required by a method at any point during its execution, part of the `Code` attribute.
/// Contains information about method parameters, part of the `MethodParameters` attribute.
/// Defines access flags for Java modules (e.g., `ACC_OPEN`, `ACC_SYNTHETIC`, `ACC_MANDATED`).
/// Defines access flags for nested (inner) classes (e.g., `ACC_PUBLIC`, `ACC_STATIC`).
/// Utilities for working with bytecode offsets, often used in parsing or manipulating code attributes.
/// Represents `opens` declarations in a `Module` attribute, specifying packages opened by a Java module.
/// Defines access flags (e.g., `ACC_SYNTHETIC`, `ACC_MANDATED`) for module opens.
/// Contains annotation information for method parameters, part of `RuntimeVisibleParameterAnnotations` or `RuntimeInvisibleParameterAnnotations`.
/// Represents `provides` declarations in a `Module` attribute, specifying services provided by a Java module.
/// Contains information about record components for Java records, part of the `Record` attribute.
/// Represents `requires` declarations in a `Module` attribute, specifying dependencies of a Java module.
/// Defines access flags (e.g., `ACC_TRANSITIVE`, `ACC_STATIC_PHASE`, `ACC_SYNTHETIC`, `ACC_MANDATED`) for module requires.
/// Represents stack frame information used for bytecode verification and debugging, part of the `StackMapTable` attribute.
/// Defines the path to a type argument or a wildcard bound within a `TypeAnnotation`.
/// Defines the target type (e.g., field, method return) for a `TypeAnnotation`.
/// Represents type annotations that can appear on various program elements, enhancing Java's type system.
/// Defines verification types used during the bytecode verification process, often found in `StackMapTable` attributes.
pub use Annotation;
pub use AnnotationElement;
pub use AnnotationValuePair;
pub use ArrayType;
pub use Attribute;
pub use BootstrapMethod;
pub use ExceptionTableEntry;
pub use Exports;
pub use ExportsFlags;
pub use InnerClass;
pub use ;
pub use LineNumber;
pub use LocalVariableTable;
pub use LocalVariableTarget;
pub use LocalVariableTypeTable;
pub use MaxLocals;
pub use MaxStack;
pub use MethodParameter;
pub use ModuleAccessFlags;
pub use NestedClassAccessFlags;
pub use Opens;
pub use OpensFlags;
pub use ParameterAnnotation;
pub use Provides;
pub use Record;
pub use Requires;
pub use RequiresFlags;
pub use StackFrame;
pub use TargetPath;
pub use TargetType;
pub use TypeAnnotation;
pub use VerificationType;
// Example for Annotation (assuming AnnotationValuePair and AnnotationElement are defined and accessible)
// To make this example runnable, you'd need to define dummy versions or import actual ones.
// For demonstration, we'll assume they exist.
//
// /// ```
// /// use ristretto_classfile::attributes::{Annotation, AnnotationValuePair, AnnotationElement};
// ///
// /// // Assuming AnnotationElement::String { const_value_index: u16 } exists
// /// // and AnnotationValuePair { name_index: u16, value: AnnotationElement } exists
// ///
// /// let annotation = Annotation {
// /// type_index: 10, // Index in constant pool for annotation type
// /// num_element_value_pairs: 1,
// /// element_value_pairs: vec![
// /// AnnotationValuePair {
// /// name_index: 11, // Index for "value"
// /// value: AnnotationElement::String { const_value_index: 12 } // Index for "Hello"
// /// }
// /// ]
// /// };
// ///
// /// assert_eq!(annotation.type_index, 10);
// /// assert_eq!(annotation.element_value_pairs.len(), 1);
// /// ```
// Example for LineNumber
//
// /// ```
// /// use ristretto_classfile::attributes::LineNumber;
// ///
// /// let line_info = LineNumber {
// /// start_pc: 100, // Bytecode offset
// /// line_number: 42, // Source line number
// /// };
// ///
// /// assert_eq!(line_info.start_pc, 100);
// /// assert_eq!(line_info.line_number, 42);
// /// ```
// Example for MaxStack
//
// /// ```
// /// use ristretto_classfile::attributes::MaxStack;
// ///
// /// let max_stack_attr = MaxStack {
// /// max_stack: 16, // Maximum operand stack size
// /// };
// ///
// /// assert_eq!(max_stack_attr.max_stack, 16);
// /// ```
// Example for Attribute (generic structure)
//
// /// ```
// /// use ristretto_classfile::attributes::Attribute;
// ///
// /// // A generic attribute, e.g., a custom one or one whose content is opaque here.
// /// let generic_attribute = Attribute {
// /// attribute_name_index: 5, // Index in constant pool for attribute name
// /// attribute_length: 4,
// /// info: vec![0xDE, 0xAD, 0xBE, 0xEF], // Raw byte data
// /// };
// ///
// /// assert_eq!(generic_attribute.attribute_name_index, 5);
// /// assert_eq!(generic_attribute.info.len(), 4);
// /// ```