1919import static com .google .errorprone .refaster .Unifier .unifications ;
2020
2121import com .google .auto .value .AutoValue ;
22- import com .google .common .base .VerifyException ;
2322import com .sun .source .tree .EnhancedForLoopTree ;
2423import com .sun .source .tree .Tree ;
2524import com .sun .source .tree .TreeVisitor ;
26- import com .sun .tools .javac .tree .JCTree ;
2725import com .sun .tools .javac .tree .JCTree .JCEnhancedForLoop ;
28- import com .sun .tools .javac .tree .JCTree .JCExpression ;
29- import com .sun .tools .javac .tree .JCTree .JCStatement ;
30- import com .sun .tools .javac .tree .JCTree .JCVariableDecl ;
31- import com .sun .tools .javac .tree .TreeMaker ;
32- import java .lang .reflect .Method ;
33- import java .util .Arrays ;
3426
3527/**
3628 * A {@link UTree} representation of a {@link EnhancedForLoopTree}.
@@ -44,63 +36,7 @@ abstract class UEnhancedForLoop extends USimpleStatement implements EnhancedForL
4436
4537 public static UEnhancedForLoop create (
4638 UVariableDecl variable , UExpression elements , UStatement statement ) {
47- // On JDK 20 and above the `EnhancedForLoopTree` interface contains a additional method
48- // `getDeclarationKind()`, referencing a type not available prior to JDK 20. AutoValue
49- // generates a corresponding field and accessor for this property. Here we find and invoke the
50- // generated constructor with the appropriate arguments, depending on context.
51- // See https://github.com/openjdk/jdk20/commit/2cb64a75578ccc15a1dfc8c2843aa11d05ca8aa7.
52- // TODO: Simplify this logic once JDK 19 and older are no longer supported.
53- return isCompiledWithJdk20Plus ()
54- ? createJdk20PlusEnhancedForLoop (variable , elements , statement )
55- : createPreJdk20EnhancedForLoop (variable , elements , statement );
56- }
57-
58- private static boolean isCompiledWithJdk20Plus () {
59- return Arrays .stream (AutoValue_UEnhancedForLoop .class .getDeclaredMethods ())
60- .anyMatch (m -> "getDeclarationKind" .equals (m .getName ()));
61- }
62-
63- private static UEnhancedForLoop createPreJdk20EnhancedForLoop (
64- UVariableDecl variable , UExpression elements , UStatement statement ) {
65- try {
66- return AutoValue_UEnhancedForLoop .class
67- .getDeclaredConstructor (UVariableDecl .class , UExpression .class , USimpleStatement .class )
68- .newInstance (variable , elements , statement );
69- } catch (ReflectiveOperationException e ) {
70- throw new LinkageError (e .getMessage (), e );
71- }
72- }
73-
74- private static UEnhancedForLoop createJdk20PlusEnhancedForLoop (
75- UVariableDecl variable , UExpression elements , UStatement statement ) {
76- Object declarationKind = getVariableDeclarationKind ();
77- try {
78- return AutoValue_UEnhancedForLoop .class
79- .getDeclaredConstructor (
80- declarationKind .getClass (),
81- UVariableDecl .class ,
82- UExpression .class ,
83- USimpleStatement .class )
84- .newInstance (declarationKind , variable , elements , statement );
85- } catch (ReflectiveOperationException e ) {
86- throw new LinkageError (e .getMessage (), e );
87- }
88- }
89-
90- private static Object getVariableDeclarationKind () {
91- Class <?> declarationKind ;
92- try {
93- declarationKind = Class .forName ("com.sun.source.tree.EnhancedForLoopTree$DeclarationKind" );
94- } catch (ClassNotFoundException e ) {
95- throw new VerifyException ("Cannot load `EnhancedForLoopTree.DeclarationKind` enum" , e );
96- }
97- return Arrays .stream (declarationKind .getEnumConstants ())
98- .filter (v -> "VARIABLE" .equals (v .toString ()))
99- .findFirst ()
100- .orElseThrow (
101- () ->
102- new VerifyException (
103- "Enum value `EnhancedForLoopTree.DeclarationKind.VARIABLE` not found" ));
39+ return new AutoValue_UEnhancedForLoop (variable , elements , (USimpleStatement ) statement );
10440 }
10541
10642 @ Override
@@ -129,38 +65,12 @@ public Kind getKind() {
12965
13066 @ Override
13167 public JCEnhancedForLoop inline (Inliner inliner ) throws CouldNotResolveImportException {
132- return makeForeachLoop (
133- inliner .maker (),
134- getVariable ().inline (inliner ),
135- getExpression ().inline (inliner ),
136- getStatement ().inline (inliner ));
137- }
138-
139- private static final Method treeMakerForeachLoopMethod = treeMakerForeachLoopMethod ();
140-
141- private static Method treeMakerForeachLoopMethod () {
142- try {
143- return TreeMaker .class .getMethod (
144- "ForeachLoop" , JCTree .class , JCExpression .class , JCStatement .class );
145- } catch (ReflectiveOperationException e1 ) {
146- try {
147- return TreeMaker .class .getMethod (
148- "ForeachLoop" , JCVariableDecl .class , JCExpression .class , JCStatement .class );
149- } catch (ReflectiveOperationException e2 ) {
150- e2 .addSuppressed (e1 );
151- throw new LinkageError (e2 .getMessage (), e2 );
152- }
153- }
154- }
155-
156- static JCEnhancedForLoop makeForeachLoop (
157- TreeMaker maker , JCVariableDecl variable , JCExpression expression , JCStatement statement ) {
158- try {
159- return (JCEnhancedForLoop )
160- treeMakerForeachLoopMethod .invoke (maker , variable , expression , statement );
161- } catch (ReflectiveOperationException e ) {
162- throw new LinkageError (e .getMessage (), e );
163- }
68+ return inliner
69+ .maker ()
70+ .ForeachLoop (
71+ getVariable ().inline (inliner ),
72+ getExpression ().inline (inliner ),
73+ getStatement ().inline (inliner ));
16474 }
16575
16676 @ Override
0 commit comments