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

Bump bytecode to 11 #1530

Merged
merged 8 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
8 changes: 4 additions & 4 deletions gradle/java-setup.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -143,18 +143,14 @@ 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;
} catch (IOException e) {
throw Errors.asRuntime(e);
}
}

String relativePath(File file) {
return formatter.getRootDir().relativize(file.toPath()).toString();
}
}

private static final int MAX_CHECK_MESSAGE_LINES = 50;
Expand All @@ -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<File> problemIter = builder.problemFiles.listIterator();
private final CleanProvider formatter;

private DiffMessageFormatter(CleanProvider formatter, List<File> problemFiles) throws IOException {
this.formatter = Objects.requireNonNull(formatter, "formatter");
ListIterator<File> 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');

Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 4 additions & 10 deletions lib/src/main/java/com/diffplug/spotless/EncodingErrorMsg.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -116,8 +115,8 @@ private static void addIfAvailable(Collection<Charset> 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) {
Expand All @@ -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);
Expand All @@ -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;
}
}
31 changes: 4 additions & 27 deletions lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.<br/>
Expand Down Expand Up @@ -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 <code>null</code> on Java 8 (and minor), otherwise <code>PlatformClassLoader</code>
*/
@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));
}
}