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

feature: add support for java 14 switch/yield #3314

Merged
merged 16 commits into from
Apr 1, 2020

Conversation

MartinWitt
Copy link
Collaborator

@MartinWitt MartinWitt commented Mar 25, 2020

Java 14 Switches

Jep361 defines a new switchExpression.

switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
    case TUESDAY                -> System.out.println(7);
    case THURSDAY, SATURDAY     -> System.out.println(8);
    case WEDNESDAY              -> System.out.println(9);
}

@Egor18 implemented them already without yield( awesome work btw).
This pr adds support for java14 switches with yield and with this change spoon supports all java14 features that are non preview(records and textblocks are only part of jdt beta branch and still preview)

Whats part of this PR

  • Remove break [expression] and java 12 preview switches
  • add support for yieldStatements in switch
  • upgrade jdt version 3.20
  • fully refactor switchCaseTest to junit5

Consequences

There are 2 consequence expect more code.

spoon loses the ability to parse java 12 preview code with break [expression]

Currently spoon supports them, but i would instantly drop support for a feature that was never part java (only preview) and wont come back. Yield replaces this.
Supported preview features should be seen as an experimental feature and i hope no one is relaying on them.
####junit5 in testclasses####
I cant guarantee that every testrunner in everyones IDE fully supports junit5.
I would be nice if you could test the refactored SwitchCaseTest.
Reward is a much clearer design for the testclasses.

upgrading jdt to 3.20 produced 2 problems

Luckily there were only 2 visible problems after upgrading it.

break.isImplicit flag was removed

wasn't a big problem, because there are no longer needed.

PackageBinding.java was changed to a abstract class

Is a bigger problem, but current there is a fix.
For (potential) fix see src/main/java/spoon/support/compiler/jdt/ReferenceBuilder.java#363
Maybe someone with more knowledge about this class could review it.

interesting more academic fact

Is allowed code

int a = switch("a){
default -> {yield 3;}
};

but the following is not allowed.

int a = switch("a){
default -> yield 3;
};

This behavior seems intended, because looking at java grammar we find:
SwitchRule:
SwitchLabel -> Expression ;
SwitchLabel -> Block
SwitchLabel -> ThrowStatement
and yield is not an expression but a StatementWithoutTrailingSubstatement from my understanding.

tl;dr;

  • spoon supports java 14 switches with yield
  • If you have some knowledge about some parts of spoon feel free to review the new code with new tests

@monperrus
Copy link
Collaborator

That's a serious PR, thanks a lot! Will look at it in detail.

Copy link
Collaborator

@monperrus monperrus left a comment

Choose a reason for hiding this comment

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

Reviewed it, it looks really good, congrats for understanding all the code generation infrastructure.

  • in pom.xml could you remove the space changes?
  • see also inline comments

Thanks!

@monperrus
Copy link
Collaborator

monperrus commented Mar 27, 2020 via email

@MartinWitt MartinWitt changed the title WIP: Feature: java 14 switch/yield Feature: java 14 switch/yield Mar 30, 2020
@monperrus monperrus changed the title Feature: java 14 switch/yield feature: add support for java 14 switch/yield Apr 1, 2020
@monperrus monperrus merged commit e7d4213 into INRIA:master Apr 1, 2020
@monperrus
Copy link
Collaborator

Thanks a lot @MartinWitt it's awesome that we now support Java 14!

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