|
| 1 | +package com.strategyobject.substrateclient.scale.codegen.writer; |
| 2 | + |
| 3 | +import com.squareup.javapoet.*; |
| 4 | +import com.strategyobject.substrateclient.common.codegen.ProcessingException; |
| 5 | +import com.strategyobject.substrateclient.common.codegen.ProcessorContext; |
| 6 | +import com.strategyobject.substrateclient.scale.ScaleWriter; |
| 7 | +import com.strategyobject.substrateclient.scale.annotation.AutoRegister; |
| 8 | +import com.strategyobject.substrateclient.scale.codegen.ScaleProcessorHelper; |
| 9 | +import lombok.NonNull; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import lombok.val; |
| 12 | + |
| 13 | +import javax.lang.model.element.Modifier; |
| 14 | +import javax.lang.model.element.TypeElement; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.OutputStream; |
| 17 | + |
| 18 | +@RequiredArgsConstructor |
| 19 | +public class ScaleWriterAnnotatedEnum { |
| 20 | + private static final String WRITERS_ARG = "writers"; |
| 21 | + |
| 22 | + private final @NonNull TypeElement enumElement; |
| 23 | + |
| 24 | + public void generateWriter(@NonNull ProcessorContext context) throws IOException, ProcessingException { |
| 25 | + val writerName = ScaleProcessorHelper.getWriterName(enumElement.getSimpleName().toString()); |
| 26 | + val enumType = TypeName.get(enumElement.asType()); |
| 27 | + |
| 28 | + val typeSpecBuilder = TypeSpec.classBuilder(writerName) |
| 29 | + .addAnnotation(AnnotationSpec.builder(AutoRegister.class) |
| 30 | + .addMember("types", "{$L.class}", enumElement.getQualifiedName().toString()) |
| 31 | + .build()) |
| 32 | + .addModifiers(Modifier.PUBLIC) |
| 33 | + .addSuperinterface(ParameterizedTypeName.get(ClassName.get(ScaleWriter.class), enumType)) |
| 34 | + .addMethod(generateWriteMethod(enumType)); |
| 35 | + |
| 36 | + JavaFile.builder( |
| 37 | + context.getPackageName(enumElement), |
| 38 | + typeSpecBuilder.build() |
| 39 | + ).build().writeTo(context.getFiler()); |
| 40 | + } |
| 41 | + |
| 42 | + private MethodSpec generateWriteMethod(TypeName classWildcardTyped) { |
| 43 | + val methodSpec = MethodSpec.methodBuilder("write") |
| 44 | + .addAnnotation(Override.class) |
| 45 | + .addModifiers(Modifier.PUBLIC) |
| 46 | + .returns(TypeName.VOID) |
| 47 | + .addParameter(classWildcardTyped, "value") |
| 48 | + .addParameter(OutputStream.class, "stream") |
| 49 | + .addParameter(ArrayTypeName.of( |
| 50 | + ParameterizedTypeName.get( |
| 51 | + ClassName.get(ScaleWriter.class), |
| 52 | + WildcardTypeName.subtypeOf(Object.class))), |
| 53 | + WRITERS_ARG) |
| 54 | + .varargs(true) |
| 55 | + .addException(IOException.class); |
| 56 | + |
| 57 | + addValidationRules(methodSpec); |
| 58 | + addMethodBody(methodSpec); |
| 59 | + return methodSpec.build(); |
| 60 | + } |
| 61 | + |
| 62 | + private void addValidationRules(MethodSpec.Builder methodSpec) { |
| 63 | + methodSpec.addStatement("if (stream == null) throw new IllegalArgumentException(\"stream is null\")"); |
| 64 | + methodSpec.addStatement("if (value == null) throw new IllegalArgumentException(\"value is null\")"); |
| 65 | + methodSpec.addStatement("if (writers != null && writers.length > 0) throw new IllegalArgumentException()"); |
| 66 | + } |
| 67 | + |
| 68 | + private void addMethodBody(MethodSpec.Builder methodSpec) { |
| 69 | + methodSpec.addStatement("stream.write(value.ordinal())"); |
| 70 | + } |
| 71 | +} |
0 commit comments