Skip to content

Commit aa78b56

Browse files
authored
Bump bytecode to java 11 (#1530)
2 parents d02cf92 + 2046daf commit aa78b56

File tree

7 files changed

+35
-59
lines changed

7 files changed

+35
-59
lines changed

Diff for: CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Changes
14+
* **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))
1315

1416
## [2.34.0] - 2023-01-26
1517
### Added

Diff for: gradle.properties

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ artifactIdMaven=spotless-maven-plugin
1616
artifactIdGradle=spotless-plugin-gradle
1717

1818
# Build requirements
19-
VER_JAVA=1.8
19+
VER_JAVA=11
2020
VER_SPOTBUGS=4.7.3
2121
VER_JSR_305=3.0.2
2222

@@ -25,7 +25,7 @@ VER_SLF4J=[1.6,2.0[
2525

2626
# Used in multiple places
2727
VER_DURIAN=1.2.0
28-
VER_JGIT=5.13.1.202206130422-r
28+
VER_JGIT=6.4.0.202211300538-r
2929
VER_JUNIT=5.9.2
3030
VER_ASSERTJ=3.24.2
31-
VER_MOCKITO=4.11.0
31+
VER_MOCKITO=5.0.0

Diff for: gradle/java-setup.gradle

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
// setup java
66
apply plugin: 'java'
7-
8-
sourceCompatibility = VER_JAVA
9-
targetCompatibility = VER_JAVA
10-
tasks.withType(JavaCompile).configureEach { options.encoding = 'UTF-8' }
7+
tasks.withType(JavaCompile).configureEach {
8+
options.encoding = 'UTF-8'
9+
options.release = Integer.parseInt(VER_JAVA)
10+
}
1111

1212
//////////////
1313
// SPOTBUGS //

Diff for: lib-extra/src/main/java/com/diffplug/spotless/extra/integration/DiffMessageFormatter.java

+17-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -143,18 +143,14 @@ public String getMessage() {
143143
Objects.requireNonNull(runToFix, "runToFix");
144144
Objects.requireNonNull(formatter, "formatter");
145145
Objects.requireNonNull(problemFiles, "problemFiles");
146-
DiffMessageFormatter diffFormater = new DiffMessageFormatter(this);
146+
DiffMessageFormatter diffFormater = new DiffMessageFormatter(formatter, problemFiles);
147147
return "The following files had format violations:\n"
148148
+ diffFormater.buffer
149149
+ runToFix;
150150
} catch (IOException e) {
151151
throw Errors.asRuntime(e);
152152
}
153153
}
154-
155-
String relativePath(File file) {
156-
return formatter.getRootDir().relativize(file.toPath()).toString();
157-
}
158154
}
159155

160156
private static final int MAX_CHECK_MESSAGE_LINES = 50;
@@ -163,25 +159,32 @@ String relativePath(File file) {
163159
private final StringBuilder buffer = new StringBuilder(MAX_CHECK_MESSAGE_LINES * 64);
164160
private int numLines = 0;
165161

166-
private DiffMessageFormatter(Builder builder) throws IOException {
167-
ListIterator<File> problemIter = builder.problemFiles.listIterator();
162+
private final CleanProvider formatter;
163+
164+
private DiffMessageFormatter(CleanProvider formatter, List<File> problemFiles) throws IOException {
165+
this.formatter = Objects.requireNonNull(formatter, "formatter");
166+
ListIterator<File> problemIter = problemFiles.listIterator();
168167
while (problemIter.hasNext() && numLines < MAX_CHECK_MESSAGE_LINES) {
169168
File file = problemIter.next();
170-
addFile(builder.relativePath(file) + "\n" + DiffMessageFormatter.diff(builder, file));
169+
addFile(relativePath(file) + "\n" + diff(file));
171170
}
172171
if (problemIter.hasNext()) {
173-
int remainingFiles = builder.problemFiles.size() - problemIter.nextIndex();
172+
int remainingFiles = problemFiles.size() - problemIter.nextIndex();
174173
if (remainingFiles >= MAX_FILES_TO_LIST) {
175174
buffer.append("Violations also present in ").append(remainingFiles).append(" other files.\n");
176175
} else {
177176
buffer.append("Violations also present in:\n");
178177
while (problemIter.hasNext()) {
179-
addIntendedLine(NORMAL_INDENT, builder.relativePath(problemIter.next()));
178+
addIntendedLine(NORMAL_INDENT, relativePath(problemIter.next()));
180179
}
181180
}
182181
}
183182
}
184183

184+
private String relativePath(File file) {
185+
return formatter.getRootDir().relativize(file.toPath()).toString();
186+
}
187+
185188
private static final int MIN_LINES_PER_FILE = 4;
186189
private static final Splitter NEWLINE_SPLITTER = Splitter.on('\n');
187190

@@ -230,10 +233,10 @@ private void addIntendedLine(String indent, String line) {
230233
* look like if formatted using the given formatter. Does not end with any newline
231234
* sequence (\n, \r, \r\n).
232235
*/
233-
private static String diff(Builder builder, File file) throws IOException {
234-
String raw = new String(Files.readAllBytes(file.toPath()), builder.formatter.getEncoding());
236+
private String diff(File file) throws IOException {
237+
String raw = new String(Files.readAllBytes(file.toPath()), formatter.getEncoding());
235238
String rawUnix = LineEnding.toUnix(raw);
236-
String formatted = builder.formatter.getFormatted(file, rawUnix);
239+
String formatted = formatter.getFormatted(file, rawUnix);
237240
String formattedUnix = LineEnding.toUnix(formatted);
238241

239242
if (rawUnix.equals(formattedUnix)) {

Diff for: lib/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ dependencies {
9090
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-experimental:0.48.0'
9191
compatKtLint0Dot48Dot0CompileAndTestOnly 'com.pinterest.ktlint:ktlint-ruleset-standard:0.48.0'
9292

93-
String VER_SCALAFMT="3.6.1"
93+
String VER_SCALAFMT="3.7.1"
9494
scalafmtCompileOnly "org.scalameta:scalafmt-core_2.13:$VER_SCALAFMT"
9595

9696
String VER_DIKTAT = "1.2.4.2"

Diff for: lib/src/main/java/com/diffplug/spotless/EncodingErrorMsg.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.spotless;
1717

18-
import java.nio.Buffer;
1918
import java.nio.ByteBuffer;
2019
import java.nio.CharBuffer;
2120
import java.nio.charset.Charset;
@@ -116,8 +115,8 @@ private static void addIfAvailable(Collection<Charset> charsets, String name) {
116115
}
117116

118117
private void appendExample(Charset charset, boolean must) {
119-
java8fix(byteBuf).clear();
120-
java8fix(charBuf).clear();
118+
byteBuf.clear();
119+
charBuf.clear();
121120

122121
CharsetDecoder decoder = charset.newDecoder();
123122
if (!must) {
@@ -135,7 +134,7 @@ private void appendExample(Charset charset, boolean must) {
135134
.onUnmappableCharacter(CodingErrorAction.REPLACE)
136135
.decode(byteBuf, charBuf, true);
137136
}
138-
java8fix(charBuf).flip();
137+
charBuf.flip();
139138

140139
int start = Math.max(unrepresentable - CONTEXT, 0);
141140
int end = Math.min(charBuf.limit(), unrepresentable + CONTEXT + 1);
@@ -147,9 +146,4 @@ private void appendExample(Charset charset, boolean must) {
147146
message.append(" <- ");
148147
message.append(charset.name());
149148
}
150-
151-
/** Fixes https://jira.mongodb.org/browse/JAVA-2559, as reported in https://github.com/diffplug/spotless/issues/1081 */
152-
private static Buffer java8fix(Buffer b) {
153-
return b;
154-
}
155149
}

Diff for: lib/src/main/java/com/diffplug/spotless/FeatureClassLoader.java

+4-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,8 +24,6 @@
2424
import java.security.ProtectionDomain;
2525
import java.util.Objects;
2626

27-
import javax.annotation.Nullable;
28-
2927
/**
3028
* This class loader is used to load classes of Spotless features from a search
3129
* path of URLs.<br/>
@@ -103,35 +101,14 @@ public URL findResource(String name) {
103101

104102
private static ByteBuffer urlToByteBuffer(URL url) throws IOException {
105103
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
106-
int nRead;
107-
byte[] data = new byte[1024];
108-
InputStream inputStream = url.openStream();
109-
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
110-
buffer.write(data, 0, nRead);
104+
try (InputStream inputStream = url.openStream()) {
105+
inputStream.transferTo(buffer);
111106
}
112107
buffer.flush();
113108
return ByteBuffer.wrap(buffer.toByteArray());
114109
}
115110

116-
/**
117-
* Making spotless Java 9+ compatible. In Java 8 (and minor) the bootstrap
118-
* class loader saw every platform class. In Java 9+ it was changed so the
119-
* bootstrap class loader does not see all classes anymore. This might lead
120-
* to ClassNotFoundException in formatters (e.g. freshmark).
121-
*
122-
* @return <code>null</code> on Java 8 (and minor), otherwise <code>PlatformClassLoader</code>
123-
*/
124-
@Nullable
125111
private static ClassLoader getParentClassLoader() {
126-
double version = Double.parseDouble(System.getProperty("java.specification.version"));
127-
if (version > 1.8) {
128-
try {
129-
return (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null);
130-
} catch (Exception e) {
131-
throw ThrowingEx.asRuntime(e);
132-
}
133-
} else {
134-
return null;
135-
}
112+
return ThrowingEx.get(() -> (ClassLoader) ClassLoader.class.getMethod("getPlatformClassLoader").invoke(null));
136113
}
137114
}

0 commit comments

Comments
 (0)