From c4369b73d3e63e3da6d768bab1fb0549e8c0e465 Mon Sep 17 00:00:00 2001 From: Alexander Schwartz Date: Thu, 5 Feb 2026 23:42:56 +0100 Subject: [PATCH 1/3] 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 --- .../io/vertx/codegen/processor/Processor.java | 16 ++++++++++++++++ 1 file changed, 16 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..8cba4c36 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 @@ -281,6 +281,7 @@ public boolean process(Set annotations, RoundEnvironment generatedClasses.values().forEach(generated -> { try { String content = generated.generate(); + content = convertToLocalLineSeparator(content); if (content.length() > 0) { JavaFileObject target = processingEnv.getFiler().createSourceFile(generated.uri); try (Writer writer = target.openWriter()) { @@ -301,6 +302,7 @@ public boolean process(Set annotations, RoundEnvironment for (GeneratedFile generated : generatedResources.values()) { try { String content = generated.generate(); + content = convertToLocalLineSeparator(content); if (generated.uri.startsWith("/")) { File f = new File(generated.uri); f.getParentFile().mkdirs(); @@ -339,6 +341,20 @@ public boolean process(Set annotations, RoundEnvironment return true; } + /** + * Convert the line endings to the local OS' line endings. + *

+ * This will allow on Windows systems for Git to check in and check out the contents by default with OS specific CRLF, + * and also any re-generation of sources will use CRLF in those line endings. + */ + private static String convertToLocalLineSeparator(String content) { + String lineSeparator = System.lineSeparator(); + if (!lineSeparator.equals("\n")) { + content = content.replaceAll("\n", lineSeparator); + } + return content; + } + private void reportGenException(GenException e) { String name = e.element.toString(); if (e.element.getKind() == ElementKind.METHOD) { From eff34d46732148227a1e9aa8ef6bbb8776931a00 Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Mon, 30 Nov 2026 16:38:18 +0100 Subject: [PATCH 2/3] 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. --- .../io/vertx/codegen/processor/Processor.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) 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 8cba4c36..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,7 +289,7 @@ public boolean process(Set annotations, RoundEnvironment generatedClasses.values().forEach(generated -> { try { String content = generated.generate(); - content = convertToLocalLineSeparator(content); + content = CONVERT_TO_LOCAL_LINE_SEPARATOR.apply(content); if (content.length() > 0) { JavaFileObject target = processingEnv.getFiler().createSourceFile(generated.uri); try (Writer writer = target.openWriter()) { @@ -302,7 +310,7 @@ public boolean process(Set annotations, RoundEnvironment for (GeneratedFile generated : generatedResources.values()) { try { String content = generated.generate(); - content = convertToLocalLineSeparator(content); + content = CONVERT_TO_LOCAL_LINE_SEPARATOR.apply(content); if (generated.uri.startsWith("/")) { File f = new File(generated.uri); f.getParentFile().mkdirs(); @@ -341,18 +349,11 @@ public boolean process(Set annotations, RoundEnvironment return true; } - /** - * Convert the line endings to the local OS' line endings. - *

- * This will allow on Windows systems for Git to check in and check out the contents by default with OS specific CRLF, - * and also any re-generation of sources will use CRLF in those line endings. - */ - private static String convertToLocalLineSeparator(String content) { - String lineSeparator = System.lineSeparator(); - if (!lineSeparator.equals("\n")) { - content = content.replaceAll("\n", lineSeparator); + private static UnaryOperator localLineSeparatorConverter() { + if ("\n".equals(System.lineSeparator())) { + return UnaryOperator.identity(); } - return content; + return content -> content.replaceAll("\n", System.lineSeparator()); } private void reportGenException(GenException e) { From dfbaf2b61bf42beb03d32055e87f274806e54bdb Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Mon, 30 Nov 2026 17:30:27 +0100 Subject: [PATCH 3/3] 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. --- .../codegen/generator/CodeGeneratorTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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