Add support for case null, default in switch and fix concrete syntax model for new switch syntax#4364
Conversation
javaparser-core/src/main/java/com/github/javaparser/printer/ConcreteSyntaxModel.java
Outdated
Show resolved
Hide resolved
...ser-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmConditional.java
Outdated
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4364 +/- ##
=============================================
+ Coverage 51.860% 51.929% +0.069%
=============================================
Files 497 503 +6
Lines 28326 28369 +43
Branches 4916 4920 +4
=============================================
+ Hits 14690 14732 +42
+ Misses 11595 11591 -4
- Partials 2041 2046 +5
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report in Codecov by Sentry.
|
|
I see there's a single test failing with which looks unrelated to this change, but I don' have permission to rerun the action. |
javaparser-core/src/main/java/com/github/javaparser/metamodel/SwitchEntryMetaModel.java
Show resolved
Hide resolved
javaparser-core/src/main/java/com/github/javaparser/metamodel/JavaParserMetaModel.java
Show resolved
Hide resolved
javaparser-core/src/main/java/com/github/javaparser/ast/stmt/SwitchEntry.java
Show resolved
Hide resolved
javaparser-core/src/main/java/com/github/javaparser/ast/observer/ObservableProperty.java
Show resolved
Hide resolved
javaparser-core/src/main/java/com/github/javaparser/printer/DefaultPrettyPrinterVisitor.java
Show resolved
Hide resolved
javaparser-core/src/main/java/com/github/javaparser/printer/PrettyPrintVisitor.java
Show resolved
Hide resolved
...-generators/src/main/java/com/github/javaparser/generator/core/other/TokenKindGenerator.java
Show resolved
Hide resolved
...ser-core/src/main/java/com/github/javaparser/printer/concretesyntaxmodel/CsmConditional.java
Outdated
Show resolved
Hide resolved
...ser-core/src/main/java/com/github/javaparser/printer/lexicalpreservation/changes/Change.java
Outdated
Show resolved
Hide resolved
|
It's a great job. Thank you very much. Your approach is exactly what I'm looking for. Let's take things one step at a time, because I don't necessarily have the time or the skills to analyse very large PR files. I also prefer it when you first explain what you want to do and what the alternatives are (which you've done perfectly on the other related subjects) because that gives me a slightly clearer idea of the content of the PR. I'll let you answer my questions before I integrate your work. However, I think that the validation part of the new syntax is missing (validator because it should only be authorised from a certain version of Java). It also seems to me that tests on the LPP are missing. Just so you know, before integrating this PR, I'm going to publish a new version of JP. |
...arser/printer/lexicalpreservation/transformations/ast/body/StatementTransformationsTest.java
Show resolved
Hide resolved
|
Thank you for the quick and thorough review! Regarding LPP tests, I did add some simple ones (I added a comment to those so that you can see them in the conversation history). If you'd like me to add more involved ones, please let me know and I'll be happy to do so. I didn't add the validation part to avoid conflicts with #4357, but if this is going to go in first, I'll add that here and then we can deal with conflicts later. Regarding a new version, that's no problem at all! I don't know what you have in mind for the JavaParser release schedule, but in my opinion releasing all of the non-preview java 21 features at once would be ideal! |
I'm not going to validate the PR #4357 because it contains too many unrelated changes and I'm therefore unable to validate its content. There are also points that need to be reworked. It's a big job but I don't want to risk destabilising the project. |
I agree with this. |
Good to know. I'll update this PR with the validation tomorrow then. |
|
And just a quick general update: another task came up that took up my time today, hence no update here. I'll be working on this PR again on Tuesday, however. And thanks again for the quick feedback and open discussion! |
There's no hurry, I've got other things to do too. |
|
I've updated the PR with changes that address the feedback given and I've mostly mentioned specific changes in the conversation threads where they're relevant. There are a few extra additions from code generation, however (specifically the If there's a better way to do this, please let me know! The eventual fix would be to automate formatting so that the project as a whole is consistent, but that's a bigger effort. |
| void recordDeclaration() { | ||
| String s = "record X() { }"; | ||
| ParseResult<CompilationUnit> result = javaParser.parse(COMPILATION_UNIT, provider(s)); | ||
| TestUtils.assertProblems(result, "(line 1,col 1) Record Declarations are not supported. Pay attention that this feature is supported starting from 'JAVA_14' language level. If you need that feature the language level must be configured in the configuration before parsing the source files."); |
There was a problem hiding this comment.
This was Intellij's auto formatting. I could change it back, but the change is more consistent with how other tests do it.
javaparser-core-testing/src/test/java/com/github/javaparser/printer/PrettyPrinterTest.java
Outdated
Show resolved
Hide resolved
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static com.github.javaparser.ParseStart.EXPRESSION; | ||
| import static com.github.javaparser.ParserConfiguration.LanguageLevel.JAVA_21_INCOMPLETE; |
There was a problem hiding this comment.
Can you rename this to JAVA_21.
| "," | ||
| "default" { isDefault = true; } | ||
| | | ||
| LOOKAHEAD({!(getToken(2).image == "," && getToken(3).image == "default")}) |
There was a problem hiding this comment.
Can you explain this new rule?
There was a problem hiding this comment.
I added an explanation as a comment in code as well, but to repeat here for convenience:
Since null is also a conditional expression, a lookahead is necessary to determine
whether this is the "null, default" case, or whether the null is a regular
conditional expression. The check is negated to maintain the ordering in the JLS.In the "null, default" case, the next 3 tokens will be "null" "," "default". Since
"default" can only be preceded by "," in this specific case (as "default" cannot
appear in a list elsewhere), it is sufficient to check that "," "default" appear
in the correct order. Since the only label that can appear before the "default"
is the single-token "null", the "," token must also be the second token.
|
Do you have anything else to add? If not, I'll merge your PR. |
|
If you're happy with the changes here, then I think this PR is done. There are things like the validators, lookahead logic for the different cases, and the CSM that I'll have to change again when adding switch pattern support, but I think it's better to do that when actually adding the relevant features. |
|
Thank you for this development. |
Related issue: #4361
Note: This does not address any of the pattern matching-related changes. I decided to start small to build familiarity with the codebase.
This PR mainly addresses my comment regarding the new
case null, defaultpossibility for switch labels by introducing anisDefaultfield for the switch entry, setting this where appropriate and using this value to distinguish cases for PP and LPP.While testing the LPP, I realized that the new
->syntax for switch expressions wasn't supported in theConcreteSyntaxModel, so I've updated that to work for what's included here (which excludes the patterns case).I see Intellij also made some formatting changes, but I use the style config from this project, so the changes should be correct.
For the code generation part, I added the
isDefaultfield, ran the code generators, then fixed the constructors by hand.