Skip to content

Use getDeclaredMethods() so we can see non-public methods of classes #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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