Skip to content

Commit 4771486

Browse files
committed
refactor guard clause PR and add a test
1 parent efcc978 commit 4771486

File tree

6 files changed

+54
-80
lines changed

6 files changed

+54
-80
lines changed

core/src/main/java/com/google/googlejavaformat/java/Formatter.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,13 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
152152
// Output the compilation unit.
153153
JavaInputAstVisitor visitor;
154154
if (Runtime.version().feature() >= 21) {
155-
try {
156-
visitor =
157-
Class.forName("com.google.googlejavaformat.java.java21.Java21InputAstVisitor")
158-
.asSubclass(JavaInputAstVisitor.class)
159-
.getConstructor(OpsBuilder.class, int.class)
160-
.newInstance(builder, options.indentationMultiplier());
161-
} catch (ReflectiveOperationException e) {
162-
throw new LinkageError(e.getMessage(), e);
163-
}
155+
visitor =
156+
createVisitor(
157+
"com.google.googlejavaformat.java.java21.Java21InputAstVisitor", builder, options);
164158
} else if (Runtime.version().feature() >= 17) {
165-
try {
166-
visitor =
167-
Class.forName("com.google.googlejavaformat.java.java17.Java17InputAstVisitor")
168-
.asSubclass(JavaInputAstVisitor.class)
169-
.getConstructor(OpsBuilder.class, int.class)
170-
.newInstance(builder, options.indentationMultiplier());
171-
} catch (ReflectiveOperationException e) {
172-
throw new LinkageError(e.getMessage(), e);
173-
}
159+
visitor =
160+
createVisitor(
161+
"com.google.googlejavaformat.java.java17.Java17InputAstVisitor", builder, options);
174162
} else {
175163
visitor = new JavaInputAstVisitor(builder, options.indentationMultiplier());
176164
}
@@ -183,6 +171,18 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
183171
javaOutput.flush();
184172
}
185173

174+
private static JavaInputAstVisitor createVisitor(
175+
final String className, final OpsBuilder builder, final JavaFormatterOptions options) {
176+
try {
177+
return Class.forName(className)
178+
.asSubclass(JavaInputAstVisitor.class)
179+
.getConstructor(OpsBuilder.class, int.class)
180+
.newInstance(builder, options.indentationMultiplier());
181+
} catch (ReflectiveOperationException e) {
182+
throw new LinkageError(e.getMessage(), e);
183+
}
184+
}
185+
186186
static boolean errorDiagnostic(Diagnostic<?> input) {
187187
if (input.getKind() != Diagnostic.Kind.ERROR) {
188188
return false;

core/src/main/java/com/google/googlejavaformat/java/java17/Java17InputAstVisitor.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ public Void visitCase(CaseTree node, Void unused) {
238238
}
239239
builder.close();
240240
}
241+
242+
final ExpressionTree guard = getGuard(node);
243+
if (guard != null) {
244+
builder.space();
245+
token("when");
246+
builder.space();
247+
scan(guard, null);
248+
}
249+
241250
switch (node.getCaseKind()) {
242251
case STATEMENT:
243252
token(":");
@@ -267,4 +276,8 @@ public Void visitCase(CaseTree node, Void unused) {
267276
}
268277
return null;
269278
}
279+
280+
protected ExpressionTree getGuard(final CaseTree node) {
281+
return null;
282+
}
270283
}
Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package com.google.googlejavaformat.java.java21;
22

3-
import static com.google.common.collect.Iterables.getOnlyElement;
4-
53
import com.google.googlejavaformat.OpsBuilder;
64
import com.google.googlejavaformat.java.java17.Java17InputAstVisitor;
7-
import com.sun.source.tree.*;
8-
import java.util.List;
5+
import com.sun.source.tree.CaseTree;
6+
import com.sun.source.tree.ExpressionTree;
97

108
public class Java21InputAstVisitor extends Java17InputAstVisitor {
119

@@ -14,63 +12,7 @@ public Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier) {
1412
}
1513

1614
@Override
17-
public Void visitCase(CaseTree node, Void unused) {
18-
sync(node);
19-
markForPartialFormat();
20-
builder.forcedBreak();
21-
List<? extends CaseLabelTree> labels = node.getLabels();
22-
boolean isDefault =
23-
labels.size() == 1 && getOnlyElement(labels).getKind().name().equals("DEFAULT_CASE_LABEL");
24-
if (isDefault) {
25-
token("default", plusTwo);
26-
} else {
27-
token("case", plusTwo);
28-
builder.open(labels.size() > 1 ? plusFour : ZERO);
29-
builder.space();
30-
boolean first = true;
31-
for (Tree expression : labels) {
32-
if (!first) {
33-
token(",");
34-
builder.breakOp(" ");
35-
}
36-
scan(expression, null);
37-
first = false;
38-
}
39-
builder.close();
40-
}
41-
if (node.getGuard() != null) {
42-
builder.space();
43-
token("when");
44-
builder.space();
45-
scan(node.getGuard(), null);
46-
}
47-
switch (node.getCaseKind()) {
48-
case STATEMENT:
49-
token(":");
50-
builder.open(plusTwo);
51-
visitStatements(node.getStatements());
52-
builder.close();
53-
break;
54-
case RULE:
55-
builder.space();
56-
token("-");
57-
token(">");
58-
builder.space();
59-
if (node.getBody().getKind() == Tree.Kind.BLOCK) {
60-
// Explicit call with {@link CollapseEmptyOrNot.YES} to handle empty case blocks.
61-
visitBlock(
62-
(BlockTree) node.getBody(),
63-
CollapseEmptyOrNot.YES,
64-
AllowLeadingBlankLine.NO,
65-
AllowTrailingBlankLine.NO);
66-
} else {
67-
scan(node.getBody(), null);
68-
}
69-
builder.guessToken(";");
70-
break;
71-
default:
72-
throw new AssertionError(node.getCaseKind());
73-
}
74-
return null;
15+
protected ExpressionTree getGuard(final CaseTree node) {
16+
return node.getGuard();
7517
}
7618
}

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class FormatterIntegrationTest {
5252
.putAll(15, "I603")
5353
.putAll(16, "I588")
5454
.putAll(17, "I683", "I684", "I696")
55+
.putAll(21, "SwitchGuardClause")
5556
.build();
5657

5758
@Parameters(name = "{index}: {0}")
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class SwitchGuardClause {
2+
boolean test(Object x) {
3+
return switch (x) {
4+
case String s when s.length() < 5 -> true;
5+
case Integer i -> false;
6+
default -> true;
7+
};
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class SwitchGuardClause {
2+
boolean test(Object x) {
3+
return switch (x) {
4+
case String s when s.length() < 5 -> true;
5+
case Integer i -> false;
6+
default -> true;
7+
};
8+
}
9+
}

0 commit comments

Comments
 (0)