Skip to content

Commit 912d08d

Browse files
authored
Use getDeclaredMethods() so we can see non-public methods of classes (#110)
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 #106.
1 parent e16ef46 commit 912d08d

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloSwift.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public HelloSwift() {
2727
this.value = initialValue;
2828
}
2929

30-
public native int sayHello(int x, int y);
31-
public native String throwMessageFromSwift(String message) throws Exception;
30+
native int sayHello(int x, int y);
31+
native String throwMessageFromSwift(String message) throws Exception;
3232

3333
// To be called back by the native code
3434
public double sayHelloBack(int i) {

Sources/Java2SwiftLib/JavaTranslator.swift

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,6 @@ extension JavaTranslator {
239239
// Members
240240
var members: [DeclSyntax] = []
241241

242-
// Members that are native and will instead go into a NativeMethods
243-
// protocol.
244-
var nativeMembers: [DeclSyntax] = []
245-
246242
// Fields
247243
var staticFields: [Field] = []
248244
var enumConstants: [Field] = []
@@ -290,7 +286,6 @@ extension JavaTranslator {
290286
)
291287

292288
if implementedInSwift {
293-
nativeMembers.append(translated)
294289
return nil
295290
}
296291

@@ -327,7 +322,6 @@ extension JavaTranslator {
327322
)
328323

329324
if implementedInSwift {
330-
nativeMembers.append(translated)
331325
return nil
332326
}
333327

@@ -441,6 +435,37 @@ extension JavaTranslator {
441435
)
442436
}
443437

438+
// Members that are native and will instead go into a NativeMethods
439+
// protocol.
440+
var nativeMembers: [DeclSyntax] = []
441+
if swiftNativeImplementations.contains(javaClass.getCanonicalName()) {
442+
nativeMembers.append(
443+
contentsOf: javaClass.getDeclaredMethods().compactMap {
444+
$0.flatMap { method in
445+
// FIXME: For now, ignore static methods
446+
if method.isStatic {
447+
return nil
448+
}
449+
450+
if !method.isNative {
451+
return nil
452+
}
453+
454+
// Translate the method if we can.
455+
do {
456+
return try translateMethod(
457+
method,
458+
implementedInSwift: true
459+
)
460+
} catch {
461+
logUntranslated("Unable to translate '\(fullName)' method '\(method.getName())': \(error)")
462+
return nil
463+
}
464+
}
465+
}
466+
)
467+
}
468+
444469
if !nativeMembers.isEmpty {
445470
let protocolDecl: DeclSyntax =
446471
"""

Sources/JavaKitReflection/JavaClass+Reflection.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ extension JavaClass {
2929
@JavaMethod
3030
public func getCanonicalName() -> String
3131

32+
@JavaMethod
33+
public func getDeclaredMethods() -> [Method?]
34+
3235
@JavaMethod
3336
public func getMethods() -> [Method?]
3437

0 commit comments

Comments
 (0)