Skip to content

wrap-java: Update subtyping logic for generic method overrides#618

Open
sidepelican wants to merge 4 commits intoswiftlang:mainfrom
sidepelican:incollect_override
Open

wrap-java: Update subtyping logic for generic method overrides#618
sidepelican wants to merge 4 commits intoswiftlang:mainfrom
sidepelican:incollect_override

Conversation

@sidepelican
Copy link
Contributor

This PR is likely the final part of the improvements for #599.

In Java,

  • Executable defines: Class<?> getDeclaringClass()
  • Constructor overrides it as: Class<T> getDeclaringClass()

Currently, wrap-java identifies these as the same method signature and adds the override keyword in Swift.
However, since Swift does not recognize a subtype relationship about them(JavaClass<JavaObject> vs JavaClass<T>), it results in the following error:

.../Constructor.swift:113:22: error: method does not override any method from its superclass
111 |   /// ```
112 | @JavaMethod
113 |   open override func getDeclaringClass() -> JavaClass<T>!
    |                      `- error: method does not override any method from its superclass
114 | 
115 |   /// Java method `getAnnotation`.

To resolve this, I have updated the type-checking logic to be more strict:

  • In Type.isEqualTo(_:), resolved an issue where Class<?> and Class<T> were incorrectly treated as identical types.
    • Removed the call to adjustToJavaBounds
  • In Type.isEqualToOrSubtypeOf(_:), changed the logic so that types with different generic arguments are no longer considered to have a subtype relationship.

@sidepelican sidepelican requested a review from ktoso as a code owner March 13, 2026 08:53
}

var methodTypeVars = method.getTypeParameters()
var methodTypeVars = method.getTypeParameters() as [TypeVariable<JavaLangReflect.Method>?]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, The override being treated as overload, explicit type annotations are now required. I believe this is unavoidable.

Copy link
Collaborator

@ktoso ktoso Mar 13, 2026

Choose a reason for hiding this comment

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

There's probably no great way to avoid this... Unless we suffix the methods in the subclass? It might be an inherent incompatibility of how swift and java do overrides though

Open to sneaky ideas, maybe @DougGregor might have some?

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