Skip to content

Commit 60c0cde

Browse files
committed
Updated and extended attribute rendering
1 parent 46e08df commit 60c0cde

File tree

3 files changed

+166
-36
lines changed

3 files changed

+166
-36
lines changed

src/bytecode/classfile.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,15 @@ pub enum Attribute {
328328
Code { max_stack: u16, max_locals: u16, code: Vec<Instruction>, exception_table: Vec<ExceptionHandler>, attributes: Vec<Attribute> },
329329
StackMapTable(Vec<StackMapFrame>),
330330
Exceptions(Vec<ConstantPoolIndex>),
331-
InnerClass(Vec<InnerClass>),
331+
InnerClasses(Vec<InnerClass>),
332332
EnclosingMethod { class_index: ConstantPoolIndex, method_index: ConstantPoolIndex },
333333
Synthetic,
334334
Signature(ConstantPoolIndex),
335335
SourceFile(ConstantPoolIndex),
336336
SourceDebugExtension(Vec<u8>),
337-
LineNumbeTable(Vec<LineNumberTable>),
337+
LineNumberTable(Vec<LineNumberTable>),
338338
LocalVariableTable(Vec<LocalVariableTable>),
339-
LocalVariableTableType(Vec<LocalVariableTableType>),
339+
LocalVariableTypeTable(Vec<LocalVariableTypeTable>),
340340
Deprecated,
341341
RuntimeVisibleAnnotations(Vec<Annotation>),
342342
RuntimeInvisibleAnnotations(Vec<Annotation>),
@@ -432,7 +432,7 @@ pub struct LocalVariableTable {
432432
}
433433

434434
#[derive(Debug)]
435-
pub struct LocalVariableTableType {
435+
pub struct LocalVariableTypeTable {
436436
pub start_pc: u16,
437437
pub length: u16,
438438
pub name_index: ConstantPoolIndex,
@@ -446,22 +446,45 @@ pub struct Annotation {
446446
pub element_value_pairs: Vec<ElementValuePair>
447447
}
448448

449+
impl Annotation {
450+
pub fn len(&self) -> usize {
451+
self.element_value_pairs.iter().fold(2, |acc, x| acc + x.len())
452+
}
453+
}
454+
449455
#[derive(Debug)]
450456
pub struct ElementValuePair {
451457
pub element_name_index: ConstantPoolIndex,
452458
pub value: ElementValue
453459
}
454460

461+
impl ElementValuePair {
462+
pub fn len(&self) -> usize {
463+
2 + self.value.len()
464+
}
465+
}
455466

456467
#[derive(Debug)]
457468
pub enum ElementValue {
458-
ConstantValue(ConstantPoolIndex),
469+
ConstantValue(u8, ConstantPoolIndex),
459470
Enum { type_name_index: ConstantPoolIndex, const_name_index: ConstantPoolIndex },
460471
ClassInfo(ConstantPoolIndex),
461472
Annotation(Annotation),
462473
Array(Vec<ElementValue>)
463474
}
464475

476+
impl ElementValue {
477+
pub fn len(&self) -> usize {
478+
match self {
479+
&ElementValue::ConstantValue(_, _) => 3,
480+
&ElementValue::Enum { type_name_index: _, const_name_index: _ } => 5,
481+
&ElementValue::ClassInfo(_) => 3,
482+
&ElementValue::Annotation(ref annotation) => 1 + annotation.len(),
483+
&ElementValue::Array(ref table) => table.iter().fold(3, |acc, x| acc + x.len())
484+
}
485+
}
486+
}
487+
465488
#[derive(Debug)]
466489
pub struct TypeAnnotation {
467490
pub target_info: TargetInfo,
@@ -503,12 +526,19 @@ pub struct BootstrapMethod {
503526
pub bootstrap_arguments: Vec<ConstantPoolIndex>
504527
}
505528

529+
impl BootstrapMethod {
530+
}
531+
506532
#[derive(Debug)]
507533
pub struct MethodParameter {
508534
pub name_index: ConstantPoolIndex,
509535
pub access_flags: AccessFlags
510536
}
511537

538+
impl MethodParameter {
539+
pub fn len(&self) -> usize { 4 }
540+
}
541+
512542
#[allow(non_camel_case_types)]
513543
#[derive(Debug)]
514544
pub enum Instruction {

src/bytecode/io/reader.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ impl ClassReader {
629629
let n = reader.get_u16();
630630
(0..n).map(|_| ConstantPoolIndex::new(reader.get_u16() as usize)).collect()
631631
})),
632-
"InnerClass" => Some(Attribute::InnerClass({
632+
"InnerClasses" => Some(Attribute::InnerClasses({
633633
let n = reader.get_u16();
634634
(0..n).map(|_| InnerClass {
635635
inner_class_info_index: ConstantPoolIndex::new(reader.get_u16() as usize),
@@ -643,7 +643,7 @@ impl ClassReader {
643643
"Signature" => Some(Attribute::Signature(ConstantPoolIndex::new(reader.get_u16() as usize))),
644644
"SourceFile" => Some(Attribute::SourceFile(ConstantPoolIndex::new(reader.get_u16() as usize))),
645645
"SourceDebugExtension" => Some(Attribute::SourceDebugExtension(reader.get_bytes())),
646-
"LineNumberTable" => Some(Attribute::LineNumbeTable({
646+
"LineNumberTable" => Some(Attribute::LineNumberTable({
647647
let n = reader.get_u16();
648648
(0..n).map(|_| LineNumberTable {
649649
start_pc: reader.get_u16(),
@@ -660,9 +660,9 @@ impl ClassReader {
660660
index: reader.get_u16()
661661
}).collect()
662662
})),
663-
"LocalVariableTypeTable" => Some(Attribute::LocalVariableTableType({
663+
"LocalVariableTypeTable" => Some(Attribute::LocalVariableTypeTable({
664664
let n = reader.get_u16();
665-
(0..n).map(|_| LocalVariableTableType {
665+
(0..n).map(|_| LocalVariableTypeTable {
666666
start_pc: reader.get_u16(),
667667
length: reader.get_u16(),
668668
name_index: ConstantPoolIndex::new(reader.get_u16() as usize),
@@ -792,15 +792,15 @@ impl ClassReader {
792792
let tag = reader.get_u8();
793793

794794
match tag {
795-
66 /* B */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
796-
67 /* C */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
797-
68 /* D */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
798-
70 /* F */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
799-
73 /* I */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
800-
74 /* J */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
801-
83 /* S */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
802-
90 /* Z */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
803-
115 /* s */ => ElementValue::ConstantValue(ConstantPoolIndex::new(reader.get_u16() as usize)),
795+
66 /* B */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
796+
67 /* C */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
797+
68 /* D */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
798+
70 /* F */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
799+
73 /* I */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
800+
74 /* J */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
801+
83 /* S */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
802+
90 /* Z */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
803+
115 /* s */ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(reader.get_u16() as usize)),
804804
101 /* e */ => ElementValue::Enum {
805805
type_name_index: ConstantPoolIndex::new(reader.get_u16() as usize),
806806
const_name_index: ConstantPoolIndex::new(reader.get_u16() as usize) },
@@ -810,7 +810,7 @@ impl ClassReader {
810810
let n = reader.get_u16();
811811
(0..n).map(|_| ClassReader::read_element_value(reader)).collect()
812812
}),
813-
_ => ElementValue::ConstantValue(ConstantPoolIndex::new(0)) // TODO this deserves a better error handling
813+
_ => ElementValue::ConstantValue(tag, ConstantPoolIndex::new(0)) // TODO this deserves a better error handling
814814
}
815815
}
816816

0 commit comments

Comments
 (0)