com.dslplatform.json.processor.EnumTemplate Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dsl-json-java8 Show documentation
Show all versions of dsl-json-java8 Show documentation
DSL Platform compatible Java JSON library (https://dsl-platform.com)
The newest version!
package com.dslplatform.json.processor;
import com.dslplatform.json.CompiledJson;
import com.dslplatform.json.Nullable;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;
class EnumTemplate {
private final Writer code;
private final Context context;
EnumTemplate(Context context) {
this.code = context.code;
this.context = context;
}
private static boolean isAllSimple(StructInfo si) {
for (String c : si.constants) {
if (!c.matches("\\w+")) return false;
}
return true;
}
void writeName(Writer code, StructInfo target, String readValue, String writerName) throws IOException {
writeName(code, target, readValue, writerName, true);
}
private void writeName(Writer code, StructInfo target, String readValue, String writerName, boolean external) throws IOException {
if (target.enumConstantNameSource != null) {
String constantNameType = extractReturnType(target.enumConstantNameSource);
StructInfo info = context.structs.get(constantNameType);
OptimizedConverter converter = context.inlinedConverters.get(constantNameType);
String value = readValue + "." + target.enumConstantNameSource;
if (info != null && info.converter != null) {
info.converter.write(code);
code.append(".write(writer, ").append(value).append(");\n");
} else if (converter != null) {
code.append(converter.nonNullableEncoder("writer", value)).append(";\n");
} else {
code.append(writerName).append(".write(writer, ").append(external ? readValue : value).append(");\n");
}
} else if (isAllSimple(target)) {
code.append("{ writer.writeByte((byte)'\"'); writer.writeAscii(").append(readValue).append(".name()); writer.writeByte((byte)'\"'); }\n");
} else {
code.append("writer.writeString(value.name());\n");
}
}
boolean isStatic(final StructInfo si) {
if (si.enumConstantNameSource == null) return true;
String constantNameType = extractReturnType(si.enumConstantNameSource);
if (constantNameType == null) return true;
StructInfo info = context.structs.get(constantNameType);
return info != null && info.converter != null
|| context.inlinedConverters.get(constantNameType) != null;
}
void create(final StructInfo si, final String className) throws IOException {
code.append("\tpublic final static class EnumConverter implements com.dslplatform.json.JsonWriter.WriteObject<");
code.append(className);
code.append(">, com.dslplatform.json.JsonReader.ReadObject<").append(className).append("> {\n");
String constantNameType = extractReturnType(si.enumConstantNameSource);
StructInfo info = constantNameType != null ? context.structs.get(constantNameType) : null;
OptimizedConverter optimizedConverter = constantNameType != null ? context.inlinedConverters.get(constantNameType) : null;
if (constantNameType != null) {
code.append("\t\tprivate static final java.util.Map<").append(constantNameType).append(", ").append(className).append("> values;\n");
code.append("\t\tstatic {\n");
code.append("\t\t\tvalues = new java.util.HashMap<").append(constantNameType).append(", ").append(className).append(">();\n");
code.append("\t\t\tfor(").append(className).append(" value : ").append(className).append(".values()) {\n");
code.append("\t\t\t\tvalues.put(value.").append(si.enumConstantNameSource.toString()).append(", value);\n");
code.append("\t\t\t}\n");
code.append("\t\t}\n");
if (optimizedConverter == null && (info == null || info.converter == null)) {
code.append("\t\tprivate final com.dslplatform.json.JsonWriter.WriteObject<").append(constantNameType).append("> valueWriter;\n");
code.append("\t\tprivate final com.dslplatform.json.JsonReader.ReadObject<").append(constantNameType).append("> valueReader;\n");
code.append("\t\tpublic EnumConverter(com.dslplatform.json.DslJson