Skip to content

JavaKit: Enable imported Java classes as Swift classes by default #141

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

Conversation

DougGregor
Copy link
Member

Flip the switch to have Java2Swift generate Swift classes to represent
each imported Java class, and make the (relatively few) adjustments
needed to the JavaKit library to make that work.

Implements issue #132.

@DougGregor
Copy link
Member Author

Note that most of the logic is #140. This PR exists to switch the default over to the new mechanism.

Flip the switch to have Java2Swift generate Swift classes to represent
each imported Java class, and make the (relatively few) adjustments
needed to the JavaKit library to make that work.

Implements issue swiftlang#132.
@DougGregor DougGregor force-pushed the java-class-as-swift-class-by-default branch from 303cb4f to 4c86346 Compare October 31, 2024 16:09
@DougGregor DougGregor merged commit 30c0a0d into swiftlang:main Nov 1, 2024
11 checks passed
@DougGregor DougGregor deleted the java-class-as-swift-class-by-default branch November 1, 2024 20:28
@lhoward
Copy link
Contributor

lhoward commented Nov 1, 2024

FYI this patch breaks building with Swift 6.0.2 (not Android-specific).

error: emit-module command failed with exit code 1 (use -v to see invocation)
macro expansion @JavaStaticField:5:5: error: 'nonmutating' is not valid on setters in classes
`- /Users/lukeh/CVSRoot/apple/swift-java/Samples/JavaKitSampleApp/.build/plugins/outputs/javakitsampleapp/JavaKitExample/destination/Java2SwiftPlugin/generated/HelloSubclass.swift:15:34: note: expanded code originates here
13 | extension JavaClass<HelloSubclass> {
14 |   @JavaStaticField(isFinal: false)
15 |   public var initialValue: Double
   +--- macro expansion @JavaStaticField -------------------------------
   |3 |         self[javaFieldName: "initialValue", fieldType: Double.self]
   |4 |     }
   |5 |     nonmutating set {
   |  |     `- error: 'nonmutating' is not valid on setters in classes
   |6 |         self[javaFieldName: "initialValue", fieldType: Double.self] = newValue
   |7 |     }
   +--------------------------------------------------------------------
16 | }
17 | 

@DougGregor
Copy link
Member Author

Sorry, must be that we aren't building this sample in CI yet. Fix here: #146

@DougGregor
Copy link
Member Author

@lhoward should be fixed on main now

@lhoward
Copy link
Contributor

lhoward commented Nov 1, 2024

Thanks, works.

@lhoward
Copy link
Contributor

lhoward commented Nov 1, 2024

Think there may be some rough edges with return type overloading.

% grep array *
JavaNIOBuffer.swift:  open func array() -> JavaObject!
JavaNIOBuffer.swift:  open func arrayOffset() -> Int32
JavaNIOByteBuffer.swift:  open override func array() -> [Int8]
JavaNIOByteBuffer.swift:  open override func arrayOffset() -> Int32
/Users/lukeh/CVSRoot/padl/inferno/ui/FlutterSwift/.build/plugins/outputs/flutterswift/AndroidFlutter/destination/Java2SwiftPlugin/generated/JavaNIOBuffer.swift:35:13: note: potential overridden instance method 'array()' here
33 | 
34 |   @JavaMethod
35 |   open func array() -> JavaObject!
   |             `- note: potential overridden instance method 'array()' here
36 | 
37 |   @JavaMethod
/Users/lukeh/CVSRoot/padl/inferno/ui/FlutterSwift/.build/plugins/outputs/flutterswift/AndroidFlutter/destination/Java2SwiftPlugin/generated/JavaNIOByteBuffer.swift:158:22: error: method does not override any method from its superclass
156 | 
157 |   @JavaMethod
158 |   open override func array() -> [Int8]
    |                      `- error: method does not override any method from its superclass
159 | 
160 |   @JavaMethod

/Users/lukeh/CVSRoot/padl/inferno/ui/FlutterSwift/.build/plugins/outputs/flutterswift/AndroidFlutter/destination/Java2SwiftPlugin/generated/JavaNIOBuffer.swift:35:13: note: potential overridden instance method 'array()' here

@DougGregor
Copy link
Member Author

Fix for the (non-)covariance of array-returning methods at #148

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.

3 participants