swiftlang / swift-java

Apache License 2.0
724 stars 27 forks source link

Java2Swift: static native methods #175

Open lhoward opened 1 week ago

lhoward commented 1 week ago

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()
  }
}