12
12
* the License.
13
13
*/
14
14
15
- package com .google .googlejavaformat .java .java14 ;
15
+ package com .google .googlejavaformat .java .java17 ;
16
16
17
17
import static com .google .common .collect .ImmutableList .toImmutableList ;
18
18
import static com .google .common .collect .Iterables .getOnlyElement ;
25
25
import com .sun .source .tree .AnnotationTree ;
26
26
import com .sun .source .tree .BindingPatternTree ;
27
27
import com .sun .source .tree .BlockTree ;
28
+ import com .sun .source .tree .CaseLabelTree ;
28
29
import com .sun .source .tree .CaseTree ;
29
30
import com .sun .source .tree .ClassTree ;
30
31
import com .sun .source .tree .CompilationUnitTree ;
39
40
import com .sun .tools .javac .tree .JCTree ;
40
41
import com .sun .tools .javac .tree .JCTree .JCVariableDecl ;
41
42
import com .sun .tools .javac .tree .TreeInfo ;
42
- import java .lang .reflect .Method ;
43
43
import java .util .List ;
44
44
import java .util .Optional ;
45
45
import javax .lang .model .element .Name ;
46
46
47
47
/**
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 .
50
50
*/
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 {
63
52
64
- public Java14InputAstVisitor (OpsBuilder builder , int indentMultiplier ) {
53
+ public Java17InputAstVisitor (OpsBuilder builder , int indentMultiplier ) {
65
54
super (builder , indentMultiplier );
66
55
}
67
56
68
57
@ Override
69
58
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 ();
75
60
if (module != null ) {
76
61
if (!first ) {
77
62
builder .blankLineWanted (BlankLineWanted .YES );
@@ -84,30 +69,15 @@ protected void handleModule(boolean first, CompilationUnitTree node) {
84
69
85
70
@ Override
86
71
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 ();
93
73
}
94
74
95
75
@ Override
96
76
public Void visitBindingPattern (BindingPatternTree node , Void unused ) {
97
77
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 ());
111
81
return null ;
112
82
}
113
83
@@ -248,17 +218,9 @@ public Void visitCase(CaseTree node, Void unused) {
248
218
sync (node );
249
219
markForPartialFormat ();
250
220
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" );
262
224
if (isDefault ) {
263
225
token ("default" , plusTwo );
264
226
} else {
@@ -305,20 +267,4 @@ public Void visitCase(CaseTree node, Void unused) {
305
267
}
306
268
return null ;
307
269
}
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
- }
324
270
}
0 commit comments