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 @@ -23,6 +23,7 @@
import java.nio.file.Path;
import java.util.*;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -35,6 +36,13 @@
public class Processor extends AbstractProcessor {

private static final String JSON_MAPPERS_PROPERTIES_PATH = "META-INF/vertx/json-mappers.properties";

/**
* Converts the line endings of generated files to the local OS' line endings, so regenerated
* files don't show up as modified in Git diffs and IDEs. No-op when the separator is LF.
*/
private static final UnaryOperator<String> CONVERT_TO_LOCAL_LINE_SEPARATOR = localLineSeparatorConverter();

private File outputDirectory;
private List<? extends Generator<?>> codeGenerators;
private Map<String, GeneratedFile> generatedResources = new HashMap<>();
Expand Down Expand Up @@ -281,6 +289,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
generatedClasses.values().forEach(generated -> {
try {
String content = generated.generate();
content = CONVERT_TO_LOCAL_LINE_SEPARATOR.apply(content);
if (content.length() > 0) {
JavaFileObject target = processingEnv.getFiler().createSourceFile(generated.uri);
try (Writer writer = target.openWriter()) {
Expand All @@ -301,6 +310,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
for (GeneratedFile generated : generatedResources.values()) {
try {
String content = generated.generate();
content = CONVERT_TO_LOCAL_LINE_SEPARATOR.apply(content);
if (generated.uri.startsWith("/")) {
File f = new File(generated.uri);
f.getParentFile().mkdirs();
Expand Down Expand Up @@ -339,6 +349,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
return true;
}

private static UnaryOperator<String> localLineSeparatorConverter() {
if ("\n".equals(System.lineSeparator())) {
return UnaryOperator.identity();
}
return content -> content.replaceAll("\n", System.lineSeparator());
}

private void reportGenException(GenException e) {
String name = e.element.toString();
if (e.element.getKind() == ElementKind.METHOD) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.time.Instant;
import java.util.*;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -158,6 +159,23 @@ public void testIncrementalClass() throws Exception {
assertEquals(expected, result);
}

@Test
public void testSystemLineSeparator() throws Exception {
Compiler compiler = new Compiler(new Processor());
compiler.setClassOutput(assertMkDirs(new File(testDir, "classes")));
compiler.setSourceOutput(assertMkDirs(new File(testDir, "sources")));
compiler.addOption("-Acodegen.generators=testgen2");
assertTrue(compiler.compile(VertxGenClass1.class));
File f = new File(testDir, "sources/testgen2/incremental_class.java".replace('/', File.separatorChar));
assertTrue(f.isFile());
String content = new String(Files.readAllBytes(f.toPath()));
// Verify the generated file line endings are in line with the system line separator
assertTrue(content.contains(System.lineSeparator()));
String remainder = content.replace(System.lineSeparator(), "");
assertFalse(remainder.contains("\n"));
assertFalse(remainder.contains("\r"));
}

@Test
public void testAbsoluteFilename() throws Exception {
// Does not pass on windows because of drive letter
Expand Down