Skip to main content

alef_backend_java/gen_bindings/
mod.rs

1use ahash::AHashSet;
2use alef_codegen::naming::to_class_name;
3use alef_core::backend::{Backend, BuildConfig, BuildDependency, Capabilities, GeneratedFile};
4use alef_core::config::{BridgeBinding, Language, ResolvedCrateConfig};
5use alef_core::ir::{ApiSurface, TypeRef};
6use std::collections::HashSet;
7use std::path::PathBuf;
8
9mod facade;
10mod ffi_class;
11mod helpers;
12mod marshal;
13mod native_lib;
14mod trait_bridge;
15mod types;
16
17use facade::gen_facade_class;
18use ffi_class::gen_main_class;
19use helpers::{gen_exception_class, gen_infrastructure_exception_class};
20use native_lib::gen_native_lib;
21use types::{gen_builder_class, gen_byte_array_serializer, gen_enum_class, gen_opaque_handle_class, gen_record_type};
22
23pub struct JavaBackend;
24
25impl JavaBackend {
26    /// Convert crate name to main class name (PascalCase + "Rs" suffix).
27    ///
28    /// The "Rs" suffix ensures the raw FFI wrapper class has a distinct name from
29    /// the public facade class (which strips the "Rs" suffix). Without this, the
30    /// facade would delegate to itself, causing infinite recursion.
31    fn resolve_main_class(api: &ApiSurface) -> String {
32        let base = to_class_name(&api.crate_name.replace('-', "_"));
33        if base.ends_with("Rs") {
34            base
35        } else {
36            format!("{}Rs", base)
37        }
38    }
39}
40
41impl Backend for JavaBackend {
42    fn name(&self) -> &str {
43        "java"
44    }
45
46    fn language(&self) -> Language {
47        Language::Java
48    }
49
50    fn capabilities(&self) -> Capabilities {
51        Capabilities {
52            supports_async: true,
53            supports_classes: true,
54            supports_enums: true,
55            supports_option: true,
56            supports_result: true,
57            ..Capabilities::default()
58        }
59    }
60
61    fn generate_bindings(&self, api: &ApiSurface, config: &ResolvedCrateConfig) -> anyhow::Result<Vec<GeneratedFile>> {
62        let package = config.java_package();
63        let prefix = config.ffi_prefix();
64        let main_class = Self::resolve_main_class(api);
65        let package_path = package.replace('.', "/");
66
67        let output_dir = config
68            .output_for("java")
69            .map(|p| p.to_string_lossy().into_owned())
70            .unwrap_or_else(|| "packages/java/src/main/java/".to_string());
71
72        // If output_dir already ends with the package path (user configured the full path),
73        // use it as-is. Otherwise, append the package path.
74        let base_path = if output_dir.ends_with(&package_path) || output_dir.ends_with(&format!("{}/", package_path)) {
75            PathBuf::from(&output_dir)
76        } else {
77            PathBuf::from(&output_dir).join(&package_path)
78        };
79
80        // Collect bridge param names and type aliases so we can strip them from generated
81        // function signatures and emit convertWithVisitor instead.
82        let bridge_param_names: HashSet<String> = config
83            .trait_bridges
84            .iter()
85            .filter_map(|b| b.param_name.clone())
86            .collect();
87        let bridge_type_aliases: HashSet<String> = config
88            .trait_bridges
89            .iter()
90            .filter_map(|b| b.type_alias.clone())
91            .collect();
92        // Generate visitor support when visitor_callbacks is enabled in FFI config (canonical check),
93        // OR when any trait bridge is bound via options_field (Java-specific activation path).
94        let has_visitor_pattern = config.ffi.as_ref().map(|f| f.visitor_callbacks).unwrap_or(false)
95            || config
96                .trait_bridges
97                .iter()
98                .any(|b| b.bind_via == BridgeBinding::OptionsField);
99
100        let mut files = Vec::new();
101
102        // 0. package-info.java - required by Checkstyle
103        let description = config
104            .scaffold
105            .as_ref()
106            .and_then(|s| s.description.as_deref())
107            .unwrap_or("High-performance HTML to Markdown converter.");
108        files.push(GeneratedFile {
109            path: base_path.join("package-info.java"),
110            content: format!(
111                "/**\n * {description}\n */\npackage {package};\n",
112                description = description,
113                package = package,
114            ),
115            generated_header: true,
116        });
117
118        // 1. NativeLib.java - FFI method handles
119        files.push(GeneratedFile {
120            path: base_path.join("NativeLib.java"),
121            content: gen_native_lib(api, config, &package, &prefix, has_visitor_pattern),
122            generated_header: true,
123        });
124
125        // 2. Main wrapper class
126        files.push(GeneratedFile {
127            path: base_path.join(format!("{}.java", main_class)),
128            content: gen_main_class(
129                api,
130                config,
131                &package,
132                &main_class,
133                &prefix,
134                &bridge_param_names,
135                &bridge_type_aliases,
136                has_visitor_pattern,
137            ),
138            generated_header: true,
139        });
140
141        // 3. Exception class
142        files.push(GeneratedFile {
143            path: base_path.join(format!("{}Exception.java", main_class)),
144            content: gen_exception_class(&package, &main_class),
145            generated_header: true,
146        });
147
148        // 3b. Infrastructure exception classes for FFI error codes 1 and 2.
149        // These are always emitted because checkLastError() hardcodes:
150        //   case 1 -> throw new InvalidInputException(msg);
151        //   case 2 -> throw new ConversionErrorException(msg);
152        // Code 1 = null pointer / invalid UTF-8 in an input arg (invalid input).
153        // Code 2 = JSON serialisation/deserialisation failure (type conversion).
154        for (class_name, code, doc) in [
155            (
156                "InvalidInputException",
157                1i32,
158                "Exception thrown when input validation fails.",
159            ),
160            (
161                "ConversionErrorException",
162                2i32,
163                "Exception thrown when type conversion fails.",
164            ),
165        ] {
166            files.push(GeneratedFile {
167                path: base_path.join(format!("{}.java", class_name)),
168                content: gen_infrastructure_exception_class(&package, &main_class, class_name, code, doc),
169                generated_header: true,
170            });
171        }
172
173        // Untagged unions with data variants now emit as JsonNode-wrapper classes
174        // (see gen_java_untagged_wrapper). The set is intentionally empty so that
175        // record fields keep their wrapper type instead of being downcast to Object.
176        let complex_enums: AHashSet<String> = AHashSet::new();
177
178        // Collect sealed union types with unwrapped/tuple variants that need custom deserializers.
179        // When a record field references one of these types, we need to add a @JsonDeserialize
180        // annotation to the field so Jackson uses the custom deserializer.
181        let sealed_unions_with_unwrapped: AHashSet<String> = api
182            .enums
183            .iter()
184            .filter(|e| {
185                e.serde_tag.is_some()
186                    && e.variants
187                        .iter()
188                        .any(|v| v.fields.len() == 1 && helpers::is_tuple_field_name(&v.fields[0].name))
189            })
190            .map(|e| e.name.clone())
191            .collect();
192
193        // Resolve language-level serde rename strategy (always wins over IR type-level).
194        let lang_rename_all = config.serde_rename_all_for_language(Language::Java);
195
196        // 4. Record types
197        // Include non-opaque types that either have fields OR are serializable unit structs
198        // (has_serde + has_default, empty fields). Unit structs like `ExcelMetadata` need a
199        // concrete Java class so they can be referenced as record components in tagged-union
200        // variant records (e.g. FormatMetadata.Excel(@JsonUnwrapped ExcelMetadata value)).
201        for typ in api.types.iter().filter(|typ| !typ.is_trait) {
202            let is_unit_serde = !typ.is_opaque && typ.fields.is_empty() && typ.has_serde;
203            if !typ.is_opaque && (!typ.fields.is_empty() || is_unit_serde) {
204                // Skip types that gen_visitor handles with richer visitor-specific versions
205                if has_visitor_pattern && (typ.name == "NodeContext" || typ.name == "VisitResult") {
206                    continue;
207                }
208                files.push(GeneratedFile {
209                    path: base_path.join(format!("{}.java", typ.name)),
210                    content: gen_record_type(
211                        &package,
212                        typ,
213                        &complex_enums,
214                        &sealed_unions_with_unwrapped,
215                        &lang_rename_all,
216                        has_visitor_pattern,
217                    ),
218                    generated_header: true,
219                });
220                // Generate builder class for types with defaults
221                if typ.has_default {
222                    files.push(GeneratedFile {
223                        path: base_path.join(format!("{}Builder.java", typ.name)),
224                        content: gen_builder_class(&package, typ, has_visitor_pattern),
225                        generated_header: true,
226                    });
227                }
228            }
229        }
230
231        // 4a. Utility serializer for byte[] → JSON int-array (needed when any record
232        // has a non-optional Bytes field). Jackson's default byte[] serialiser emits
233        // base64, which Rust's serde Vec<u8> cannot accept. Emit the class once.
234        let needs_bytes_serializer = api
235            .types
236            .iter()
237            .any(|t| !t.is_opaque && t.fields.iter().any(|f| !f.optional && matches!(f.ty, TypeRef::Bytes)));
238        if needs_bytes_serializer {
239            files.push(GeneratedFile {
240                path: base_path.join("ByteArrayToIntArraySerializer.java"),
241                content: gen_byte_array_serializer(&package),
242                generated_header: true,
243            });
244        }
245
246        // Collect builder class names generated from record types with defaults,
247        // so we can skip opaque types that would collide with them.
248        let builder_class_names: AHashSet<String> = api
249            .types
250            .iter()
251            .filter(|t| !t.is_opaque && (!t.fields.is_empty() || (t.has_serde && t.fields.is_empty())) && t.has_default)
252            .map(|t| format!("{}Builder", t.name))
253            .collect();
254
255        // 4b. Opaque handle types (skip if a pure-Java builder already covers this name)
256        for typ in api.types.iter().filter(|typ| !typ.is_trait) {
257            if typ.is_opaque && !builder_class_names.contains(&typ.name) {
258                files.push(GeneratedFile {
259                    path: base_path.join(format!("{}.java", typ.name)),
260                    content: gen_opaque_handle_class(&package, typ, &prefix, &config.adapters, &main_class),
261                    generated_header: true,
262                });
263            }
264        }
265
266        // 5. Enums
267        for enum_def in &api.enums {
268            // Skip enums that gen_visitor handles with richer visitor-specific versions
269            if has_visitor_pattern && enum_def.name == "VisitResult" {
270                continue;
271            }
272            files.push(GeneratedFile {
273                path: base_path.join(format!("{}.java", enum_def.name)),
274                content: gen_enum_class(&package, enum_def),
275                generated_header: true,
276            });
277        }
278
279        // 6. Error exception classes
280        for error in &api.errors {
281            for (class_name, content) in alef_codegen::error_gen::gen_java_error_types(error, &package) {
282                files.push(GeneratedFile {
283                    path: base_path.join(format!("{}.java", class_name)),
284                    content,
285                    generated_header: true,
286                });
287            }
288        }
289
290        // 7. Visitor support files (only when ConversionOptions/ConversionResult types exist)
291        if has_visitor_pattern {
292            for (filename, content) in crate::gen_visitor::gen_visitor_files(&package, &main_class) {
293                files.push(GeneratedFile {
294                    path: base_path.join(filename),
295                    content,
296                    generated_header: false, // already has header comment
297                });
298            }
299        }
300
301        // 8. Trait bridge plugin registration files
302        // Emits two files per trait: I{Trait}.java (managed interface) and
303        // {Trait}Bridge.java (Panama upcall stubs + register/unregister helpers).
304        for bridge_cfg in &config.trait_bridges {
305            if bridge_cfg.exclude_languages.contains(&Language::Java.to_string()) {
306                continue;
307            }
308
309            // When visitor_callbacks is active, visitor traits bound via options_field are
310            // surfaced through Visitor.java + VisitorBridge.java (generated by gen_visitor_files).
311            // The raw trait bridge I{Trait}.java emitted here would be an unreferenced orphan
312            // with snake_case method names. Suppress it for options_field-bound visitor traits.
313            if has_visitor_pattern && bridge_cfg.bind_via == BridgeBinding::OptionsField {
314                continue;
315            }
316
317            if let Some(trait_def) = api.types.iter().find(|t| t.name == bridge_cfg.trait_name && t.is_trait) {
318                let has_super_trait = bridge_cfg.super_trait.is_some();
319                let trait_bridge::BridgeFiles {
320                    interface_content,
321                    bridge_content,
322                } = trait_bridge::gen_trait_bridge_files(trait_def, &prefix, &package, has_super_trait);
323
324                files.push(GeneratedFile {
325                    path: base_path.join(format!("I{}.java", trait_def.name)),
326                    content: interface_content,
327                    generated_header: true,
328                });
329                files.push(GeneratedFile {
330                    path: base_path.join(format!("{}Bridge.java", trait_def.name)),
331                    content: bridge_content,
332                    generated_header: true,
333                });
334            }
335        }
336
337        Ok(files)
338    }
339
340    fn generate_public_api(
341        &self,
342        api: &ApiSurface,
343        config: &ResolvedCrateConfig,
344    ) -> anyhow::Result<Vec<GeneratedFile>> {
345        let package = config.java_package();
346        let prefix = config.ffi_prefix();
347        let main_class = Self::resolve_main_class(api);
348        let package_path = package.replace('.', "/");
349
350        let output_dir = config
351            .output_for("java")
352            .map(|p| p.to_string_lossy().into_owned())
353            .unwrap_or_else(|| "packages/java/src/main/java/".to_string());
354
355        // If output_dir already ends with the package path (user configured the full path),
356        // use it as-is. Otherwise, append the package path.
357        let base_path = if output_dir.ends_with(&package_path) || output_dir.ends_with(&format!("{}/", package_path)) {
358            PathBuf::from(&output_dir)
359        } else {
360            PathBuf::from(&output_dir).join(&package_path)
361        };
362
363        // Collect bridge param names/aliases to strip from the public facade.
364        let bridge_param_names: HashSet<String> = config
365            .trait_bridges
366            .iter()
367            .filter_map(|b| b.param_name.clone())
368            .collect();
369        let bridge_type_aliases: HashSet<String> = config
370            .trait_bridges
371            .iter()
372            .filter_map(|b| b.type_alias.clone())
373            .collect();
374        let has_visitor_pattern = config.ffi.as_ref().map(|f| f.visitor_callbacks).unwrap_or(false)
375            || config
376                .trait_bridges
377                .iter()
378                .any(|b| b.bind_via == BridgeBinding::OptionsField);
379
380        // Generate a high-level public API class that wraps the raw FFI class.
381        // Class name = main_class without "Rs" suffix (e.g., HtmlToMarkdownRs -> HtmlToMarkdown)
382        let public_class = main_class.trim_end_matches("Rs").to_string();
383        let facade_content = gen_facade_class(
384            api,
385            &package,
386            &public_class,
387            &main_class,
388            &prefix,
389            &bridge_param_names,
390            &bridge_type_aliases,
391            has_visitor_pattern,
392        );
393
394        Ok(vec![GeneratedFile {
395            path: base_path.join(format!("{}.java", public_class)),
396            content: facade_content,
397            generated_header: true,
398        }])
399    }
400
401    fn build_config(&self) -> Option<BuildConfig> {
402        Some(BuildConfig {
403            tool: "mvn",
404            crate_suffix: "",
405            build_dep: BuildDependency::Ffi,
406            post_build: vec![],
407        })
408    }
409}