Skip to content

Commit 6f4d6f1

Browse files
committed
Make ATreeNode.getChildren return an immutable list to improve code safety.
1 parent 19f6e04 commit 6f4d6f1

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

SpecsUtils/src/pt/up/fe/specs/util/treenode/ATreeNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ private List<K> initChildren(Collection<? extends K> children) {
7272
*/
7373
@Override
7474
public List<K> getChildren() {
75-
// FIXME: Should be 'Collections.unmodifiableList(this.children);'
76-
// but the current implementation breaks when you do.
75+
return Collections.unmodifiableList(this.children);
76+
}
77+
78+
@Override
79+
public List<K> getChildrenMutable() {
7780
return this.children;
7881
}
7982

SpecsUtils/src/pt/up/fe/specs/util/treenode/ChildrenIterator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class ChildrenIterator<N extends TreeNode<N>> implements ListIterator<N>
2525
public ChildrenIterator(TreeNode<N> parent) {
2626

2727
this.parent = parent;
28-
// Currently cannot enforce immutable children view due to MATISSE passes
29-
this.iterator = parent.getChildren().listIterator();
28+
// Access internal mutable list for modification operations
29+
this.iterator = parent.getChildrenMutable().listIterator();
3030

3131
this.lastReturned = null;
3232
}

SpecsUtils/src/pt/up/fe/specs/util/treenode/TreeNode.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ default K getChild(int index1, int index2, int... indexes) {
169169
*/
170170
List<K> getChildren();
171171

172+
/**
173+
* Returns a mutable list of children for internal use only.
174+
* Should only be used by ChildrenIterator and internal tree operations.
175+
*
176+
* @return the mutable children list
177+
*/
178+
List<K> getChildrenMutable();
179+
172180
/**
173181
* TODO: Rename to castChildren.
174182
*

0 commit comments

Comments
 (0)