3939import com .sun .tools .javac .tree .JCTree ;
4040import com .sun .tools .javac .tree .JCTree .JCVariableDecl ;
4141import com .sun .tools .javac .tree .TreeInfo ;
42+ import java .lang .reflect .Method ;
4243import java .util .List ;
4344import java .util .Optional ;
4445import javax .lang .model .element .Name ;
4849 * Java 14.
4950 */
5051public 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" );
5162
5263 public Java14InputAstVisitor (OpsBuilder builder , int indentMultiplier ) {
5364 super (builder , indentMultiplier );
5465 }
5566
5667 @ Override
5768 protected void handleModule (boolean first , CompilationUnitTree node ) {
58- try {
59- ModuleTree module =
60- (ModuleTree ) CompilationUnitTree .class .getMethod ("getModule" ).invoke (node );
61- if (module != null ) {
62- if (!first ) {
63- builder .blankLineWanted (BlankLineWanted .YES );
64- }
65- markForPartialFormat ();
66- visitModule (module , null );
67- builder .forcedBreak ();
68- }
69- } catch (ReflectiveOperationException e ) {
69+ if (COMPILATION_UNIT_TREE_GET_MODULE == null ) {
7070 // Java < 17, see https://bugs.openjdk.java.net/browse/JDK-8255464
71+ return ;
72+ }
73+ ModuleTree module = (ModuleTree ) invoke (COMPILATION_UNIT_TREE_GET_MODULE , node );
74+ if (module != null ) {
75+ if (!first ) {
76+ builder .blankLineWanted (BlankLineWanted .YES );
77+ }
78+ markForPartialFormat ();
79+ visitModule (module , null );
80+ builder .forcedBreak ();
7181 }
7282 }
7383
7484 @ Override
7585 protected List <? extends Tree > getPermitsClause (ClassTree node ) {
76- try {
77- return (List <? extends Tree >) ClassTree . class . getMethod ( "getPermitsClause" ). invoke (node );
78- } catch ( ReflectiveOperationException e ) {
86+ if ( CLASS_TREE_GET_PERMITS_CLAUSE != null ) {
87+ return (List <? extends Tree >) invoke (CLASS_TREE_GET_PERMITS_CLAUSE , node );
88+ } else {
7989 // Java < 15
8090 return super .getPermitsClause (node );
8191 }
@@ -84,20 +94,18 @@ protected List<? extends Tree> getPermitsClause(ClassTree node) {
8494 @ Override
8595 public Void visitBindingPattern (BindingPatternTree node , Void unused ) {
8696 sync (node );
87- try {
88- VariableTree variableTree =
89- (VariableTree ) BindingPatternTree .class .getMethod ("getVariable" ).invoke (node );
97+ if (BINDING_PATTERN_TREE_GET_VARIABLE != null ) {
98+ VariableTree variableTree = (VariableTree ) invoke (BINDING_PATTERN_TREE_GET_VARIABLE , node );
9099 visitBindingPattern (
91100 variableTree .getModifiers (), variableTree .getType (), variableTree .getName ());
92- } catch (ReflectiveOperationException e1 ) {
93- try {
94- Tree type = (Tree ) BindingPatternTree .class .getMethod ("getType" ).invoke (node );
95- Name name = (Name ) BindingPatternTree .class .getMethod ("getBinding" ).invoke (node );
96- visitBindingPattern (/* modifiers= */ null , type , name );
97- } catch (ReflectiveOperationException e2 ) {
98- e2 .addSuppressed (e1 );
99- throw new LinkageError (e2 .getMessage (), e2 );
100- }
101+ } else if (BINDING_PATTERN_TREE_GET_TYPE != null && BINDING_PATTERN_TREE_GET_BINDING != null ) {
102+ Tree type = (Tree ) invoke (BINDING_PATTERN_TREE_GET_TYPE , node );
103+ Name name = (Name ) invoke (BINDING_PATTERN_TREE_GET_BINDING , node );
104+ visitBindingPattern (/* modifiers= */ null , type , name );
105+ } else {
106+ throw new LinkageError (
107+ "BindingPatternTree must have either getVariable() or both getType() and getBinding(),"
108+ + " but does not" );
101109 }
102110 return null ;
103111 }
@@ -288,4 +296,20 @@ public Void visitCase(CaseTree node, Void unused) {
288296 }
289297 return null ;
290298 }
299+
300+ private static Method maybeGetMethod (Class <?> c , String name ) {
301+ try {
302+ return c .getMethod (name );
303+ } catch (ReflectiveOperationException e ) {
304+ return null ;
305+ }
306+ }
307+
308+ private static Object invoke (Method m , Object target ) {
309+ try {
310+ return m .invoke (target );
311+ } catch (ReflectiveOperationException e ) {
312+ throw new LinkageError (e .getMessage (), e );
313+ }
314+ }
291315}
0 commit comments