Skip to content

Add a SwiftPM build plugin to generate Swift wrappers for Java classes #77

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 15, 2024

Conversation

DougGregor
Copy link
Member

Introduce the plugin Java2SwiftPlugin, which will invoke Java2Swift to generate Swift wrappers for the specified Java classes. Each SwiftPM target that uses this plugin must contain a file named Java2Swift.config that specifies options for the Java2Swift tool. This is a JSON file with a top-level dictionary that currently can contain keys for the Java class path (classPath) and a dictionary called classes whose keys are Java canonical class names (such as java.util.Vector) and whose values are the corresponding Swift names. For example:

{
  "classes" : {
    "java.util.Vector" : "JavaVector"
  }
}

will translate the class java.util.Vector into the Swift type JavaVector. The resulting Swift files will become part of the Swift module that uses this plugin.

I've updated the JavaKitSampleApp to make use of this, albeit in a very narrow way that is not at all compelling. The user guide also needs updating.

Fixes issue #16

Introduce the plugin `Java2SwiftPlugin`, which will invoke Java2Swift to
generate Swift wrappers for the specified Java classes. Each SwiftPM
target that uses this plugin must contain a file named `Java2Swift.config`
that specifies options for the Java2Swift tool. This is a JSON file
with a top-level dictionary that currently can contain keys for the
Java class path (`classPath`) and a dictionary called `classes` whose
keys are Java canonical class names (such as `java.util.Vector`) and
whose values are the corresponding Swift names. For example:

```swift
{
  "classes" : {
    "java.util.Vector" : "JavaVector"
  }
}

```

will translate the class `java.util.Vector` into the Swift type `JavaVector`.
The resulting Swift files will become part of the Swift module that uses
this plugin.

I've updated the JavaKitSampleApp to make use of this, albeit in a very
narrow way that is not at all compelling. The user guide also needs updating.

Fixes issue swiftlang#16
@ktoso ktoso self-requested a review October 15, 2024 01:18
@@ -0,0 +1,5 @@
{
"classes" : {
"java.util.Vector" : null
Copy link
Collaborator

@ktoso ktoso Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vector is a rarely used type that folks generally avoid as it predates the "proper collections framework", I'd recommend avoiding it in examples (it'll look weird to Java developers that we're using that anywhere) and using java.util.ArrayList in examples.


double checked in docs:

As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was picking something silly that we wouldn't actually end up wrapping in any of the JavaKit* modules, but yeah... Vector is extra silly. Thanks for the ArrayList suggestion.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, no harm but a bit too silly maybe :) No prob

Package.swift Outdated
@@ -56,6 +56,11 @@ let package = Package(
targets: ["JavaKit"]
),

.library(
name: "JavaRuntime",
targets: [ "JavaRuntime"]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
targets: [ "JavaRuntime"]
targets: ["JavaRuntime"]

Need to enable the Swift formatting enforcement soon 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, sure

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good start, LGTM, minor nitpick inline

@@ -5,6 +5,7 @@
.swift-format
.github/*
*.md
**/*.config
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to fix the license checking CI job, since it checked and failed on the new file not having a license header

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thank you!

@DougGregor
Copy link
Member Author

I have improvements I'd still like to make (e.g., handling Jar files, documenting this new path), but this is a good start

@DougGregor DougGregor merged commit f041f10 into swiftlang:main Oct 15, 2024
11 checks passed
@DougGregor DougGregor deleted the java2swift-plugin branch October 15, 2024 05:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants