From a3421aca9af4d7119b6500caf52d55304a475c56 Mon Sep 17 00:00:00 2001 From: Sinri Edogawa Date: Mon, 6 May 2024 15:31:35 +0800 Subject: [PATCH] 3.2.6 Fix the Table Row Class Generate Source Code, to avoid non-null check in nullable declared field. Signed-off-by: Sinri Edogawa --- pom.xml | 4 +-- .../keel/mysql/dev/TableRowClassField.java | 28 +++++++++++-------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index 3bf52a42..f486059e 100644 --- a/pom.xml +++ b/pom.xml @@ -6,8 +6,8 @@ io.github.sinri Keel - 3.2.6-SNAPSHOT - + + 3.2.6 Keel diff --git a/src/main/java/io/github/sinri/keel/mysql/dev/TableRowClassField.java b/src/main/java/io/github/sinri/keel/mysql/dev/TableRowClassField.java index 0e222695..73bf11f1 100644 --- a/src/main/java/io/github/sinri/keel/mysql/dev/TableRowClassField.java +++ b/src/main/java/io/github/sinri/keel/mysql/dev/TableRowClassField.java @@ -163,12 +163,14 @@ public String build() { } code .append("\tpublic ").append(looseEnum.looseEnumName()).append(" ").append(getter).append("() {\n") - .append("\t\treturn ").append(looseEnum.looseEnumName()).append(".valueOf(\n") - .append("\t\t\t") - .append(nullable ? "" : "Objects.requireNonNull(") - .append(readMethod).append("(\"").append(field).append("\")") - .append(nullable ? "" : ")").append("\n") - .append("\t\t);\n") + .append("\t\t@Nullable String enumExpression=").append(readMethod).append("(\"").append(field).append("\");\n"); + if (nullable) { + code.append("\t\tif (enumExpression==null) return null;\n"); + } else { + code.append("\t\tObjects.requireNonNull(enumExpression,\"The Enum Field `").append(field).append("` should not be null!\");\n"); + } + code + .append("\t\treturn ").append(looseEnum.looseEnumName()).append(".valueOf(enumExpression);\n") .append("\t}\n"); } else if (strictEnum != null) { code.append("\t/*\n") @@ -185,12 +187,14 @@ public String build() { } code .append("\tpublic ").append(strictEnum.fullEnumRef()).append(" ").append(getter).append("() {\n") - .append("\t\treturn ").append(strictEnum.fullEnumRef()).append(".valueOf(\n") - .append("\t\t\t") - .append(nullable ? "" : "Objects.requireNonNull(") - .append(readMethod).append("(\"").append(field).append("\")") - .append(nullable ? "" : ")").append("\n") - .append("\t\t);\n") + .append("\t\t@Nullable String enumExpression=").append(readMethod).append("(\"").append(field).append("\");\n"); + if (nullable) { + code.append("\t\tif (enumExpression==null) return null;\n"); + } else { + code.append("\t\tObjects.requireNonNull(enumExpression,\"The Enum Field `").append(field).append("` should not be null!\");\n"); + } + code + .append("\t\treturn ").append(strictEnum.fullEnumRef()).append(".valueOf(enumExpression);\n") .append("\t}\n"); } else { code.append("\t/*\n");