Parse enums
This commit is contained in:
parent
fe2b1fbe24
commit
ad238ac2b3
|
@ -13,6 +13,7 @@ The goal of this AP is to
|
|||
- Arrays
|
||||
- Collections (Sets, Lists, Queues, Deques)
|
||||
- java.util.Date as iso8601
|
||||
- Enums
|
||||
|
||||
## Used properties
|
||||
Use `@GPrefer` to choose one construction method if multiple are available
|
||||
|
@ -30,7 +31,6 @@ Use `@GPrefer` to choose one construction method if multiple are available
|
|||
|
||||
## TODO
|
||||
- Maps with string, primitive or enum keys
|
||||
- Enums
|
||||
- Support for nested types from libraries
|
||||
- Static classes (for configs)
|
||||
|
||||
|
|
|
@ -1,21 +1,7 @@
|
|||
package io.gitlab.jfronny.gson.compile.core;
|
||||
|
||||
import io.gitlab.jfronny.commons.serialize.gson.api.v1.GsonHolder;
|
||||
import io.gitlab.jfronny.gson.JsonSyntaxException;
|
||||
import io.gitlab.jfronny.gson.internal.bind.util.ISO8601Utils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Date;
|
||||
|
||||
public class CCore {
|
||||
public static final GsonHolder HOLDER = new GsonHolder();
|
||||
|
||||
public static Date parseDate(String date) {
|
||||
try {
|
||||
return ISO8601Utils.parse(date, new ParsePosition(0));
|
||||
} catch (ParseException e) {
|
||||
throw new JsonSyntaxException("Failed parsing '" + date + "' as Date", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -33,6 +33,8 @@ public class Main {
|
|||
|
||||
public Date date;
|
||||
|
||||
public EeE eEe;
|
||||
|
||||
public void setJoe(String joe) {
|
||||
}
|
||||
|
||||
|
@ -81,4 +83,8 @@ public class Main {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum EeE {
|
||||
Yes, Yay, Aaee;
|
||||
}
|
||||
}
|
|
@ -10,7 +10,6 @@ import javax.lang.model.element.Element;
|
|||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.lang.model.util.Types;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public abstract class Adapter<T extends Adapter<T>.Hydrated> {
|
||||
protected Messager message;
|
||||
|
|
|
@ -20,6 +20,7 @@ public class Adapters {
|
|||
new PrimitiveAdapter(),
|
||||
new StringAdapter(),
|
||||
new DateAdapter(),
|
||||
new EnumAdapter(),
|
||||
new ArrayAdapter(),
|
||||
new CollectionAdapter(),
|
||||
new OtherSerializableAdapter(),
|
||||
|
|
|
@ -1,8 +1,13 @@
|
|||
package io.gitlab.jfronny.gson.compile.processor.adapter.impl;
|
||||
|
||||
import com.squareup.javapoet.CodeBlock;
|
||||
import com.squareup.javapoet.MethodSpec;
|
||||
import io.gitlab.jfronny.gson.compile.processor.Cl;
|
||||
import io.gitlab.jfronny.gson.compile.processor.adapter.Adapter;
|
||||
|
||||
import javax.lang.model.element.Modifier;
|
||||
import java.text.ParseException;
|
||||
import java.text.ParsePosition;
|
||||
import java.util.Date;
|
||||
|
||||
public class DateAdapter extends Adapter<DateAdapter.Hydrated> {
|
||||
|
@ -26,7 +31,32 @@ public class DateAdapter extends Adapter<DateAdapter.Hydrated> {
|
|||
|
||||
@Override
|
||||
public void generateRead() {
|
||||
code.add("$T.parseDate(reader.nextString())", Cl.CCORE);
|
||||
boolean found = false;
|
||||
for (MethodSpec spec : klazz.methodSpecs) {
|
||||
if (spec.name.equals("parseDate")) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
kode.beginControlFlow("try")
|
||||
.addStatement("return $T.parse(date, new $T(0))", Cl.GISO8601UTILS, ParsePosition.class)
|
||||
.nextControlFlow("catch ($T e)", ParseException.class)
|
||||
.addStatement("throw new JsonSyntaxException(\"Failed Parsing '\" + date + \"' as Date\", e)")
|
||||
.endControlFlow();
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder("parseDate")
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(Date.class)
|
||||
.addParameter(String.class, "date")
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
|
||||
code.add("parseDate(reader.nextString())");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package io.gitlab.jfronny.gson.compile.processor.adapter.impl;
|
||||
|
||||
import com.squareup.javapoet.*;
|
||||
import io.gitlab.jfronny.gson.compile.processor.TypeHelper;
|
||||
import io.gitlab.jfronny.gson.compile.processor.adapter.Adapter;
|
||||
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.Modifier;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
|
||||
public class EnumAdapter extends Adapter<EnumAdapter.Hydrated> {
|
||||
@Override
|
||||
public Hydrated instantiate() {
|
||||
return new Hydrated();
|
||||
}
|
||||
|
||||
public class Hydrated extends Adapter<Hydrated>.Hydrated {
|
||||
private DeclaredType tel;
|
||||
|
||||
@Override
|
||||
protected void afterHydrate() {
|
||||
super.afterHydrate();
|
||||
tel = null;
|
||||
DeclaredType declared = TypeHelper.asDeclaredType(type);
|
||||
if (declared == null) return;
|
||||
if (declared.asElement().getKind() != ElementKind.ENUM) return;
|
||||
tel = declared;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean applies() {
|
||||
return tel != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateWrite(Runnable writeGet) {
|
||||
code.add("writer.value(");
|
||||
writeGet.run();
|
||||
code.add(".name());\n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateRead() {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
kode.beginControlFlow("for ($1T t : $1T.values())", tel)
|
||||
.addStatement("if (t.name().equals(value)) return t")
|
||||
.endControlFlow()
|
||||
.addStatement("return null");
|
||||
|
||||
String methodName = "read$" + name;
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(TypeName.get(tel))
|
||||
.addParameter(String.class, "value")
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
code.add("$N(reader.nextString())", methodName);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue