-
-
Notifications
You must be signed in to change notification settings - Fork 380
[Bug]: CtCase<?>::getIncludesDefault only works with multiple case expressions #6666
Copy link
Copy link
Open
Labels
Description
Describe the bug
This may be intented as the javadoc specifies: Returns whether this case includes a trailing default.
But I interpreted it as even if there is only a default statement that should still return true.
Here is a corresponding test case that passes (taken from test/pattern/SwitchPatternTest.java)
@Test
void testCaseNullDefault() {
// contract: "case null, default" is represented by a null literal and the includesDefault property set to true
CtSwitch<?> sw = createFromSwitchStatement("case null, default", false);
CtCase<?> ctCase = sw.getCases().get(0);
List<? extends CtExpression<?>> caseExpressions = ctCase.getCaseExpressions();
assertThat(caseExpressions).hasSize(1);
assertThat(ctCase.getIncludesDefault()).isTrue();
assertThat(ctCase.toString()).contains("case null, default ->");
}Source code you are trying to analyze/transform
Source code for your Spoon processing
@Test
void failingTestCase() {
CtSwitch<?> sw = createFromSwitchStatement("case default", false);
CtCase<?> ctCase = sw.getCases().get(0);
assertTrue(ctCase.getIncludesDefault());
}
private static CtSwitch<?> createFromSwitchStatement(String cases, boolean def) {
return createModelFromString(
"""
class Foo {
void foo(Object arg) {
switch (arg) {
%s -> {}
%s
}
}
""".formatted(cases, def ? "default -> {}" : ""))
.getElements(new TypeFilter<>(CtSwitch.class)).iterator().next();
}
private static CtModel createModelFromString(String code) {
Launcher launcher = new Launcher();
launcher.getEnvironment().setComplianceLevel(22);
launcher.addInputResource(new VirtualFile(code));
return launcher.buildModel();
}Actual output
false instead of trueExpected output
trueSpoon Version
11.3.1-beta-7
JVM Version
Java 21
What operating system are you using?
Linux x86-64
Reactions are currently unavailable