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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::byte_reader::ByteReader;
use crate::error::Result;
use bitflags::bitflags;
use byteorder::{BigEndian, WriteBytesExt};
use std::fmt;
bitflags! {
/// Method access flags used in Java class files to specify the access permissions and
/// properties of methods.
///
/// These flags determine visibility (public, private, protected), mutability (final), execution
/// context (static, synchronized, native), and other characteristics of class methods. Multiple
/// flags can be combined using bitwise OR operations.
///
/// # Examples
///
/// Creating method access flags for common method types:
///
/// ```rust
/// use ristretto_classfile::MethodAccessFlags;
/// use ristretto_classfile::byte_reader::ByteReader;
///
/// // A public static method
/// let flags = MethodAccessFlags::PUBLIC | MethodAccessFlags::STATIC;
///
/// // Check if specific flags are set
/// assert!(flags.contains(MethodAccessFlags::PUBLIC));
/// assert!(flags.contains(MethodAccessFlags::STATIC));
/// assert!(!flags.contains(MethodAccessFlags::FINAL));
/// assert!(!flags.contains(MethodAccessFlags::SYNCHRONIZED));
///
/// // Get a code representation
/// assert_eq!("public static", flags.as_code());
///
/// // Serialize to bytes
/// let mut bytes = Vec::new();
/// flags.to_bytes(&mut bytes)?;
/// assert_eq!(vec![0x00, 0x09], bytes); // 0x0009 = PUBLIC | STATIC
///
/// // Deserialize from bytes
/// let mut reader = ByteReader::new(&bytes);
/// let deserialized = MethodAccessFlags::from_bytes(&mut reader)?;
/// assert_eq!(flags, deserialized);
///
/// // Display as string
/// assert_eq!("(0x0009) ACC_PUBLIC, ACC_STATIC", flags.to_string());
/// # Ok::<(), ristretto_classfile::Error>(())
/// ```
///
/// # References
///
/// See: <https://docs.oracle.com/javase/specs/jvms/se25/html/jvms-4.html#jvms-4.5:~:text=method_info%20structure%20are%20as%20follows%3A-,access_flags,-The%20value%20of%20the%20access_flags>
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct MethodAccessFlags: u16 {
/// Declared public; may be accessed from outside its package.
const PUBLIC = 0x0001;
/// Declared private; accessible only within the defining class and other classes belonging to the same nest (§5.4.4).
const PRIVATE = 0x0002;
/// Declared protected; may be accessed within subclasses.
const PROTECTED = 0x0004;
/// Declared static.
const STATIC = 0x0008;
/// Declared final; must not be overridden (§5.4.5).
const FINAL = 0x0010;
/// Declared synchronized; invocation is wrapped by a monitor use.
const SYNCHRONIZED = 0x0020;
/// A bridge method, generated by the compiler.
const BRIDGE = 0x0040;
/// Declared with variable number of arguments.
const VARARGS = 0x0080;
/// Declared native; implemented in a language other than the Java programming language.
const NATIVE = 0x0100;
/// Declared abstract; no implementation is provided.
const ABSTRACT = 0x0400;
/// In a class file whose major version number is at least 46 and at most 60: Declared strictfp.
const STRICT = 0x0800;
/// Declared synthetic; not present in the source code.
const SYNTHETIC = 0x1000;
}
}
impl Default for MethodAccessFlags {
/// Returns an empty set of method access flags.
///
/// # Examples
///
/// ```rust
/// use ristretto_classfile::MethodAccessFlags;
///
/// let flags = MethodAccessFlags::default();
/// assert!(flags.is_empty());
/// assert_eq!(flags.bits(), 0);
/// ```
fn default() -> MethodAccessFlags {
MethodAccessFlags::empty()
}
}
impl MethodAccessFlags {
/// Deserialize the `MethodAccessFlags` from bytes.
///
/// This method reads a 16-bit big-endian value from the provided byte reader and constructs a
/// `MethodAccessFlags` value from it.
///
/// # Errors
///
/// Returns an error if reading from the byte reader fails.
///
/// # Examples
///
/// ```rust
/// use ristretto_classfile::MethodAccessFlags;
/// use ristretto_classfile::byte_reader::ByteReader;
///
/// // Create a reader with bytes representing PUBLIC | STATIC (0x0009)
/// let mut bytes = ByteReader::new(&[0x00, 0x09]);
/// let flags = MethodAccessFlags::from_bytes(&mut bytes)?;
///
/// assert!(flags.contains(MethodAccessFlags::PUBLIC));
/// assert!(flags.contains(MethodAccessFlags::STATIC));
/// assert_eq!(flags.bits(), 0x0009);
/// # Ok::<(), ristretto_classfile::Error>(())
/// ```
pub fn from_bytes(bytes: &mut ByteReader<'_>) -> Result<MethodAccessFlags> {
let access_flags = bytes.read_u16()?;
let method_access_flags = MethodAccessFlags::from_bits_truncate(access_flags);
Ok(method_access_flags)
}
/// Serialize the `MethodAccessFlags` to bytes.
///
/// This method writes the flags as a 16-bit big-endian value to the provided byte vector.
///
/// # Examples
///
/// ```rust
/// use ristretto_classfile::MethodAccessFlags;
///
/// let flags = MethodAccessFlags::PUBLIC | MethodAccessFlags::STATIC;
/// let mut bytes = Vec::new();
///
/// flags.to_bytes(&mut bytes)?;
/// assert_eq!(bytes, vec![0x00, 0x09]); // 0x0009 in big-endian
/// # Ok::<(), ristretto_classfile::Error>(())
/// ```
///
/// # Errors
/// Returns an error if writing to the byte vector fails.
pub fn to_bytes(&self, bytes: &mut Vec<u8>) -> Result<()> {
bytes.write_u16::<BigEndian>(self.bits())?;
Ok(())
}
/// Get the `MethodAccessFlags` as a string of Java modifiers.
///
/// This method returns a string representation of the access flags as they would
/// appear in Java source code. Note that not all flags (like BRIDGE, VARARGS, etc.)
/// have a corresponding Java modifier and will be omitted from the result.
///
/// # Examples
///
/// ```rust
/// use ristretto_classfile::MethodAccessFlags;
///
/// // Single flags
/// assert_eq!("public", MethodAccessFlags::PUBLIC.as_code());
/// assert_eq!("static", MethodAccessFlags::STATIC.as_code());
///
/// // Multiple flags
/// let flags = MethodAccessFlags::PUBLIC | MethodAccessFlags::STATIC | MethodAccessFlags::FINAL;
/// assert_eq!("public static final", flags.as_code());
///
/// // Flags without Java modifiers return empty strings
/// assert_eq!("", MethodAccessFlags::empty().as_code());
/// ```
#[must_use]
pub fn as_code(&self) -> String {
let mut modifiers = Vec::new();
if self.contains(MethodAccessFlags::PUBLIC) {
modifiers.push("public");
}
if self.contains(MethodAccessFlags::PRIVATE) {
modifiers.push("private");
}
if self.contains(MethodAccessFlags::PROTECTED) {
modifiers.push("protected");
}
if self.contains(MethodAccessFlags::STATIC) {
modifiers.push("static");
}
if self.contains(MethodAccessFlags::ABSTRACT) {
modifiers.push("abstract");
}
if self.contains(MethodAccessFlags::FINAL) {
modifiers.push("final");
}
if self.contains(MethodAccessFlags::SYNCHRONIZED) {
modifiers.push("synchronized");
}
if self.contains(MethodAccessFlags::NATIVE) {
modifiers.push("native");
}
modifiers.join(" ")
}
}
impl fmt::Display for MethodAccessFlags {
/// Formats the `MethodAccessFlags` as a string showing its hexadecimal value and a
/// comma-separated list of flag names.
///
/// # Examples
///
/// ```rust
/// use ristretto_classfile::MethodAccessFlags;
///
/// // Public method
/// let flags = MethodAccessFlags::PUBLIC;
/// assert_eq!("(0x0001) ACC_PUBLIC", flags.to_string());
///
/// // Public static final method
/// let flags = MethodAccessFlags::PUBLIC | MethodAccessFlags::STATIC | MethodAccessFlags::FINAL;
/// assert_eq!("(0x0019) ACC_PUBLIC, ACC_STATIC, ACC_FINAL", flags.to_string());
/// ```
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut access_flags = Vec::new();
if self.contains(MethodAccessFlags::PUBLIC) {
access_flags.push("ACC_PUBLIC");
}
if self.contains(MethodAccessFlags::PRIVATE) {
access_flags.push("ACC_PRIVATE");
}
if self.contains(MethodAccessFlags::PROTECTED) {
access_flags.push("ACC_PROTECTED");
}
if self.contains(MethodAccessFlags::STATIC) {
access_flags.push("ACC_STATIC");
}
if self.contains(MethodAccessFlags::FINAL) {
access_flags.push("ACC_FINAL");
}
if self.contains(MethodAccessFlags::SYNCHRONIZED) {
access_flags.push("ACC_SYNCHRONIZED");
}
if self.contains(MethodAccessFlags::BRIDGE) {
access_flags.push("ACC_BRIDGE");
}
if self.contains(MethodAccessFlags::VARARGS) {
access_flags.push("ACC_VARARGS");
}
if self.contains(MethodAccessFlags::NATIVE) {
access_flags.push("ACC_NATIVE");
}
if self.contains(MethodAccessFlags::ABSTRACT) {
access_flags.push("ACC_ABSTRACT");
}
if self.contains(MethodAccessFlags::STRICT) {
access_flags.push("ACC_STRICT");
}
if self.contains(MethodAccessFlags::SYNTHETIC) {
access_flags.push("ACC_SYNTHETIC");
}
write!(f, "({:#06X}) {}", self.bits(), access_flags.join(", "))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_default() {
assert_eq!(MethodAccessFlags::empty(), MethodAccessFlags::default());
}
#[test]
fn test_all_access_flags() {
let access_flags: u16 = u16::MAX;
let binding = access_flags.to_be_bytes();
let mut bytes = ByteReader::new(&binding);
assert_eq!(
Ok(MethodAccessFlags::PUBLIC
| MethodAccessFlags::PRIVATE
| MethodAccessFlags::PROTECTED
| MethodAccessFlags::STATIC
| MethodAccessFlags::FINAL
| MethodAccessFlags::SYNCHRONIZED
| MethodAccessFlags::BRIDGE
| MethodAccessFlags::VARARGS
| MethodAccessFlags::NATIVE
| MethodAccessFlags::ABSTRACT
| MethodAccessFlags::STRICT
| MethodAccessFlags::SYNTHETIC),
MethodAccessFlags::from_bytes(&mut bytes)
);
}
#[test]
fn test_access_flags() -> Result<()> {
let access_flags = MethodAccessFlags::PUBLIC | MethodAccessFlags::FINAL;
let mut bytes = Vec::new();
access_flags.to_bytes(&mut bytes)?;
let mut bytes = ByteReader::new(&bytes);
assert_eq!(Ok(access_flags), MethodAccessFlags::from_bytes(&mut bytes));
Ok(())
}
#[test]
fn test_as_code() {
assert_eq!("public", MethodAccessFlags::PUBLIC.as_code());
assert_eq!("private", MethodAccessFlags::PRIVATE.as_code());
assert_eq!("protected", MethodAccessFlags::PROTECTED.as_code());
assert_eq!("static", MethodAccessFlags::STATIC.as_code());
assert_eq!("final", MethodAccessFlags::FINAL.as_code());
assert_eq!("synchronized", MethodAccessFlags::SYNCHRONIZED.as_code());
assert_eq!("", MethodAccessFlags::BRIDGE.as_code());
assert_eq!("", MethodAccessFlags::VARARGS.as_code());
assert_eq!("native", MethodAccessFlags::NATIVE.as_code());
assert_eq!("abstract", MethodAccessFlags::ABSTRACT.as_code());
assert_eq!("", MethodAccessFlags::STRICT.as_code());
assert_eq!("", MethodAccessFlags::SYNTHETIC.as_code());
let access_flags =
MethodAccessFlags::PUBLIC | MethodAccessFlags::STATIC | MethodAccessFlags::FINAL;
assert_eq!("public static final", access_flags.as_code());
}
#[test]
fn test_to_string() {
assert_eq!("(0x0001) ACC_PUBLIC", MethodAccessFlags::PUBLIC.to_string());
assert_eq!(
"(0x0002) ACC_PRIVATE",
MethodAccessFlags::PRIVATE.to_string()
);
assert_eq!(
"(0x0004) ACC_PROTECTED",
MethodAccessFlags::PROTECTED.to_string()
);
assert_eq!("(0x0008) ACC_STATIC", MethodAccessFlags::STATIC.to_string());
assert_eq!("(0x0010) ACC_FINAL", MethodAccessFlags::FINAL.to_string());
assert_eq!(
"(0x0020) ACC_SYNCHRONIZED",
MethodAccessFlags::SYNCHRONIZED.to_string()
);
assert_eq!("(0x0040) ACC_BRIDGE", MethodAccessFlags::BRIDGE.to_string());
assert_eq!(
"(0x0080) ACC_VARARGS",
MethodAccessFlags::VARARGS.to_string()
);
assert_eq!("(0x0100) ACC_NATIVE", MethodAccessFlags::NATIVE.to_string());
assert_eq!(
"(0x0400) ACC_ABSTRACT",
MethodAccessFlags::ABSTRACT.to_string()
);
assert_eq!("(0x0800) ACC_STRICT", MethodAccessFlags::STRICT.to_string());
assert_eq!(
"(0x1000) ACC_SYNTHETIC",
MethodAccessFlags::SYNTHETIC.to_string()
);
let access_flags =
MethodAccessFlags::PUBLIC | MethodAccessFlags::STATIC | MethodAccessFlags::FINAL;
assert_eq!(
"(0x0019) ACC_PUBLIC, ACC_STATIC, ACC_FINAL",
access_flags.to_string()
);
}
}