Skip to content

Commit

Permalink
Initial support for JDK versions through 13
Browse files Browse the repository at this point in the history
see #1106

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=311771528
  • Loading branch information
cushon authored and kluever committed May 16, 2020
1 parent 7be2c43 commit 8de8a63
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
// recompiles to avoid infinite recursion.
continue;
}
if ((key.equals("-source") || key.equals("-target")) && originalOptions.isSet("--release")) {
if (SOURCE_TARGET_OPTIONS.contains(key) && originalOptions.isSet("--release")) {
// javac does not allow -source and -target to be specified explicitly when --release is,
// but does add them in response to passing --release. Here we invert that operation.
continue;
Expand Down Expand Up @@ -1234,6 +1234,9 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
return true;
}

private static final ImmutableSet<String> SOURCE_TARGET_OPTIONS =
ImmutableSet.of("-source", "--source", "-target", "--target");

/** Create a plausible URI to use in {@link #compilesWithFix}. */
@VisibleForTesting
static URI sourceURI(URI uri) {
Expand Down
16 changes: 15 additions & 1 deletion core/src/main/java/com/google/errorprone/refaster/Inliner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.errorprone.refaster.Bindings.Key;
import com.google.errorprone.refaster.UTypeVar.TypeWithExpression;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.RuntimeVersion;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
Expand Down Expand Up @@ -228,11 +229,24 @@ public TypeVar inlineAsVar(UTypeVar var) throws CouldNotResolveImportException {
sym.type = typeVar;
typeVarCache.put(var.getName(), typeVar);
// Any recursive uses of var will point to the same TypeVar object generated above.
typeVar.bound = var.getUpperBound().inline(this);
setUpperBound(typeVar, var.getUpperBound().inline(this));
typeVar.lower = var.getLowerBound().inline(this);
return typeVar;
}

private static void setUpperBound(TypeVar typeVar, Type bound) {
try {
// https://bugs.openjdk.java.net/browse/JDK-8193367
if (RuntimeVersion.isAtLeast13()) {
TypeVar.class.getMethod("setUpperBound", Type.class).invoke(typeVar, bound);
} else {
TypeVar.class.getField("bound").set(typeVar, bound);
}
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

Type inlineTypeVar(UTypeVar var) throws CouldNotResolveImportException {
Optional<TypeWithExpression> typeVarBinding = getOptionalBinding(var.key());
if (typeVarBinding.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.google.errorprone.refaster;

import static com.google.common.base.Preconditions.checkState;

import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.errorprone.refaster.PlaceholderUnificationVisitor.State;
import com.google.errorprone.refaster.UPlaceholderExpression.PlaceholderParamIdent;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.RuntimeVersion;
import com.sun.source.tree.ArrayAccessTree;
import com.sun.source.tree.AssignmentTree;
import com.sun.source.tree.BinaryTree;
Expand Down Expand Up @@ -517,7 +520,7 @@ public Choice<State<JCAssignOp>> visitCompoundAssignment(
state,
s -> unifyExpression(node.getVariable(), s),
s -> unifyExpression(node.getExpression(), s),
(var, expr) -> maker().Assignop(((JCAssignOp) node).getTag(), var, expr))
(variable, expr) -> maker().Assignop(((JCAssignOp) node).getTag(), variable, expr))
.condition(assignOp -> !(assignOp.result().getVariable() instanceof PlaceholderParamIdent));
}

Expand Down Expand Up @@ -663,9 +666,39 @@ public Choice<State<JCSwitch>> visitSwitch(final SwitchTree node, State<?> state
@Override
public Choice<State<JCCase>> visitCase(final CaseTree node, State<?> state) {
return chooseSubtrees(
state,
s -> unifyStatements(node.getStatements(), s),
stmts -> maker().Case((JCExpression) node.getExpression(), stmts));
state, s -> unifyStatements(node.getStatements(), s), stmts -> makeCase(node, stmts));
}

private JCCase makeCase(CaseTree node, List<JCStatement> stmts) {
try {
if (RuntimeVersion.isAtLeast12()) {
Enum<?> caseKind = (Enum) CaseTree.class.getMethod("getCaseKind").invoke(node);
checkState(
caseKind.name().contentEquals("STATEMENT"),
"expression switches are not supported yet");
return (JCCase)
TreeMaker.class
.getMethod(
"Case",
Class.forName("com.sun.source.tree.CaseTree.CaseKind"),
List.class,
List.class,
JCTree.class)
.invoke(
maker(),
caseKind,
List.of((JCExpression) node.getExpression()),
stmts,
/* body= */ null);
} else {
return (JCCase)
TreeMaker.class
.getMethod("Case", JCExpression.class, List.class)
.invoke(maker(), node.getExpression(), stmts);
}
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

@Override
Expand Down
60 changes: 54 additions & 6 deletions core/src/main/java/com/google/errorprone/refaster/UBreak.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,44 @@

package com.google.errorprone.refaster;

import com.google.auto.value.AutoValue;
import com.google.errorprone.util.RuntimeVersion;
import com.sun.source.tree.BreakTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.TreeVisitor;
import com.sun.tools.javac.tree.JCTree.JCBreak;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.Name;
import java.util.Objects;
import javax.annotation.Nullable;

/**
* {@code UTree} representation of {@code BreakTree}.
*
* @author lowasser@google.com (Louis Wasserman)
*/
@AutoValue
abstract class UBreak extends USimpleStatement implements BreakTree {
final class UBreak extends USimpleStatement implements BreakTree {

private final StringName label;

UBreak(StringName label) {
this.label = label;
}

static UBreak create(@Nullable CharSequence label) {
return new AutoValue_UBreak((label == null) ? null : StringName.of(label));
return new UBreak((label == null) ? null : StringName.of(label));
}

// @Override for JDK 12 only
public ExpressionTree getValue() {
return null;
}

@Override
@Nullable
public abstract StringName getLabel();
public StringName getLabel() {
return label;
}

@Override
public Kind getKind() {
Expand All @@ -53,7 +71,23 @@ private ULabeledStatement.Key key() {

@Override
public JCBreak inline(Inliner inliner) {
return inliner.maker().Break(ULabeledStatement.inlineLabel(getLabel(), inliner));
return makeBreak(ULabeledStatement.inlineLabel(getLabel(), inliner), inliner);
}

private static JCBreak makeBreak(Name label, Inliner inliner) {
try {
if (RuntimeVersion.isAtLeast12()) {
return (JCBreak)
TreeMaker.class
.getMethod("Break", JCExpression.class)
.invoke(inliner.maker(), inliner.maker().Ident(label));
} else {
return (JCBreak)
TreeMaker.class.getMethod("Break", Name.class).invoke(inliner.maker(), label);
}
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}

@Override
Expand All @@ -66,4 +100,18 @@ public Choice<Unifier> visitBreak(BreakTree node, Unifier unifier) {
boundName != null && node.getLabel().contentEquals(boundName), unifier);
}
}

@Override
public int hashCode() {
return Objects.hashCode(label);
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof UBreak)) {
return false;
}
UBreak other = (UBreak) obj;
return Objects.equals(label, other.label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeFalse;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -30,6 +33,7 @@ public class ArrayFillIncompatibleTypeTest {

@Test
public void testPrimitiveBoxingIntoObject() {
assumeFalse(RuntimeVersion.isAtLeast12()); // https://bugs.openjdk.java.net/browse/JDK-8028563
compilationHelper
.addSourceLines(
"Test.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeFalse;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -45,6 +48,7 @@ public void testNegativeCase_recursive() {

@Test
public void testPrimitiveBoxingIntoObject() {
assumeFalse(RuntimeVersion.isAtLeast12()); // https://bugs.openjdk.java.net/browse/JDK-8028563
compilationHelper
.addSourceLines(
"Test.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public class MoreThanOneScopeAnnotationOnClassPositiveCases {
/** Class has two scope annotations */
@Singleton
@SessionScoped
// BUG: Diagnostic contains: @Singleton(), @SessionScoped().
// BUG: Diagnostic contains:
class TestClass1 {}

/** Class has three annotations, two of which are scope annotations. */
@Singleton
@SuppressWarnings("foo")
@SessionScoped
// BUG: Diagnostic contains: @Singleton(), @SessionScoped().
// BUG: Diagnostic contains:
class TestClass2 {}

@Scope
Expand All @@ -42,6 +42,6 @@ class TestClass2 {}
@Singleton
@CustomScope
@SessionScoped
// BUG: Diagnostic contains: @Singleton(), @CustomScope(), @SessionScoped().
// BUG: Diagnostic contains:
class TestClass3 {}
}

0 comments on commit 8de8a63

Please sign in to comment.