Skip to content
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

StatementSwitchToExpressionSwitch: only trigger on compatible target versions #3646

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 2023 The Error Prone Authors.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems safe to assume that this PR won't be merged before the fireworks ;)

*
* 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>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of changes:

  • Sorted.
  • Removed duplicate com.sun.tools.javac.util entry.
  • Added com.sun.tools.javac.jvm.

<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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this PR is accepted, then the documentation should also be updated to include this flag.

--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