diff --git a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleAdapterWriter.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleAdapterWriter.java index 0d53cd51..73b4fcac 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleAdapterWriter.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleAdapterWriter.java @@ -112,7 +112,7 @@ private void writeClassStart() { writer.append("@SuppressWarnings({\"unchecked\", \"rawtypes\"})").eol(); } writer.append("@Generated(\"io.avaje.jsonb.generator\")").eol(); - writer.append("public final class %sJsonAdapter implements JsonAdapter<%s>", adapterShortName, beanReader.shortName()); + writer.append("public final %sclass %sJsonAdapter implements JsonAdapter<%s>", Util.valhalla(), adapterShortName, beanReader.shortName()); if (beanReader.supportsViewBuilder()) { writer.append(", ViewBuilderAware "); } diff --git a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleComponentWriter.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleComponentWriter.java index 5953e36a..84f7ada3 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleComponentWriter.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/SimpleComponentWriter.java @@ -99,7 +99,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/jsonb-generator/src/main/java/io/avaje/jsonb/generator/Util.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/Util.java index 71901e5b..47e25c08 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/Util.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/Util.java @@ -232,4 +232,13 @@ static boolean isPublic(Element element) { } return !isImported; } + + static String valhalla() { + try { + if (Modifier.valueOf("VALUE") != null && APContext.previewEnabled()) return "value "; + } catch (IllegalArgumentException e) { + // no valhalla + } + return ""; + } } diff --git a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ValueReader.java b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ValueReader.java index 64fd9a0c..22c00476 100644 --- a/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ValueReader.java +++ b/jsonb-generator/src/main/java/io/avaje/jsonb/generator/ValueReader.java @@ -106,20 +106,20 @@ public void writeFields(Append writer) { writer.append(" private static final Map<%s, %s> toValue = new EnumMap<>(%s.class);", shortName, returnTypeStr, shortName).eol(); writer.append(" private static final Map<%s, %s> toEnum = new HashMap<>();", returnTypeStr, shortName).eol(); } - writer.append(" private final %s adapter;", adapterShortType).eol(); - writer.eol(); + writer.append(" private final %s adapter;", adapterShortType).eol().eol(); } @Override public void writeConstructor(Append writer) { writer.append(" this.adapter = jsonb.adapter(%s);", genericType.asTypeDeclaration().replace("? extends ", "")).eol(); if (isEnum) { - writer.append(" if(!toValue.isEmpty()) return;").eol(); - writer.append(" for(final var enumConst : %s.values()) {", shortName).eol(); - writer.append(" var val = enumConst.%s();", method.getSimpleName()).eol(); - writer.append(" toValue.put(enumConst, val);").eol(); - writer.append(" if(toEnum.containsKey(val)) throw new IllegalArgumentException(\"Duplicate value \"+ val + \" from enum method %s. @Json.Value methods must return unique values\");",method.getSimpleName()).eol(); - writer.append(" toEnum.put(val, enumConst);").eol(); + writer.append(" if (toValue.isEmpty()) {").eol(); + writer.append(" for (final var enumConst : %s.values()) {", shortName).eol(); + writer.append(" var val = enumConst.%s();", method.getSimpleName()).eol(); + writer.append(" toValue.put(enumConst, val);").eol(); + writer.append(" if (toEnum.containsKey(val)) throw new IllegalArgumentException(\"Duplicate value \" + val + \" from enum method %s. @Json.Value methods must return unique values\");",method.getSimpleName()).eol(); + writer.append(" toEnum.put(val, enumConst);").eol(); + writer.append(" }").eol(); writer.append(" }").eol(); } } @@ -146,10 +146,9 @@ public void writeFromJson(Append writer) { if (!isEnum) { var constructMethod = - constructor.getKind() == ElementKind.CONSTRUCTOR - ? "new " + shortName - : shortName + "." + constructor.getSimpleName(); - + constructor.getKind() == ElementKind.CONSTRUCTOR + ? "new " + shortName + : shortName + "." + constructor.getSimpleName(); writer.append(" return %s(adapter.fromJson(reader));", constructMethod).eol(); } else {