-
Notifications
You must be signed in to change notification settings - Fork 747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix JDK 20-ea build compatibility #3610
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,14 @@ | |
import static com.google.errorprone.refaster.Unifier.unifications; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import com.google.common.base.VerifyException; | ||
import com.google.errorprone.util.RuntimeVersion; | ||
import com.sun.source.tree.EnhancedForLoopTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.tree.TreeVisitor; | ||
import com.sun.tools.javac.tree.JCTree; | ||
import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop; | ||
import com.sun.tools.javac.tree.JCTree.JCExpression; | ||
import com.sun.tools.javac.tree.JCTree.JCStatement; | ||
import com.sun.tools.javac.tree.JCTree.JCVariableDecl; | ||
import com.sun.tools.javac.tree.TreeMaker; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* A {@link UTree} representation of a {@link EnhancedForLoopTree}. | ||
|
@@ -39,7 +37,69 @@ | |
abstract class UEnhancedForLoop extends USimpleStatement implements EnhancedForLoopTree { | ||
public static UEnhancedForLoop create( | ||
UVariableDecl variable, UExpression elements, UStatement statement) { | ||
return new AutoValue_UEnhancedForLoop(variable, elements, (USimpleStatement) statement); | ||
// On JDK 20 and above the `EnhancedForLoopTree` interface contains a additional method | ||
// `getDeclarationKind()`, referencing a type not available prior to JDK 20. AutoValue | ||
// generates a corresponding field and accessor for this property. Here we find and invoke the | ||
// generated constructor with the appropriate arguments, depending on context. | ||
// See https://github.com/openjdk/jdk20/commit/2cb64a75578ccc15a1dfc8c2843aa11d05ca8aa7. | ||
// TODO: Simplify this logic once JDK 19 and older are no longer supported. | ||
return isCompiledWithJdk20Plus() | ||
? createJdk20PlusEnhancedForLoop(variable, elements, statement) | ||
: createPreJdk20EnhancedForLoop(variable, elements, statement); | ||
} | ||
|
||
private static boolean isCompiledWithJdk20Plus() { | ||
return Arrays.stream(AutoValue_UEnhancedForLoop.class.getDeclaredMethods()) | ||
.anyMatch(m -> "getDeclarationKind".equals(m.getName())); | ||
} | ||
|
||
private static UEnhancedForLoop createPreJdk20EnhancedForLoop( | ||
UVariableDecl variable, UExpression elements, UStatement statement) { | ||
try { | ||
return AutoValue_UEnhancedForLoop.class | ||
.getDeclaredConstructor(UVariableDecl.class, UExpression.class, USimpleStatement.class) | ||
.newInstance(variable, elements, statement); | ||
} catch (IllegalAccessException | ||
| InstantiationException | ||
| InvocationTargetException | ||
| NoSuchMethodException e) { | ||
throw new LinkageError(e.getMessage(), e); | ||
} | ||
} | ||
|
||
private static UEnhancedForLoop createJdk20PlusEnhancedForLoop( | ||
UVariableDecl variable, UExpression elements, UStatement statement) { | ||
Object declarationKind = getVariableDeclarationKind(); | ||
try { | ||
return AutoValue_UEnhancedForLoop.class | ||
.getDeclaredConstructor( | ||
declarationKind.getClass(), | ||
UVariableDecl.class, | ||
UExpression.class, | ||
USimpleStatement.class) | ||
.newInstance(declarationKind, variable, elements, statement); | ||
} catch (IllegalAccessException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: |
||
| InstantiationException | ||
| InvocationTargetException | ||
| NoSuchMethodException e) { | ||
throw new LinkageError(e.getMessage(), e); | ||
} | ||
} | ||
|
||
private static Object getVariableDeclarationKind() { | ||
Class<?> declarationKind; | ||
try { | ||
declarationKind = Class.forName("com.sun.source.tree.EnhancedForLoopTree$DeclarationKind"); | ||
} catch (ClassNotFoundException e) { | ||
throw new VerifyException("Cannot load `EnhancedForLoopTree.DeclarationKind` enum", e); | ||
} | ||
return Arrays.stream(declarationKind.getEnumConstants()) | ||
.filter(v -> "VARIABLE".equals(v.toString())) | ||
.findFirst() | ||
.orElseThrow( | ||
() -> | ||
new VerifyException( | ||
"Enum value `EnhancedForLoopTree.DeclarationKind.VARIABLE` not found")); | ||
} | ||
|
||
@Override | ||
|
@@ -68,31 +128,12 @@ public Kind getKind() { | |
|
||
@Override | ||
public JCEnhancedForLoop inline(Inliner inliner) throws CouldNotResolveImportException { | ||
return makeForeachLoop( | ||
inliner.maker(), | ||
getVariable().inline(inliner), | ||
getExpression().inline(inliner), | ||
getStatement().inline(inliner)); | ||
} | ||
|
||
private static JCEnhancedForLoop makeForeachLoop( | ||
TreeMaker maker, JCVariableDecl variable, JCExpression expression, JCStatement statement) { | ||
try { | ||
if (RuntimeVersion.isAtLeast20()) { | ||
return (JCEnhancedForLoop) | ||
TreeMaker.class | ||
.getMethod("ForeachLoop", JCTree.class, JCExpression.class, JCStatement.class) | ||
.invoke(maker, variable, expression, statement); | ||
} else { | ||
return (JCEnhancedForLoop) | ||
TreeMaker.class | ||
.getMethod( | ||
"ForeachLoop", JCVariableDecl.class, JCExpression.class, JCStatement.class) | ||
.invoke(maker, variable, expression, statement); | ||
} | ||
} catch (ReflectiveOperationException e) { | ||
throw new LinkageError(e.getMessage(), e); | ||
} | ||
return inliner | ||
.maker() | ||
.ForeachLoop( | ||
getVariable().inline(inliner), | ||
getExpression().inline(inliner), | ||
getStatement().inline(inliner)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cushon based on my tests (with JDK 20-ea build 28) the build also passes without these changes. Let me know if I should revert this part of the PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I prepared df033f0 to fix an error in the internal CI, which was building at the Java 11 language level and APIs and then running on a newer JDK. We might not need this with your patch, though. Let me think about this a little harder. (I think there was some related discussion in an external bug about how our CI is compiling and testing at each JDK version, but we're only releasing one set of artifacts built at a fixed version, and maybe we should have the CI do one build that matches the releases and then run the tests at different versions.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a regression when testing this compiled against the Java 11 APIs, and running on the latest version. I think that makes sense: the change to I think the reflection is still necessary, and I should also try to add that configuration to the external CI.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could not reproduce this locally, but the stack trace speaks for itself. I added a commit to revert this section 👍 |
||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not happy with all the reflection going on here, but found no better way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've mostly been using reflection for similar workarounds recently.
I've been thinking about whether could use Multi-Release jars. At some point it may be worth branches source files like this when there are significant API changes, to avoid the reflection. But at least the last time I looked into it, setting maven up to build mr-jars didn't seem trivial.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using Multi-Release JARs might be a nice idea indeed! I currently don't have any experience with those, so can't comment on the effort required for Maven. (But this does sound like a nice topic to dive into; might also be nice for Error Prone Support, where we're also discussing how to depend on/use newer JDKs while still supporting JDK 11.)