Skip to content

Commit

Permalink
#4 Java Class Viewer - support cp_index as zero
Browse files Browse the repository at this point in the history
  • Loading branch information
amosshi committed Oct 4, 2019
1 parent 3c58000 commit 148e273
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
* </a>
*/
public class AttributeCode extends AttributeInfo {

public static final String ATTRIBUTE_CODE_NODE = "code";

public transient final u2 max_stack;
Expand Down Expand Up @@ -231,14 +232,13 @@ public void generateTreeNode(DefaultMutableTreeNode parentNode, final ClassFile
(i + 1) + ". " + attr.getName()
));
AttributeInfo.generateTreeNode(treeNodeAttributeItem, attr, classFile);

treeNodeAttribute.add(treeNodeAttributeItem);
}

parentNode.add(treeNodeAttribute);
}
}


private void generateSubnode(final DefaultMutableTreeNode rootNode, final AttributeCode.ExceptionTable et, final ClassFile classFile) {
if (et == null) {
Expand All @@ -262,14 +262,14 @@ private void generateSubnode(final DefaultMutableTreeNode rootNode, final Attrib
2,
"handler_pc: " + et.handler_pc.value
)));
int catch_type = et.catch_type.value;
final int catch_type = et.catch_type.value;
String catch_type_desc = (catch_type == 0) ? "" : " - " + classFile.getCPDescription(catch_type);
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving + 6,
2,
"catch_type: " + catch_type + " - " + classFile.getCPDescription(catch_type)
"catch_type: " + catch_type + catch_type_desc
)));
}


/**
* The {@code exception_table} structure in {@code Code} attribute.
Expand All @@ -282,6 +282,18 @@ public final class ExceptionTable extends FileComponent {
public transient final u2 start_pc;
public transient final u2 end_pc;
public transient final u2 handler_pc;

/**
* If the value of the catch_type item is nonzero, it must be a valid
* index into the constant_pool table. The constant_pool entry at that
* index must be a CONSTANT_Class_info structure representing a class of
* exceptions that this exception handler is designated to catch. The
* exception handler will be called only if the thrown exception is an
* instance of the given class or one of its subclasses.
*
* If the value of the catch_type item is zero, this exception handler
* is called for all exceptions.
*/
public transient final u2 catch_type;

private ExceptionTable(final PosDataInputStream posDataInputStream) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void generateTreeNode(DefaultMutableTreeNode parentNode, final ClassFile
parentNode.add(treeNodeInnerClass);
}
}

private void generateSubnode(final DefaultMutableTreeNode rootNode, final AttributeInnerClasses.Class innerClass, final ClassFile classFile) {
final int startPosMoving = innerClass.getStartPos();

Expand All @@ -124,17 +124,19 @@ private void generateSubnode(final DefaultMutableTreeNode rootNode, final Attrib
)));

cp_index = innerClass.outer_class_info_index.value;
final String outer_class_info_index_desc = (cp_index == 0) ? "" : " - " + classFile.getCPDescription(cp_index);
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving + 2,
2,
"outer_class_info_index: " + cp_index + " - " + classFile.getCPDescription(cp_index)
"outer_class_info_index: " + cp_index + outer_class_info_index_desc
)));

cp_index = innerClass.inner_name_index.value;
final String inner_name_index_desc = (cp_index == 0) ? "" : " - " + classFile.getCPDescription(cp_index);
rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPosMoving + 4,
2,
"inner_name_index: " + cp_index + " - " + classFile.getCPDescription(cp_index)
"inner_name_index: " + cp_index + inner_name_index_desc
)));

rootNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
Expand All @@ -143,7 +145,6 @@ private void generateSubnode(final DefaultMutableTreeNode rootNode, final Attrib
"inner_class_access_flags: " + innerClass.inner_class_access_flags.value + " - " + innerClass.getModifiers()
)));
}


/**
* The {@code classes} structure in {@code InnerClasses} attribute.
Expand All @@ -158,7 +159,29 @@ public final class Class extends FileComponent {
public static final int LENGTH = 8;

public transient final u2 inner_class_info_index;
/**
* If C is not a member of a class or an interface - that is, if C is a
* top-level class or interface or a local class or an anonymous class -
* then the value of the outer_class_info_index item must be zero.
*
* Otherwise, the value of the outer_class_info_index item must be a
* valid index into the constant_pool table, and the entry at that index
* must be a CONSTANT_Class_info structure representing the class or
* interface of which C is a member. The value of the
* outer_class_info_index item must not equal the the value of the
* inner_class_info_index item.
*/
public transient final u2 outer_class_info_index;
/**
* If C is anonymous, the value of the inner_name_index item must be
* zero.
*
* Otherwise, the value of the inner_name_index item must be a valid
* index into the constant_pool table, and the entry at that index must
* be a CONSTANT_Utf8_info structure that represents the original simple
* name of C, as given in the source code from which this class file was
* compiled.
*/
public transient final u2 inner_name_index;
public transient final u2 inner_class_access_flags;

Expand Down

0 comments on commit 148e273

Please sign in to comment.