From e3352906368512b8a1c645c52cbe8606e7ff2766 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 24 Oct 2024 14:40:45 +0900 Subject: [PATCH 1/2] javac plugin: keep parameter names By adding `-parameters` when we build java code using the java compiler plugin we retain parameter names and therefore we get: ``` func sayHello(_ x: Int32, _ y: Int32) -> Int32 ``` rather than ``` func sayHello(_ arg0: Int32, _ arg1: Int32) -> Int32 ``` which is a slightly nicer developer experience. When using with already built jars we can't influence that, but whenever using with java sources we actually build we should recommend keeping that flag probably. --- Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift b/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift index 3a6c4286..64e1d2a7 100644 --- a/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift +++ b/Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift @@ -58,7 +58,8 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin { .appending(path: "bin") .appending(path: "javac"), arguments: javaFiles.map { $0.path(percentEncoded: false) } + [ - "-d", javaClassFileURL.path() + "-d", javaClassFileURL.path(), + "-parameters", // keep parameter names, which allows us to emit them in generated Swift decls ], inputFiles: javaFiles, outputFiles: classFiles From 94a56132931ce03c869ce5ccd1b72b1c805e5060 Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 24 Oct 2024 16:01:36 +0900 Subject: [PATCH 2/2] more docs --- USER_GUIDE.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 1e75b93d..72202f2a 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -326,6 +326,19 @@ A number of JavaKit modules provide Swift projections of Java classes and interf The `Java2Swift` tool can translate any other Java classes into Swift projections. The easiest way to use `Java2Swift` is with the SwiftPM plugin described above. More information about using this tool directly are provided later in this document. +#### Improve parameter names of imported Java methods +When building Java libraries you can pass the `-parameters` option to javac +in your build system (Gradle, Maven, sbt, etc) in order to retain the parameter names in the resulting byte code. + +This way the imported methods will keep their original parameter names, and you'd get e.g.: +```swift +// public func hello(String name) +func hello(_ name: String) +``` +rather than just `arg0` parameters. + +When building Java sources using the JavaCompilerPlugin this option is passed by default. + ### Up and downcasting All `AnyJavaObject` instances provide `is` and `as` methods to check whether an object dynamically matches another type. The `is` operation is the equivalent of Java's `instanceof` and Swift's `is` operator, and will checkin whether a given object is of the specified type, e.g.,