Skip to content

javakit-macros: Support wrapping generic arguments#601

Open
sidepelican wants to merge 7 commits intoswiftlang:mainfrom
sidepelican:wrapjava_generic_arg
Open

javakit-macros: Support wrapping generic arguments#601
sidepelican wants to merge 7 commits intoswiftlang:mainfrom
sidepelican:wrapjava_generic_arg

Conversation

@sidepelican
Copy link
Contributor

This PR is part of the improvements for #599.

Previously, generic method arguments (such as T) had to be manually replaced with JavaObject.
I have updated the @JavaMethod macro to handle this automatically.
The macro now uses a heuristic search to determine if a type is generic and inserts the necessary conversion to JavaObject.

Additionally, I have updated the implementation of JavaOptional.swift and (partially) List.swift using wrap-java.

@sidepelican sidepelican requested a review from ktoso as a code owner March 10, 2026 03:50
let javaList = try XCTUnwrap(ArrayList<JavaInteger>(environment: environment).as(List<JavaInteger>.self))
_ = javaList.add(JavaInteger(0, environment: environment))
_ = javaList.add(JavaInteger(1, environment: environment))
_ = javaList.add(JavaInteger(2, environment: environment))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently, this add function uses a generic implementation.

if let decl = contextNode.asProtocol(WithGenericParametersSyntax.self) {
if decl.genericParameterClause?.parameters.contains(where: {
$0.name.text == typeName
}) == true {
Copy link
Collaborator

Choose a reason for hiding this comment

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

no need to == true

Suggested change
}) == true {
}) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

genericParameterClause? produces optional bool here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ah i see


/// Determines whether an argument is generic.
/// Since Optional does not appear in JNI signatures, it is removed before checking.
private static func isJNIGenericParameter(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's document that this is a heuristic please, since it may get things wrong. It's good to mark this as source of potential mixups

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upon further review, I noticed that typeErasedResult references JavaClass information, which I believe is more preferable. I've added a comment regarding this point.

Copy link
Collaborator

Choose a reason for hiding this comment

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

nice, thank you

@ktoso
Copy link
Collaborator

ktoso commented Mar 10, 2026

Minor requests here and there, looks good, thank you!

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