Skip to content

Commit

Permalink
Inline RuntimeVersion API
Browse files Browse the repository at this point in the history
This used to be a reflective wrapper around Runtime.version() for JDKs that
didn't support that API, but now that we're on JDK 11+ it isn't providing a lot
of value.

Also migrate to using Truth's assume API instead of assumeTrue/assumeFalse.

PiperOrigin-RevId: 662132187
  • Loading branch information
cushon authored and Error Prone Team committed Aug 13, 2024
1 parent 9d4c3d8 commit f97a916
Show file tree
Hide file tree
Showing 46 changed files with 205 additions and 340 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2822,7 +2822,7 @@ public static boolean isRuleKind(CaseTree caseTree) {
"unchecked", // reflection
})
public static Stream<? extends ExpressionTree> getCaseExpressions(CaseTree caseTree) {
if (!RuntimeVersion.isAtLeast12()) {
if (Runtime.version().feature() < 12) {
// "default" case gives an empty stream
return Stream.ofNullable(caseTree.getExpression());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean anyMatch(Predicate<Symbol> predicate) {
private static final Class<?> FILTER_CLASS = getFilterClass();

private static @Nullable Class<?> getFilterClass() {
if (RuntimeVersion.isAtLeast17()) {
if (Runtime.version().feature() >= 17) {
return null;
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static Symbol findIdent(String name, VisitorState state) {
private static Symbol findIdent(
String name, VisitorState state, KindSelector kind, Env<AttrContext> env)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
if (RuntimeVersion.isAtLeast13()) {
if (Runtime.version().feature() >= 13) {
Method method =
Resolve.class.getDeclaredMethod(
"findIdent", DiagnosticPosition.class, Env.class, Name.class, KindSelector.class);
Expand Down
102 changes: 0 additions & 102 deletions check_api/src/main/java/com/google/errorprone/util/RuntimeVersion.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.util.Context;

/**
* JDK source version utilities.
*
* @see RuntimeVersion
*/
/** JDK source version utilities. */
public final class SourceVersion {
/** Returns true if the compiler source version level supports switch expressions. */
public static boolean supportsSwitchExpressions(Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void supportsSwitchExpressions_conditionallySupported() {
Context context = contextWithSourceVersion("14");

assertThat(SourceVersion.supportsSwitchExpressions(context))
.isEqualTo(RuntimeVersion.isAtLeast14());
.isEqualTo(Runtime.version().feature() >= 14);
}

@Test
Expand All @@ -52,7 +52,8 @@ public void supportsTextBlocks_notSupported() {
public void supportsTextBlocks_conditionallySupported() {
Context context = contextWithSourceVersion("15");

assertThat(SourceVersion.supportsTextBlocks(context)).isEqualTo(RuntimeVersion.isAtLeast15());
assertThat(SourceVersion.supportsTextBlocks(context))
.isEqualTo(Runtime.version().feature() >= 15);
}

private static Context contextWithSourceVersion(String versionString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
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 @@ -668,12 +667,12 @@ public Choice<State<JCCase>> visitCase(CaseTree node, State<?> state) {

private JCCase makeCase(CaseTree node, List<JCStatement> stmts) {
try {
if (RuntimeVersion.isAtLeast12()) {
if (Runtime.version().feature() >= 12) {
Enum<?> caseKind = (Enum) CaseTree.class.getMethod("getCaseKind").invoke(node);
checkState(
caseKind.name().contentEquals("STATEMENT"),
"expression switches are not supported yet");
if (RuntimeVersion.isAtLeast21()) {
if (Runtime.version().feature() >= 21) {
return (JCCase)
TreeMaker.class
.getMethod(
Expand Down Expand Up @@ -702,7 +701,7 @@ private JCCase makeCase(CaseTree node, List<JCStatement> stmts) {
.invoke(
maker(),
caseKind,
RuntimeVersion.isAtLeast17()
Runtime.version().feature() >= 17
? CaseTree.class.getMethod("getLabels").invoke(node)
: List.of((JCExpression) node.getExpression()),
stmts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.errorprone.refaster;

import com.google.errorprone.util.RuntimeVersion;
import com.sun.source.tree.BreakTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.TreeVisitor;
Expand Down Expand Up @@ -75,7 +74,7 @@ public JCBreak inline(Inliner inliner) {

private static JCBreak makeBreak(Name label, Inliner inliner) {
try {
if (RuntimeVersion.isAtLeast12()) {
if (Runtime.version().feature() >= 12) {
return (JCBreak)
TreeMaker.class
.getMethod("Break", JCExpression.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;
import static com.google.common.truth.TruthJUnit.assume;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -50,7 +49,7 @@ public void positiveCase() {

@Test
public void formatted() {
assumeTrue(RuntimeVersion.isAtLeast15());
assume().that(Runtime.version().feature()).isAtLeast(15);
compilationHelper
.addSourceLines(
"AnnotateFormatMethodPositiveCases.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.google.errorprone.bugpatterns;

import static com.google.common.truth.TruthJUnit.assume;
import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;
import static org.junit.Assume.assumeTrue;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -361,7 +360,7 @@ public void diagnostic() {
// TODO(b/168625474): 'sealed' doesn't have a TokenKind
@Test
public void sealedInterface() {
assumeTrue(RuntimeVersion.isAtLeast15());
assume().that(Runtime.version().feature()).isAtLeast(15);
refactoringHelper
.addInputLines(
"Test.java", //
Expand All @@ -375,7 +374,7 @@ public void sealedInterface() {
"sealed @Deprecated interface Test {",
" final class A implements Test {}",
"}")
.setArgs("--enable-preview", "--release", Integer.toString(RuntimeVersion.release()))
.setArgs("--enable-preview", "--release", Integer.toString(Runtime.version().feature()))
.doTest(TEXT_MATCH);
}

Expand Down Expand Up @@ -532,7 +531,7 @@ public void varKeyword() {

@Test
public void recordAnnotation() {
assumeTrue(RuntimeVersion.isAtLeast16());
assume().that(Runtime.version().feature()).isAtLeast(16);
refactoringHelper
.addInputLines(
"Test.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeFalse;
import static com.google.common.truth.TruthJUnit.assume;

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 @@ -33,7 +32,9 @@ public class ArrayFillIncompatibleTypeTest {

@Test
public void primitiveBoxingIntoObject() {
assumeFalse(RuntimeVersion.isAtLeast12()); // https://bugs.openjdk.java.net/browse/JDK-8028563
assume()
.that(Runtime.version().feature())
.isLessThan(12); // 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,10 +16,9 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;
import static com.google.common.truth.TruthJUnit.assume;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -41,7 +40,7 @@ public void positiveCase() {

@Test
public void positiveCase_record() {
assumeTrue(RuntimeVersion.isAtLeast16());
assume().that(Runtime.version().feature()).isAtLeast(16);

compilationHelper
.addSourceLines(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;
import static com.google.common.truth.TruthJUnit.assume;

import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -346,7 +345,7 @@ public void nestedInLocal() {

@Test
public void nestedInLocal_static() {
assumeTrue(RuntimeVersion.isAtLeast16());
assume().that(Runtime.version().feature()).isAtLeast(16);
compilationHelper
.addSourceLines(
"A.java", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

package com.google.errorprone.bugpatterns;

import static org.junit.Assume.assumeTrue;
import static com.google.common.truth.TruthJUnit.assume;

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.util.RuntimeVersion;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -505,7 +504,7 @@ public void scannerDefaultCharset() {

@Test
public void withVar() {
assumeTrue(RuntimeVersion.isAtLeast15());
assume().that(Runtime.version().feature()).isAtLeast(15);
refactoringTest()
.addInputLines(
"in/Test.java",
Expand Down
Loading

0 comments on commit f97a916

Please sign in to comment.