Skip to content

Commit 149bab2

Browse files
committed
Import StandardCharsets.UTF_8 statically
The 36 uses of StandardCharsets.UTF_8 now read UTF_8, as the rest of the code already wrote it, and the ten files no longer import StandardCharsets. The import in a MainTest string is input to the formatter and stays.
1 parent 3c57634 commit 149bab2

10 files changed

Lines changed: 48 additions & 52 deletions

File tree

‎gradle-open-java-format/src/main/java/com/palantir/javaformat/gradle/BundledFormatter.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414

1515
package com.palantir.javaformat.gradle;
1616

17+
import static java.nio.charset.StandardCharsets.UTF_8;
18+
1719
import java.io.File;
1820
import java.io.IOException;
1921
import java.io.InputStream;
2022
import java.io.UncheckedIOException;
21-
import java.nio.charset.StandardCharsets;
2223
import java.nio.file.Files;
2324
import java.nio.file.Path;
2425
import java.security.DigestInputStream;
@@ -86,10 +87,7 @@ static List<File> jars(Path gradleUserHome, ClassLoader plugin) {
8687
}
8788

8889
private static List<Jar> list(ClassLoader plugin) {
89-
return new String(resource(plugin, LIST), StandardCharsets.UTF_8)
90-
.lines()
91-
.map(Jar::parse)
92-
.toList();
90+
return new String(resource(plugin, LIST), UTF_8).lines().map(Jar::parse).toList();
9391
}
9492

9593
private static boolean isComplete(Path dir, List<Jar> list) {

‎gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/BundledFormatterTest.java‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.palantir.javaformat.gradle;
1616

17+
import static java.nio.charset.StandardCharsets.UTF_8;
1718
import static org.assertj.core.api.Assertions.assertThat;
1819
import static org.assertj.core.api.Assertions.assertThatThrownBy;
1920

@@ -23,7 +24,6 @@
2324
import java.io.UncheckedIOException;
2425
import java.net.URL;
2526
import java.net.URLClassLoader;
26-
import java.nio.charset.StandardCharsets;
2727
import java.nio.file.Files;
2828
import java.nio.file.Path;
2929
import java.nio.file.attribute.FileTime;
@@ -148,8 +148,7 @@ void a_jar_that_does_not_match_the_list_fails_and_leaves_nothing_behind(@TempDir
148148
var bundle = Files.createDirectories(plugin.resolve(BUNDLE));
149149
Files.writeString(bundle.resolve("formatter.jar"), "not what the list says");
150150
Files.writeString(
151-
bundle.resolve("jars.txt"),
152-
sha256("what the list says".getBytes(StandardCharsets.UTF_8)) + " formatter.jar\n");
151+
bundle.resolve("jars.txt"), sha256("what the list says".getBytes(UTF_8)) + " formatter.jar\n");
153152

154153
try (var loader = new URLClassLoader(new URL[] {plugin.toUri().toURL()}, null)) {
155154
assertThatThrownBy(() -> BundledFormatter.jars(gradleUserHome, loader))
@@ -161,9 +160,7 @@ void a_jar_that_does_not_match_the_list_fails_and_leaves_nothing_behind(@TempDir
161160
}
162161

163162
private static List<String> bundledList() {
164-
return new String(resource(BUNDLE + "jars.txt"), StandardCharsets.UTF_8)
165-
.lines()
166-
.toList();
163+
return new String(resource(BUNDLE + "jars.txt"), UTF_8).lines().toList();
167164
}
168165

169166
private static List<String> bundledNames() {

‎gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/NativeImageResolutionTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.palantir.javaformat.gradle;
1616

17+
import static java.nio.charset.StandardCharsets.UTF_8;
1718
import static org.assertj.core.api.Assertions.assertThat;
1819

1920
import com.fasterxml.jackson.databind.json.JsonMapper;
@@ -22,7 +23,6 @@
2223
import com.palantir.javaformat.java.Replacement;
2324
import java.io.IOException;
2425
import java.io.UncheckedIOException;
25-
import java.nio.charset.StandardCharsets;
2626
import java.nio.file.Files;
2727
import java.nio.file.Path;
2828
import java.nio.file.attribute.PosixFilePermissions;
@@ -154,7 +154,7 @@ private static String lddVersion() {
154154
var process = new ProcessBuilder("ldd", "--version")
155155
.redirectErrorStream(true)
156156
.start();
157-
var output = new String(process.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
157+
var output = new String(process.getInputStream().readAllBytes(), UTF_8);
158158
process.waitFor();
159159
return output.lines().findFirst().orElse("");
160160
} catch (IOException e) {

‎gradle-open-java-format/src/test/java/com/palantir/javaformat/gradle/testing/GradleTestProject.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
package com.palantir.javaformat.gradle.testing;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
1921
import java.io.File;
2022
import java.io.IOException;
2123
import java.io.InputStream;
2224
import java.io.UncheckedIOException;
23-
import java.nio.charset.StandardCharsets;
2425
import java.nio.file.Files;
2526
import java.nio.file.Path;
2627
import java.util.ArrayList;
@@ -164,7 +165,7 @@ public GradleTestProject writeFile(String relativePath, String content) {
164165
Path target = file(relativePath);
165166
try {
166167
Files.createDirectories(target.getParent());
167-
Files.writeString(target, content, StandardCharsets.UTF_8);
168+
Files.writeString(target, content, UTF_8);
168169
} catch (IOException e) {
169170
throw new UncheckedIOException("Could not write " + relativePath, e);
170171
}
@@ -189,7 +190,7 @@ public GradleTestProject delete(String relativePath) {
189190

190191
public String readFile(String relativePath) {
191192
try {
192-
return Files.readString(file(relativePath), StandardCharsets.UTF_8);
193+
return Files.readString(file(relativePath), UTF_8);
193194
} catch (IOException e) {
194195
throw new UncheckedIOException("Could not read " + relativePath, e);
195196
}

‎open-java-format-jdk-bootstrap/src/main/java/com/palantir/javaformat/bootstrap/FormatterCommandRunner.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
package com.palantir.javaformat.bootstrap;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
1921
import java.io.IOException;
2022
import java.io.InputStream;
2123
import java.io.OutputStream;
22-
import java.nio.charset.StandardCharsets;
2324
import java.nio.file.Path;
2425
import java.util.List;
2526
import java.util.Optional;
@@ -37,7 +38,7 @@ static Optional<String> runWithStdin(List<String> command, String input, Optiona
3738
.start();
3839

3940
try (OutputStream outputStream = process.getOutputStream()) {
40-
outputStream.write(input.getBytes(StandardCharsets.UTF_8));
41+
outputStream.write(input.getBytes(UTF_8));
4142
}
4243

4344
// Make sure to drain stdout before waiting for the process to exit as this can result in a deadlock otherwise.
@@ -72,7 +73,7 @@ private static boolean isSyntaxError(String stderr) {
7273

7374
private static String readToString(InputStream input) throws IOException {
7475
try (input) {
75-
return new String(input.readAllBytes(), StandardCharsets.UTF_8);
76+
return new String(input.readAllBytes(), UTF_8);
7677
}
7778
}
7879

‎open-java-format/src/main/java/com/palantir/javaformat/java/DebugRenderer.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.palantir.javaformat.java;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
20+
1921
import com.fasterxml.jackson.core.JsonProcessingException;
2022
import com.fasterxml.jackson.databind.ObjectMapper;
2123
import com.fasterxml.jackson.databind.node.ArrayNode;
@@ -37,7 +39,6 @@
3739
import com.palantir.javaformat.doc.Token;
3840
import java.io.IOException;
3941
import java.io.UncheckedIOException;
40-
import java.nio.charset.StandardCharsets;
4142
import java.nio.file.Files;
4243
import java.nio.file.Path;
4344
import java.nio.file.Paths;
@@ -77,7 +78,7 @@ static void render(
7778
formatterDecisionsJson);
7879

7980
try {
80-
Files.write(getOutputFile(), javascript.getBytes(StandardCharsets.UTF_8));
81+
Files.write(getOutputFile(), javascript.getBytes(UTF_8));
8182
} catch (IOException e) {
8283
throw new UncheckedIOException(e);
8384
}

‎open-java-format/src/test/java/com/palantir/javaformat/java/DiagnosticTest.java‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.io.InputStream;
2424
import java.io.PrintWriter;
2525
import java.io.StringWriter;
26-
import java.nio.charset.StandardCharsets;
2726
import java.nio.file.Files;
2827
import java.nio.file.Path;
2928
import java.util.Locale;
@@ -148,7 +147,7 @@ void parseError2() throws FormatterException, IOException, UsageException {
148147
String input = "class Foo { void f() {\n g() } }";
149148

150149
Path path = testFolder.resolve("A.java");
151-
Files.write(path, input.getBytes(StandardCharsets.UTF_8));
150+
Files.write(path, input.getBytes(UTF_8));
152151

153152
StringWriter out = new StringWriter();
154153
StringWriter err = new StringWriter();
@@ -165,7 +164,7 @@ void parseError2() throws FormatterException, IOException, UsageException {
165164
void parseErrorStdin() throws FormatterException, IOException, UsageException {
166165
String input = "class Foo { void f() {\n g() } }";
167166

168-
InputStream inStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
167+
InputStream inStream = new ByteArrayInputStream(input.getBytes(UTF_8));
169168
StringWriter out = new StringWriter();
170169
StringWriter err = new StringWriter();
171170
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), inStream);
@@ -181,7 +180,7 @@ void lexError2() throws FormatterException, IOException, UsageException {
181180
String input = "class Foo { void f() {\n g('foo'); } }";
182181

183182
Path path = testFolder.resolve("A.java");
184-
Files.write(path, input.getBytes(StandardCharsets.UTF_8));
183+
Files.write(path, input.getBytes(UTF_8));
185184

186185
StringWriter out = new StringWriter();
187186
StringWriter err = new StringWriter();
@@ -197,7 +196,7 @@ void lexError2() throws FormatterException, IOException, UsageException {
197196
@Test
198197
void lexErrorStdin() throws FormatterException, IOException, UsageException {
199198
String input = "class Foo { void f() {\n g('foo'); } }";
200-
InputStream inStream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
199+
InputStream inStream = new ByteArrayInputStream(input.getBytes(UTF_8));
201200
StringWriter out = new StringWriter();
202201
StringWriter err = new StringWriter();
203202
Main main = new Main(new PrintWriter(out, true), new PrintWriter(err, true), inStream);

‎open-java-format/src/test/java/com/palantir/javaformat/java/FormatterTest.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import static com.google.common.truth.Truth.assertThat;
1818
import static com.google.common.truth.Truth.assertWithMessage;
1919
import static com.palantir.javaformat.java.JavaFormatterOptions.Style;
20+
import static java.nio.charset.StandardCharsets.UTF_8;
2021
import static org.assertj.core.api.Assertions.assertThatCode;
2122
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2223
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
@@ -30,7 +31,6 @@
3031
import java.io.InputStreamReader;
3132
import java.io.PrintWriter;
3233
import java.io.StringWriter;
33-
import java.nio.charset.StandardCharsets;
3434
import java.nio.file.Files;
3535
import java.nio.file.Path;
3636
import java.time.Duration;
@@ -73,7 +73,7 @@ void formatStdinStdoutWithDashFlag() throws Exception {
7373
String input = "class Foo{\n" + "void f\n" + "() {\n" + "}\n" + "}\n";
7474
String expectedOutput = "class Foo {\n" + " void f() {}\n" + "}\n";
7575

76-
InputStream in = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
76+
InputStream in = new ByteArrayInputStream(input.getBytes(UTF_8));
7777
StringWriter out = new StringWriter();
7878
StringWriter err = new StringWriter();
7979

@@ -93,7 +93,7 @@ void formatLengthUpToEOF() throws Exception {
9393
String expectedOutput = "class Foo {\n" + " void f() {}\n" + "}\n";
9494

9595
Path path = testFolder.resolve("Foo.java");
96-
Files.write(path, input.getBytes(StandardCharsets.UTF_8));
96+
Files.write(path, input.getBytes(UTF_8));
9797

9898
StringWriter out = new StringWriter();
9999
StringWriter err = new StringWriter();
@@ -109,7 +109,7 @@ void formatLengthOutOfRange() throws Exception {
109109
String input = "class Foo{}\n";
110110

111111
Path path = testFolder.resolve("Foo.java");
112-
Files.write(path, input.getBytes(StandardCharsets.UTF_8));
112+
Files.write(path, input.getBytes(UTF_8));
113113

114114
StringWriter out = new StringWriter();
115115
StringWriter err = new StringWriter();
@@ -181,7 +181,7 @@ private void importOrdering(@Nullable String sortArg, String outputResourceName)
181181
String inputResourceName = "com/palantir/javaformat/java/testimports/A.input";
182182
String input = getResource(inputResourceName);
183183
String expectedOutput = getResource(outputResourceName);
184-
Files.write(path, input.getBytes(StandardCharsets.UTF_8));
184+
Files.write(path, input.getBytes(UTF_8));
185185

186186
StringWriter out = new StringWriter();
187187
StringWriter err = new StringWriter();
@@ -192,14 +192,14 @@ private void importOrdering(@Nullable String sortArg, String outputResourceName)
192192

193193
assertThat(err.toString()).isEmpty();
194194
assertThat(out.toString()).isEmpty();
195-
String output = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
195+
String output = new String(Files.readAllBytes(path), UTF_8);
196196
assertThat(output).isEqualTo(expectedOutput);
197197
}
198198

199199
private String getResource(String resourceName) throws IOException {
200200
try (InputStream stream = getClass().getClassLoader().getResourceAsStream(resourceName)) {
201201
assertWithMessage("Missing resource: " + resourceName).that(stream).isNotNull();
202-
return CharStreams.toString(new InputStreamReader(stream, StandardCharsets.UTF_8));
202+
return CharStreams.toString(new InputStreamReader(stream, UTF_8));
203203
}
204204
}
205205

‎open-java-format/src/test/java/com/palantir/javaformat/java/ModuleImportTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package com.palantir.javaformat.java;
1818

19+
import static java.nio.charset.StandardCharsets.UTF_8;
1920
import static org.assertj.core.api.Assertions.assertThat;
2021

2122
import java.io.ByteArrayInputStream;
2223
import java.io.PrintWriter;
2324
import java.io.StringWriter;
24-
import java.nio.charset.StandardCharsets;
2525
import org.junit.jupiter.api.Assumptions;
2626
import org.junit.jupiter.api.BeforeAll;
2727
import org.junit.jupiter.api.Test;
@@ -79,7 +79,7 @@ class Example {}
7979
Main main = new Main(
8080
new PrintWriter(out, true),
8181
new PrintWriter(err, true),
82-
new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)));
82+
new ByteArrayInputStream(input.getBytes(UTF_8)));
8383
int exitCode = main.format("-", "--fix-imports-only", "--skip-removing-unused-imports");
8484
assertThat(err.toString()).isEmpty();
8585
assertThat(exitCode).isZero();

0 commit comments

Comments
 (0)