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

Keep reference to type of SimpleNode value #9

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/main/java/ru/mingun/kaitai/struct/tree/ChunkNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ public abstract class ChunkNode extends ValueNode {
* @throws ReflectiveOperationException If {@code value} is {@link KaitaiStruct}
* and it was compiled without debug info (which includes position information)
*/
protected ChunkNode create(String name, Object value, long offset, long start, long end) throws ReflectiveOperationException {
protected ChunkNode create(String name, Object value, Class<?> valueClass, long offset, long start, long end) throws ReflectiveOperationException {
return value instanceof KaitaiStruct
? new StructNode(name, (KaitaiStruct)value, this, offset, start, end)
: new SimpleNode(name, value, this, offset, start, end);
: new SimpleNode(name, value, valueClass, this, offset, start, end);
}
}
14 changes: 10 additions & 4 deletions src/main/java/ru/mingun/kaitai/struct/tree/ListNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
*/
package ru.mingun.kaitai.struct.tree;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import static java.util.Collections.enumeration;
import java.util.Enumeration;
import java.util.List;
import javax.swing.tree.TreeNode;

import static java.util.Collections.enumeration;

/**
* Node, that represents a repeated data in struct definition. An each repeated value
Expand All @@ -37,20 +38,25 @@
*/
public class ListNode extends ChunkNode {
private final List<?> value;

/** The parameter type of <code>value</code>. */
private final Class<?> valueClass;

/** Lazy populated list of child nodes. */
private List<ChunkNode> children;
/** Start positions in root stream of each value object in {@link #value}. */
private final List<Integer> arrStart;
/** Endo positions in root stream of each value object in {@link #value} (exclusive). */
private final List<Integer> arrEnd;

ListNode(String name, List<?> value, StructNode parent,
ListNode(String name, List<?> value, Class<?> valueClass, StructNode parent,
long offset, long start, long end,
List<Integer> arrStart,
List<Integer> arrEnd
) {
super(name, parent, offset, start, end);
this.value = value;
this.valueClass = valueClass;
this.arrStart = arrStart;
this.arrEnd = arrEnd;
}
Expand Down Expand Up @@ -91,7 +97,7 @@ private List<ChunkNode> init() {
try {
final int s = arrStart.get(index);
final int e = arrEnd.get(index);
children.add(create("[" + index + ']', obj, 0, s, e));
children.add(create("[" + index + ']', obj, valueClass, 0, s, e));
++index;
} catch (ReflectiveOperationException ex) {
throw new UnsupportedOperationException("Can't get list value at index " + index, ex);
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/ru/mingun/kaitai/struct/tree/SimpleNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ public class SimpleNode extends ChunkNode {
/** Parsed value of non-constructed type. */
private final Object value;

SimpleNode(String name, Object value, ChunkNode parent, long offset, long start, long end) {
/** Static type of <code>value</code>, to identify the type when value is null. */
private final Class<?> valueClass;

SimpleNode(String name, Object value, Class<?> valueClass, ChunkNode parent, long offset, long start, long end) {
super(name, parent, offset, start, end);
this.value = value;
this.valueClass = valueClass;
}

@Override
public Object getValue() { return value; }

public Class<?> getValueClass() { return valueClass; }

//<editor-fold defaultstate="collapsed" desc="TreeNode">
@Override
public ChunkNode getChildAt(int childIndex) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/ru/mingun/kaitai/struct/tree/StructNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import io.kaitai.struct.KaitaiStruct;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import static java.util.Collections.enumeration;
Expand Down Expand Up @@ -160,9 +162,13 @@ private ChunkNode create(Method getter) throws ReflectiveOperationException {
if (List.class.isAssignableFrom(getter.getReturnType())) {
final List<Integer> sa = arrStart.get(name);
final List<Integer> se = arrEnd.get(name);
return new ListNode(name, (List<?>)field, this, offset, s, e, sa, se);

final ParameterizedType returnType = (ParameterizedType) getter.getGenericReturnType();
final Type typeArgument = returnType.getActualTypeArguments()[0];

return new ListNode(name, (List<?>) field, (Class<?>) typeArgument, this, offset, s, e, sa, se);
}
return create(name, field, start, s, e);
return create(name, field, getter.getReturnType(), start, s, e);
}

private ArrayList<ChunkNode> init() {
Expand Down