Skip to content

Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.

Java: count Set.of, Map.of and the unordered collectors as a hash iteration order risk (#1367)

jakalijey7e4793a996f7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ae8558a0..9a123364 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -188,6 +188,10 @@
fails. Before, a Python unit built on `BeautifulSoup(html, "html.parser")`, which no crate
reproduces, could only meet the cap by dropping behavior. The skill and TASK.md tell the agent
to ask the user instead of writing one (#1189 by @sungkilhan221).
+- Java: `Set.of`, `Set.copyOf`, `Map.of`, `Map.ofEntries`, `Map.copyOf`, `Collectors.toSet`,
+ `Collectors.toMap` and `Collectors.groupingBy` (with or without their package) are a hash iteration
+ order risk, as `new HashSet` already was. `Set.of` and `Map.of` order their elements differently
+ from one JVM start to the next. `List.of` and `Collectors.toList` are not (#1343 by @jakalijey).
- Ruby: `Time.new` counts as reading the clock only with no arguments (`Time.new`, `Time.new()`,
`Time.new.year`). `Time.new(2000, 1, 1)` builds a fixed date, so a function that calls it is
bridged, and `analyze` no longer counts it as a clock risk (#1337 by @jakalijey).
@@ -211,6 +215,8 @@
`Time.new()`, `Time.new.year`, `Time.new(in: "+09:00")`). `Time.new(2000, 1, 1)` builds a fixed
date, so a function that calls it is bridged, and `analyze` no longer counts it as a clock risk
(#1337 by @jakalijey).
+ from one JVM start to the next. `List.of`, `Collectors.toList`, and a `toMap` or `groupingBy`
+ given a `TreeMap` or `LinkedHashMap` supplier are not (#1343 by @jakalijey).
## 5.1.1 (2026-12-02)
diff --git a/src/lang/java.rs b/src/lang/java.rs
index 859639e8..af6c2a64 100644
--- a/src/lang/java.rs
+++ b/src/lang/java.rs
@@ -315,6 +315,9 @@ pub fn scan(source: &str) -> FileFacts {
"hashCode" => add(&mut facts, Risk::IntegerSemantics, node),
_ => {}
}
+ if unordered_factory(node, source, name) {
+ add(&mut facts, Risk::UnorderedIteration, node);
+ }
let owner = match node.child_by_field_name("object") {
None => Some("Self".to_string()),
Some(o) if matches!(o.kind(), "identifier" | "field_access") => {
@@ -874,6 +877,48 @@ pub fn receiver(f: &ExportedFn) -> (String, String) {
}
}
+/// `Set.of(...)`, `Map.of(...)` and the like, and `Collectors.toSet()`, `toMap(...)` and
+/// `groupingBy(...)`, with or without their package: a set or map whose order is not the order its
+/// elements were given in. `Set.of` and `Map.of` even salt it per JVM start. `List.of` and
+/// `Collectors.toList` keep the order.
+fn unordered_factory(node: Node, source: &str, name: &str) -> bool {
+ let Some(object) = node.child_by_field_name("object") else {
+ return false;
+ };
+ if !matches!(
+ object.kind(),
+ "identifier" | "field_access" | "scoped_identifier"
+ ) {
+ return false;
+ }
+ let object: String = text(object, source).split_whitespace().collect();
+ let class = object.rsplit('.').next().unwrap_or(&object);
+ match class {
+ "Set" => matches!(name, "of" | "copyOf"),
+ "Map" => matches!(name, "of" | "ofEntries" | "copyOf"),
+ "Collectors" => match name {
+ "toSet" => true,
+ "toMap" | "groupingBy" => !ordered_map_supplier(node, source, name),
+ _ => false,
+ },
+ _ => false,
+ }
+}
+
+fn ordered_map_supplier(node: Node, source: &str, name: &str) -> bool {
+ let args = node
+ .child_by_field_name("arguments")
+ .map(named_children)
+ .unwrap_or_default();
+ let (count, at) = if name == "toMap" { (4, 3) } else { (3, 1) };
+ args.len() == count && {
+ let supplier = text(args[at], source);
+ ["TreeMap", "LinkedHashMap", "EnumMap", "SkipListMap"]
+ .iter()
+ .any(|m| supplier.contains(m))
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -1030,6 +1075,53 @@ public class Totals {
assert!(!ordered.risks.contains_key(&Risk::UnorderedIteration));
}
+ #[test]
+ fn a_set_or_map_factory_and_an_unordered_collector_are_found() {
+ for call in [
+ "Set.of(\"a\")",
+ "Set.copyOf(xs)",
+ "Map.of(\"a\", 1)",
+ "Map.ofEntries(Map.entry(\"a\", 1))",
+ "Map.copyOf(m)",
+ "java.util.Set.of(\"a\")",
+ "java.util.Map.of(\"a\", 1)",
+ "xs.stream().collect(Collectors.toSet())",
+ "xs.stream().collect(Collectors.toMap(x -> x, x -> 1))",
+ "xs.stream().collect(Collectors.groupingBy(String::length))",
+ "xs.stream().collect(java.util.stream.Collectors.toSet())",
+ "xs.stream().collect(Collectors.toMap(x -> x, x -> 1, (a, b) -> a))",
+ "xs.stream().collect(Collectors.groupingBy(String::length, java.util.HashMap::new, Collectors.toList()))",
+ ] {
+ let source = format!(
+ "package a;\npublic class A {{\n Object f(java.util.List<String> xs, java.util.Map<String, Integer> m) {{ return {call}; }}\n}}\n"
+ );
+ let facts = scan(&source);
+ assert!(
+ facts.risks.contains_key(&Risk::UnorderedIteration),
+ "{call}: {:?}",
+ facts.risks
+ );
+ }
+ for call in [
+ "List.of(\"a\")",
+ "xs.stream().collect(Collectors.toList())",
+ "Optional.of(\"a\")",
+ "xs.of(\"a\")",
+ "xs.stream().collect(Collectors.groupingBy(String::length, java.util.TreeMap::new, Collectors.toList()))",
+ "xs.stream().collect(Collectors.toMap(x -> x, x -> 1, (a, b) -> a, java.util.LinkedHashMap::new))",
+ ] {
+ let source = format!(
+ "package a;\npublic class A {{\n Object f(java.util.List<String> xs) {{ return {call}; }}\n}}\n"
+ );
+ let facts = scan(&source);
+ assert!(
+ !facts.risks.contains_key(&Risk::UnorderedIteration),
+ "{call}: {:?}",
+ facts.risks
+ );
+ }
+ }
+
#[test]
fn only_a_class_accessor_or_a_changing_static_field_reads_app_state() {
let source = "package a;\n\