/// Variant: {{ variant_name }} for {{ base_method }}
///
/// Wrapper constructor builds {{ wrapper_type_name }} with fixed and free args.
#[no_mangle]
pub extern "system" fn {{ symbol }}(
    env: EnvUnowned,
    _class: JClass,
    owner_handle: jlong,
    handler: JObject,
    {{ free_params_decl }}
) -> jint {
    // Validate owner handle
    if owner_handle == 0 {
        return 1; // Error: null pointer
    }

    let jvm = match env.get_java_vm() {
        Ok(vm) => vm,
        Err(_) => return 2, // Error: failed to get JavaVM
    };

    let global_ref = match env.new_global_ref(&handler) {
        Ok(g) => g,
        Err(_) => return 3, // Error: failed to create global reference
    };

    let method_id = match env.get_method_id(
        &handler,
        "{{ dispatch_method }}",
        "(Ljava/lang/String;)Ljava/lang/String;"
    ) {
        Ok(id) => id,
        Err(_) => return 4, // Error: failed to find method
    };

    let bridge = {{ bridge_name }} {
        global_ref,
        jvm,
        method_id,
    };
    let handler_arc: Arc<dyn {{ core_import }}::{{ contract_name }}> = Arc::new(bridge);

    // Build wrapper: {{ wrapper_type_name }}::{{ constructor_method }}({{ constructor_args }})
    let wrapper = {{ wrapper_type_path }}::{{ constructor_method }}({{ constructor_call_args }});

    // SAFETY: owner_handle was returned by the service constructor and
    // is valid until freed. The caller is responsible for ensuring no use-after-free.
    match unsafe {
        let owner_opaque = owner_handle as *mut {{ opaque_name }};
        (*owner_opaque).inner.{{ base_method }}({{ base_call_args }})
    } {
        Ok(_) => 0, // Success
        Err(_) => 5, // Error: registration failed
    }
}
