diff --git a/SKIP-REQUIRE-JAVADOC b/SKIP-REQUIRE-JAVADOC new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/build.gradle b/build.gradle index e9981f728af0..f3518f63ffe0 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ plugins { // https://github.com/diffplug/spotless/tags ; see tags starting "gradle/" // Only works on JDK 11+ (even including the plugin crashes Gradle on JDK 8). id 'com.diffplug.spotless' version '8.2.1' + id 'com.github.andygoossens.modernizer' version '1.12.0' } // There is another `repositories { ... }` block below; if you change this one, change that one as well. @@ -174,14 +175,14 @@ allprojects { currentProj -> // * backward-incompatible changes have been made to APIs or elsewhere. // To make a snapshot release, version should end in "-SNAPSHOT", then: ./gradlew publish version = '3.49.5-eisop1-SNAPSHOT' + group = 'io.github.eisop' apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'com.gradleup.shadow' apply plugin: 'de.undercouch.download' apply plugin: 'net.ltgt.errorprone' - - group = 'io.github.eisop' + apply plugin: 'com.github.andygoossens.modernizer' // Keep in sync with "repositories { ... }" block above. repositories { @@ -443,6 +444,12 @@ allprojects { currentProj -> } } + modernizer { + failOnViolations = true + includeTestClasses = true + javaVersion = 8 + } + // Add the fat checker.jar to the classpath of every Javadoc task. This allows Javadoc in // any module to reference classes in any other module. // Also, build and use ManualTaglet as a taglet. diff --git a/checker-util/src/test/java/org/checkerframework/checker/optional/util/OptionalUtilTest.java b/checker-util/src/test/java/org/checkerframework/checker/optional/util/OptionalUtilTest.java index 285e44fc2b62..e145f485c706 100644 --- a/checker-util/src/test/java/org/checkerframework/checker/optional/util/OptionalUtilTest.java +++ b/checker-util/src/test/java/org/checkerframework/checker/optional/util/OptionalUtilTest.java @@ -16,7 +16,7 @@ public void test_castPresent() { Assert.assertTrue(nonEmptyOpt.isPresent()); @Present Optional foo = OptionalUtil.castPresent(nonEmptyOpt); - Assert.assertEquals(foo.get(), "non-empty"); + Assert.assertEquals("non-empty", foo.orElseThrow()); Assert.assertFalse(emptyOpt.isPresent()); Assert.assertThrows(Error.class, () -> OptionalUtil.castPresent(emptyOpt)); diff --git a/checker/bin-devel/test-misc.sh b/checker/bin-devel/test-misc.sh index 1606f36527ea..47d3b0ed0865 100755 --- a/checker/bin-devel/test-misc.sh +++ b/checker/bin-devel/test-misc.sh @@ -70,3 +70,6 @@ git diff --exit-code docs/manual/contributors.tex \ # Check gradle tasks are configured properly ./gradlew tasks + +# Check that modern APIs are used +./gradlew modernizer diff --git a/checker/src/main/java/org/checkerframework/checker/units/UnitsAnnotatedTypeFactory.java b/checker/src/main/java/org/checkerframework/checker/units/UnitsAnnotatedTypeFactory.java index aee2f42ff7b1..4609d8ee1ea1 100644 --- a/checker/src/main/java/org/checkerframework/checker/units/UnitsAnnotatedTypeFactory.java +++ b/checker/src/main/java/org/checkerframework/checker/units/UnitsAnnotatedTypeFactory.java @@ -43,8 +43,8 @@ import org.checkerframework.javacutil.UserError; import org.plumelib.reflection.Signatures; -import java.io.File; import java.lang.annotation.Annotation; +import java.nio.file.Paths; import java.util.Collection; import java.util.HashMap; import java.util.Map; @@ -229,7 +229,7 @@ private void loadAllExternalUnits() { // load external directories of units for (String directoryName : checker.getStringsOption("unitsDirs", ':')) { - if (!new File(directoryName).exists()) { + if (!Paths.get(directoryName).toFile().exists()) { throw new UserError("Nonexistent directory in -AunitsDirs: " + directoryName); } loadExternalDirectory(directoryName); diff --git a/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/CFGVisualizeOptions.java b/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/CFGVisualizeOptions.java index 39ba302c5adf..414179fe9296 100644 --- a/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/CFGVisualizeOptions.java +++ b/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/CFGVisualizeOptions.java @@ -3,6 +3,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.io.File; +import java.nio.file.Path; /** * Options for running analysis on files. @@ -90,7 +91,7 @@ public static CFGVisualizeOptions parseArgs(String[] args) { System.exit(1); } String input = args[0]; - File file = new File(input); + File file = Path.of(input).toFile(); if (!file.canRead()) { printError("Cannot read input file: " + file.getAbsolutePath()); printUsage(); diff --git a/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/DOTCFGVisualizer.java b/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/DOTCFGVisualizer.java index 2b1afb248aec..2d98c0499842 100644 --- a/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/DOTCFGVisualizer.java +++ b/dataflow/src/main/java/org/checkerframework/dataflow/cfg/visualize/DOTCFGVisualizer.java @@ -346,7 +346,7 @@ public void shutdown() { // Open for append, in case of multiple sub-checkers. try (Writer fstream = Files.newBufferedWriter( - Paths.get(outDir + "/methods.txt"), + Paths.get(outDir, "methods.txt"), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/AinferValidatePerDirectoryTest.java b/framework-test/src/main/java/org/checkerframework/framework/test/AinferValidatePerDirectoryTest.java index 2571017c1786..5a9842815d7b 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/AinferValidatePerDirectoryTest.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/AinferValidatePerDirectoryTest.java @@ -172,7 +172,7 @@ public void run() { // Only run if annotated files have been created. // See ainferTest task. if (generationTest != null - && !new File("tests/ainfer-" + CHECKER_SHORT_NAME + "/annotated/").exists()) { + && !Files.exists(Paths.get("tests", "ainfer-" + CHECKER_SHORT_NAME, "annotated"))) { throw new RuntimeException(generationTest + " must be run before this test."); } super.run(); diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerDirectoryTest.java b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerDirectoryTest.java index 338347700c3b..a60f15c5c659 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerDirectoryTest.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerDirectoryTest.java @@ -155,7 +155,7 @@ public void run() { customizeOptions(Collections.unmodifiableList(checkerOptions)); TestConfiguration config = TestConfigurationBuilder.buildDefaultConfiguration( - new File(resolveTestDirectory(), testDir).getPath(), + resolveTestDirectory().toPath().resolve(testDir).toString(), testFiles, classpathExtra, checkerNames, diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerFileTest.java b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerFileTest.java index de1f9c7fb109..ee3ee9caa721 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerFileTest.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkPerFileTest.java @@ -94,7 +94,7 @@ public void run() { customizeOptions(Collections.unmodifiableList(checkerOptions)); TestConfiguration config = TestConfigurationBuilder.buildDefaultConfiguration( - new File(resolveTestDirectory(), testDir).getPath(), + resolveTestDirectory().toPath().resolve(testDir).toString(), testFile, checker, customizedOptions, diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkRootedTest.java b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkRootedTest.java index 8d2f358a21fa..26b74ae507d6 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkRootedTest.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkRootedTest.java @@ -1,6 +1,7 @@ package org.checkerframework.framework.test; import java.io.File; +import java.nio.file.Paths; /** Encapsulates the directory root to search within for test files to compile. */ abstract class CheckerFrameworkRootedTest { @@ -17,9 +18,9 @@ public CheckerFrameworkRootedTest() {} protected File resolveTestDirectory() { TestRootDirectory annotation = getClass().getAnnotation(TestRootDirectory.class); if (annotation != null) { - return new File(annotation.value()); + return Paths.get(annotation.value()).toFile(); } - return new File("tests"); + return Paths.get("tests").toFile(); } /** diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkWPIPerDirectoryTest.java b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkWPIPerDirectoryTest.java index bb05b9b26c19..ee115fe991d5 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkWPIPerDirectoryTest.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/CheckerFrameworkWPIPerDirectoryTest.java @@ -106,7 +106,7 @@ protected void doNotTypecheck( * @return whether {@code file} contains {@code skipComment} */ public static boolean hasSkipComment(File file, String skipComment) { - try (Scanner in = new Scanner(file, StandardCharsets.UTF_8.name())) { + try (Scanner in = new Scanner(file, StandardCharsets.UTF_8)) { while (in.hasNext()) { String nextLine = in.nextLine(); if (nextLine.contains(skipComment)) { diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/RootedSuite.java b/framework-test/src/main/java/org/checkerframework/framework/test/RootedSuite.java index 37e2be289ed1..26509873d92f 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/RootedSuite.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/RootedSuite.java @@ -5,6 +5,7 @@ import org.junit.runners.model.InitializationError; import java.io.File; +import java.nio.file.Path; import java.util.List; /** @@ -33,8 +34,8 @@ public RootedSuite(Class klass, List runners) throws InitializationEr protected final File resolveTestDirectory() { TestRootDirectory annotation = getTestClass().getAnnotation(TestRootDirectory.class); if (annotation != null) { - return new File(annotation.value()); + return Path.of(annotation.value()).toFile(); } - return new File("tests"); + return Path.of("tests").toFile(); } } diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/TestConfigurationBuilder.java b/framework-test/src/main/java/org/checkerframework/framework/test/TestConfigurationBuilder.java index cd58435c965e..766fc19b6d54 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/TestConfigurationBuilder.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/TestConfigurationBuilder.java @@ -8,6 +8,7 @@ import org.plumelib.util.StringsPlume; import java.io.File; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -597,10 +598,15 @@ private static List catListAndIterable( * @return the output directory */ public static File getOutputDirFromProperty() { - return new File( - System.getProperty( - "tests.outputDir", - "tests" + File.separator + "build" + File.separator + "testclasses")); + return Paths.get( + System.getProperty( + "tests.outputDir", + "tests" + + File.separator + + "build" + + File.separator + + "testclasses")) + .toFile(); } /** diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/TestUtilities.java b/framework-test/src/main/java/org/checkerframework/framework/test/TestUtilities.java index bd9418a49fb5..a05595c36ac0 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/TestUtilities.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/TestUtilities.java @@ -85,7 +85,7 @@ public class TestUtilities { * @return found files */ public static List findNestedJavaTestFiles(String... dirNames) { - return findRelativeNestedJavaFiles(new File("tests"), dirNames); + return findRelativeNestedJavaFiles(Paths.get("tests").toFile(), dirNames); } /** @@ -96,7 +96,7 @@ public static List findNestedJavaTestFiles(String... dirNames) { * @return found files */ public static List findRelativeNestedJavaFiles(String parent, String... dirNames) { - return findRelativeNestedJavaFiles(new File(parent), dirNames); + return findRelativeNestedJavaFiles(Paths.get(parent).toFile(), dirNames); } /** @@ -111,7 +111,7 @@ public static List findRelativeNestedJavaFiles(File parent, String... dirN int i = 0; for (String dirName : dirNames) { - dirs[i] = new File(parent, dirName); + dirs[i] = parent.toPath().resolve(dirName).toFile(); ++i; } @@ -140,7 +140,7 @@ public static List> findJavaFilesPerDirectory(File parent, String... List> filesPerDirectory = new ArrayList<>(); for (String dirName : dirNames) { - File dir = new File(parent, dirName).toPath().toAbsolutePath().normalize().toFile(); + File dir = parent.toPath().resolve(dirName).toAbsolutePath().normalize().toFile(); if (dir.isDirectory()) { filesPerDirectory.addAll(findJavaTestFilesInDirectory(dir)); } else { @@ -168,8 +168,8 @@ public static List> findJavaFilesPerDirectory(File parent, String... throw new BugInCF("test directory does not exist: %s", dir); } p = - new File(parent, allSystemPath.replace("/", File.separator)) - .toPath() + parent.toPath() + .resolve(allSystemPath.replace("/", File.separator)) .toAbsolutePath() .normalize() .toFile(); @@ -206,7 +206,7 @@ private static List> findJavaTestFilesInDirectory(File dir) { } Arrays.sort(dirContents); for (String fileName : dirContents) { - File file = new File(dir, fileName); + File file = dir.toPath().resolve(fileName).toFile(); if (file.isDirectory()) { fileGroupedByDirectory.addAll(findJavaTestFilesInDirectory(file)); } else if (isJavaTestFile(file)) { @@ -228,7 +228,7 @@ private static List> findJavaTestFilesInDirectory(File dir) { */ public static List findFilesInParent(File parent, String... fileNames) { return CollectionsPlume.mapList( - (String fileName) -> new Object[] {new File(parent, fileName)}, fileNames); + (String fileName) -> new Object[] {parent.toPath().resolve(fileName)}, fileNames); } /** @@ -385,12 +385,13 @@ public static String summarizeSourceFiles(List javaFiles) { } public static File getTestFile(String fileRelativeToTestsDir) { - return new File("tests", fileRelativeToTestsDir); + return Paths.get("tests", fileRelativeToTestsDir).toFile(); } public static File findComparisonFile(File testFile) { File comparisonFile = - new File(testFile.getParent(), testFile.getName().replace(".java", ".out")); + Paths.get(testFile.getParent(), testFile.getName().replace(".java", ".out")) + .toFile(); return comparisonFile; } diff --git a/framework-test/src/main/java/org/checkerframework/framework/test/diagnostics/JavaDiagnosticReader.java b/framework-test/src/main/java/org/checkerframework/framework/test/diagnostics/JavaDiagnosticReader.java index ec5b80253af2..952333e7e7a6 100644 --- a/framework-test/src/main/java/org/checkerframework/framework/test/diagnostics/JavaDiagnosticReader.java +++ b/framework-test/src/main/java/org/checkerframework/framework/test/diagnostics/JavaDiagnosticReader.java @@ -14,6 +14,7 @@ import java.io.LineNumberReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -208,7 +209,7 @@ private JavaDiagnosticReader(File toRead, StringToTestDiagnosticLine codec) { private JavaDiagnosticReader( JavaFileObject toReadFileObject, StringToTestDiagnosticLine codec) { this.codec = codec; - this.filename = new File(toReadFileObject.getName()).getName(); + this.filename = Paths.get(toReadFileObject.getName()).toFile().getName(); LineNumberReader reader = null; try { reader = new LineNumberReader(toReadFileObject.openReader(true)); diff --git a/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java b/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java index db8853ad15e2..1c13feb8388e 100644 --- a/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java +++ b/framework/src/main/java/org/checkerframework/framework/util/CheckerMain.java @@ -12,8 +12,8 @@ import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -22,6 +22,7 @@ import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -65,7 +66,7 @@ public class CheckerMain { * @param args command-line arguments */ public static void main(String[] args) { - File pathToThisJar = new File(findPathTo(CheckerMain.class, false)); + File pathToThisJar = Paths.get(findPathTo(CheckerMain.class, false)).toFile(); ArrayList alargs = new ArrayList<>(Arrays.asList(args)); CheckerMain program = new CheckerMain(pathToThisJar, alargs); int exitStatus = program.invokeCompiler(); @@ -139,20 +140,25 @@ public static void main(String[] args) { public CheckerMain(File checkerJar, List args) { this.checkerJar = checkerJar; - File searchPath = checkerJar.getParentFile(); + Path searchPath = checkerJar.getParentFile().toPath(); replaceShorthandProcessor(args); argListFiles = collectArgFiles(args); this.checkerQualJar = extractFileArg( - CHECKER_QUAL_PATH_OPT, new File(searchPath, "checker-qual.jar"), args); + CHECKER_QUAL_PATH_OPT, + searchPath.resolve("checker-qual.jar").toFile(), + args); this.checkerUtilJar = extractFileArg( - CHECKER_UTIL_PATH_OPT, new File(searchPath, "checker-util.jar"), args); + CHECKER_UTIL_PATH_OPT, + searchPath.resolve("checker-util.jar").toFile(), + args); - this.javacJar = extractFileArg(JAVAC_PATH_OPT, new File(searchPath, "javac.jar"), args); + this.javacJar = + extractFileArg(JAVAC_PATH_OPT, searchPath.resolve("javac.jar").toFile(), args); this.compilationBootclasspath = createCompilationBootclasspath(args); this.runtimeClasspath = createRuntimeClasspath(args); @@ -240,7 +246,7 @@ protected List collectArgFiles(List args) { List argListFiles = new ArrayList<>(); for (String arg : args) { if (arg.startsWith("@")) { - argListFiles.add(new File(arg.substring(1))); + argListFiles.add(Paths.get(arg.substring(1)).toFile()); } } @@ -286,7 +292,7 @@ protected static File extractFileArg(String argumentName, File alternative, List if (filePath == null) { return alternative; } else { - return new File(filePath); + return Paths.get(filePath).toFile(); } } @@ -554,7 +560,7 @@ private List expandWildcards(String pathElement) { * @return all the .jar and .JAR files in the given directory */ private List jarFiles(String directory) { - File dir = new File(directory); + File dir = Paths.get(directory).toFile(); String[] jarFiles = dir.list((d, name) -> name.endsWith(".jar") || name.endsWith(".JAR")); if (jarFiles == null) { return Collections.emptyList(); @@ -748,7 +754,7 @@ public static String findPathTo(@Nullable Class cls, boolean errIfFromDirecto URLDecoder.decode( uri.substring("jar:file:".length(), idx), Charset.defaultCharset().name()); - return new File(fileName).getAbsolutePath(); + return Paths.get(fileName).toFile().getAbsolutePath(); } catch (UnsupportedEncodingException e) { throw new BugInCF("Default charset doesn't exist. Your VM is borked."); } @@ -862,7 +868,7 @@ protected void replaceShorthandProcessor(List args) { */ private List<@FullyQualifiedName String> getAllCheckerClassNames() { ArrayList<@FullyQualifiedName String> checkerClassNames = new ArrayList<>(); - try (FileInputStream fis = new FileInputStream(checkerJar); + try (InputStream fis = Files.newInputStream(checkerJar.toPath()); JarInputStream checkerJarIs = new JarInputStream(fis)) { ZipEntry entry; while ((entry = checkerJarIs.getNextEntry()) != null) { diff --git a/javacutil/src/main/java/org/checkerframework/javacutil/SystemUtil.java b/javacutil/src/main/java/org/checkerframework/javacutil/SystemUtil.java index 19e78a87a253..2f8ba33b7f9f 100644 --- a/javacutil/src/main/java/org/checkerframework/javacutil/SystemUtil.java +++ b/javacutil/src/main/java/org/checkerframework/javacutil/SystemUtil.java @@ -9,6 +9,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; import java.io.File; +import java.nio.file.Path; import java.util.Collections; import java.util.List; import java.util.regex.Matcher; @@ -132,7 +133,7 @@ private static int getJreVersion() { throw new Error("Can't infer Java home; java.home=" + javaHomeProperty); } } - File toolsJarFile = new File(new File(javaHome, "lib"), "tools.jar"); + File toolsJarFile = Path.of(javaHome, "lib", "tools.jar").toFile(); if (!toolsJarFile.exists()) { throw new Error( String.format(