-
Notifications
You must be signed in to change notification settings - Fork 46
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=google/google-java-format#988 from TheCK:master 4771486db7d8aab84eb4ecf8e68af2612d0c2b5c PiperOrigin-RevId: 588913297
- Loading branch information
Showing
8 changed files
with
122 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,6 @@ jdks { | |
} | ||
|
||
javaVersions { | ||
libraryTarget = 17 | ||
libraryTarget = 21 | ||
runtime = 21 | ||
} |
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
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
...-java-format/src/main/java/com/palantir/javaformat/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 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.javaformat.java.java21; | ||
|
||
import com.palantir.javaformat.OpsBuilder; | ||
import com.palantir.javaformat.java.java14.Java14InputAstVisitor; | ||
import com.sun.source.tree.CaseTree; | ||
import com.sun.source.tree.ExpressionTree; | ||
/** | ||
* Extends {@link Java14InputAstVisitor} with support for AST nodes that were added or modified in | ||
* Java 21. | ||
*/ | ||
public class Java21InputAstVisitor extends Java14InputAstVisitor { | ||
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
...a-format/src/test/resources/com/palantir/javaformat/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
...-format/src/test/resources/com/palantir/javaformat/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; | ||
}; | ||
} | ||
} |