3333import static com .sun .source .tree .Tree .Kind .ANNOTATION ;
3434import static com .sun .source .tree .Tree .Kind .EXTENDS_WILDCARD ;
3535import static com .sun .source .tree .Tree .Kind .STRING_LITERAL ;
36+ import static com .sun .tools .javac .code .Flags .COMPACT_RECORD_CONSTRUCTOR ;
37+ import static com .sun .tools .javac .code .Flags .RECORD ;
3638
3739import com .google .common .base .Enums ;
3840import com .google .common .base .MoreObjects ;
7577import com .sun .source .tree .BindingPatternTree ;
7678import com .sun .source .tree .BlockTree ;
7779import com .sun .source .tree .BreakTree ;
80+ import com .sun .source .tree .CaseLabelTree ;
7881import com .sun .source .tree .CaseTree ;
7982import com .sun .source .tree .CatchTree ;
8083import com .sun .source .tree .ClassTree ;
@@ -400,16 +403,8 @@ public Void visitCompilationUnit(CompilationUnitTree node, Void unused) {
400403 return null ;
401404 }
402405
403- @ Nullable
404- private static final Method COMPILATION_UNIT_TREE_GET_MODULE =
405- maybeGetMethod (CompilationUnitTree .class , "getModule" );
406-
407406 protected void handleModule (boolean first , CompilationUnitTree node ) {
408- if (COMPILATION_UNIT_TREE_GET_MODULE == null ) {
409- // Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464
410- return ;
411- }
412- ModuleTree module = (ModuleTree ) invoke (COMPILATION_UNIT_TREE_GET_MODULE , node );
407+ ModuleTree module = node .getModule ();
413408 if (module != null ) {
414409 if (!first ) {
415410 builder .blankLineWanted (BlankLineWanted .YES );
@@ -1287,32 +1282,11 @@ public Void visitInstanceOf(InstanceOfTree node, Void unused) {
12871282 return null ;
12881283 }
12891284
1290- @ Nullable
1291- private static final Method BINDING_PATTERN_TREE_GET_VARIABLE =
1292- maybeGetMethod (BindingPatternTree .class , "getVariable" );
1293-
1294- @ Nullable
1295- private static final Method BINDING_PATTERN_TREE_GET_TYPE = maybeGetMethod (BindingPatternTree .class , "getType" );
1296-
1297- @ Nullable
1298- private static final Method BINDING_PATTERN_TREE_GET_BINDING =
1299- maybeGetMethod (BindingPatternTree .class , "getBinding" );
1300-
13011285 @ Override
13021286 public Void visitBindingPattern (BindingPatternTree node , Void unused ) {
13031287 sync (node );
1304- if (BINDING_PATTERN_TREE_GET_VARIABLE != null ) {
1305- VariableTree variableTree = (VariableTree ) invoke (BINDING_PATTERN_TREE_GET_VARIABLE , node );
1306- visitBindingPattern (variableTree .getModifiers (), variableTree .getType (), variableTree .getName ());
1307- } else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null ) {
1308- Tree type = (Tree ) invoke (BINDING_PATTERN_TREE_GET_TYPE , node );
1309- Name name = (Name ) invoke (BINDING_PATTERN_TREE_GET_BINDING , node );
1310- visitBindingPattern (/* modifiers= */ null , type , name );
1311- } else {
1312- throw new RuntimeException (
1313- "BindingPatternTree must have either getVariable() or both getType() and getBinding(),"
1314- + " but does not" );
1315- }
1288+ VariableTree variableTree = node .getVariable ();
1289+ visitBindingPattern (variableTree .getModifiers (), variableTree .getType (), variableTree .getName ());
13161290 return null ;
13171291 }
13181292
@@ -1324,11 +1298,9 @@ private static boolean isVarType(Tree type) {
13241298 return type .getKind ().name ().equals ("VAR_TYPE" );
13251299 }
13261300
1327- private void visitBindingPattern (@ Nullable ModifiersTree modifiers , @ Nullable Tree type , Name name ) {
1301+ private void visitBindingPattern (ModifiersTree modifiers , @ Nullable Tree type , Name name ) {
13281302 builder .open (plusFour );
1329- if (modifiers != null ) {
1330- builder .addAll (visitModifiers (modifiers , Direction .HORIZONTAL , Optional .empty ()));
1331- }
1303+ builder .addAll (visitModifiers (modifiers , Direction .HORIZONTAL , Optional .empty ()));
13321304 if (type == null || isVarType (type )) {
13331305 token ("var" );
13341306 } else {
@@ -1533,11 +1505,6 @@ public Void visitAnnotatedType(AnnotatedTypeTree node, Void unused) {
15331505 return null ;
15341506 }
15351507
1536- // TODO(fawind): Use Flags.COMPACT_RECORD_CONSTRUCTOR once if/when we drop support for Java 11
1537- protected static final long COMPACT_RECORD_CONSTRUCTOR = 1L << 51 ;
1538-
1539- protected static final long RECORD = 1L << 61 ;
1540-
15411508 // TODO: use Flags.IMPLICIT_CLASS once this module compiles with JDK 22 or later (JDK 21 calls
15421509 // bit 19 UNNAMED_CLASS). javac sets it on the class it synthesizes for a compact source file.
15431510 private static final long IMPLICIT_CLASS = 1L << 19 ;
@@ -1990,24 +1957,14 @@ boolean visitSingleMemberAnnotation(AnnotationTree node) {
19901957 return true ;
19911958 }
19921959
1993- @ Nullable
1994- private static final Method CASE_TREE_GET_LABELS = maybeGetMethod (CaseTree .class , "getLabels" );
1995-
19961960 @ Override
19971961 public Void visitCase (CaseTree node , Void unused ) {
19981962 sync (node );
19991963 markForPartialFormat ();
20001964 builder .forcedBreak ();
2001- List <? extends Tree > labels ;
2002- boolean isDefault ;
2003- if (CASE_TREE_GET_LABELS != null ) {
2004- labels = (List <? extends Tree >) invoke (CASE_TREE_GET_LABELS , node );
2005- isDefault = labels .size () == 1
2006- && Iterables .getOnlyElement (labels ).getKind ().name ().equals ("DEFAULT_CASE_LABEL" );
2007- } else {
2008- labels = node .getExpressions ();
2009- isDefault = labels .isEmpty ();
2010- }
1965+ List <? extends CaseLabelTree > labels = node .getLabels ();
1966+ boolean isDefault =
1967+ labels .size () == 1 && getOnlyElement (labels ).getKind ().name ().equals ("DEFAULT_CASE_LABEL" );
20111968 if (isDefault ) {
20121969 token ("default" , ZERO );
20131970 } else {
@@ -4069,17 +4026,9 @@ private void addBodyDeclarations(
40694026 }
40704027 }
40714028
4072- @ Nullable
4073- private static final Method CLASS_TREE_GET_PERMITS_CLAUSE = maybeGetMethod (ClassTree .class , "getPermitsClause" );
4074-
4075- /** Gets the permits clause for the given node. This is only available in Java 15 and later. */
4029+ /** Gets the permits clause for the given node, empty unless the class or interface is sealed. */
40764030 protected List <? extends Tree > getPermitsClause (ClassTree node ) {
4077- if (CLASS_TREE_GET_PERMITS_CLAUSE != null ) {
4078- return (List <? extends Tree >) invoke (CLASS_TREE_GET_PERMITS_CLAUSE , node );
4079- } else {
4080- // Java < 15
4081- return List .of ();
4082- }
4031+ return node .getPermitsClause ();
40834032 }
40844033
40854034 private void classDeclarationTypeList (String token , List <? extends Tree > types ) {
0 commit comments