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: import concrete collections and primitive optionals in the wrapper (#1278)

jakalijey5373e62d800d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fa94e9c1..ea784b3e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -96,6 +96,9 @@
included (`:save!` names `save!`, not `save`; `:+` and `:[]=` stay listed); before,
`:soundex_str` also matched `soundex` as a substring, so the export was listed twice (found
porting the `text` gem's `soundex.rb`, by @cfreyes7).
+- Java: the wrapper and shadow classes import `ArrayList`, `LinkedList`, `HashMap`, `LinkedHashMap`,
+ `TreeMap`, `OptionalInt`, `OptionalLong` and `OptionalDouble` when a signature uses them. Before,
+ `bridge` accepted those types and generated a wrapper `javac` refused (#1253 by @jakalijey).
- The Claude directory copy of the plugin (`directory/`) no longer tells Claude to add the local MCP
server (`rust-anything mcp`, `rust-anything setup`), which the directory refuses for plugins
that are not from a Verified organization. It uses the CLI only; the plugin in `plugin/` keeps
diff --git a/src/codegen_java.rs b/src/codegen_java.rs
index 6b5f86cf..8efc2e5a 100644
--- a/src/codegen_java.rs
+++ b/src/codegen_java.rs
@@ -782,6 +782,14 @@ fn imports_of(m: &Manifest) -> String {
"NavigableSet",
"TreeSet",
"LinkedHashSet",
+ "ArrayList",
+ "LinkedList",
+ "HashMap",
+ "LinkedHashMap",
+ "TreeMap",
+ "OptionalInt",
+ "OptionalLong",
+ "OptionalDouble",
] {
if t.split(|c: char| !c.is_alphanumeric()).any(|w| w == name)
&& !names.contains(&name)
@@ -2630,6 +2638,58 @@ public class Sets {
}
}
+ #[test]
+ fn concrete_collections_and_primitive_optionals_are_imported_in_the_wrapper() {
+ let src = "package demo;\nimport java.util.*;\npublic class Stats {\n public static int total(ArrayList<Integer> xs) { int s = 0; for (int x : xs) s += x; return s; }\n public static int width(TreeMap<String, Integer> m) { return m.size(); }\n public static OptionalInt first(List<Integer> xs) { return xs.isEmpty() ? OptionalInt.empty() : OptionalInt.of(xs.get(0)); }\n}\n";
+ let exports = crate::lang::java::exports("demo/Stats.java", src);
+ assert!(exports.iter().all(|f| f.blocker.is_none()), "{exports:?}");
+ let manifest = Manifest {
+ schema: String::new(),
+ root: std::path::PathBuf::new(),
+ root_written: None,
+ unit: "demo/Stats.java".into(),
+ language: crate::lang::Lang::Java,
+ kind: crate::bridge::Kind::Ffi,
+ crate_name: "stats-rs".into(),
+ exports,
+ classes: vec![],
+ js_only: vec![],
+ batches: vec![],
+ allocator: Default::default(),
+ float_tolerance: None,
+ ignore_key_order: false,
+ messages: false,
+ end_state: None,
+ };
+ let imports = imports_of(&manifest);
+ assert_eq!(
+ imports,
+ "import java.util.ArrayList;\nimport java.util.List;\nimport java.util.OptionalInt;\nimport java.util.TreeMap;\n\n"
+ );
+ let mut api = crate::codegen::parse_port(&crate::codegen::port_rs(&manifest));
+ crate::bridge::match_export_names(&manifest, &mut api);
+ let wrappers = wrappers(&manifest, &api);
+ for imp in [
+ "import java.util.ArrayList;",
+ "import java.util.TreeMap;",
+ "import java.util.OptionalInt;",
+ ] {
+ assert!(
+ wrappers[0].1.contains(imp),
+ "missing {imp} in:\n{}",
+ wrappers[0].1
+ );
+ }
+
+ // `List` alone imports `List` only.
+ let mut only_list = manifest.clone();
+ only_list.exports.retain(|f| f.name == "first");
+ for f in &mut only_list.exports {
+ f.native_return = Some("int".into());
+ }
+ assert_eq!(imports_of(&only_list), "import java.util.List;\n\n");
+ }
+
#[test]
fn typed_rs_avoids_redundant_closure_for_zero_arg_fallible_functions() {
let src = r#"package acme;