From fd76e92a005173e037b699bfa06667ad3513e346 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 11 Dec 2024 15:04:05 +0100 Subject: [PATCH] [jOOQ/jOOQ#16379] Compilation error in generated Indexes.java when index references embeddables that replace their underlying fields --- .../codegen/AbstractGeneratorStrategy.java | 12 ++++++++++ .../java/org/jooq/codegen/GenerationUtil.java | 18 ++++++++++++++ .../java/org/jooq/codegen/JavaGenerator.java | 24 ++++--------------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/AbstractGeneratorStrategy.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/AbstractGeneratorStrategy.java index 66d2cd982c..6559de4056 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/AbstractGeneratorStrategy.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/AbstractGeneratorStrategy.java @@ -46,6 +46,7 @@ import java.util.List; import java.util.Objects; import static java.util.stream.Collectors.toList; +import static org.jooq.codegen.GenerationUtil.escapeString0; import static org.jooq.codegen.Language.KOTLIN; /** @@ -198,6 +199,17 @@ public abstract class AbstractGeneratorStrategy implements GeneratorStrategy { } sb.append("."); + + + + + + + + + + + sb.append(getJavaIdentifier(definition)); return sb.toString(); diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/GenerationUtil.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/GenerationUtil.java index 32ff254ec0..045ac82793 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/GenerationUtil.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/GenerationUtil.java @@ -506,6 +506,24 @@ class GenerationUtil { return "_" + Integer.toHexString(c); } + static String escapeString0(Language language, String string) { + if (string == null) + return null; + + // [#3450] Escape also the escape sequence, among other things that break Java strings. + String result = string.replace("\\", "\\\\") + .replace("\"", "\\\"") + .replace("\n", "\\n") + .replace("\r", "\\r"); + + // [#10869] Prevent string interpolation in Kotlin + if (language.isKotlin()) + result = result.replace("$", "\\$"); + + return result; + } + + /** * Take a qualified Java type and make it a simple type * diff --git a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java index 2feea65f74..46c6b290f1 100644 --- a/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java +++ b/jOOQ-codegen/src/main/java/org/jooq/codegen/JavaGenerator.java @@ -54,6 +54,7 @@ import static org.jooq.SQLDialect.MYSQL; import static org.jooq.SQLDialect.POSTGRES; import static org.jooq.SQLDialect.YUGABYTEDB; import static org.jooq.SortOrder.DESC; +import static org.jooq.codegen.GenerationUtil.escapeString0; import static org.jooq.codegen.GeneratorStrategy.Mode.POJO; import static org.jooq.codegen.GeneratorStrategy.Mode.RECORD; import static org.jooq.codegen.Language.JAVA; @@ -1329,7 +1330,7 @@ public class JavaGenerator extends AbstractGenerator { for (IndexColumnDefinition column : index.getIndexColumns()) { orderFields.append(sortFieldSeparator); - orderFields.append(out.ref(getStrategy().getFullJavaIdentifier(column.getColumn()), colRefSegments(null))); + orderFields.append(out.ref(getStrategy().getFullJavaIdentifier(column.getColumn()), colRefSegments(column))); orderFields.append(column.getSortOrder() == DESC ? ".desc()" : ""); sortFieldSeparator = ", "; @@ -8993,7 +8994,7 @@ public class JavaGenerator extends AbstractGenerator { // [#10007] [#10318] Very long strings cannot be handled by the javac compiler. int max = 16384; if (string.length() <= max) - return escapeString0(string); + return escapeString0(language, string); StringBuilder sb = new StringBuilder("\" + \""); for (int i = 0; i < string.length(); i += max) { @@ -9001,29 +9002,12 @@ public class JavaGenerator extends AbstractGenerator { sb.append("\".toString() + \""); // [#16662] Delay escaping the string for individual split parts to avoid edge cases - sb.append(escapeString0(string.substring(i, Math.min(i + max, string.length())))); + sb.append(escapeString0(language, string.substring(i, Math.min(i + max, string.length())))); } return sb.append("\".toString() + \"").toString(); } - private String escapeString0(String string) { - if (string == null) - return null; - - // [#3450] Escape also the escape sequence, among other things that break Java strings. - String result = string.replace("\\", "\\\\") - .replace("\"", "\\\"") - .replace("\n", "\\n") - .replace("\r", "\\r"); - - // [#10869] Prevent string interpolation in Kotlin - if (kotlin) - result = result.replace("$", "\\$"); - - return result; - } - /** * Subclasses may override this method to provide table class footer code. */