fix(serialize-generator): more checks to prevent double declarations
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Johannes Frohnmeyer 2024-04-24 10:35:03 +02:00
parent 5a9c3ee58f
commit 78ec439ccb
Signed by: Johannes
GPG Key ID: E76429612C2929F4
5 changed files with 126 additions and 126 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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)

View File

@ -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);
}
}

View File

@ -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);
}
}