-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -23,11 +23,14 @@ | |||||
*/ | ||||||
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 java.util.Objects; | ||||||
import java.util.Optional; | ||||||
|
||||||
import static java.util.Collections.enumeration; | ||||||
|
||||||
/** | ||||||
* Node, that represents a repeated data in struct definition. An each repeated value | ||||||
|
@@ -91,7 +94,21 @@ 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)); | ||||||
|
||||||
/* | ||||||
We cannot access the generic type of the List at runtime due to | ||||||
type erasure. Instead, look for a non-null element in the List, | ||||||
which will tell us the type of the whole list. | ||||||
*/ | ||||||
final Class<?> valueClass; | ||||||
final Optional<?> nonNullElement = value.stream().filter(Objects::nonNull).findAny(); | ||||||
if (nonNullElement.isPresent()) { | ||||||
valueClass = nonNullElement.get().getClass(); | ||||||
} else { | ||||||
valueClass = null; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to avoid
Suggested change
|
||||||
} | ||||||
|
||||||
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); | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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) { | ||||||||||
/** Replace value.getClass() when value==null. */ | ||||||||||
private final Class<?> classOfValue; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
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.classOfValue = valueClass; | ||||||||||
} | ||||||||||
|
||||||||||
@Override | ||||||||||
public Object getValue() { return value; } | ||||||||||
|
||||||||||
public Class<?> getClassOfValue() { return classOfValue; } | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I bet, that this name is better. |
||||||||||
|
||||||||||
//<editor-fold defaultstate="collapsed" desc="TreeNode"> | ||||||||||
@Override | ||||||||||
public ChunkNode getChildAt(int childIndex) { | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like the idea of looking at the content, because the type will depend on the specific object. Probably it is possible to extract some type from generic boundaries? I remember, that this should be possible.