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=google/google-java-format#988 from TheCK:master 4771486db7d8aab84eb4ecf8e68af2612d0c2b5c
PiperOrigin-RevId: 588913297
  • Loading branch information
TheCK authored and eholmer1 committed Oct 19, 2024
1 parent e658a4f commit 065c379
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ jdks {
}

javaVersions {
libraryTarget = 17
libraryTarget = 21
runtime = 21
}
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 @@ -131,15 +131,10 @@ static JavaOutput format(
OpsBuilder opsBuilder = new OpsBuilder(javaInput);

JavaInputAstVisitor visitor;
if (getRuntimeVersion() >= 14) {
try {
visitor = Class.forName("com.palantir.javaformat.java.java14.Java14InputAstVisitor")
.asSubclass(JavaInputAstVisitor.class)
.getConstructor(OpsBuilder.class, int.class)
.newInstance(opsBuilder, options.indentationMultiplier());
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e.getMessage(), e);
}
if (getRuntimeVersion() >= 21) {
visitor = createVisitor("com.palantir.javaformat.java.java21.Java21InputAstVisitor", opsBuilder, options);
} else if (getRuntimeVersion() >= 14) {
visitor = createVisitor("com.palantir.javaformat.java.java14.Java14InputAstVisitor", opsBuilder, options);
} else {
visitor = new JavaInputAstVisitor(opsBuilder, options.indentationMultiplier());
}
Expand Down Expand Up @@ -206,6 +201,18 @@ static int getRuntimeVersion() {
return Runtime.version().feature();
}

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 @@ -31,6 +31,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.LambdaExpressionTree;
import com.sun.source.tree.ModifiersTree;
Expand Down Expand Up @@ -275,6 +276,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 @@ -341,4 +351,8 @@ private static Object invoke(Method m, Object target) {
throw new RuntimeException(e.getMessage(), e);
}
}

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

private final Class<?> testClass;
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 065c379

Please sign in to comment.