-
Notifications
You must be signed in to change notification settings - Fork 861
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for guard clauses in Java 21 switch expressions
This PR adds support for `switch` statements where a `case` has a guard clause. See Issue #983 Fixes #988 COPYBARA_INTEGRATE_REVIEW=#988 from TheCK:master 4771486 PiperOrigin-RevId: 588913297
- Loading branch information
Showing
7 changed files
with
117 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
core/src/main/java/com/google/googlejavaformat/java/java21/Java21InputAstVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2023 The google-java-format Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.googlejavaformat.java.java21; | ||
|
||
import com.google.googlejavaformat.OpsBuilder; | ||
import com.google.googlejavaformat.java.java17.Java17InputAstVisitor; | ||
import com.sun.source.tree.CaseTree; | ||
import com.sun.source.tree.ExpressionTree; | ||
|
||
/** | ||
* Extends {@link Java17InputAstVisitor} with support for AST nodes that were added or modified in | ||
* Java 21. | ||
*/ | ||
public class Java21InputAstVisitor extends Java17InputAstVisitor { | ||
|
||
public Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier) { | ||
super(builder, indentMultiplier); | ||
} | ||
|
||
@Override | ||
protected ExpressionTree getGuard(final CaseTree node) { | ||
return node.getGuard(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class SwitchGuardClause { | ||
boolean test(Object x) { | ||
return switch (x) { | ||
case String s when s.length() < 5 -> true; | ||
case Integer i -> false; | ||
default -> true; | ||
}; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
core/src/test/resources/com/google/googlejavaformat/java/testdata/SwitchGuardClause.output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class SwitchGuardClause { | ||
boolean test(Object x) { | ||
return switch (x) { | ||
case String s when s.length() < 5 -> true; | ||
case Integer i -> false; | ||
default -> true; | ||
}; | ||
} | ||
} |