diff --git a/CHANGES.md b/CHANGES.md index 1f27bbb299..401998b233 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Changes +* **POTENTIALLY BREAKING** Bump bytecode from Java 8 to 11 ([#1530](https://github.com/diffplug/spotless/pull/1530) part 2 of [#1337](https://github.com/diffplug/spotless/issues/1337)) ## [2.34.0] - 2023-01-26 ### Added diff --git a/gradle.properties b/gradle.properties index d99bdac6ce..dc4f24a36b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,7 +16,7 @@ artifactIdMaven=spotless-maven-plugin artifactIdGradle=spotless-plugin-gradle # Build requirements -VER_JAVA=1.8 +VER_JAVA=11 VER_SPOTBUGS=4.7.3 VER_JSR_305=3.0.2 @@ -25,7 +25,7 @@ VER_SLF4J=[1.6,2.0[ # Used in multiple places VER_DURIAN=1.2.0 -VER_JGIT=5.13.1.202206130422-r +VER_JGIT=6.4.0.202211300538-r VER_JUNIT=5.9.2 VER_ASSERTJ=3.24.2 -VER_MOCKITO=4.11.0 +VER_MOCKITO=5.0.0 diff --git a/gradle/java-setup.gradle b/gradle/java-setup.gradle index 5de128bda7..10eca7a2d4 100644 --- a/gradle/java-setup.gradle +++ b/gradle/java-setup.gradle @@ -4,10 +4,10 @@ // setup java apply plugin: 'java' - -sourceCompatibility = VER_JAVA -targetCompatibility = VER_JAVA -tasks.withType(JavaCompile).configureEach { options.encoding = 'UTF-8' } +tasks.withType(JavaCompile).configureEach { + options.encoding = 'UTF-8' + options.release = Integer.parseInt(VER_JAVA) +} ////////////// // SPOTBUGS // diff --git a/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java b/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java index 15c43c815b..55336e8c04 100644 --- a/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java +++ b/lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2021 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -143,7 +143,7 @@ public String getMessage() { Objects.requireNonNull(runToFix, "runToFix"); Objects.requireNonNull(formatter, "formatter"); Objects.requireNonNull(problemFiles, "problemFiles"); - DiffMessageFormatter diffFormater = new DiffMessageFormatter(this); + DiffMessageFormatter diffFormater = new DiffMessageFormatter(formatter, problemFiles); return "The following files had format violations:\n" + diffFormater.buffer + runToFix; @@ -151,10 +151,6 @@ public String getMessage() { throw Errors.asRuntime(e); } } - - String relativePath(File file) { - return formatter.getRootDir().relativize(file.toPath()).toString(); - } } private static final int MAX_CHECK_MESSAGE_LINES = 50; @@ -163,25 +159,32 @@ String relativePath(File file) { private final StringBuilder buffer = new StringBuilder(MAX_CHECK_MESSAGE_LINES * 64); private int numLines = 0; - private DiffMessageFormatter(Builder builder) throws IOException { - ListIterator problemIter = builder.problemFiles.listIterator(); + private final CleanProvider formatter; + + private DiffMessageFormatter(CleanProvider formatter, List problemFiles) throws IOException { + this.formatter = Objects.requireNonNull(formatter, "formatter"); + ListIterator problemIter = problemFiles.listIterator(); while (problemIter.hasNext() && numLines < MAX_CHECK_MESSAGE_LINES) { File file = problemIter.next(); - addFile(builder.relativePath(file) + "\n" + DiffMessageFormatter.diff(builder, file)); + addFile(relativePath(file) + "\n" + diff(file)); } if (problemIter.hasNext()) { - int remainingFiles = builder.problemFiles.size() - problemIter.nextIndex(); + int remainingFiles = problemFiles.size() - problemIter.nextIndex(); if (remainingFiles >= MAX_FILES_TO_LIST) { buffer.append("Violations also present in ").append(remainingFiles).append(" other files.\n"); } else { buffer.append("Violations also present in:\n"); while (problemIter.hasNext()) { - addIntendedLine(NORMAL_INDENT, builder.relativePath(problemIter.next())); + addIntendedLine(NORMAL_INDENT, relativePath(problemIter.next())); } } } } + private String relativePath(File file) { + return formatter.getRootDir().relativize(file.toPath()).toString(); + } + private static final int MIN_LINES_PER_FILE = 4; private static final Splitter NEWLINE_SPLITTER = Splitter.on('\n'); @@ -230,10 +233,10 @@ private void addIntendedLine(String indent, String line) { * look like if formatted using the given formatter. Does not end with any newline * sequence (\n, \r, \r\n). */ - private static String diff(Builder builder, File file) throws IOException { - String raw = new String(Files.readAllBytes(file.toPath()), builder.formatter.getEncoding()); + private String diff(File file) throws IOException { + String raw = new String(Files.readAllBytes(file.toPath()), formatter.getEncoding()); String rawUnix = LineEnding.toUnix(raw); - String formatted = builder.formatter.getFormatted(file, rawUnix); + String formatted = formatter.getFormatted(file, rawUnix); String formattedUnix = LineEnding.toUnix(formatted); if (rawUnix.equals(formattedUnix)) { diff --git a/lib/build.gradle b/lib/build.gradle index f2ca13ccb1..9d95007bc1 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -90,7 +90,7 @@ dependencies { compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0' compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0' - String VER_SCALAFMT="3.6.1" + String VER_SCALAFMT="3.7.1" scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT" String VER_DIKTAT = "1.2.4.2" diff --git a/lib/src/main/java/com/diffplug/spotless/EncodingErrorMsg.java b/lib/src/main/java/com/diffplug/spotless/EncodingErrorMsg.java index 168bf9acff..3e4bb3423b 100644 --- a/lib/src/main/java/com/diffplug/spotless/EncodingErrorMsg.java +++ b/lib/src/main/java/com/diffplug/spotless/EncodingErrorMsg.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,7 +15,6 @@ */ package com.diffplug.spotless; -import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; @@ -116,8 +115,8 @@ private static void addIfAvailable(Collection charsets, String name) { } private void appendExample(Charset charset, boolean must) { - java8fix(byteBuf).clear(); - java8fix(charBuf).clear(); + byteBuf.clear(); + charBuf.clear(); CharsetDecoder decoder = charset.newDecoder(); if (!must) { @@ -135,7 +134,7 @@ private void appendExample(Charset charset, boolean must) { .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(byteBuf, charBuf, true); } - java8fix(charBuf).flip(); + charBuf.flip(); int start = Math.max(unrepresentable - CONTEXT, 0); int end = Math.min(charBuf.limit(), unrepresentable + CONTEXT + 1); @@ -147,9 +146,4 @@ private void appendExample(Charset charset, boolean must) { message.append(" <- "); message.append(charset.name()); } - - /** Fixes https://jira.mongodb.org/browse/JAVA-2559, as reported in https://github.com/diffplug/spotless/issues/1081 */ - private static Buffer java8fix(Buffer b) { - return b; - } } diff --git a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java index b4a69bef11..0703a91318 100644 --- a/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java +++ b/lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,8 +24,6 @@ import java.security.ProtectionDomain; import java.util.Objects; -import javax.annotation.Nullable; - /** * This class loader is used to load classes of Spotless features from a search * path of URLs.
@@ -103,35 +101,14 @@ public URL findResource(String name) { private static ByteBuffer urlToByteBuffer(URL url) throws IOException { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); - int nRead; - byte[] data = new byte[1024]; - InputStream inputStream = url.openStream(); - while ((nRead = inputStream.read(data, 0, data.length)) != -1) { - buffer.write(data, 0, nRead); + try (InputStream inputStream = url.openStream()) { + inputStream.transferTo(buffer); } buffer.flush(); return ByteBuffer.wrap(buffer.toByteArray()); } - /** - * Making spotless Java 9+ compatible. In Java 8 (and minor) the bootstrap - * class loader saw every platform class. In Java 9+ it was changed so the - * bootstrap class loader does not see all classes anymore. This might lead - * to ClassNotFoundException in formatters (e.g. freshmark). - * - * @return null on Java 8 (and minor), otherwise PlatformClassLoader - */ - @Nullable private static ClassLoader getParentClassLoader() { - double version = Double.parseDouble(System.getProperty("java.specification.version")); - if (version > 1.8) { - try { - return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null); - } catch (Exception e) { - throw ThrowingEx.asRuntime(e); - } - } else { - return null; - } + return ThrowingEx.get(() -> (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null)); } }