diff --git a/rewrite-core/src/main/java/org/openrewrite/internal/AdaptiveRadixTree.java b/rewrite-core/src/main/java/org/openrewrite/internal/AdaptiveRadixTree.java index b8260ec7883..222ebd6e8cb 100644 --- a/rewrite-core/src/main/java/org/openrewrite/internal/AdaptiveRadixTree.java +++ b/rewrite-core/src/main/java/org/openrewrite/internal/AdaptiveRadixTree.java @@ -60,7 +60,7 @@ protected boolean matchesPartialKey(byte[] key, int depth) { } // Helper method to find common prefix length - protected static int findCommonPrefixLength(byte[] key1, int start1, byte[] key2, int start2) { + protected static int findCommonPrefixLength(byte[] key1, int start1, byte[] key2) { int maxLength = Math.min(key1.length - start1, key2.length); int i = 0; while (i < maxLength && key1[start1 + i] == key2[i]) { @@ -118,7 +118,7 @@ Node insert(byte[] key, int depth, V value) { return new LeafNode<>(partialKey, value); } - int commonPrefix = findCommonPrefixLength(key, depth, partialKey, 0); + int commonPrefix = findCommonPrefixLength(key, depth, partialKey); byte[] commonKey = Arrays.copyOfRange(key, depth, depth + commonPrefix); Node4 newNode = new Node4<>(commonKey); @@ -194,7 +194,7 @@ V search(byte[] key, int depth) { @Override Node insert(byte[] key, int depth, V value) { if (!matchesPartialKey(key, depth)) { - int commonPrefix = findCommonPrefixLength(key, depth, partialKey, 0); + int commonPrefix = findCommonPrefixLength(key, depth, partialKey); byte[] commonKey = Arrays.copyOfRange(key, depth, depth + commonPrefix); Node4 newNode = new Node4<>(commonKey); @@ -272,7 +272,7 @@ Node insert(byte[] key, int depth, V value) { private static class Node4 extends InternalNode { // Keys and children inline to avoid array overhead private byte k0, k1, k2, k3; - private Node c0, c1, c2, c3; + private @Nullable Node c0, c1, c2, c3; private byte size; Node4(byte[] partialKey) { @@ -290,6 +290,7 @@ private static class Node4 extends InternalNode { return null; } + @SuppressWarnings("DataFlowIssue") @Override @Nullable InternalNode addChild(byte key, Node child) { // Check if we're replacing an existing child @@ -390,6 +391,7 @@ InternalNode cloneWithNewKey(byte[] newKey) { return clone; } + @SuppressWarnings("DataFlowIssue") @Override Node copy() { Node4 clone = new Node4<>(Arrays.copyOf(partialKey, partialKey.length)); @@ -410,7 +412,7 @@ Node copy() { private static class Node16 extends InternalNode { private static final int LINEAR_SEARCH_THRESHOLD = 8; private byte[] keys; - private Node[] children; + private @Nullable Node[] children; private int size; @SuppressWarnings("unchecked") @@ -432,13 +434,13 @@ Node getChild(byte key) { return null; } - int idx = unsignedBinarySearch(keys, 0, size, key & 0xFF); + int idx = unsignedBinarySearch(keys, size, key & 0xFF); return idx >= 0 ? children[idx] : null; } // Custom binary search for unsigned bytes - private int unsignedBinarySearch(byte[] array, int fromIndex, int toIndex, int key) { - int low = fromIndex; + private int unsignedBinarySearch(byte[] array, int toIndex, int key) { + int low = 0; int high = toIndex - 1; while (low <= high) { @@ -471,6 +473,7 @@ InternalNode addChild(byte key, Node child) { Node48 node = new Node48<>(partialKey); node.value = this.value; for (int i = 0; i < size; i++) { + //noinspection DataFlowIssue node.addChild(keys[i], children[i]); } node.addChild(key, child); @@ -511,6 +514,7 @@ Node copy() { clone.children = Arrays.copyOf(this.children, this.children.length); // Deep copy children for (int i = 0; i < size; i++) { + //noinspection DataFlowIssue clone.children[i] = children[i].copy(); } return clone; @@ -519,7 +523,7 @@ Node copy() { private static class Node48 extends InternalNode { private byte[] index; - private Node[] children; + private @Nullable Node[] children; private int size; @SuppressWarnings("unchecked") @@ -552,6 +556,7 @@ InternalNode addChild(byte key, Node child) { node.value = this.value; for (int i = 0; i < 256; i++) { if (index[i] >= 0) { + //noinspection DataFlowIssue node.addChild((byte) i, children[index[i]]); } } @@ -584,6 +589,7 @@ Node copy() { clone.children = Arrays.copyOf(this.children, this.children.length); // Deep copy children for (int i = 0; i < size; i++) { + //noinspection DataFlowIssue clone.children[i] = children[i].copy(); } return clone; @@ -591,7 +597,7 @@ Node copy() { } private static class Node256 extends InternalNode { - private final Node @Nullable[] children; + private final @Nullable Node [] children; @SuppressWarnings("unchecked") Node256(byte[] partialKey) { @@ -628,8 +634,9 @@ Node copy() { System.arraycopy(this.children, 0, clone.children, 0, this.children.length); // Deep copy children for (int i = 0; i < 256; i++) { - if (children[i] != null) { - clone.children[i] = children[i].copy(); + Node child = children[i]; + if (child != null) { + clone.children[i] = child.copy(); } } return clone;