<commit_before>package classfile

import (
	. "jvmgo/any"
	"jvmgo/util"
)

/*
RuntimeVisibleAnnotations_attribute {
    u2         attribute_name_index;
    u4         attribute_length;
    u2         num_annotations;
    annotation annotations[num_annotations];
}
RuntimeInvisibleAnnotations_attribute {
    u2         attribute_name_index;
    u4         attribute_length;
    u2         num_annotations;
    annotation annotations[num_annotations];
}
*/
type AnnotationsAttribute struct {
	annotations []*Annotation
}

func (self *AnnotationsAttribute) readInfo(reader *ClassReader) {
	numAnnotations := reader.readUint16()
	self.annotations = make([]*Annotation, numAnnotations)
	for i := range self.annotations {
		self.annotations[i] = readAnnotation(reader)
	}
}

/*
annotation {
    u2 type_index;
    u2 num_element_value_pairs;
    {   u2            element_name_index;
        element_value value;
    } element_value_pairs[num_element_value_pairs];
}
*/
type Annotation struct {
	typeIndex         uint16
	elementValuePairs []*ElementValuePair
}

func readAnnotation(reader *ClassReader) *Annotation {
	typeIndex := reader.readUint16()
	numElementValuePairs := reader.readUint16()
	elementValuePairs := make([]*ElementValuePair, numElementValuePairs)
	for i := range elementValuePairs {
		elementValuePairs[i] = readElementValuePair(reader)
	}
	return &Annotation{typeIndex, elementValuePairs}
}

type ElementValuePair struct {
	elementNameIndex uint16
	value            *ElementValue
}

func readElementValuePair(reader *ClassReader) *ElementValuePair {
	elementNameIndex := reader.readUint16()
	value := readElementValue(reader)
	return &ElementValuePair{elementNameIndex, value}
}

/*
element_value {
    u1 tag;
    union {
        u2 const_value_index;

        {   u2 type_name_index;
            u2 const_name_index;
        } enum_const_value;

        u2 class_info_index;

        annotation annotation_value;

        {   u2            num_values;
            element_value values[num_values];
        } array_value;
    } value;
}
*/
type ElementValue struct {
	tag   uint8
	value Any
}

func readElementValue(reader *ClassReader) *ElementValue {
	var value Any
	tag := reader.readUint8()
	switch tag {
	case 'B', 'C', 'D', 'F', 'I', 'J', 'S', 'Z', 's', 'c':
		value = reader.readUint16()
	case 'e':
		value = readEnumConstValue(reader)
	case '@':
		value = readAnnotation(reader)
	case '[':
		value = readArrayValue(reader)
	default:
		util.Panicf("BAD element_value tag: %v", tag)
	}
	return &ElementValue{tag, value}
}

type EnumConstValue struct {
	typeNameIndex  uint16
	constNameIndex uint16
}

func readEnumConstValue(reader *ClassReader) *EnumConstValue {
	typeNameIndex := reader.readUint16()
	constNameIndex := reader.readUint16()
	return &EnumConstValue{typeNameIndex, constNameIndex}
}

func readArrayValue(reader *ClassReader) []*ElementValue {
	numValues := reader.readUint16()
	values := make([]*ElementValue, numValues)
	for i := range values {
		values[i] = readElementValue(reader)
	}
	return values
}<commit_msg>code refactor<commit_after>package classfile

import (
	. "jvmgo/any"
	"jvmgo/util"
)

/*
RuntimeVisibleAnnotations_attribute {
    u2         attribute_name_index;
    u4         attribute_length;
    u2         num_annotations;
    annotation annotations[num_annotations];
}
RuntimeInvisibleAnnotations_attribute {
    u2         attribute_name_index;
    u4         attribute_length;
    u2         num_annotations;
    annotation annotations[num_annotations];
}
*/
type AnnotationsAttribute struct {
	annotations []*Annotation
}

func (self *AnnotationsAttribute) readInfo(reader *ClassReader) {
	numAnnotations := reader.readUint16()
	self.annotations = make([]*Annotation, numAnnotations)
	for i := range self.annotations {
		self.annotations[i] = readAnnotation(reader)
	}
}

/*
annotation {
    u2 type_index;
    u2 num_element_value_pairs;
    {   u2            element_name_index;
        element_value value;
    } element_value_pairs[num_element_value_pairs];
}
*/
type Annotation struct {
	typeIndex         uint16
	elementValuePairs []*ElementValuePair
}

func readAnnotation(reader *ClassReader) *Annotation {
	typeIndex := reader.readUint16()
	numElementValuePairs := reader.readUint16()
	elementValuePairs := make([]*ElementValuePair, numElementValuePairs)
	for i := range elementValuePairs {
		elementValuePairs[i] = readElementValuePair(reader)
	}
	return &Annotation{typeIndex, elementValuePairs}
}

type ElementValuePair struct {
	elementNameIndex uint16
	value            *ElementValue
}

func readElementValuePair(reader *ClassReader) *ElementValuePair {
	return &ElementValuePair{
		elementNameIndex: reader.readUint16(),
		value:            readElementValue(reader),
	}
}

/*
element_value {
    u1 tag;
    union {
        u2 const_value_index;

        {   u2 type_name_index;
            u2 const_name_index;
        } enum_const_value;

        u2 class_info_index;

        annotation annotation_value;

        {   u2            num_values;
            element_value values[num_values];
        } array_value;
    } value;
}
*/
type ElementValue struct {
	tag   uint8
	value Any
}

func readElementValue(reader *ClassReader) *ElementValue {
	var value Any
	tag := reader.readUint8()
	switch tag {
	case 'B', 'C', 'D', 'F', 'I', 'J', 'S', 'Z', 's', 'c':
		value = reader.readUint16()
	case 'e':
		value = readEnumConstValue(reader)
	case '@':
		value = readAnnotation(reader)
	case '[':
		value = readArrayValue(reader)
	default:
		util.Panicf("BAD element_value tag: %v", tag)
	}
	return &ElementValue{tag, value}
}

type EnumConstValue struct {
	typeNameIndex  uint16
	constNameIndex uint16
}

func readEnumConstValue(reader *ClassReader) *EnumConstValue {
	return &EnumConstValue{
		typeNameIndex:  reader.readUint16(),
		constNameIndex: reader.readUint16(),
	}
}

func readArrayValue(reader *ClassReader) []*ElementValue {
	numValues := reader.readUint16()
	values := make([]*ElementValue, numValues)
	for i := range values {
		values[i] = readElementValue(reader)
	}
	return values
}
{'fraction_non_alphanumeric': 0.10075732630885742, 'fraction_numerical': 0.0135001646361541, 'mean_word_length': 3.7985781990521326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3298852', 'n_tokens_mistral': 1987, 'n_tokens_neox': 1921, 'n_words': 409}