Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
@Override
public String formatSourceReflowStringsAndFixImports(String input) {
try {
return runFormatterCommand(input);
return runFormatterCommand(input, /* fixImportsOnly= */ false);
} catch (IOException e) {
throw new UncheckedIOException("Error running formatter command", e);
}
Expand All @@ -73,7 +73,7 @@ public String formatSourceReflowStringsAndFixImports(String input) {
@Override
public String fixImports(String input) throws FormatterException {
try {
return runFormatterCommand(input);
return runFormatterCommand(input, /* fixImportsOnly= */ true);
} catch (IOException e) {
throw new UncheckedIOException("Error running formatter command", e);
}
Expand All @@ -86,6 +86,7 @@ private ImmutableList<Replacement> getFormatReplacementsInternal(String input, C
jdkPath,
jvmArgsForVersion(jdkMajorVersion),
implementationClassPath,
/* fixImportsOnly= */ false,
/* outputReplacements= */ true,
ranges.stream().map(RangeUtils::toStringRange).toList());

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

private String runFormatterCommand(String input) throws IOException {
private String runFormatterCommand(String input, boolean fixImportsOnly) throws IOException {
FormatterCliArgs command = new FormatterCliArgs(
jdkPath,
jvmArgsForVersion(jdkMajorVersion),
implementationClassPath,
fixImportsOnly,
/* outputReplacements= */ false,
/* characterRanges= */ List.of());
return FormatterCommandRunner.runWithStdin(command.toArgs(), input, Optional.ofNullable(jdkPath.getParent()))
Expand All @@ -124,6 +126,7 @@ record FormatterCliArgs(
Path jdkPath,
List<String> jvmArgs,
List<Path> implementationClasspath,
boolean fixImportsOnly,
boolean outputReplacements,
List<String> characterRanges) {

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

if (fixImportsOnly) {
args.add("--fix-imports-only");
}
if (!characterRanges.isEmpty()) {
args.add("--character-ranges");
args.add(Joiner.on(',').join(characterRanges));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
try {
FormatterNativeImageArgs command = new FormatterNativeImageArgs(
nativeImagePath,
/* fixImportsOnly= */ false,
/* outputReplacements= */ true,
ranges.stream().map(RangeUtils::toStringRange).toList());

Expand All @@ -67,7 +68,7 @@ public ImmutableList<Replacement> getFormatReplacements(String input, Collection
@Override
public String formatSourceReflowStringsAndFixImports(String input) {
try {
return runFormatterCommand(input);
return runFormatterCommand(input, /* fixImportsOnly= */ false);
} catch (IOException e) {
throw new UncheckedIOException("Error running the native image command", e);
}
Expand All @@ -76,26 +77,30 @@ public String formatSourceReflowStringsAndFixImports(String input) {
@Override
public String fixImports(String input) {
try {
return runFormatterCommand(input);
return runFormatterCommand(input, /* fixImportsOnly= */ true);
} catch (IOException e) {
throw new UncheckedIOException("Error running the native image command", e);
}
}

private String runFormatterCommand(String input) throws IOException {
private String runFormatterCommand(String input, boolean fixImportsOnly) throws IOException {
FormatterNativeImageArgs command = new FormatterNativeImageArgs(
nativeImagePath, /* outputReplacements= */ false, /* characterRanges= */ List.of());
nativeImagePath, fixImportsOnly, /* outputReplacements= */ false, /* characterRanges= */ List.of());
return FormatterCommandRunner.runWithStdin(
command.toArgs(), input, Optional.ofNullable(nativeImagePath.getParent()))
.orElse(input);
}

record FormatterNativeImageArgs(Path nativeImagePath, boolean outputReplacements, List<String> characterRanges) {
record FormatterNativeImageArgs(
Path nativeImagePath, boolean fixImportsOnly, boolean outputReplacements, List<String> characterRanges) {

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

if (fixImportsOnly) {
args.add("--fix-imports-only");
}
if (!characterRanges.isEmpty()) {
args.add("--character-ranges");
args.add(Joiner.on(',').join(characterRanges));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ void can_format_full_file(FormatterService formatterService) throws FormatterExc
assertThat(formatted).isEqualTo(expectedOutput);
}

@ParameterizedTest
@MethodSource("getFormatters")
void fixes_only_the_imports(FormatterService formatterService) throws FormatterException {
String input = getTestResourceContent("fix-imports.input");
String expectedOutput = getTestResourceContent("fix-imports.output");

assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput);
}

@ParameterizedTest
@MethodSource("getFormatters")
void can_format_large_input_file(FormatterService formatterService) throws FormatterException {
Expand Down Expand Up @@ -147,7 +156,7 @@ private String getTestResourceContent(String resourceName) {
}
}

private static List<Path> getClasspath() {
static List<Path> getClasspath() {
String classpath = System.getProperty("java.class.path");
return Splitter.on(File.pathSeparatorChar)
.trimResults()
Expand All @@ -157,7 +166,7 @@ private static List<Path> getClasspath() {
.toList();
}

private static Path javaBinPath() {
static Path javaBinPath() {
String javaHome = Preconditions.checkNotNull(System.getProperty("java.home"), "java.home property not set");
return Path.of(javaHome).resolve("bin").resolve("java" + (isWindows() ? ".exe" : ""));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.javaformat.bootstrap;

import static org.assertj.core.api.Assertions.assertThat;

import com.palantir.javaformat.java.FormatterException;
import com.palantir.javaformat.java.FormatterService;
import com.palantir.javaformat.java.FormatterServiceImpl;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

/**
* The services that need no native binary: the one in this JVM and the one that starts a new JVM. Unlike
* {@link FormatterServicesTest}, this runs in every build.
*/
final class JvmFormatterServicesTest {

@ParameterizedTest
@MethodSource("getFormatters")
void fixes_only_the_imports(FormatterService formatterService) throws FormatterException {
String input = getTestResourceContent("fix-imports.input");
String expectedOutput = getTestResourceContent("fix-imports.output");

assertThat(formatterService.fixImports(input)).isEqualTo(expectedOutput);
}

private static Stream<FormatterService> getFormatters() {
return Stream.of(
new FormatterServiceImpl(),
new BootstrappingFormatterService(
FormatterServicesTest.javaBinPath(),
Runtime.version().feature(),
FormatterServicesTest.getClasspath()));
}

private String getTestResourceContent(String resourceName) {
try {
URI resource = getClass().getClassLoader().getResource(resourceName).toURI();
return Files.readString(Path.of(resource));
} catch (Exception e) {
throw new RuntimeException("Error reading resource: " + resourceName, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package p;
import java.util.Map;
import java.io.File;
import java.util.List;
class C { List<String> l; Map<String,String> m; }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package p;
import java.util.List;
import java.util.Map;

class C { List<String> l; Map<String,String> m; }
Loading