1 /* 2 * Copyright 2023 The google-java-format Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.googlejavaformat.java.java21; 16 17 import com.google.googlejavaformat.OpsBuilder; 18 import com.google.googlejavaformat.java.java17.Java17InputAstVisitor; 19 import com.sun.source.tree.CaseTree; 20 import com.sun.source.tree.ConstantCaseLabelTree; 21 import com.sun.source.tree.DeconstructionPatternTree; 22 import com.sun.source.tree.DefaultCaseLabelTree; 23 import com.sun.source.tree.ExpressionTree; 24 import com.sun.source.tree.PatternCaseLabelTree; 25 import com.sun.source.tree.PatternTree; 26 import com.sun.source.tree.StringTemplateTree; 27 import javax.lang.model.element.Name; 28 29 /** 30 * Extends {@link Java17InputAstVisitor} with support for AST nodes that were added or modified in 31 * Java 21. 32 */ 33 public class Java21InputAstVisitor extends Java17InputAstVisitor { 34 Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier)35 public Java21InputAstVisitor(OpsBuilder builder, int indentMultiplier) { 36 super(builder, indentMultiplier); 37 } 38 39 @Override getGuard(final CaseTree node)40 protected ExpressionTree getGuard(final CaseTree node) { 41 return node.getGuard(); 42 } 43 44 @Override visitDefaultCaseLabel(DefaultCaseLabelTree node, Void unused)45 public Void visitDefaultCaseLabel(DefaultCaseLabelTree node, Void unused) { 46 token("default"); 47 return null; 48 } 49 50 @Override visitPatternCaseLabel(PatternCaseLabelTree node, Void unused)51 public Void visitPatternCaseLabel(PatternCaseLabelTree node, Void unused) { 52 scan(node.getPattern(), null); 53 return null; 54 } 55 56 @Override visitConstantCaseLabel(ConstantCaseLabelTree node, Void aVoid)57 public Void visitConstantCaseLabel(ConstantCaseLabelTree node, Void aVoid) { 58 scan(node.getConstantExpression(), null); 59 return null; 60 } 61 62 @Override visitDeconstructionPattern(DeconstructionPatternTree node, Void unused)63 public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void unused) { 64 sync(node); 65 scan(node.getDeconstructor(), null); 66 builder.open(plusFour); 67 token("("); 68 builder.breakOp(); 69 boolean first = true; 70 for (PatternTree pattern : node.getNestedPatterns()) { 71 if (!first) { 72 token(","); 73 builder.breakOp(" "); 74 } 75 first = false; 76 scan(pattern, null); 77 } 78 builder.close(); 79 token(")"); 80 return null; 81 } 82 83 @SuppressWarnings("preview") 84 @Override visitStringTemplate(StringTemplateTree node, Void aVoid)85 public Void visitStringTemplate(StringTemplateTree node, Void aVoid) { 86 sync(node); 87 scan(node.getProcessor(), null); 88 token("."); 89 token(builder.peekToken().get()); 90 return null; 91 } 92 93 @Override variableName(Name name)94 protected void variableName(Name name) { 95 if (name.isEmpty()) { 96 token("_"); 97 } else { 98 visit(name); 99 } 100 } 101 } 102