diff --git a/check_api/src/main/java/com/google/errorprone/util/SourceVersion.java b/check_api/src/main/java/com/google/errorprone/util/SourceVersion.java index 23de9c6402a..39fdc7d6947 100644 --- a/check_api/src/main/java/com/google/errorprone/util/SourceVersion.java +++ b/check_api/src/main/java/com/google/errorprone/util/SourceVersion.java @@ -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; } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/VarChecker.java b/core/src/main/java/com/google/errorprone/bugpatterns/VarChecker.java index d0e8b625834..e69d8591055 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/VarChecker.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/VarChecker.java @@ -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; @@ -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 fix = SuggestedFixes.removeModifiers(tree, state, Modifier.FINAL);