From aeecec5e309eb19d32ee56f2e20ad5f2339b129b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gild=C3=A9ric=20DERUETTE?= Date: Thu, 2 May 2024 19:03:11 +0200 Subject: [PATCH] [JPA] Dans les fieldsEnum, exposer le getter directement Fixes #352 --- TopModel.Generator.Jpa/JpaModelGenerator.cs | 24 ++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/TopModel.Generator.Jpa/JpaModelGenerator.cs b/TopModel.Generator.Jpa/JpaModelGenerator.cs index 89703e24..cff11b92 100644 --- a/TopModel.Generator.Jpa/JpaModelGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelGenerator.cs @@ -394,27 +394,41 @@ private void WriteFieldsEnum(JavaWriter fw, Class classe, string tag) name = prop.Name.ToConstantCase(); } + var propertyName = Config.UseJdbc ? prop.NameCamel : prop.NameByClassCamel; + var getterPrefix = Config.GetType(prop, Classes, true) == "boolean" ? "is" : "get"; var javaType = Config.GetType(prop, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc); - javaType = javaType.Split("<").First(); - return $" {name}({javaType}.class)"; + javaType = javaType.Split("<")[0]; + return $" {name}({javaType}.class, {classe.NamePascal}::{propertyName.ToFirstUpper().WithPrefix(getterPrefix)})"; }); fw.WriteLine(string.Join(", //\n", props) + ";"); - fw.WriteLine(); fw.WriteLine(2, "private Class type;"); + + fw.WriteLine(); + + fw.AddImport("java.util.function.Function"); + fw.WriteLine(2, $@"private Function<{classe.NamePascal}, ?> getter;"); + fw.WriteLine(); - fw.WriteLine(2, "private Fields(Class type) {"); + + fw.WriteLine(2, $@"private Fields(Class type, Function<{classe.NamePascal}, T> getter) {{"); fw.WriteLine(3, "this.type = type;"); + fw.WriteLine(3, "this.getter = getter;"); fw.WriteLine(2, "}"); fw.WriteLine(); - fw.WriteLine(2, "public Class getType() {"); fw.WriteLine(3, "return this.type;"); fw.WriteLine(2, "}"); + fw.WriteLine(); + + fw.WriteLine(2, @$"public Function<{classe.NamePascal}, ?> getter() {{"); + fw.WriteLine(3, "return this.getter;"); + fw.WriteLine(2, "}"); + fw.WriteLine(1, "}"); }