Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show JTree children in correct order #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions src/main/java/io/kaitai/struct/visualizer/DataNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DataNode extends DefaultMutableTreeNode {
private boolean explored = false;
Expand Down Expand Up @@ -139,36 +142,69 @@ protected List<DataNode> doInBackground() throws Exception {
} else if (value instanceof KaitaiStruct) {
DebugAids debug = DebugAids.fromStruct((KaitaiStruct) value);

////////// Find sequential fields and show them in order. //////////
final Field seqFieldsField;
try {
seqFieldsField = cl.getDeclaredField("_seqFields");
} catch (NoSuchFieldException ex) {
ex.printStackTrace();
return children;
}
final Object seqFieldsValue = seqFieldsField.get(value);
final String[] seqFieldNamesInOrder = (String[]) seqFieldsValue;
for (String fieldName : seqFieldNamesInOrder) {
Method accessorMethod = cl.getDeclaredMethod(fieldName);
addDataNode(children, cl, debug, accessorMethod, fieldName);
}
final Set<String> seqFieldNames = new HashSet<>(Arrays.asList(seqFieldNamesInOrder));

////////// Find instances and show them after the sequential fields. //////////
for (Method m : cl.getDeclaredMethods()) {

// Ignore static methods, i.e. "fromFile"
if (Modifier.isStatic(m.getModifiers()))
if (Modifier.isStatic(m.getModifiers())) {
continue;
}

String methodName = m.getName();

// Ignore all internal methods, i.e. "_io", "_parent", "_root"
if (methodName.charAt(0) == '_')
if (methodName.charAt(0) == '_') {
continue;
}

try {
Field field = cl.getDeclaredField(methodName);
field.setAccessible(true);
Object curValue = field.get(value);

Integer posStart = debug.getStart(methodName);
Integer posEnd = debug.getEnd(methodName);

DataNode dn = new DataNode(depth + 1, curValue, m, posStart, posEnd);
children.add(dn);
} catch (NoSuchFieldException e) {
System.out.println("no field, ignoring method " + methodName);
// Ignore methods that get sequential fields
if (seqFieldNames.contains(methodName)) {
continue;
}

addDataNode(children, cl, debug, m, methodName);
}

}
setProgress(0);
return children;
}

private void addDataNode(List<DataNode> children, Class<?> kaitaiStructClass,
DebugAids debug, Method accessorMethod, String fieldName)
throws IllegalAccessException {
try {
Field field = kaitaiStructClass.getDeclaredField(fieldName);
field.setAccessible(true);
Object curValue = field.get(value);

Integer posStart = debug.getStart(fieldName);
Integer posEnd = debug.getEnd(fieldName);

DataNode dn = new DataNode(depth + 1, curValue, accessorMethod, posStart, posEnd);
children.add(dn);
} catch (NoSuchFieldException e) {
System.out.println("no field, ignoring " + fieldName);
}
}


@Override
protected void done() {
try {
Expand Down