Skip to content

Commit

Permalink
Synchronize Target Node
Browse files Browse the repository at this point in the history
The org.eclipse.pde.internal.genericeditor.target.extension.model.Node
is accessed in a concurrent way but its internal implementation uses
unsynchronized mutator methods.

This add synchronized to all methods and copy internal datastructures to
make the class at least concurrent-safe and prevent unsyncronized data
structures to escape the scope of a node.
  • Loading branch information
laeubi committed Dec 27, 2024
1 parent 0b123e0 commit b127fb1
Showing 1 changed file with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,48 @@ public class Node {
private List<Node> childNodes;
private Node parentNode;

public int getOffsetStart() {
public synchronized int getOffsetStart() {
return offsetStart;
}

public void setOffsetStart(int offsetStart) {
public synchronized void setOffsetStart(int offsetStart) {
this.offsetStart = offsetStart;
}

public int getOffsetEnd() {
public synchronized int getOffsetEnd() {
return offsetEnd;
}

public void setOffsetEnd(int offsetEnd) {
public synchronized void setOffsetEnd(int offsetEnd) {
this.offsetEnd = offsetEnd;
}

public String getNodeTag() {
public synchronized String getNodeTag() {
return nodeTag;
}

public void setNodeTag(String nodeTag) {
public synchronized void setNodeTag(String nodeTag) {
this.nodeTag = nodeTag;
}

public List<Node> getChildNodes() {
List<Node> nodes = childNodes;
return nodes == null ? new ArrayList<>() : nodes;
public synchronized List<Node> getChildNodes() {
return childNodes == null ? List.of() : List.copyOf(childNodes);
}

public List<Node> getChildNodesByTag(String nodeTag) {
return childNodes.stream().filter(n -> Objects.equals(n.getNodeTag(), nodeTag)).collect(Collectors.toList());
public synchronized List<Node> getChildNodesByTag(String nodeTag) {
return getChildNodes().stream().filter(n -> Objects.equals(n.getNodeTag(), nodeTag))
.collect(Collectors.toList());
}

public void addChildNode(Node child) {
public synchronized void addChildNode(Node child) {
if (childNodes == null) {
childNodes = new ArrayList<>();
}
childNodes.add(child);
child.setParentNode(this);
child.parentNode = this;
}

public Node getParentNode() {
public synchronized Node getParentNode() {
return parentNode;
}

private void setParentNode(Node parentNode) {
this.parentNode = parentNode;
}
}

0 comments on commit b127fb1

Please sign in to comment.