From c2da73e435b64dfb451bd6bc7c848b35169d507a Mon Sep 17 00:00:00 2001 From: Josiah Noel <32279667+SentryMan@users.noreply.github.com> Date: Mon, 5 Aug 2024 06:56:01 -0400 Subject: [PATCH] Generate Value Types when on Valhalla (#249) * generate value types * Format, remove unused method --------- Co-authored-by: Rob Bygrave --- .../generator/SimpleAdapterWriter.java | 2 +- .../generator/SimpleComponentWriter.java | 2 +- .../generator/SimpleParamBeanWriter.java | 36 ++++++++----------- .../io/avaje/validation/generator/Util.java | 10 ++++++ 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleAdapterWriter.java b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleAdapterWriter.java index c9b114ec..a898d23b 100644 --- a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleAdapterWriter.java +++ b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleAdapterWriter.java @@ -92,7 +92,7 @@ private void writeClassStart() { writer.append("@ConstraintAdapter(%s.class)", beanReader.contraintTarget()).eol(); } - writer.append("public final class %sValidationAdapter implements ValidationAdapter<%s> ", adapterShortName, beanReader.shortName()); + writer.append("public final %sclass %sValidationAdapter implements ValidationAdapter<%s> ", Util.valhalla(), adapterShortName, beanReader.shortName()); writer.append("{").eol().eol(); } diff --git a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java index 14e380e2..b167c3fd 100644 --- a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java +++ b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleComponentWriter.java @@ -117,7 +117,7 @@ private void writeClassStart() { writeMetaDataEntry(all); writer.append("})").eol(); - writer.append("public class %s implements GeneratedComponent {", shortName).eol().eol(); + writer.append("public %sclass %s implements GeneratedComponent {", Util.valhalla(), shortName).eol().eol(); } private void writeMetaDataEntry(List entries) { diff --git a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleParamBeanWriter.java b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleParamBeanWriter.java index 6a5f628f..966cc8e5 100644 --- a/validator-generator/src/main/java/io/avaje/validation/generator/SimpleParamBeanWriter.java +++ b/validator-generator/src/main/java/io/avaje/validation/generator/SimpleParamBeanWriter.java @@ -19,24 +19,18 @@ final class SimpleParamBeanWriter { SimpleParamBeanWriter(ValidMethodReader beanReader) { this.beanReader = beanReader; final var method = beanReader.getBeanType(); - this.adapterPackage = ProcessorUtils.packageOf(method.getEnclosingElement().asType().toString()); - adapterFullName = - adapterPackage - + "." - + method - .getSimpleName() - .toString() - .transform(str -> str.substring(0, 1).toUpperCase() + str.substring(1)) - + "ParamProvider"; + this.adapterFullName = adapterPackage + + "." + + method + .getSimpleName() + .toString() + .transform(str -> str.substring(0, 1).toUpperCase() + str.substring(1)) + + "ParamProvider"; this.adapterShortName = Util.shortName(adapterFullName); } - String fullName() { - return adapterFullName; - } - private Writer createFileWriter() throws IOException { final JavaFileObject jfo = createSourceFile(adapterFullName); return jfo.openWriter(); @@ -62,14 +56,14 @@ private void writePackage() { private void writeClassStart() { writer - .append( - """ - @Generated("avaje-validator-generator") - @Named - @%s - public final class %s implements MethodAdapterProvider {""", - Util.shortName(diAnnotation()), adapterShortName) - .eol(); + .append( + """ + @Generated("avaje-validator-generator") + @Named + @%s + public final %sclass %s implements MethodAdapterProvider {""", + Util.shortName(diAnnotation()), Util.valhalla(), adapterShortName) + .eol(); } private void writeMethods() { diff --git a/validator-generator/src/main/java/io/avaje/validation/generator/Util.java b/validator-generator/src/main/java/io/avaje/validation/generator/Util.java index c93262a2..22208488 100644 --- a/validator-generator/src/main/java/io/avaje/validation/generator/Util.java +++ b/validator-generator/src/main/java/io/avaje/validation/generator/Util.java @@ -158,4 +158,14 @@ static boolean isPublic(Element element) { } return !ProcessingContext.isImported(element); } + + static String valhalla() { + try { + if (Modifier.valueOf("VALUE") != null && APContext.previewEnabled()) return "value "; + } catch (IllegalArgumentException e) { + // no valhalla + } + return ""; + } + }