Skip to content

Commit c15deb4

Browse files
authored
Merge pull request #236 from openjavaformat/abashev/Fix-imports-out-of-process
Fix only the imports in the out-of-process fixImports
2 parents 48ebc8a + 8a7e2b4 commit c15deb4

6 files changed

Lines changed: 101 additions & 10 deletions

File tree

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
6464
@Override
6565
public String formatSourceReflowStringsAndFixImports(String input) {
6666
try {
67-
return runFormatterCommand(input);
67+
return runFormatterCommand(input, /* fixImportsOnly= */ false);
6868
} catch (IOException e) {
6969
throw new UncheckedIOException("Error running formatter command", e);
7070
}
@@ -73,7 +73,7 @@ public String formatSourceReflowStringsAndFixImports(String input) {
7373
@Override
7474
public String fixImports(String input) throws FormatterException {
7575
try {
76-
return runFormatterCommand(input);
76+
return runFormatterCommand(input, /* fixImportsOnly= */ true);
7777
} catch (IOException e) {
7878
throw new UncheckedIOException("Error running formatter command", e);
7979
}
@@ -86,6 +86,7 @@ private ImmutableList<Replacement> getFormatReplacementsInternal(String input, C
8686
jdkPath,
8787
jvmArgsForVersion(jdkMajorVersion),
8888
implementationClassPath,
89+
/* fixImportsOnly= */ false,
8990
/* outputReplacements= */ true,
9091
ranges.stream().map(RangeUtils::toStringRange).toList());
9192

@@ -97,11 +98,12 @@ private ImmutableList<Replacement> getFormatReplacementsInternal(String input, C
9798
return MAPPER.readValue(output.get(), new TypeReference<>() {});
9899
}
99100

100-
private String runFormatterCommand(String input) throws IOException {
101+
private String runFormatterCommand(String input, boolean fixImportsOnly) throws IOException {
101102
FormatterCliArgs command = new FormatterCliArgs(
102103
jdkPath,
103104
jvmArgsForVersion(jdkMajorVersion),
104105
implementationClassPath,
106+
fixImportsOnly,
105107
/* outputReplacements= */ false,
106108
/* characterRanges= */ List.of());
107109
return FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.ofNullable(jdkPath.getParent()))
@@ -124,6 +126,7 @@ record FormatterCliArgs(
124126
Path jdkPath,
125127
List<String> jvmArgs,
126128
List<Path> implementationClasspath,
129+
boolean fixImportsOnly,
127130
boolean outputReplacements,
128131
List<String> characterRanges) {
129132

@@ -137,6 +140,9 @@ List<String> toArgs() {
137140
.collect(Collectors.joining(System.getProperty("path.separator"))));
138141
args.add(FORMATTER_MAIN_CLASS);
139142

143+
if (fixImportsOnly) {
144+
args.add("--fix-imports-only");
145+
}
140146
if (!characterRanges.isEmpty()) {
141147
args.add("--character-ranges");
142148
args.add(Joiner.on(',').join(characterRanges));

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
4949
try {
5050
FormatterNativeImageArgs command = new FormatterNativeImageArgs(
5151
nativeImagePath,
52+
/* fixImportsOnly= */ false,
5253
/* outputReplacements= */ true,
5354
ranges.stream().map(RangeUtils::toStringRange).toList());
5455

@@ -67,7 +68,7 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
6768
@Override
6869
public String formatSourceReflowStringsAndFixImports(String input) {
6970
try {
70-
return runFormatterCommand(input);
71+
return runFormatterCommand(input, /* fixImportsOnly= */ false);
7172
} catch (IOException e) {
7273
throw new UncheckedIOException("Error running the native image command", e);
7374
}
@@ -76,26 +77,30 @@ public String formatSourceReflowStringsAndFixImports(String input) {
7677
@Override
7778
public String fixImports(String input) {
7879
try {
79-
return runFormatterCommand(input);
80+
return runFormatterCommand(input, /* fixImportsOnly= */ true);
8081
} catch (IOException e) {
8182
throw new UncheckedIOException("Error running the native image command", e);
8283
}
8384
}
8485

85-
private String runFormatterCommand(String input) throws IOException {
86+
private String runFormatterCommand(String input, boolean fixImportsOnly) throws IOException {
8687
FormatterNativeImageArgs command = new FormatterNativeImageArgs(
87-
nativeImagePath, /* outputReplacements= */ false, /* characterRanges= */ List.of());
88+
nativeImagePath, fixImportsOnly, /* outputReplacements= */ false, /* characterRanges= */ List.of());
8889
return FormatterCommandRunner.runWithStdin(
8990
command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent()))
9091
.orElse(input);
9192
}
9293

93-
record FormatterNativeImageArgs(Path nativeImagePath, boolean outputReplacements, List<String> characterRanges) {
94+
record FormatterNativeImageArgs(
95+
Path nativeImagePath, boolean fixImportsOnly, boolean outputReplacements, List<String> characterRanges) {
9496

9597
List<String> toArgs() {
9698
var args = new ArrayList<String>();
9799
args.add(nativeImagePath.toAbsolutePath().toString());
98100

101+
if (fixImportsOnly) {
102+
args.add("--fix-imports-only");
103+
}
99104
if (!characterRanges.isEmpty()) {
100105
args.add("--character-ranges");
101106
args.add(Joiner.on(',').join(characterRanges));

‎open-java-format-jdk-bootstrap/src/test/java/com/palantir/javaformat/bootstrap/FormatterServicesTest.java‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ void can_format_full_file(FormatterService formatterService) throws FormatterExc
6464
assertThat(formatted).isEqualTo(expectedOutput);
6565
}
6666

67+
@ParameterizedTest
68+
@MethodSource("getFormatters")
69+
void fixes_only_the_imports(FormatterService formatterService) throws FormatterException {
70+
String input = getTestResourceContent("fix-imports.input");
71+
String expectedOutput = getTestResourceContent("fix-imports.output");
72+
73+
assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput);
74+
}
75+
6776
@ParameterizedTest
6877
@MethodSource("getFormatters")
6978
void can_format_large_input_file(FormatterService formatterService) throws FormatterException {
@@ -147,7 +156,7 @@ private String getTestResourceContent(String resourceName) {
147156
}
148157
}
149158

150-
private static List<Path> getClasspath() {
159+
static List<Path> getClasspath() {
151160
String classpath = System.getProperty("java.class.path");
152161
return Splitter.on(File.pathSeparatorChar)
153162
.trimResults()
@@ -157,7 +166,7 @@ private static List<Path> getClasspath() {
157166
.toList();
158167
}
159168

160-
private static Path javaBinPath() {
169+
static Path javaBinPath() {
161170
String javaHome = Preconditions.checkNotNull(System.getProperty("java.home"), "java.home property not set");
162171
return Path.of(javaHome).resolve("bin").resolve("java" + (isWindows() ? ".exe" : ""));
163172
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* You may obtain a copy of the License at
5+
*
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software
9+
* distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
* See the License for the specific language governing permissions and
12+
* limitations under the License.
13+
*/
14+
15+
package com.palantir.javaformat.bootstrap;
16+
17+
import static org.assertj.core.api.Assertions.assertThat;
18+
19+
import com.palantir.javaformat.java.FormatterException;
20+
import com.palantir.javaformat.java.FormatterService;
21+
import com.palantir.javaformat.java.FormatterServiceImpl;
22+
import java.net.URI;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
import java.util.stream.Stream;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.MethodSource;
28+
29+
/**
30+
* The services that need no native binary: the one in this JVM and the one that starts a new JVM. Unlike
31+
* {@link FormatterServicesTest}, this runs in every build.
32+
*/
33+
final class JvmFormatterServicesTest {
34+
35+
@ParameterizedTest
36+
@MethodSource("getFormatters")
37+
void fixes_only_the_imports(FormatterService formatterService) throws FormatterException {
38+
String input = getTestResourceContent("fix-imports.input");
39+
String expectedOutput = getTestResourceContent("fix-imports.output");
40+
41+
assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput);
42+
}
43+
44+
private static Stream<FormatterService> getFormatters() {
45+
return Stream.of(
46+
new FormatterServiceImpl(),
47+
new BootstrappingFormatterService(
48+
FormatterServicesTest.javaBinPath(),
49+
Runtime.version().feature(),
50+
FormatterServicesTest.getClasspath()));
51+
}
52+
53+
private String getTestResourceContent(String resourceName) {
54+
try {
55+
URI resource = getClass().getClassLoader().getResource(resourceName).toURI();
56+
return Files.readString(Path.of(resource));
57+
} catch (Exception e) {
58+
throw new RuntimeException("Error reading resource: " + resourceName, e);
59+
}
60+
}
61+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package p;
2+
import java.util.Map;
3+
import java.io.File;
4+
import java.util.List;
5+
class C { List<String> l; Map<String,String> m; }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package p;
2+
import java.util.List;
3+
import java.util.Map;
4+
5+
class C { List<String> l; Map<String,String> m; }

0 commit comments

Comments
 (0)