swiftlang / swiftlang/swift-java

Correctly importing Optional::get() as -> T causes MethodNotFound

Open
#439 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature:jextract feature:wrap-java mode:jni
Dominant language
Swift
Stars
1.2k
Forks
123
Avg merge
1d 7h
Merged PRs (30d)
16

Description

We are now able to handle generics more properly in wrap-java and now we import Optional as:

@JavaClass("java.util.Optional")
open class JavaOptional<T: AnyJavaObject>: JavaObject {
  @JavaMethod
  open func get() -> T!

which then crashes

SwiftJava/JavaOptional.swift:2: Fatal error: 'try!' expression unexpectedly raised an error: java.lang.NoSuchMethodError: Ljava/util/Optional;.get()Ljava/lang/String;

so the String bit is coming unexpectedly because Swift sees the reified generic type as "String" and not "some T" therefore the downcall is forming an incorrect signature.

The problem is somewhere in the dynamicJavaMethodCall infra which can't handle the generic properly when forming the downcall signature

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash from SwiftJava/JavaOptional.swift, then trace the dynamicJavaMethodCall infrastructure that forms the downcall signature for Optional::get(). Verify how the reified generic return type is represented and ensure the completed behavior no longer requests the String-specific Java method signature or raises NoSuchMethodError.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, swift
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.