diff --git a/Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java b/Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java index f988bbb6..c59baf33 100644 --- a/Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java +++ b/Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java @@ -27,8 +27,8 @@ public HelloSwift() { this.value = initialValue; } - public native int sayHello(int x, int y); - public native String throwMessageFromSwift(String message) throws Exception; + native int sayHello(int x, int y); + native String throwMessageFromSwift(String message) throws Exception; // To be called back by the native code public double sayHelloBack(int i) { diff --git a/Sources/Java2SwiftLib/JavaTranslator.swift b/Sources/Java2SwiftLib/JavaTranslator.swift index a5af9baa..c48591b9 100644 --- a/Sources/Java2SwiftLib/JavaTranslator.swift +++ b/Sources/Java2SwiftLib/JavaTranslator.swift @@ -239,10 +239,6 @@ extension JavaTranslator { // Members var members: [DeclSyntax] = [] - // Members that are native and will instead go into a NativeMethods - // protocol. - var nativeMembers: [DeclSyntax] = [] - // Fields var staticFields: [Field] = [] var enumConstants: [Field] = [] @@ -290,7 +286,6 @@ extension JavaTranslator { ) if implementedInSwift { - nativeMembers.append(translated) return nil } @@ -327,7 +322,6 @@ extension JavaTranslator { ) if implementedInSwift { - nativeMembers.append(translated) return nil } @@ -441,6 +435,37 @@ extension JavaTranslator { ) } + // Members that are native and will instead go into a NativeMethods + // protocol. + var nativeMembers: [DeclSyntax] = [] + if swiftNativeImplementations.contains(javaClass.getCanonicalName()) { + nativeMembers.append( + contentsOf: javaClass.getDeclaredMethods().compactMap { + $0.flatMap { method in + // FIXME: For now, ignore static methods + if method.isStatic { + return nil + } + + if !method.isNative { + return nil + } + + // Translate the method if we can. + do { + return try translateMethod( + method, + implementedInSwift: true + ) + } catch { + logUntranslated("Unable to translate '\(fullName)' method '\(method.getName())': \(error)") + return nil + } + } + } + ) + } + if !nativeMembers.isEmpty { let protocolDecl: DeclSyntax = """ diff --git a/Sources/JavaKitReflection/JavaClass+Reflection.swift b/Sources/JavaKitReflection/JavaClass+Reflection.swift index 65c93e15..13e080ac 100644 --- a/Sources/JavaKitReflection/JavaClass+Reflection.swift +++ b/Sources/JavaKitReflection/JavaClass+Reflection.swift @@ -29,6 +29,9 @@ extension JavaClass { @JavaMethod public func getCanonicalName() -> String + @JavaMethod + public func getDeclaredMethods() -> [Method?] + @JavaMethod public func getMethods() -> [Method?]