swiftlang / swiftlang/swift-java

Generating SaticMethods protocol ignores 'static native' java methods

Open
#225 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature:wrap-java
Dominant language
Swift
Stars
1.2k
Forks
123
Avg merge
1d 7h
Merged PRs (30d)
16

Description



// JNIExample.java

package com.example;

public class JNIExample {
    static {
        System.loadLibrary("my-swift-lib");
    }

    public native int compute(int a, int b);
    public native int compute(int a, Integer b);

    public static native int computeStatic(int a, int b);

    public static void main(String[] args) {
        int result = new JNIExample().compute(5, 7);
        System.out.println("Result from native code: " + result);
    }
}

will generate the sayHello and compute but will ignore the computeStatic:

/Users/ktoso/code/swift-java/Samples/JavaKitSampleApp/.build/arm64-apple-macosx/debug/Java2Swift-tool --module-name SwiftJNIExample --depends-on JavaKit=/Users/ktoso/code/swift-java/Sources/JavaKit/swift-java.config --swift-native-implementation com.example.JNIExample $(pwd)/swift-java.config

// Auto-generated by Java-to-Swift wrapper generator.
import JavaKit
import JavaRuntime

@JavaClass("com.example.JNIExample")
open class JNIExample: JavaObject {
  @JavaMethod
  @_nonoverride public convenience init(environment: JNIEnvironment? = nil)
}
extension JavaClass<JNIExample> {
  @JavaStaticMethod
  public func main(_ arg0: [String])
}
/// Describes the Java `native` methods for ``JNIExample``.
///
/// To implement all of the `native` methods for JNIExample in Swift,
/// extend JNIExample to conform to this protocol and mark
/// each implementation of the protocol requirement with
/// `@JavaMethod`.
protocol JNIExampleNativeMethods {
  func compute(_ arg0: Int32, _ arg1: JavaInteger?) -> Int32

  func compute(_ arg0: Int32, _ arg1: Int32) -> Int32
}

We need to be able to handle static native methods as well.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with JNIExample.java and the Java2Swift-tool invocation shown, then inspect how the generated JNIExampleNativeMethods protocol is produced. Confirm that the static native computeStatic method is represented in the generated output, while preserving the existing instance methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.