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        // Collect complex enums (enums with data variants and no serde tag) — use Object for these fields.
174        // Tagged unions (serde_tag is set) are now generated as proper sealed interfaces
175        // and can be deserialized as their concrete types, so they are NOT complex_enums.
176        let complex_enums: AHashSet<String> = api
177            .enums
178            .iter()
179            .filter(|e| e.serde_tag.is_none() && e.variants.iter().any(|v| !v.fields.is_empty()))
180            .map(|e| e.name.clone())
181            .collect();
182
183        // Collect sealed union types with unwrapped/tuple variants that need custom deserializers.
184        // When a record field references one of these types, we need to add a @JsonDeserialize
185        // annotation to the field so Jackson uses the custom deserializer.
186        let sealed_unions_with_unwrapped: AHashSet<String> = api
187            .enums
188            .iter()
189            .filter(|e| {
190                e.serde_tag.is_some()
191                    && e.variants
192                        .iter()
193                        .any(|v| v.fields.len() == 1 && helpers::is_tuple_field_name(&v.fields[0].name))
194            })
195            .map(|e| e.name.clone())
196            .collect();
197
198        // Resolve language-level serde rename strategy (always wins over IR type-level).
199        let lang_rename_all = config.serde_rename_all_for_language(Language::Java);
200
201        // 4. Record types
202        // Include non-opaque types that either have fields OR are serializable unit structs
203        // (has_serde + has_default, empty fields). Unit structs like `ExcelMetadata` need a
204        // concrete Java class so they can be referenced as record components in tagged-union
205        // variant records (e.g. FormatMetadata.Excel(@JsonUnwrapped ExcelMetadata value)).
206        for typ in api.types.iter().filter(|typ| !typ.is_trait) {
207            let is_unit_serde = !typ.is_opaque && typ.fields.is_empty() && typ.has_serde;
208            if !typ.is_opaque && (!typ.fields.is_empty() || is_unit_serde) {
209                // Skip types that gen_visitor handles with richer visitor-specific versions
210                if has_visitor_pattern && (typ.name == "NodeContext" || typ.name == "VisitResult") {
211                    continue;
212                }
213                files.push(GeneratedFile {
214                    path: base_path.join(format!("{}.java", typ.name)),
215                    content: gen_record_type(
216                        &package,
217                        typ,
218                        &complex_enums,
219                        &sealed_unions_with_unwrapped,
220                        &lang_rename_all,
221                        has_visitor_pattern,
222                    ),
223                    generated_header: true,
224                });
225                // Generate builder class for types with defaults
226                if typ.has_default {
227                    files.push(GeneratedFile {
228                        path: base_path.join(format!("{}Builder.java", typ.name)),
229                        content: gen_builder_class(&package, typ, has_visitor_pattern),
230                        generated_header: true,
231                    });
232                }
233            }
234        }
235
236        // 4a. Utility serializer for byte[] → JSON int-array (needed when any record
237        // has a non-optional Bytes field). Jackson's default byte[] serialiser emits
238        // base64, which Rust's serde Vec<u8> cannot accept. Emit the class once.
239        let needs_bytes_serializer = api
240            .types
241            .iter()
242            .any(|t| !t.is_opaque && t.fields.iter().any(|f| !f.optional && matches!(f.ty, TypeRef::Bytes)));
243        if needs_bytes_serializer {
244            files.push(GeneratedFile {
245                path: base_path.join("ByteArrayToIntArraySerializer.java"),
246                content: gen_byte_array_serializer(&package),
247                generated_header: true,
248            });
249        }
250
251        // Collect builder class names generated from record types with defaults,
252        // so we can skip opaque types that would collide with them.
253        let builder_class_names: AHashSet<String> = api
254            .types
255            .iter()
256            .filter(|t| !t.is_opaque && (!t.fields.is_empty() || (t.has_serde && t.fields.is_empty())) && t.has_default)
257            .map(|t| format!("{}Builder", t.name))
258            .collect();
259
260        // 4b. Opaque handle types (skip if a pure-Java builder already covers this name)
261        for typ in api.types.iter().filter(|typ| !typ.is_trait) {
262            if typ.is_opaque && !builder_class_names.contains(&typ.name) {
263                files.push(GeneratedFile {
264                    path: base_path.join(format!("{}.java", typ.name)),
265                    content: gen_opaque_handle_class(&package, typ, &prefix),
266                    generated_header: true,
267                });
268            }
269        }
270
271        // 5. Enums
272        for enum_def in &api.enums {
273            // Skip enums that gen_visitor handles with richer visitor-specific versions
274            if has_visitor_pattern && enum_def.name == "VisitResult" {
275                continue;
276            }
277            files.push(GeneratedFile {
278                path: base_path.join(format!("{}.java", enum_def.name)),
279                content: gen_enum_class(&package, enum_def),
280                generated_header: true,
281            });
282        }
283
284        // 6. Error exception classes
285        for error in &api.errors {
286            for (class_name, content) in alef_codegen::error_gen::gen_java_error_types(error, &package) {
287                files.push(GeneratedFile {
288                    path: base_path.join(format!("{}.java", class_name)),
289                    content,
290                    generated_header: true,
291                });
292            }
293        }
294
295        // 7. Visitor support files (only when ConversionOptions/ConversionResult types exist)
296        if has_visitor_pattern {
297            for (filename, content) in crate::gen_visitor::gen_visitor_files(&package, &main_class) {
298                files.push(GeneratedFile {
299                    path: base_path.join(filename),
300                    content,
301                    generated_header: false, // already has header comment
302                });
303            }
304        }
305
306        // 8. Trait bridge plugin registration files
307        // Emits two files per trait: I{Trait}.java (managed interface) and
308        // {Trait}Bridge.java (Panama upcall stubs + register/unregister helpers).
309        for bridge_cfg in &config.trait_bridges {
310            if bridge_cfg.exclude_languages.contains(&Language::Java.to_string()) {
311                continue;
312            }
313
314            // When visitor_callbacks is active, visitor traits bound via options_field are
315            // surfaced through Visitor.java + VisitorBridge.java (generated by gen_visitor_files).
316            // The raw trait bridge I{Trait}.java emitted here would be an unreferenced orphan
317            // with snake_case method names. Suppress it for options_field-bound visitor traits.
318            if has_visitor_pattern && bridge_cfg.bind_via == BridgeBinding::OptionsField {
319                continue;
320            }
321
322            if let Some(trait_def) = api.types.iter().find(|t| t.name == bridge_cfg.trait_name && t.is_trait) {
323                let has_super_trait = bridge_cfg.super_trait.is_some();
324                let trait_bridge::BridgeFiles {
325                    interface_content,
326                    bridge_content,
327                } = trait_bridge::gen_trait_bridge_files(trait_def, &prefix, &package, has_super_trait);
328
329                files.push(GeneratedFile {
330                    path: base_path.join(format!("I{}.java", trait_def.name)),
331                    content: interface_content,
332                    generated_header: true,
333                });
334                files.push(GeneratedFile {
335                    path: base_path.join(format!("{}Bridge.java", trait_def.name)),
336                    content: bridge_content,
337                    generated_header: true,
338                });
339            }
340        }
341
342        Ok(files)
343    }
344
345    fn generate_public_api(
346        &self,
347        api: &ApiSurface,
348        config: &ResolvedCrateConfig,
349    ) -> anyhow::Result<Vec<GeneratedFile>> {
350        let package = config.java_package();
351        let prefix = config.ffi_prefix();
352        let main_class = Self::resolve_main_class(api);
353        let package_path = package.replace('.', "/");
354
355        let output_dir = config
356            .output_for("java")
357            .map(|p| p.to_string_lossy().into_owned())
358            .unwrap_or_else(|| "packages/java/src/main/java/".to_string());
359
360        // If output_dir already ends with the package path (user configured the full path),
361        // use it as-is. Otherwise, append the package path.
362        let base_path = if output_dir.ends_with(&package_path) || output_dir.ends_with(&format!("{}/", package_path)) {
363            PathBuf::from(&output_dir)
364        } else {
365            PathBuf::from(&output_dir).join(&package_path)
366        };
367
368        // Collect bridge param names/aliases to strip from the public facade.
369        let bridge_param_names: HashSet<String> = config
370            .trait_bridges
371            .iter()
372            .filter_map(|b| b.param_name.clone())
373            .collect();
374        let bridge_type_aliases: HashSet<String> = config
375            .trait_bridges
376            .iter()
377            .filter_map(|b| b.type_alias.clone())
378            .collect();
379        let has_visitor_pattern = config.ffi.as_ref().map(|f| f.visitor_callbacks).unwrap_or(false)
380            || config
381                .trait_bridges
382                .iter()
383                .any(|b| b.bind_via == BridgeBinding::OptionsField);
384
385        // Generate a high-level public API class that wraps the raw FFI class.
386        // Class name = main_class without "Rs" suffix (e.g., HtmlToMarkdownRs -> HtmlToMarkdown)
387        let public_class = main_class.trim_end_matches("Rs").to_string();
388        let facade_content = gen_facade_class(
389            api,
390            &package,
391            &public_class,
392            &main_class,
393            &prefix,
394            &bridge_param_names,
395            &bridge_type_aliases,
396            has_visitor_pattern,
397        );
398
399        Ok(vec![GeneratedFile {
400            path: base_path.join(format!("{}.java", public_class)),
401            content: facade_content,
402            generated_header: true,
403        }])
404    }
405
406    fn build_config(&self) -> Option<BuildConfig> {
407        Some(BuildConfig {
408            tool: "mvn",
409            crate_suffix: "",
410            build_dep: BuildDependency::Ffi,
411            post_build: vec![],
412        })
413    }
414}