Skip to content

Add Support for Static Fields to Java2Swift tool #61

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 4 commits into from
Oct 10, 2024

Conversation

jrosen081
Copy link
Contributor

Closes #39

This adds support to adding static fields to the Java2Swift tool. The issue recommends adding a @JavaStaticField macro, but it looks like due to the fact that the subscript created by the JavaField macro works for static properties when defined on a JavaClass, so I'm not sure a @JavaStaticField really buys anything here, so I'm open to removing it.

Same testing as in #55 (since that one also adds a static String).

I also noticed that we had a staticMethods array, but were re-requesting all methods in the Java2Swift tool when handling static methods, so I updated it to just use the staticMethods array to simplify the code a bit.

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.

Looks good to me but I'll give @DougGregor a chance to answer the question asked 👍

@@ -103,7 +103,7 @@ struct HelloSwift {
}

extension JavaClass<HelloSwift> {
@JavaField
@JavaStaticField
var initialValue: Double
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this be a static var?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an instance in JavaClass similar to how static methods are instance methods on their Java class. I was thinking of how to get this be a static var, but everything needs the JNIEnvironment (from my understanding), which is an instance var on all Java structs

Copy link
Member

Choose a reason for hiding this comment

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

Right. Java static methods/vars become non-static members on the appropriately-specialized JavaClass instance.

Copy link
Member

Choose a reason for hiding this comment

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

For bonus points, one could have the @JavaStaticField macro detect the static and produce an error diagnosing the problem, but it's not really necessary. Folks shouldn't have to write these macro uses themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmmm do we have access to the surrounding context of a macro? I know you did some work there, but I lost where that ended up.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the lexicalContext of the macro captures this information. To be very clear, I don't think this diagnostic is something that should block your PR from being merged. I just wanted to note that we can implement these kinds of checks in the macro implementation if we want.

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

This looks good to me, with the one minor request to the log message to make it easier to figure out when static fields aren't getting translated.

@@ -103,7 +103,7 @@ struct HelloSwift {
}

extension JavaClass<HelloSwift> {
@JavaField
@JavaStaticField
var initialValue: Double
Copy link
Member

Choose a reason for hiding this comment

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

For bonus points, one could have the @JavaStaticField macro detect the static and produce an error diagnosing the problem, but it's not really necessary. Folks shouldn't have to write these macro uses themselves.

@ktoso ktoso merged commit b69f4d7 into swiftlang:main Oct 10, 2024
9 of 10 checks passed
@ktoso
Copy link
Collaborator

ktoso commented Oct 10, 2024

Nice thank you!

@ktoso
Copy link
Collaborator

ktoso commented Oct 10, 2024

Issue for the follow up : #65

@DougGregor
Copy link
Member

#73 makes it possible to write unit tests for Java2Swift, so long as you can find some class in the JDK that has the kind of thing you want to test.

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.

JavaKit support for static fields (wrapping Java types in Swift)
3 participants