Skip to content
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

Fix "fix return type" quickfix for type param return type #1559

Conversation

datho7561
Copy link
Contributor

@datho7561 datho7561 commented Jul 30, 2024

Fixes #1558

What it does

The old implementation would propose replacing the return type with erasure of the type parameter if the compliance was set pre 1.5. If the compliance was 1.5 or later, it would propose replacing the return type with the type parameter as it appears in the declaration of the method being overridden.

Instead, these are now two separate entries which may be generated:

  1. Replace the return type with the erasure of the type parameter

This entry is only generated if it is valid to use the erasure for the return type. Specifically, if the method declares the type parameters of the method being overriden, and one of type variables has an upper bound that is a parameterized type, then using the type erasure of that variable in place of the variable is not valid, since the type erasure allows a wider range of types. Code example:

public class Example {
  private abstract static class Parent {
    <E extends List<String>> E myMethod(E arg1);
  }
  private static class Child extends Parent {
    @Override
    <E extends List<String>> List myMethod(E arg1) { return null; }; // NOT VALID, because it doesn't override myMethod
  }
}
  1. Replace the return type with the type parameter

This entry now takes into account that the type parameters may not yet be declared on the type. i.e. if the method has no type parameters, the quick fix introduces them, adds the correct bounds to them, and augments the existing parameter types with the type variables using the information from the parent declaration.

This entry also takes into account the fact that the type parameters may have different names in the overriding method than in the original declaration.

How to test

Try out the quickfixes by invoking quickfix (Ctrl+1) in the snippet from the linked issue.

Also check out the snippets from the test cases, which go further in depth.

Author checklist

@akurtakov
Copy link
Contributor

@datho7561
Copy link
Contributor Author

Yes, I'll add a test for this. Thanks for the pointer!

I'm waiting to see the test result to see if I broken any existing tests.

@datho7561 datho7561 force-pushed the 1558-handle-genericized-method-return-type-post-1.4 branch 5 times, most recently from 5b98382 to d29a722 Compare July 31, 2024 18:50
The old method would propose the erasure of the type param if the
compliance was set pre 1.5.
If the compliance

Instead, now two quickfixes are always propose:
1. Replace the return type with the erasure
2. Introduce a new type param with the same bounds,
   importing the bounds and adding the type variable for any raw types

Fixes eclipse-jdt#1558

Signed-off-by: David Thompson <davthomp@redhat.com>
@datho7561 datho7561 force-pushed the 1558-handle-genericized-method-return-type-post-1.4 branch from d29a722 to e4f01a3 Compare July 31, 2024 18:55
@datho7561 datho7561 marked this pull request as ready for review July 31, 2024 18:55
@datho7561
Copy link
Contributor Author

@jjohnstn If you have some time, could you please take a look at this PR?

@jjohnstn
Copy link
Contributor

jjohnstn commented Aug 6, 2024

@datho7561 Sure, I'll look at it today.

@datho7561
Copy link
Contributor Author

Thank you!

Copy link
Contributor

@jjohnstn jjohnstn 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. Thanks.

@jjohnstn jjohnstn merged commit 5248d96 into eclipse-jdt:master Aug 6, 2024
9 checks passed
@datho7561 datho7561 deleted the 1558-handle-genericized-method-return-type-post-1.4 branch August 6, 2024 21:10
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.

Quickfix for return type that's a wildcard doesn't work post 1.4
3 participants