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 diff --git a/USER_GUIDE.md b/USER_GUIDE.md index dccdd27e..33e08423 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -389,6 +389,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. + ### Class objects and static methods Every `AnyJavaObject` has a property `javaClass` that provides an instance of `JavaClass` specialized to the type. For example, `url.javaClass` will produce an instance of `JavaClass`. The `JavaClass` instance is a wrapper around a Java class object (`java.lang.Class`) that has two roles in Swift. First, it provides access to all of the APIs on the Java class object. The `JavaKitReflection` library, for example, exposes these APIs and the types they depend on (`Method`,