Skip to content

Commit

Permalink
StatementSwitchToExpressionSwitch: only trigger on compatible target …
Browse files Browse the repository at this point in the history
…versions

This change introduces a `TargetVersion` utility class, analogous to the
existing `RuntimeVersion` class. The new class tells whether the current
compilation process targets a bytecode version that is generally
associated with a given (or more recent) JDK version.

The new utility class is used to make sure that
`StatementSwitchToExpressionSwitch` flags statement switches only if the
target version supports expression switches, irrespective of whether the
current runtime supports such switch types. Concretely this makes the
check compatible with projects that target JDK 11, yet support building
with JDK 17.
  • Loading branch information
Stephan202 committed Dec 31, 2022
1 parent 721096f commit c7a23da
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 The Error Prone Authors.
* Copyright 2023 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,9 +16,12 @@

package com.google.errorprone.util;

/** JDK version string utilities. */
/**
* JDK runtime version utilities.
*
* @see TargetVersion
*/
public final class RuntimeVersion {

private static final int FEATURE = Runtime.version().feature();

/** Returns true if the current runtime is JDK 12 or newer. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2018 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.util;

import com.sun.tools.javac.jvm.Target;
import com.sun.tools.javac.util.Context;

/**
* JDK target version utilities.
*
* @see RuntimeVersion
*/
public final class TargetVersion {
/** Returns true if the compiler targets JRE 11 or newer. */
public static boolean isAtLeast11(Context context) {
return majorVersion(context) >= 55;
}

/** Returns true if the compiler targets JRE 12 or newer. */
public static boolean isAtLeast12(Context context) {
return majorVersion(context) >= 56;
}

/** Returns true if the compiler targets JRE 13 or newer. */
public static boolean isAtLeast13(Context context) {
return majorVersion(context) >= 57;
}

/** Returns true if the compiler targets JRE 14 or newer. */
public static boolean isAtLeast14(Context context) {
return majorVersion(context) >= 58;
}

/** Returns true if the compiler targets JRE 15 or newer. */
public static boolean isAtLeast15(Context context) {
return majorVersion(context) >= 59;
}

/** Returns true if the compiler targets JRE 16 or newer. */
public static boolean isAtLeast16(Context context) {
return majorVersion(context) >= 60;
}

/** Returns true if the compiler targets JRE 17 or newer. */
public static boolean isAtLeast17(Context context) {
return majorVersion(context) >= 61;
}

/** Returns true if the compiler targets JRE 18 or newer. */
public static boolean isAtLeast18(Context context) {
return majorVersion(context) >= 62;
}

/** Returns true if the compiler targets JRE 19 or newer. */
public static boolean isAtLeast19(Context context) {
return majorVersion(context) >= 63;
}

/** Returns true if the compiler targets JRE 20 or newer. */
public static boolean isAtLeast20(Context context) {
return majorVersion(context) >= 64;
}

/** Returns true if the compiler targets JRE 21 or newer. */
public static boolean isAtLeast21(Context context) {
return majorVersion(context) >= 65;
}

private static int majorVersion(Context context) {
return Target.instance(context).majorVersion;
}

private TargetVersion() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.Reachability;
import com.google.errorprone.util.RuntimeVersion;
import com.google.errorprone.util.TargetVersion;
import com.sun.source.tree.BreakTree;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ExpressionTree;
Expand Down Expand Up @@ -86,7 +87,7 @@ public StatementSwitchToExpressionSwitch(ErrorProneFlags flags) {
@Override
public Description matchSwitch(SwitchTree switchTree, VisitorState state) {
// Expression switches finalized in Java 14
if (!RuntimeVersion.isAtLeast14()) {
if (!TargetVersion.isAtLeast14(state.context)) {
return NO_MATCH;
}

Expand Down
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@
<compilerArgs>
<arg>--add-exports=java.base/jdk.internal.javac=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED</arg>
<arg>--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED</arg>
Expand Down Expand Up @@ -213,6 +213,7 @@
-Xmx1g
--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED
--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED
Expand Down

0 comments on commit c7a23da

Please sign in to comment.