fix(serialize-generator): more checks to prevent double declarations
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
5a9c3ee58f
commit
78ec439ccb
@ -50,41 +50,43 @@ public class ArrayAdapter extends Adapter<ArrayAdapter.Hydrated> {
|
||||
|
||||
@Override
|
||||
public void generateRead() {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
// Coerce
|
||||
kode.beginControlFlow("if (reader.isLenient() && reader.peek() != $T.BEGIN_ARRAY)", Cl.GSON_TOKEN)
|
||||
.add("return new $T[] { ", componentType);
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(" };\n").endControlFlow();
|
||||
|
||||
kode.addStatement("$T<$T> list = new $T<>()", List.class, componentType, ArrayList.class)
|
||||
.addStatement("reader.beginArray()")
|
||||
.beginControlFlow("while (reader.hasNext())")
|
||||
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
|
||||
.addStatement("reader.nextNull()")
|
||||
.addStatement("list.add(null)")
|
||||
.nextControlFlow("else")
|
||||
.add("list.add(");
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(");\n")
|
||||
.endControlFlow()
|
||||
.endControlFlow()
|
||||
.addStatement("reader.endArray()")
|
||||
.addStatement("return list.toArray($T[]::new)", componentType);
|
||||
|
||||
String methodName = "deserialize$" + name;
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(typeName)
|
||||
.addTypeVariable(TypeVariableName.get("TEx", Exception.class))
|
||||
.addTypeVariable(TypeVariableName.get("Reader", ParameterizedTypeName.get(Cl.SERIALIZE_READER, TypeVariableName.get("TEx"), TypeVariableName.get("Reader"))))
|
||||
.addParameter(TypeVariableName.get("Reader"), "reader")
|
||||
.addException(TypeVariableName.get("TEx"))
|
||||
.addException(Cl.MALFORMED_DATA_EXCEPTION)
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
if (klazz.methodSpecs.stream().noneMatch(s -> s.name.equals(methodName))) {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
// Coerce
|
||||
kode.beginControlFlow("if (reader.isLenient() && reader.peek() != $T.BEGIN_ARRAY)", Cl.GSON_TOKEN)
|
||||
.add("return new $T[] { ", componentType);
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(" };\n").endControlFlow();
|
||||
|
||||
kode.addStatement("$T<$T> list = new $T<>()", List.class, componentType, ArrayList.class)
|
||||
.addStatement("reader.beginArray()")
|
||||
.beginControlFlow("while (reader.hasNext())")
|
||||
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
|
||||
.addStatement("reader.nextNull()")
|
||||
.addStatement("list.add(null)")
|
||||
.nextControlFlow("else")
|
||||
.add("list.add(");
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(");\n")
|
||||
.endControlFlow()
|
||||
.endControlFlow()
|
||||
.addStatement("reader.endArray()")
|
||||
.addStatement("return list.toArray($T[]::new)", componentType);
|
||||
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(typeName)
|
||||
.addTypeVariable(TypeVariableName.get("TEx", Exception.class))
|
||||
.addTypeVariable(TypeVariableName.get("Reader", ParameterizedTypeName.get(Cl.SERIALIZE_READER, TypeVariableName.get("TEx"), TypeVariableName.get("Reader"))))
|
||||
.addParameter(TypeVariableName.get("Reader"), "reader")
|
||||
.addException(TypeVariableName.get("TEx"))
|
||||
.addException(Cl.MALFORMED_DATA_EXCEPTION)
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
code.add("$N(reader)", methodName);
|
||||
}
|
||||
}
|
||||
|
@ -78,41 +78,43 @@ public class CollectionAdapter extends Adapter<CollectionAdapter.Hydrated> {
|
||||
|
||||
@Override
|
||||
public void generateRead() {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
kode.addStatement("$T list = new $T<>()", typeName, implType);
|
||||
// Coerce
|
||||
kode.beginControlFlow("if (reader.isLenient() && reader.peek() != $T.BEGIN_ARRAY)", Cl.GSON_TOKEN)
|
||||
.add("list.add(");
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(");\n").addStatement("return list").endControlFlow();
|
||||
|
||||
kode.addStatement("reader.beginArray()")
|
||||
.beginControlFlow("while (reader.hasNext())")
|
||||
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
|
||||
.addStatement("reader.nextNull()")
|
||||
.addStatement("list.add(null)")
|
||||
.nextControlFlow("else")
|
||||
.add("list.add(");
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(");\n")
|
||||
.endControlFlow()
|
||||
.endControlFlow()
|
||||
.addStatement("reader.endArray()")
|
||||
.addStatement("return list");
|
||||
|
||||
String methodName = "read$" + name;
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(typeName)
|
||||
.addTypeVariable(TypeVariableName.get("TEx", Exception.class))
|
||||
.addTypeVariable(TypeVariableName.get("Reader", ParameterizedTypeName.get(Cl.SERIALIZE_READER, TypeVariableName.get("TEx"), TypeVariableName.get("Reader"))))
|
||||
.addParameter(TypeVariableName.get("Reader"), "reader")
|
||||
.addException(TypeVariableName.get("TEx"))
|
||||
.addException(Cl.MALFORMED_DATA_EXCEPTION)
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
if (klazz.methodSpecs.stream().noneMatch(s -> s.name.equals(methodName))) {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
kode.addStatement("$T list = new $T<>()", typeName, implType);
|
||||
// Coerce
|
||||
kode.beginControlFlow("if (reader.isLenient() && reader.peek() != $T.BEGIN_ARRAY)", Cl.GSON_TOKEN)
|
||||
.add("list.add(");
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(");\n").addStatement("return list").endControlFlow();
|
||||
|
||||
kode.addStatement("reader.beginArray()")
|
||||
.beginControlFlow("while (reader.hasNext())")
|
||||
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
|
||||
.addStatement("reader.nextNull()")
|
||||
.addStatement("list.add(null)")
|
||||
.nextControlFlow("else")
|
||||
.add("list.add(");
|
||||
generateRead(kode, componentType, argName, componentType.getAnnotationMirrors());
|
||||
kode.add(");\n")
|
||||
.endControlFlow()
|
||||
.endControlFlow()
|
||||
.addStatement("reader.endArray()")
|
||||
.addStatement("return list");
|
||||
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(typeName)
|
||||
.addTypeVariable(TypeVariableName.get("TEx", Exception.class))
|
||||
.addTypeVariable(TypeVariableName.get("Reader", ParameterizedTypeName.get(Cl.SERIALIZE_READER, TypeVariableName.get("TEx"), TypeVariableName.get("Reader"))))
|
||||
.addParameter(TypeVariableName.get("Reader"), "reader")
|
||||
.addException(TypeVariableName.get("TEx"))
|
||||
.addException(Cl.MALFORMED_DATA_EXCEPTION)
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
code.add("$N(reader)", methodName);
|
||||
}
|
||||
}
|
||||
|
@ -32,15 +32,7 @@ public class DateAdapter extends Adapter<DateAdapter.Hydrated> {
|
||||
|
||||
@Override
|
||||
public void generateRead() {
|
||||
boolean found = false;
|
||||
for (MethodSpec spec : klazz.methodSpecs) {
|
||||
if (spec.name.equals("parseDate")) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
if (klazz.methodSpecs.stream().noneMatch(s -> s.name.equals("parseDate"))) {
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder("parseDate")
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
|
@ -44,21 +44,23 @@ public class EnumAdapter extends Adapter<EnumAdapter.Hydrated> {
|
||||
@Override
|
||||
public void generateRead() {
|
||||
String methodName = "read$" + name;
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(TypeName.get(tel))
|
||||
.addParameter(String.class, "value")
|
||||
.addCode(
|
||||
CodeBlock.builder()
|
||||
.beginControlFlow("for ($1T t : $1T.values())", tel)
|
||||
.addStatement("if (t.name().equals(value)) return t")
|
||||
.endControlFlow()
|
||||
.addStatement("return null")
|
||||
.build()
|
||||
)
|
||||
.build()
|
||||
);
|
||||
if (klazz.methodSpecs.stream().noneMatch(s -> s.name.equals(methodName))) {
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(TypeName.get(tel))
|
||||
.addParameter(String.class, "value")
|
||||
.addCode(
|
||||
CodeBlock.builder()
|
||||
.beginControlFlow("for ($1T t : $1T.values())", tel)
|
||||
.addStatement("if (t.name().equals(value)) return t")
|
||||
.endControlFlow()
|
||||
.addStatement("return null")
|
||||
.build()
|
||||
)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
code.add("$N(reader.nextString())", methodName);
|
||||
}
|
||||
}
|
||||
|
@ -156,41 +156,43 @@ public class MapAdapter extends Adapter<MapAdapter.Hydrated> {
|
||||
|
||||
@Override
|
||||
public void generateRead() {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
kode.addStatement("$T map = new $T<>()", typeName, implType);
|
||||
|
||||
kode.addStatement("reader.beginObject()")
|
||||
.beginControlFlow("while (reader.hasNext())")
|
||||
.addStatement("String name = reader.nextName()")
|
||||
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
|
||||
.addStatement("reader.nextNull()")
|
||||
.add("map.put(");
|
||||
generateConvertKey(kode);
|
||||
kode.addStatement(", null)")
|
||||
.nextControlFlow("else")
|
||||
.add("map.put(");
|
||||
generateConvertKey(kode);
|
||||
kode.add(", ");
|
||||
generateRead(kode, componentType2, argName, componentType2.getAnnotationMirrors());
|
||||
kode.addStatement(")")
|
||||
.endControlFlow()
|
||||
.endControlFlow()
|
||||
.addStatement("reader.endObject()")
|
||||
.addStatement("return map");
|
||||
|
||||
String methodName = "read$" + name;
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(typeName)
|
||||
.addTypeVariable(TypeVariableName.get("TEx", Exception.class))
|
||||
.addTypeVariable(TypeVariableName.get("Reader", ParameterizedTypeName.get(Cl.SERIALIZE_READER, TypeVariableName.get("TEx"), TypeVariableName.get("Reader"))))
|
||||
.addParameter(TypeVariableName.get("Reader"), "reader")
|
||||
.addException(TypeVariableName.get("TEx"))
|
||||
.addException(Cl.MALFORMED_DATA_EXCEPTION)
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
if (klazz.methodSpecs.stream().noneMatch(s -> s.name.equals(methodName))) {
|
||||
CodeBlock.Builder kode = CodeBlock.builder();
|
||||
kode.addStatement("$T map = new $T<>()", typeName, implType);
|
||||
|
||||
kode.addStatement("reader.beginObject()")
|
||||
.beginControlFlow("while (reader.hasNext())")
|
||||
.addStatement("String name = reader.nextName()")
|
||||
.beginControlFlow("if (reader.peek() == $T.NULL)", Cl.GSON_TOKEN)
|
||||
.addStatement("reader.nextNull()")
|
||||
.add("map.put(");
|
||||
generateConvertKey(kode);
|
||||
kode.addStatement(", null)")
|
||||
.nextControlFlow("else")
|
||||
.add("map.put(");
|
||||
generateConvertKey(kode);
|
||||
kode.add(", ");
|
||||
generateRead(kode, componentType2, argName, componentType2.getAnnotationMirrors());
|
||||
kode.addStatement(")")
|
||||
.endControlFlow()
|
||||
.endControlFlow()
|
||||
.addStatement("reader.endObject()")
|
||||
.addStatement("return map");
|
||||
|
||||
klazz.addMethod(
|
||||
MethodSpec.methodBuilder(methodName)
|
||||
.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
|
||||
.returns(typeName)
|
||||
.addTypeVariable(TypeVariableName.get("TEx", Exception.class))
|
||||
.addTypeVariable(TypeVariableName.get("Reader", ParameterizedTypeName.get(Cl.SERIALIZE_READER, TypeVariableName.get("TEx"), TypeVariableName.get("Reader"))))
|
||||
.addParameter(TypeVariableName.get("Reader"), "reader")
|
||||
.addException(TypeVariableName.get("TEx"))
|
||||
.addException(Cl.MALFORMED_DATA_EXCEPTION)
|
||||
.addCode(kode.build())
|
||||
.build()
|
||||
);
|
||||
}
|
||||
code.add("$N(reader)", methodName);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user