Skip to content

Commit

Permalink
Use getDeclaredMethods() so we can see non-public methods of classes
Browse files Browse the repository at this point in the history
When we are translating a class that was built as part of the Swift module,
use getDeclaredMethods() so we also see non-public methods.

Part of issue swiftlang#106.
  • Loading branch information
DougGregor committed Oct 25, 2024
1 parent e16ef46 commit c51ec75
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
37 changes: 31 additions & 6 deletions Sources/Java2SwiftLib/JavaTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []
Expand Down Expand Up @@ -290,7 +286,6 @@ extension JavaTranslator {
)

if implementedInSwift {
nativeMembers.append(translated)
return nil
}

Expand Down Expand Up @@ -327,7 +322,6 @@ extension JavaTranslator {
)

if implementedInSwift {
nativeMembers.append(translated)
return nil
}

Expand Down Expand Up @@ -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 =
"""
Expand Down
3 changes: 3 additions & 0 deletions Sources/JavaKitReflection/JavaClass+Reflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ extension JavaClass {
@JavaMethod
public func getCanonicalName() -> String

@JavaMethod
public func getDeclaredMethods() -> [Method?]

@JavaMethod
public func getMethods() -> [Method?]

Expand Down

0 comments on commit c51ec75

Please sign in to comment.