Skip to content

Add support for objects in @JavaImplementation #595

@madsodgaard

Description

@madsodgaard

Currently the @JavaImplementation macro does not support native functions with Java classes as parameters or return objects.

If we added support for this, we could for example write a OkHttpClientCallback using native functions that allows us to implement a Callback interface in Swift:

public abstract class OkHttpCallback implements Callback {
    @Override
    public void onFailure(Call call, IOException e) {
        onFailure(e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        onResponse(response);
    }

    public native void onFailure(String message);
    public native void onResponse(Response response);
}
@JavaClass("com.madsodgaard.swiftjava.OkHttpCallback", implements: Callback.self)
open class OkHttpCallback: JavaObject {
    private let continuation: CheckedContinuation<Response, any Error>

    public init(
        continuation: CheckedContinuation<Response, any Error>
    ) {
        self.continuation = continuation
        super.init()
    }
}

@JavaImplementation("com.madsodgaard.swiftjava.OkHttpCallback")
extension OkHttpCallback {
    @JavaMethod
    func onFailure(message: String) {
        continuation.resume(throwing: OkHttpClientTransport.Error.networkError(message))
    }

    @JavaMethod
    func onResponse(response: Response) {
        continuation.resume(returning: response)
    }
}

The above currently does not compile with

'JNIType' is not a member type of class 'OkHttp.Response'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions