Skip to content
Open
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
5 changes: 5 additions & 0 deletions Sources/SwiftJavaToolLib/JavaClassTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ extension JavaClassTranslator {
}
// Do we need to record any generic information, in order to enable type-erasure for the upcalls?
var parameters: [String] = []
// If the method name is "init", we need to explicitly specify it in the annotation
// because "init" is a Swift keyword and will be escaped in the function name via `init`
if javaMethod.getName() == "init" {
parameters.append("\"init\"")
}
if hasTypeEraseGenericResultType {
parameters.append("typeErasedResult: \"\(resultType)\"")
}
Expand Down
32 changes: 32 additions & 0 deletions Tests/SwiftJavaToolLibTests/WrapJavaTests/BasicWrapJavaTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,36 @@ final class BasicWrapJavaTests: XCTestCase {
)
}

// Test that Java methods named "init" get @JavaMethod("init") annotation.
// Since "init" is a Swift keyword and gets escaped with backticks in the function name,
// we explicitly specify the Java method name in the annotation.
// See KeyAgreement.init() methods as a real-world example.
func test_wrapJava_initMethodAnnotation() async throws {
let classpathURL = try await compileJava(
"""
package com.example;

class TestClass {
public void init(String arg) throws Exception {}
public void init() throws Exception {}
}
""")

try assertWrapJavaOutput(
javaClassNames: [
"com.example.TestClass"
],
classpath: [classpathURL],
expectedChunks: [
"""
@JavaMethod("init")
open func `init`(_ arg0: String) throws
""",
"""
@JavaMethod("init")
open func `init`() throws
""",
]
)
}
}
Loading