Skip to content

Commit

Permalink
#4 Refactor Tree Node Generator for Attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
amosshi committed Sep 8, 2019
1 parent 6981b2d commit b6ca698
Show file tree
Hide file tree
Showing 39 changed files with 2,001 additions and 2,096 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public enum AccessFlag {
* Declared as an <code>enum</code> type.
*/
ACC_ENUM(0x4000, "enum"),
/**
* Is a module, not a class or interface.
*/
ACC_MODULE(0x8000, "module"),
/**
* Indicates that the formal parameter was implicitly declared in source
* code, according to the specification of the language in which the source
Expand Down Expand Up @@ -141,6 +145,7 @@ public enum AccessFlag {
AccessFlag.ForClass.add(AccessFlag.ACC_SYNTHETIC);
AccessFlag.ForClass.add(AccessFlag.ACC_ANNOTATION);
AccessFlag.ForClass.add(AccessFlag.ACC_ENUM);
AccessFlag.ForClass.add(AccessFlag.ACC_MODULE);

// Access flags for a Field
AccessFlag.ForField.add(AccessFlag.ACC_PUBLIC);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.freeinternals.format.classfile.attribute;

import java.io.IOException;
import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.core.FileComponent;
import org.freeinternals.commonlib.core.PosDataInputStream;
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;
import org.freeinternals.format.FileFormatException;
import org.freeinternals.format.classfile.ClassFile;
import org.freeinternals.format.classfile.u2;

/**
Expand Down Expand Up @@ -73,6 +76,147 @@ public Annotation.ElementValuePair getElementvaluePair(final int index) {
}
return p;
}


// 4.7.16, 4.7.17: The RuntimeAnnotations Attribute
protected static void generateSubnode(final DefaultMutableTreeNode rootNode, final Annotation.ElementValue elementValue, final ClassFile classFile) {

int startPosMoving = elementValue.getStartPos();
char tag = elementValue.tag;
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
1,
"tag: " + tag + " - " + ElementValue.TagEnum.getType(tag)
)));
startPosMoving += 1;

if (elementValue.union_const_value_index != null) {
int constValueIndex = elementValue.union_const_value_index.value;
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"const_value_index: " + constValueIndex + " - " + classFile.getCPDescription(constValueIndex)
)));
} else if (elementValue.union_enum_const_value != null) {
int cp_index = elementValue.union_enum_const_value.type_name_index.value;
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"type_name_index: " + cp_index + " - " + classFile.getCPDescription(cp_index)
)));
startPosMoving += u2.LENGTH;

cp_index = elementValue.union_enum_const_value.const_name_index.value;
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"const_name_index: " + cp_index + " - " + classFile.getCPDescription(cp_index)
)));
} else if (elementValue.union_class_info_index != null) {
int classInfoIndex = elementValue.union_class_info_index.value;
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"class_info_index: " + classInfoIndex + " - " + classFile.getCPDescription(classInfoIndex)
)));
} else if (elementValue.union_annotation_value != null) {
generateSubnode(rootNode, elementValue.union_annotation_value, classFile);
} else if (elementValue.union_array_value != null) {
int num_values = elementValue.union_array_value.num_values.value;
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"num_values: " + num_values
)));
startPosMoving += u2.LENGTH;

if (elementValue.union_array_value.values != null
&& elementValue.union_array_value.values.length > 0) {
DefaultMutableTreeNode values = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
elementValue.getLength() - 3,
"values: " + num_values
));
rootNode.add(values);

for (int i = 0; i < elementValue.union_array_value.values.length; i++) {
DefaultMutableTreeNode value = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
elementValue.union_array_value.values[i].getStartPos(),
elementValue.union_array_value.values[i].getLength(),
"value " + (i + 1)
));
values.add(value);
generateSubnode(value, elementValue.union_array_value.values[i], classFile);
}
}
}
}


// 4.7.16, 4.7.17: The RuntimeAnnotations Attribute
private static void generateSubnode(final DefaultMutableTreeNode rootNode, final Annotation.ElementValuePair pair, final ClassFile classFile) {

int startPosMoving = pair.getStartPos();
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
2,
"element_name_index: " + pair.element_name_index.value + " - " + classFile.getCPDescription(pair.element_name_index.value)
)));
DefaultMutableTreeNode value = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving + 2,
pair.getLength() - 2,
"value"
));
rootNode.add(value);

generateSubnode(value, pair.value, classFile);

}

// 4.7.16, 4.7.17: The RuntimeAnnotations Attribute
protected static void generateSubnode(final DefaultMutableTreeNode rootNode, final Annotation a, final ClassFile classFile) {
generateSubnode(rootNode, a, a.getStartPos(), classFile);
}

// 4.7.16, 4.7.17: The RuntimeAnnotations Attribute
protected static void generateSubnode(final DefaultMutableTreeNode rootNode, final Annotation a, int currentPos, final ClassFile classFile) {

// int currentPos = a.getStartPos();
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
currentPos,
u2.LENGTH,
"type_index: " + a.type_index.value + " - " + classFile.getCPDescription(a.type_index.value)
)));
currentPos += u2.LENGTH;
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
currentPos,
u2.LENGTH,
"num_element_value_pairs: " + a.num_element_value_pairs.value
)));
currentPos += u2.LENGTH;

if (a.num_element_value_pairs.value > 0) {
DefaultMutableTreeNode element_value_pairs = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
currentPos,
a.getStartPos() + a.getLength() - currentPos,
"element_value_pairs"
));
rootNode.add(element_value_pairs);

for (int i = 0; i < a.num_element_value_pairs.value; i++) {
Annotation.ElementValuePair pair = a.getElementvaluePair(i);
DefaultMutableTreeNode element_value_pair_node = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
pair.getStartPos(),
pair.getLength(),
String.format("element_value_pair %d", i + 1)
));
element_value_pairs.add(element_value_pair_node);

generateSubnode(element_value_pair_node, pair, classFile);
}
}
}


/**
* Each value of the {@link Annotation#element_value_pairs} table represents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/
package org.freeinternals.format.classfile.attribute;

import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.core.PosDataInputStream;
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;
import org.freeinternals.format.FileFormatException;
import org.freeinternals.format.classfile.ClassFile;
import org.freeinternals.format.classfile.JavaSEVersion;
Expand Down Expand Up @@ -50,4 +52,16 @@ public class AttributeAnnotationDefault extends AttributeInfo {
this.default_value = new Annotation.ElementValue(posDataInputStream);
super.checkSize(posDataInputStream.getPos());
}

@Override
public void generateTreeNode(DefaultMutableTreeNode parentNode, ClassFile classFile) {
DefaultMutableTreeNode default_value_node = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
super.startPos + 6,
this.getLength() - 6,
"default_value"
));
parentNode.add(default_value_node);

Annotation.generateSubnode(default_value_node, this.default_value, classFile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
package org.freeinternals.format.classfile.attribute;

import java.io.IOException;
import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.core.FileComponent;
import org.freeinternals.commonlib.core.PosDataInputStream;
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;
import org.freeinternals.format.FileFormatException;
import org.freeinternals.format.classfile.ClassFile;
import org.freeinternals.format.classfile.JavaSEVersion;
Expand Down Expand Up @@ -59,6 +61,76 @@ public class AttributeBootstrapMethods extends AttributeInfo {
super.checkSize(posDataInputStream.getPos());
}

@Override
public void generateTreeNode(DefaultMutableTreeNode parentNode, final ClassFile classFile) {
int startPosMoving = this.getStartPos();

parentNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving + 6,
2,
"num_bootstrap_methods: " + this.num_bootstrap_methods.value
)));

if (this.bootstrap_methods != null && this.bootstrap_methods.length > 0) {
DefaultMutableTreeNode bootstrap_methods_node = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving + 8,
this.getLength() - 8,
"bootstrap_methods"));
parentNode.add(bootstrap_methods_node);

for (int i = 0; i < this.bootstrap_methods.length; i++) {
BootstrapMethod m = this.bootstrap_methods[i];
DefaultMutableTreeNode bootstrap_method = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
m.getStartPos(),
m.getLength(),
String.format("bootstrap_method %d", i + 1)
));
bootstrap_methods_node.add(bootstrap_method);
this.generateSubnode(bootstrap_method, m, classFile);
}
}
}

/**
* Generate tree node for {@link BootstrapMethod}.
*/
private void generateSubnode(final DefaultMutableTreeNode rootNode, final BootstrapMethod m, final ClassFile classFile) {

int startPosMoving = m.getStartPos();

rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"bootstrap_method_ref: " + m.bootstrap_method_ref.value + " - " + classFile.getCPDescription(m.bootstrap_method_ref.value)
)));
startPosMoving += u2.LENGTH;

rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"num_bootstrap_arguments: " + m.num_bootstrap_arguments.value
)));
startPosMoving += u2.LENGTH;

if (m.bootstrap_arguments != null && m.bootstrap_arguments.length > 0) {
DefaultMutableTreeNode bootstrap_arguments = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
m.bootstrap_arguments.length * u2.LENGTH,
"bootstrap_arguments"));
rootNode.add(bootstrap_arguments);

for (int i = 0; i < m.bootstrap_arguments.length; i++) {
DefaultMutableTreeNode bootstrap_method = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving,
u2.LENGTH,
"argument " + (i + 1) + ": " + m.bootstrap_arguments[i].value + " - " + classFile.getCPDescription(m.bootstrap_arguments[i].value)
));
startPosMoving += u2.LENGTH;
bootstrap_arguments.add(bootstrap_method);
}
}
}

public final class BootstrapMethod extends FileComponent {

/**
Expand Down
Loading

0 comments on commit b6ca698

Please sign in to comment.