Skip to content

Commit

Permalink
Add another method to SourceVersion to check if 'effectively final'…
Browse files Browse the repository at this point in the history
… is supported

Follow-up to unknown commit

Startblock:
    unknown commit is submitted
PiperOrigin-RevId: 499081505
  • Loading branch information
cushon authored and Error Prone Team committed Jan 2, 2023
1 parent b5cbee9 commit 6c0d670
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,21 @@
public final class SourceVersion {
/** Returns true if the compiler source version level supports switch expressions. */
public static boolean supportsSwitchExpressions(Context context) {
return sourceIsAtLeast(context, "14");
return sourceIsAtLeast(context, 14);
}

/** Returns true if the compiler source version level supports text blocks. */
public static boolean supportsTextBlocks(Context context) {
return sourceIsAtLeast(context, "15");
return sourceIsAtLeast(context, 15);
}

private static boolean sourceIsAtLeast(Context context, String versionString) {
Source lowerBound = Source.lookup(versionString);
/** Returns true if the compiler source version level supports effectively final. */
public static boolean supportsEffectivelyFinal(Context context) {
return sourceIsAtLeast(context, 8);
}

private static boolean sourceIsAtLeast(Context context, int version) {
Source lowerBound = Source.lookup(Integer.toString(version));
return lowerBound != null && Source.instance(context).compareTo(lowerBound) >= 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.SourceVersion;
import com.sun.source.tree.ForLoopTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Source;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeInfo;
Expand Down Expand Up @@ -99,7 +99,7 @@ boolean forLoopVariable(VariableTree tree, TreePath path) {

private Description handleLocalOrParam(VariableTree tree, VisitorState state, Symbol sym) {
if (sym.getModifiers().contains(Modifier.FINAL)) {
if (Source.instance(state.context).compareTo(Source.lookup("1.8")) >= 0) {
if (SourceVersion.supportsEffectivelyFinal(state.context)) {
// In Java 8, the final modifier is never necessary on locals/parameters because
// effectively final variables can be used anywhere a final variable is required.
Optional<SuggestedFix> fix = SuggestedFixes.removeModifiers(tree, state, Modifier.FINAL);
Expand Down

0 comments on commit 6c0d670

Please sign in to comment.