Skip to content

javac plugin: keep parameter names #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 24, 2024
Merged
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
3 changes: 2 additions & 1 deletion Plugins/JavaCompilerPlugin/JavaCompilerPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<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`,
Expand Down