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

ConvertToLambda hint should ignore default methods. #6658

Merged
merged 1 commit into from
Jan 13, 2024

Conversation

mbien
Copy link
Member

@mbien mbien commented Nov 3, 2023

Default methods don't qualify for functional interfaces since they are not abstract and can therefore not be converted into lambdas.

The hint has to go through the interface hierarchy and check if the implemented method was a default method.

example:

    private interface NotFunctional1 {
        default void a(int i) {};
    }
    
    // can't be converted to lambda
    NotFunctional1 nf = new NotFunctional1() { 
        @Override
        public void a(int i) {
        }
    };

real world example: com.sun.source.util.TaskListener

@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) hints labels Nov 3, 2023
@mbien mbien added this to the NB21 milestone Nov 3, 2023
@mbien mbien requested a review from lahodaj November 3, 2023 05:13
@mbien mbien marked this pull request as ready for review November 4, 2023 18:51
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

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

Looks like a reasonable direction to me, with some minor comments inline.

@@ -137,9 +137,48 @@ private MethodTree getMethodFromFunctionalInterface(TreePath pathToNewClassTree)
candidate = (MethodTree)member;
}
}
// default methods can't be implemented with a lambda
ExecutableElement candidateElement = (ExecutableElement) info.getTrees().getElement(new TreePath(pathToNewClassTree, candidate));
Copy link
Contributor

Choose a reason for hiding this comment

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

So, two comments here:

  • should we rather check the method implements an abstract interface method, rather than checking it is not default? (Meaning, if the method based is not found, we don't try to do the conversion?)
  • should this be moved to be part of ensurePreconditionsAreChecked? (And then passesAllPreconditions and passesFatalPreconditions?)

Copy link
Member Author

@mbien mbien Nov 30, 2023

Choose a reason for hiding this comment

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

should we rather check the method implements an abstract interface method, rather than checking it is not default? (Meaning, if the method based is not found, we don't try to do the conversion?)

changed it to an override-abstract-method check. More straight forward, I agree.

should this be moved to be part of ensurePreconditionsAreChecked? (And then passesAllPreconditions and passesFatalPreconditions?)

I haven't implemented this so far. since the method getMethodFromFunctionalInterface is wrong without the change - it would return a method even when the interface is not functional. The precondition checks are all working, since the existence of the method is part of the checks.

Please take another look if you agree, if not I can still make it a precondition - but we probably should also rename the method. Also please note that many methods are actually unused. E.g passesAllPreconditions isn't used anywhere which means the test coverage is also not ideal atm.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jlahoda forgot to ping

Default methods don't qualify for functional interfaces since they
are not abstract and can therefore not be converted into lambdas.
@mbien mbien force-pushed the no-lambda-for-default-methods branch from 33a73e5 to b2e0288 Compare November 30, 2023 02:00
@mbien
Copy link
Member Author

mbien commented Dec 6, 2023

would like to start to merge a few of my PRs this weekend since they start to pile up, this would be a potential candidate, cc @lahodaj

@mbien
Copy link
Member Author

mbien commented Jan 12, 2024

planning to merge this this weekend before feature freeze unless someone objects.

Copy link
Contributor

@matthiasblaesing matthiasblaesing left a comment

Choose a reason for hiding this comment

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

Implementation looks sane to me and I was able to reproduce the problem and found it fixed after this.

@mbien mbien merged commit 3e5661f into apache:master Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hints Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants