|
| 1 | +package com.strategyobject.substrateclient.scale.codegen.reader; |
| 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.common.io.Streamer; |
| 7 | +import com.strategyobject.substrateclient.common.types.Enums; |
| 8 | +import com.strategyobject.substrateclient.scale.ScaleReader; |
| 9 | +import com.strategyobject.substrateclient.scale.annotation.AutoRegister; |
| 10 | +import com.strategyobject.substrateclient.scale.codegen.ScaleProcessorHelper; |
| 11 | +import lombok.NonNull; |
| 12 | +import lombok.RequiredArgsConstructor; |
| 13 | +import lombok.val; |
| 14 | + |
| 15 | +import javax.lang.model.element.Modifier; |
| 16 | +import javax.lang.model.element.TypeElement; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | + |
| 20 | +@RequiredArgsConstructor |
| 21 | +public class ScaleReaderAnnotatedEnum { |
| 22 | + private static final String READERS_ARG = "readers"; |
| 23 | + private static final String ENUM_VALUES = "values"; |
| 24 | + |
| 25 | + private final @NonNull TypeElement enumElement; |
| 26 | + |
| 27 | + public void generateReader(@NonNull ProcessorContext context) throws IOException, ProcessingException { |
| 28 | + val readerName = ScaleProcessorHelper.getReaderName(enumElement.getSimpleName().toString()); |
| 29 | + val enumType = TypeName.get(enumElement.asType()); |
| 30 | + |
| 31 | + val typeSpecBuilder = TypeSpec.classBuilder(readerName) |
| 32 | + .addAnnotation(AnnotationSpec.builder(AutoRegister.class) |
| 33 | + .addMember("types", "{$L.class}", enumElement.getQualifiedName().toString()) |
| 34 | + .build()) |
| 35 | + .addModifiers(Modifier.PUBLIC) |
| 36 | + .addSuperinterface(ParameterizedTypeName.get(ClassName.get(ScaleReader.class), enumType)) |
| 37 | + .addField( |
| 38 | + FieldSpec.builder(ArrayTypeName.of(enumType), ENUM_VALUES, Modifier.PRIVATE, Modifier.FINAL) |
| 39 | + .initializer("$T.values()", enumType) |
| 40 | + .build()) |
| 41 | + .addMethod(generateReadMethod(enumType)); |
| 42 | + |
| 43 | + JavaFile.builder( |
| 44 | + context.getPackageName(enumElement), |
| 45 | + typeSpecBuilder.build() |
| 46 | + ).build().writeTo(context.getFiler()); |
| 47 | + } |
| 48 | + |
| 49 | + private MethodSpec generateReadMethod(TypeName enumType) { |
| 50 | + val methodSpec = MethodSpec.methodBuilder("read") |
| 51 | + .addAnnotation(Override.class) |
| 52 | + .addModifiers(Modifier.PUBLIC) |
| 53 | + .returns(enumType) |
| 54 | + .addParameter(InputStream.class, "stream") |
| 55 | + .addParameter(ArrayTypeName.of( |
| 56 | + ParameterizedTypeName.get( |
| 57 | + ClassName.get(ScaleReader.class), |
| 58 | + WildcardTypeName.subtypeOf(Object.class))), |
| 59 | + READERS_ARG) |
| 60 | + .varargs(true) |
| 61 | + .addException(IOException.class); |
| 62 | + |
| 63 | + addValidationRules(methodSpec); |
| 64 | + addMethodBody(methodSpec); |
| 65 | + return methodSpec.build(); |
| 66 | + } |
| 67 | + |
| 68 | + private void addValidationRules(MethodSpec.Builder methodSpec) { |
| 69 | + methodSpec |
| 70 | + .addStatement("if (stream == null) throw new IllegalArgumentException(\"stream is null\")") |
| 71 | + .addStatement("if (readers != null && readers.length > 0) throw new IllegalArgumentException()"); |
| 72 | + } |
| 73 | + |
| 74 | + private void addMethodBody(MethodSpec.Builder methodSpec) { |
| 75 | + methodSpec.addStatement("return $T.lookup($L, $T.readByte(stream))", Enums.class, ENUM_VALUES, Streamer.class); |
| 76 | + } |
| 77 | +} |
0 commit comments