Skip to content

Commit

Permalink
Add support for guard clauses in Java 21 switch expressions
Browse files Browse the repository at this point in the history
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
TheCK authored and google-java-format Team committed Dec 7, 2023
1 parent ad77154 commit b86c508
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 11 deletions.
29 changes: 28 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
<profile>
<id>jdk11</id>
<activation>
<jdk>(,17)</jdk>
<jdk>[11,17)</jdk>
</activation>
<build>
<plugins>
Expand All @@ -236,13 +236,40 @@
<configuration>
<excludes>
<exclude>**/Java17InputAstVisitor.java</exclude>
<exclude>**/Java21InputAstVisitor.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<excludePackageNames>com.google.googlejavaformat.java.java17</excludePackageNames>
<excludePackageNames>com.google.googlejavaformat.java.java21</excludePackageNames>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>jdk17</id>
<activation>
<jdk>[17,21)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/Java21InputAstVisitor.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<excludePackageNames>com.google.googlejavaformat.java.java21</excludePackageNames>
</configuration>
</plugin>
</plugins>
Expand Down
30 changes: 20 additions & 10 deletions core/src/main/java/com/google/googlejavaformat/java/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,14 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
OpsBuilder builder = new OpsBuilder(javaInput, javaOutput);
// Output the compilation unit.
JavaInputAstVisitor visitor;
if (Runtime.version().feature() >= 17) {
try {
visitor =
Class.forName("com.google.googlejavaformat.java.java17.Java17InputAstVisitor")
.asSubclass(JavaInputAstVisitor.class)
.getConstructor(OpsBuilder.class, int.class)
.newInstance(builder, options.indentationMultiplier());
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
if (Runtime.version().feature() >= 21) {
visitor =
createVisitor(
"com.google.googlejavaformat.java.java21.Java21InputAstVisitor", builder, options);
} else if (Runtime.version().feature() >= 17) {
visitor =
createVisitor(
"com.google.googlejavaformat.java.java17.Java17InputAstVisitor", builder, options);
} else {
visitor = new JavaInputAstVisitor(builder, options.indentationMultiplier());
}
Expand All @@ -173,6 +171,18 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
javaOutput.flush();
}

private static JavaInputAstVisitor createVisitor(
final String className, final OpsBuilder builder, final JavaFormatterOptions options) {
try {
return Class.forName(className)
.asSubclass(JavaInputAstVisitor.class)
.getConstructor(OpsBuilder.class, int.class)
.newInstance(builder, options.indentationMultiplier());
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

static boolean errorDiagnostic(Diagnostic<?> input) {
if (input.getKind() != Diagnostic.Kind.ERROR) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.CompilationUnitTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.InstanceOfTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.ModuleTree;
Expand Down Expand Up @@ -238,6 +239,15 @@ public Void visitCase(CaseTree node, Void unused) {
}
builder.close();
}

final ExpressionTree guard = getGuard(node);
if (guard != null) {
builder.space();
token("when");
builder.space();
scan(guard, null);
}

switch (node.getCaseKind()) {
case STATEMENT:
token(":");
Expand Down Expand Up @@ -267,4 +277,8 @@ public Void visitCase(CaseTree node, Void unused) {
}
return null;
}

protected ExpressionTree getGuard(final CaseTree node) {
return null;
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class FormatterIntegrationTest {
.putAll(15, "I603")
.putAll(16, "I588")
.putAll(17, "I683", "I684", "I696")
.putAll(21, "SwitchGuardClause")
.build();

@Parameters(name = "{index}: {0}")
Expand Down
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;
};
}
}
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;
};
}
}

0 comments on commit b86c508

Please sign in to comment.