|
| 1 | +package de.bethibande.serial.processor.serializer.types; |
| 2 | + |
| 3 | +import com.google.auto.service.AutoService; |
| 4 | +import com.palantir.javapoet.*; |
| 5 | +import de.bethibande.serial.allocation.ParameterizedObjectAllocator; |
| 6 | +import de.bethibande.serial.processor.TypeHelper; |
| 7 | +import de.bethibande.serial.processor.context.SerializationContext; |
| 8 | +import de.bethibande.serial.processor.generator.FieldInfo; |
| 9 | +import de.bethibande.serial.processor.serializer.ElementSerializer; |
| 10 | +import de.bethibande.serial.processor.serializer.EmbeddedTypeTransformer; |
| 11 | +import de.bethibande.serial.processor.serializer.FieldBasedObjectTransformer; |
| 12 | + |
| 13 | +import javax.lang.model.element.Modifier; |
| 14 | +import javax.lang.model.type.DeclaredType; |
| 15 | +import javax.lang.model.type.TypeMirror; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.Optional; |
| 18 | + |
| 19 | +@AutoService(FieldBasedObjectTransformer.class) |
| 20 | +public class CollectionTypes extends EmbeddedTypeTransformer { |
| 21 | + |
| 22 | + @Override |
| 23 | + protected boolean isApplicable0(final FieldInfo field) { |
| 24 | + return !field.isNullable() && TypeHelper.isAssignable(TypeHelper.erasure(field.getType()), TypeHelper.asType(Collection.class)); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + protected FieldInfo createChildType(final FieldInfo field, final ElementSerializer serializer) { |
| 29 | + if (field.getType() instanceof DeclaredType declaredType) { |
| 30 | + final TypeMirror type = declaredType.getTypeArguments().getFirst(); |
| 31 | + |
| 32 | + return field.toBuilder() |
| 33 | + .type(type) |
| 34 | + .nullable(TypeHelper.isNullable(type)) |
| 35 | + .build(); |
| 36 | + } |
| 37 | + throw new IllegalArgumentException("Type is not a declared type"); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public CodeBlock createSerializationCode(final FieldInfo field, final SerializationContext ctx) { |
| 42 | + return CodeBlock.builder() |
| 43 | + .addStatement("$L.writeInt($L.size())", FIELD_WRITER, FIELD_VALUE) |
| 44 | + .beginControlFlow("for ($T item : $L)", field.getFirstChild().getType(), FIELD_VALUE) |
| 45 | + .addStatement("$L(item)", embeddedMethodName(field)) |
| 46 | + .endControlFlow() |
| 47 | + .addStatement("return this") |
| 48 | + .build(); |
| 49 | + } |
| 50 | + |
| 51 | + protected String allocatorFieldName(final FieldInfo field) { |
| 52 | + return methodName(field) + "Allocator"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Optional<String> wrappedMethodNameSuffix() { |
| 57 | + return Optional.of("CollectionValue"); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public void applyDeserializerTransformation(final TypeSpec.Builder builder, |
| 62 | + final FieldInfo field, |
| 63 | + final SerializationContext ctx) { |
| 64 | + final TypeName fieldType = TypeName.get(field.getType()); |
| 65 | + final TypeName allocatorType = ParameterizedTypeName.get( |
| 66 | + ClassName.get(ParameterizedObjectAllocator.class), |
| 67 | + ClassName.get(Integer.class), |
| 68 | + fieldType |
| 69 | + ); |
| 70 | + final String allocatorFieldName = allocatorFieldName(field); |
| 71 | + |
| 72 | + final FieldSpec allocatorField = FieldSpec.builder(allocatorType, allocatorFieldName) |
| 73 | + .addModifiers(Modifier.PRIVATE) |
| 74 | + .build(); |
| 75 | + |
| 76 | + final MethodSpec allocatorMethod = MethodSpec.methodBuilder(allocatorFieldName) |
| 77 | + .addModifiers(Modifier.PUBLIC) |
| 78 | + .addParameter(ParameterSpec.builder(allocatorType, allocatorFieldName) |
| 79 | + .addModifiers(Modifier.FINAL) |
| 80 | + .build()) |
| 81 | + .addStatement("this.$L = $L", allocatorFieldName, allocatorFieldName) |
| 82 | + .addStatement("return this") |
| 83 | + .returns(ctx.deserializerType()) |
| 84 | + .build(); |
| 85 | + |
| 86 | + builder.addField(allocatorField); |
| 87 | + builder.addMethod(allocatorMethod); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public CodeBlock createDeserializationCode(final FieldInfo field, final SerializationContext ctx) { |
| 92 | + return CodeBlock.builder() |
| 93 | + .addStatement("final int length = $L.readInt()", FIELD_READER) |
| 94 | + .addStatement("final $T $L = $L.allocate(length)", field.getType(), FIELD_VALUE, allocatorFieldName(field)) |
| 95 | + .beginControlFlow("for (int i = 0; i < length; i++)") |
| 96 | + .addStatement("$L.add($L())", FIELD_VALUE, embeddedMethodName(field)) |
| 97 | + .endControlFlow() |
| 98 | + .addStatement("return $L", FIELD_VALUE) |
| 99 | + .build(); |
| 100 | + } |
| 101 | +} |
0 commit comments