Add support for guard clauses in Java 21 switch expressions#988
Add support for guard clauses in Java 21 switch expressions#988TheCK wants to merge 3 commits intogoogle:masterfrom
Conversation
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
cushon
left a comment
There was a problem hiding this comment.
Thanks for the fix!
Could you add a test? There are some examples of tests that are only run on newer Java versions here:
| } | ||
| builder.close(); | ||
| } | ||
| if (node.getGuard() != null) { |
There was a problem hiding this comment.
Since this block is the only part that needs to be different between 17 and 21, I would like to avoid duplicating the rest of the method.
An alternative would be to do something like this:
There could be a getGuard method that returns null in the base class, and Java21InputAstVisitor could override it
| Class.forName("com.google.googlejavaformat.java.java21.Java21InputAstVisitor") | ||
| .asSubclass(JavaInputAstVisitor.class) | ||
| .getConstructor(OpsBuilder.class, int.class) | ||
| .newInstance(builder, options.indentationMultiplier()); |
There was a problem hiding this comment.
It might be worth creating a shared helper to reflectively instantiate the visitor at this point:
private static JavaInputAstVisitor createVisitor(String className) { .. }
|
@cushon I've implemented your requested changes. Hope this works for you. If you think it needs more attention still, let me know. |
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
This PR adds support for
switchstatements where acasehas a guard clause.See Issue #983