I don't think we make much use of switch statements, but we should see if there are places where using switch expressions will make the code more concise.
Comment From: philwebb
I tried to use one today and got:
> Task :spring-boot-project:spring-boot-docs:aggregatedJavadoc FAILED
/Volumes/Data/projects/spring-boot/code30x/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java:137: error: switch expressions are not supported in -source 8
return switch (kind) {
^
(use -source 14 or higher to enable switch expressions)
/Volumes/Data/projects/spring-boot/code30x/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/AotProcessor.java:138: error: switch rules are not supported in -source 8
case SOURCE -> this.sourceOutput;
^
(use -source 14 or higher to enable switch rules)
2 errors
Comment From: wilkinsona
@philwebb I think I've fixed that in https://github.com/spring-projects/spring-boot/issues/31210. You should be able to use switch expressions on main now.
Comment From: dreis2211
I think there needs some work to be done on the formatting end:
Running :format turns this:
return switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS -> true;
case ON_PARAM -> getErrorsParameter(request);
default -> false;
};
into this:
return switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS -> true;
case ON_PARAM -> getErrorsParameter(request);
default -> false;
};
Arguably, the missing indentation makes it harder to read.
Otherwise this is ready on my local branch.
Comment From: wilkinsona
Thanks, @dreis2211. I prefer the pre-formatted version too. We can change this in Spring Java Format with this setting:
org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=true
It also affects a "traditional" switch. This:
protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS:
return true;
case ON_PARAM:
return getErrorsParameter(request);
default:
return false;
}
}
Will become this:
protected boolean isIncludeBindingErrors(HttpServletRequest request, MediaType produces) {
switch (getErrorProperties().getIncludeBindingErrors()) {
case ALWAYS:
return true;
case ON_PARAM:
return getErrorsParameter(request);
default:
return false;
}
}
Comment From: dreis2211
@wilkinsona thanks.
I will hold my changes back until this is done. Could we maybe assign this issue to me, so nobody starts to work on it in parallel?
Comment From: philwebb
Assigned
Comment From: wilkinsona
@dreis2211 No rush whatsoever, but we've improved the formatting of switches so I think we're now ready when you are.
Comment From: dreis2211
There you go #31527 @wilkinsona
Comment From: wilkinsona
Thanks, Christoph. Closing in favor of #31527.