Skip to content

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

.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

.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