Skip to content

Commit e16ef46

Browse files
authored
javac plugin: keep parameter names (#108)
1 parent b1a82db commit e16ef46

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ struct JavaCompilerBuildToolPlugin: BuildToolPlugin {
5858
.appending(path: "bin")
5959
.appending(path: "javac"),
6060
arguments: javaFiles.map { $0.path(percentEncoded: false) } + [
61-
"-d", javaClassFileURL.path()
61+
"-d", javaClassFileURL.path(),
62+
"-parameters", // keep parameter names, which allows us to emit them in generated Swift decls
6263
],
6364
inputFiles: javaFiles,
6465
outputFiles: classFiles

USER_GUIDE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,19 @@ A number of JavaKit modules provide Swift projections of Java classes and interf
389389

390390
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
391391

392+
#### Improve parameter names of imported Java methods
393+
When building Java libraries you can pass the `-parameters` option to javac
394+
in your build system (Gradle, Maven, sbt, etc) in order to retain the parameter names in the resulting byte code.
395+
396+
This way the imported methods will keep their original parameter names, and you'd get e.g.:
397+
```swift
398+
// public func hello(String name)
399+
func hello(_ name: String)
400+
```
401+
rather than just `arg0` parameters.
402+
403+
When building Java sources using the JavaCompilerPlugin this option is passed by default.
404+
392405
### Class objects and static methods
393406

394407
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<URL>`. 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`,

0 commit comments

Comments
 (0)