1212 * the License.
1313 */
1414
15- package com .google .googlejavaformat .java .java14 ;
15+ package com .google .googlejavaformat .java .java17 ;
1616
1717import static com .google .common .collect .ImmutableList .toImmutableList ;
1818import static com .google .common .collect .Iterables .getOnlyElement ;
2525import com .sun .source .tree .AnnotationTree ;
2626import com .sun .source .tree .BindingPatternTree ;
2727import com .sun .source .tree .BlockTree ;
28+ import com .sun .source .tree .CaseLabelTree ;
2829import com .sun .source .tree .CaseTree ;
2930import com .sun .source .tree .ClassTree ;
3031import com .sun .source .tree .CompilationUnitTree ;
3940import com .sun .tools .javac .tree .JCTree ;
4041import com .sun .tools .javac .tree .JCTree .JCVariableDecl ;
4142import com .sun .tools .javac .tree .TreeInfo ;
42- import java .lang .reflect .Method ;
4343import java .util .List ;
4444import java .util .Optional ;
4545import javax .lang .model .element .Name ;
4646
4747/**
48- * Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified for
49- * Java 14 .
48+ * Extends {@link JavaInputAstVisitor} with support for AST nodes that were added or modified in
49+ * Java 17 .
5050 */
51- public class Java14InputAstVisitor extends JavaInputAstVisitor {
52- private static final Method COMPILATION_UNIT_TREE_GET_MODULE =
53- maybeGetMethod (CompilationUnitTree .class , "getModule" );
54- private static final Method CLASS_TREE_GET_PERMITS_CLAUSE =
55- maybeGetMethod (ClassTree .class , "getPermitsClause" );
56- private static final Method BINDING_PATTERN_TREE_GET_VARIABLE =
57- maybeGetMethod (BindingPatternTree .class , "getVariable" );
58- private static final Method BINDING_PATTERN_TREE_GET_TYPE =
59- maybeGetMethod (BindingPatternTree .class , "getType" );
60- private static final Method BINDING_PATTERN_TREE_GET_BINDING =
61- maybeGetMethod (BindingPatternTree .class , "getBinding" );
62- private static final Method CASE_TREE_GET_LABELS = maybeGetMethod (CaseTree .class , "getLabels" );
51+ public class Java17InputAstVisitor extends JavaInputAstVisitor {
6352
64- public Java14InputAstVisitor (OpsBuilder builder , int indentMultiplier ) {
53+ public Java17InputAstVisitor (OpsBuilder builder , int indentMultiplier ) {
6554 super (builder , indentMultiplier );
6655 }
6756
6857 @ Override
6958 protected void handleModule (boolean first , CompilationUnitTree node ) {
70- if (COMPILATION_UNIT_TREE_GET_MODULE == null ) {
71- // Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464
72- return ;
73- }
74- ModuleTree module = (ModuleTree ) invoke (COMPILATION_UNIT_TREE_GET_MODULE , node );
59+ ModuleTree module = node .getModule ();
7560 if (module != null ) {
7661 if (!first ) {
7762 builder .blankLineWanted (BlankLineWanted .YES );
@@ -84,30 +69,15 @@ protected void handleModule(boolean first, CompilationUnitTree node) {
8469
8570 @ Override
8671 protected List <? extends Tree > getPermitsClause (ClassTree node ) {
87- if (CLASS_TREE_GET_PERMITS_CLAUSE != null ) {
88- return (List <? extends Tree >) invoke (CLASS_TREE_GET_PERMITS_CLAUSE , node );
89- } else {
90- // Java < 15
91- return super .getPermitsClause (node );
92- }
72+ return node .getPermitsClause ();
9373 }
9474
9575 @ Override
9676 public Void visitBindingPattern (BindingPatternTree node , Void unused ) {
9777 sync (node );
98- if (BINDING_PATTERN_TREE_GET_VARIABLE != null ) {
99- VariableTree variableTree = (VariableTree ) invoke (BINDING_PATTERN_TREE_GET_VARIABLE , node );
100- visitBindingPattern (
101- variableTree .getModifiers (), variableTree .getType (), variableTree .getName ());
102- } else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null ) {
103- Tree type = (Tree ) invoke (BINDING_PATTERN_TREE_GET_TYPE , node );
104- Name name = (Name ) invoke (BINDING_PATTERN_TREE_GET_BINDING , node );
105- visitBindingPattern (/* modifiers= */ null , type , name );
106- } else {
107- throw new LinkageError (
108- "BindingPatternTree must have either getVariable() or both getType() and getBinding(),"
109- + " but does not" );
110- }
78+ VariableTree variableTree = node .getVariable ();
79+ visitBindingPattern (
80+ variableTree .getModifiers (), variableTree .getType (), variableTree .getName ());
11181 return null ;
11282 }
11383
@@ -248,17 +218,9 @@ public Void visitCase(CaseTree node, Void unused) {
248218 sync (node );
249219 markForPartialFormat ();
250220 builder .forcedBreak ();
251- List <? extends Tree > labels ;
252- boolean isDefault ;
253- if (CASE_TREE_GET_LABELS != null ) {
254- labels = (List <? extends Tree >) invoke (CASE_TREE_GET_LABELS , node );
255- isDefault =
256- labels .size () == 1
257- && getOnlyElement (labels ).getKind ().name ().equals ("DEFAULT_CASE_LABEL" );
258- } else {
259- labels = node .getExpressions ();
260- isDefault = labels .isEmpty ();
261- }
221+ List <? extends CaseLabelTree > labels = node .getLabels ();
222+ boolean isDefault =
223+ labels .size () == 1 && getOnlyElement (labels ).getKind ().name ().equals ("DEFAULT_CASE_LABEL" );
262224 if (isDefault ) {
263225 token ("default" , plusTwo );
264226 } else {
@@ -305,20 +267,4 @@ public Void visitCase(CaseTree node, Void unused) {
305267 }
306268 return null ;
307269 }
308-
309- private static Method maybeGetMethod (Class <?> c , String name ) {
310- try {
311- return c .getMethod (name );
312- } catch (ReflectiveOperationException e ) {
313- return null ;
314- }
315- }
316-
317- private static Object invoke (Method m , Object target ) {
318- try {
319- return m .invoke (target );
320- } catch (ReflectiveOperationException e ) {
321- throw new LinkageError (e .getMessage (), e );
322- }
323- }
324270}
0 commit comments