From 157b7723fbbf0f593786702453de5653c1a8dd2f Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Mon, 30 Nov 2026 17:35:18 +0100 Subject: [PATCH] Respect local OS line endings when generating files (#406) * Respect local OS line endings when generating files This avoids listing all files changes in a Git diff and IDEs. Closes eclipse-vertx/vert.x#5849 * Read the line separator at class load and use a constant converter Instead of reading the system line separator on each pass over the generated files, it is read once when the Processor class is loaded. The converter is a constant that is a no-op when the local line separator is LF, and otherwise replaces LF with the local separator. * Verify generated files use the system line separator Compile a model with the testgen2 generator and check the generated Java source contains only the system line separator. The 5.x CI matrix already runs the tests on Windows (JDK 25), where the LF to CRLF conversion is exercised. --------- Co-authored-by: Thomas Segismont (cherry picked from commit e1f5ce51e7dcaf7cac2ca7dff969f9ba553ace29) Signed-off-by: Thomas Segismont --- .../io/vertx/codegen/processor/Processor.java | 17 +++++++++++++++++ .../codegen/generator/CodeGeneratorTest.java | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/Processor.java b/vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/Processor.java index 7d00b38e..28a90d77 100644 --- a/vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/Processor.java +++ b/vertx-codegen-processor/src/main/java/io/vertx/codegen/processor/Processor.java @@ -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; @@ -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 CONVERT_TO_LOCAL_LINE_SEPARATOR = localLineSeparatorConverter(); + private File outputDirectory; private List> codeGenerators; private Map generatedResources = new HashMap<>(); @@ -281,6 +289,7 @@ public boolean process(Set 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()) { @@ -301,6 +310,7 @@ public boolean process(Set 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(); @@ -339,6 +349,13 @@ public boolean process(Set annotations, RoundEnvironment return true; } + private static UnaryOperator 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) { diff --git a/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/generator/CodeGeneratorTest.java b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/generator/CodeGeneratorTest.java index b552e6be..5d62e1b9 100644 --- a/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/generator/CodeGeneratorTest.java +++ b/vertx-codegen-processor/src/test/java/io/vertx/test/codegen/generator/CodeGeneratorTest.java @@ -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; @@ -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