Skip to content

Commit

Permalink
Work around a change in EnhancedForLoopTree
Browse files Browse the repository at this point in the history
See https://bugs.openjdk.org/browse/JDK-8294943.

Refaster still doesn't support records in enhanced for, but this prevents
the visitor implementation from crashing on existing enhanced for loops.

PiperOrigin-RevId: 494254872
  • Loading branch information
cushon authored and Error Prone Team committed Dec 9, 2022
1 parent 5aa39dc commit df033f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public static boolean isAtLeast20() {
return FEATURE >= 20;
}

/** Returns true if the current runtime is JDK 21 or newer. */
public static boolean isAtLeast21() {
return FEATURE >= 21;
}

/**
* Returns the latest {@code --release} version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@
import static com.google.errorprone.refaster.Unifier.unifications;

import com.google.auto.value.AutoValue;
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;

/**
* A {@link UTree} representation of a {@link EnhancedForLoopTree}.
Expand All @@ -44,6 +51,11 @@ public static UEnhancedForLoop create(
@Override
public abstract USimpleStatement getStatement();

// TODO(cushon): support record patterns in enhanced for
public Tree getVariableOrRecordPattern() {
return getVariable();
}

@Override
public <R, D> R accept(TreeVisitor<R, D> visitor, D data) {
return visitor.visitEnhancedForLoop(this, data);
Expand All @@ -56,12 +68,31 @@ public Kind getKind() {

@Override
public JCEnhancedForLoop inline(Inliner inliner) throws CouldNotResolveImportException {
return inliner
.maker()
.ForeachLoop(
getVariable().inline(inliner),
getExpression().inline(inliner),
getStatement().inline(inliner));
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);
}
}

@Override
Expand Down

0 comments on commit df033f0

Please sign in to comment.