Skip to content

Commit

Permalink
add some new attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Apr 4, 2024
1 parent 95e0399 commit 42ab515
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 8 deletions.
32 changes: 32 additions & 0 deletions class/attr_runtime_invisible_annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package class

import (
"encoding/binary"
"fmt"
"github.com/phith0n/zkar/commons"
)

// AttrRuntimeInvisibleAnnotations https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.7.17
type AttrRuntimeInvisibleAnnotations struct {
*AttributeBase

Annotations []*Annotation
}

func (a *AttrRuntimeInvisibleAnnotations) readInfo(stream *commons.Stream) error {
bs, err := stream.ReadN(2)
if err != nil {
return fmt.Errorf("read AttrRuntimeInvisibleAnnotations failed, no enough data in the stream")
}

for i := uint16(0); i < binary.BigEndian.Uint16(bs); i++ {
annotation, err := NewAnnotation(stream)
if err != nil {
return fmt.Errorf("read AttrRuntimeInvisibleAnnotations failed, caused by: %v", err)
}

a.Annotations = append(a.Annotations, annotation)
}

return nil
}
51 changes: 51 additions & 0 deletions class/attr_runtime_invisible_parameter_annotations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package class

import (
"encoding/binary"
"fmt"
"github.com/phith0n/zkar/commons"
)

// AttrRuntimeInvisibleParameterAnnotations https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.7.19
type AttrRuntimeInvisibleParameterAnnotations struct {
*AttributeBase

Parameters []*ParameterAnnotation
}

func (a *AttrRuntimeInvisibleParameterAnnotations) readInfo(stream *commons.Stream) error {
bs, err := stream.ReadN(1)
if err != nil {
return fmt.Errorf("read AttrRuntimeInvisibleParameterAnnotations failed, no enough data in the stream")
}

for i := uint8(0); i < bs[0]; i++ {
p, err := a.readParameter(stream)
if err != nil {
return err
}

a.Parameters = append(a.Parameters, p)
}

return nil
}

func (a *AttrRuntimeInvisibleParameterAnnotations) readParameter(stream *commons.Stream) (*ParameterAnnotation, error) {
bs, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read AttrRuntimeInvisibleParameterAnnotations ParameterAnnotation failed, no enough data in the stream")
}

parameter := &ParameterAnnotation{}
for i := uint16(0); i < binary.BigEndian.Uint16(bs); i++ {
annotation, err := NewAnnotation(stream)
if err != nil {
return nil, fmt.Errorf("read AttrRuntimeInvisibleParameterAnnotations ParameterAnnotation failed, caused by: %v", err)
}

parameter.Annotations = append(parameter.Annotations, annotation)
}

return parameter, nil
}
1 change: 1 addition & 0 deletions class/attr_runtime_visible_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/phith0n/zkar/commons"
)

// AttrRuntimeVisibleAnnotations https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.7.16
type AttrRuntimeVisibleAnnotations struct {
*AttributeBase

Expand Down
7 changes: 4 additions & 3 deletions class/attr_runtime_visible_parameter_annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/phith0n/zkar/commons"
)

// AttrRuntimeVisibleParameterAnnotations https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.7.18
type AttrRuntimeVisibleParameterAnnotations struct {
*AttributeBase

Expand All @@ -19,7 +20,7 @@ type ParameterAnnotation struct {
func (a *AttrRuntimeVisibleParameterAnnotations) readInfo(stream *commons.Stream) error {
bs, err := stream.ReadN(1)
if err != nil {
return fmt.Errorf("read Annotation failed, no enough data in the stream")
return fmt.Errorf("read AttrRuntimeVisibleParameterAnnotations failed, no enough data in the stream")
}

for i := uint8(0); i < bs[0]; i++ {
Expand All @@ -37,14 +38,14 @@ func (a *AttrRuntimeVisibleParameterAnnotations) readInfo(stream *commons.Stream
func (a *AttrRuntimeVisibleParameterAnnotations) readParameter(stream *commons.Stream) (*ParameterAnnotation, error) {
bs, err := stream.ReadN(2)
if err != nil {
return nil, fmt.Errorf("read ParameterAnnotation failed, no enough data in the stream")
return nil, fmt.Errorf("read AttrRuntimeVisibleParameterAnnotations ParameterAnnotation failed, no enough data in the stream")
}

parameter := &ParameterAnnotation{}
for i := uint16(0); i < binary.BigEndian.Uint16(bs); i++ {
annotation, err := NewAnnotation(stream)
if err != nil {
return nil, fmt.Errorf("read ParameterAnnotation failed, caused by: %v", err)
return nil, fmt.Errorf("read AttrRuntimeVisibleParameterAnnotations ParameterAnnotation failed, caused by: %v", err)
}

parameter.Annotations = append(parameter.Annotations, annotation)
Expand Down
33 changes: 33 additions & 0 deletions class/attr_runtime_visible_type_annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package class

import (
"encoding/binary"
"fmt"
"github.com/phith0n/zkar/commons"
)

// AttrRuntimeVisibleTypeAnnotations https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.7.20
type AttrRuntimeVisibleTypeAnnotations struct {
*AttributeBase

Annotations []*TypeAnnotation
}

func (a *AttrRuntimeVisibleTypeAnnotations) readInfo(stream *commons.Stream) error {
bs, err := stream.ReadN(2)
if err != nil {
return fmt.Errorf("read AttrRuntimeVisibleTypeAnnotations failed, no enough data in the stream")
}

for i := uint16(0); i < binary.BigEndian.Uint16(bs); i++ {
var annotation *TypeAnnotation
annotation, err = NewTypeAnnotation(stream)
if err != nil {
return fmt.Errorf("read AttrRuntimeVisibleTypeAnnotations TypeAnnotation[%d] failed, caused by: %v", i, err)
}

a.Annotations = append(a.Annotations, annotation)
}

return nil
}
12 changes: 7 additions & 5 deletions class/element_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ type ElementValue struct {
Tag byte

ConstValueIndex uint16
EnumConstValue *EnumConstValue
ClassInfoIndex uint16
EnumConstValue *EnumConstValue
ClassInfoIndex uint16
AnnotationValue *Annotation
ArrayValue []*ElementValue
ArrayValue []*ElementValue
}

type EnumConstValue struct {
TypeNameIndex uint16
TypeNameIndex uint16
ConstNameIndex uint16
}

Expand Down Expand Up @@ -47,7 +47,7 @@ func NewElementValue(stream *commons.Stream) (*ElementValue, error) {
}

element.EnumConstValue = &EnumConstValue{
TypeNameIndex: binary.BigEndian.Uint16(bs[:2]),
TypeNameIndex: binary.BigEndian.Uint16(bs[:2]),
ConstNameIndex: binary.BigEndian.Uint16(bs[2:]),
}
case 'c':
Expand Down Expand Up @@ -78,6 +78,8 @@ func NewElementValue(stream *commons.Stream) (*ElementValue, error) {

element.ArrayValue = append(element.ArrayValue, subElement)
}
default:
return nil, fmt.Errorf("read ElementValue tag failed, tag %v not supported", element.Tag)
}

return element, nil
Expand Down
80 changes: 80 additions & 0 deletions class/type_annotation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package class

import (
"fmt"
"github.com/phith0n/zkar/commons"
)

// TypeAnnotation https://docs.oracle.com/javase/specs/jvms/se17/html/jvms-4.html#jvms-4.7.20
type TypeAnnotation struct {
TargetType uint8
TypeParameterTarget *TypeParameterTarget
SuperTypeTarget *SuperTypeTarget
TypeParameterBoundTarget *TypeParameterBoundTarget
EmptyTarget *EmptyTarget
FormalParameterTarget *FormalParameterTarget
ThrowsTarget *ThrowsTarget
LocalVarTarget *LocalVarTarget
CatchTarget *CatchTarget
OffsetTarget *OffsetTarget
TypeArgumentTarget *TypeArgumentTarget

TargetPath *TypePath

// same as Annotation
TypeIndex uint16
ElementValuePairs []*ElementValuePair
}

func NewTypeAnnotation(stream *commons.Stream) (*TypeAnnotation, error) {
bs, err := stream.ReadN(1)
if err != nil {
return nil, fmt.Errorf("read TypeAnnotation TargetType failed, no enough data in the stream")
}

ta := &TypeAnnotation{
TargetType: bs[0],
}
switch ta.TargetType {
case 0x00, 0x01:
ta.TypeParameterTarget, err = NewTypeParameterTarget(stream)
case 0x10:
ta.SuperTypeTarget, err = NewSuperTypeTarget(stream)
case 0x11, 0x12:
ta.TypeParameterBoundTarget, err = NewTypeParameterBoundTarget(stream)
case 0x13, 0x14, 0x15:
ta.EmptyTarget = &EmptyTarget{}
case 0x16:
ta.FormalParameterTarget, err = NewFormalParameterTarget(stream)
case 0x17:
ta.ThrowsTarget, err = NewThrowsTarget(stream)
case 0x40, 0x41:
ta.LocalVarTarget, err = NewLocalVarTarget(stream)
case 0x42:
ta.CatchTarget, err = NewCatchTarget(stream)
case 0x43, 0x44, 0x45, 0x46:
ta.OffsetTarget, err = NewOffsetTarget(stream)
case 0x47, 0x48, 0x49, 0x4A, 0x4B:
ta.TypeArgumentTarget, err = NewTypeArgumentTarget(stream)
default:
return nil, fmt.Errorf("read TypeAnnotation failed, TargetType %v not found", ta.TargetType)
}

if err != nil {
return nil, fmt.Errorf("read TypeAnnotation TargetInfo failed, caused by: %v", err)
}

ta.TargetPath, err = NewTypePath(stream)
if err != nil {
return nil, fmt.Errorf("read TypeAnnotation TargetPath failed, caused by: %v", err)
}

annotation, err := NewAnnotation(stream)
if err != nil {
return nil, fmt.Errorf("read TypeAnnotation Annotation failed, caused by: %v", err)
}

ta.TypeIndex = annotation.TypeIndex
ta.ElementValuePairs = annotation.ElementValuePairs
return ta, nil
}
Loading

0 comments on commit 42ab515

Please sign in to comment.