Skip to content

Commit

Permalink
Write value classes when running on Valhalla (#283)
Browse files Browse the repository at this point in the history
* write value classes

* Add missing whitespace for enum generated code

---------

Co-authored-by: Rob Bygrave <[email protected]>
  • Loading branch information
SentryMan and rbygrave authored Aug 5, 2024
1 parent be78dd3 commit c129c6a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> entries) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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 {
Expand Down

0 comments on commit c129c6a

Please sign in to comment.