swiftlang / swiftlang/swift-java

Java2Swift: static native methods

Open
#175 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Are static native methods supported?

I have (yes, related to #168) this:

public class SwiftHeapObjectHolder implements AutoCloseable {
...
  public static native void _releaseSwiftObject(long swiftObject);
}

but I don't see anything in the generated Swift file. If I just naively attempt to implement as an extension on JavaClass<SwiftHeapObjectHolder>, I get a load time error – weirdly it's looking for Java_com_padl_FlutterAndroid_SwiftHeapObjectHolder__1releaseSwiftObject. No idea where the 1 prefix came from. If I put that in the Swift method name, it at least loads OK:

@JavaImplementation("com.padl.FlutterAndroid.SwiftHeapObjectHolder")
extension JavaClass<SwiftHeapObjectHolder> {
  @JavaMethod
  public static func _1releaseSwiftObject(swiftObject: Int64, environment: JNIEnvironment? = nil) {
    guard swiftObject != 0 else { return }
    let swiftObjectIntPtr = Int(swiftObject)
    unsafeBitCast(Int(swiftObjectIntPtr), to: Unmanaged<AnyObject>.self).release()
  }
}

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

Start with the Java declaration and compare it with the generated Swift file, then inspect how native methods are represented for JavaClass extensions. Verify that static native methods are generated and that the resulting Swift implementation loads with the expected native name.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, swift
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.