-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
255 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
rpc/rpc-api/src/main/java/com/strategyobject/substrateclient/rpc/api/AddressKind.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
rpc/rpc-api/src/main/java/com/strategyobject/substrateclient/rpc/api/AddressKindWriter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ava/com/strategyobject/substrateclient/scale/codegen/reader/ScaleReaderAnnotatedEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.strategyobject.substrateclient.scale.codegen.reader; | ||
|
||
import com.squareup.javapoet.*; | ||
import com.strategyobject.substrateclient.common.codegen.ProcessingException; | ||
import com.strategyobject.substrateclient.common.codegen.ProcessorContext; | ||
import com.strategyobject.substrateclient.common.io.Streamer; | ||
import com.strategyobject.substrateclient.scale.ScaleReader; | ||
import com.strategyobject.substrateclient.scale.annotation.AutoRegister; | ||
import com.strategyobject.substrateclient.scale.codegen.ScaleProcessorHelper; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
|
||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.TypeElement; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
@RequiredArgsConstructor | ||
public class ScaleReaderAnnotatedEnum { | ||
private static final String READERS_ARG = "readers"; | ||
|
||
private final @NonNull TypeElement enumElement; | ||
|
||
public void generateReader(@NonNull ProcessorContext context) throws IOException, ProcessingException { | ||
val readerName = ScaleProcessorHelper.getReaderName(enumElement.getSimpleName().toString()); | ||
val enumType = TypeName.get(enumElement.asType()); | ||
|
||
val typeSpecBuilder = TypeSpec.classBuilder(readerName) | ||
.addAnnotation(AnnotationSpec.builder(AutoRegister.class) | ||
.addMember("types", "{$L.class}", enumElement.getQualifiedName().toString()) | ||
.build()) | ||
.addModifiers(Modifier.PUBLIC) | ||
.addSuperinterface(ParameterizedTypeName.get(ClassName.get(ScaleReader.class), enumType)) | ||
.addMethod(generateReadMethod(enumType)); | ||
|
||
JavaFile.builder( | ||
context.getPackageName(enumElement), | ||
typeSpecBuilder.build() | ||
).build().writeTo(context.getFiler()); | ||
} | ||
|
||
private MethodSpec generateReadMethod(TypeName enumType) { | ||
val methodSpec = MethodSpec.methodBuilder("read") | ||
.addAnnotation(Override.class) | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(enumType) | ||
.addParameter(InputStream.class, "stream") | ||
.addParameter(ArrayTypeName.of( | ||
ParameterizedTypeName.get( | ||
ClassName.get(ScaleReader.class), | ||
WildcardTypeName.subtypeOf(Object.class))), | ||
READERS_ARG) | ||
.varargs(true) | ||
.addException(IOException.class); | ||
|
||
addValidationRules(methodSpec); | ||
addMethodBody(methodSpec); | ||
return methodSpec.build(); | ||
} | ||
|
||
private void addValidationRules(MethodSpec.Builder methodSpec) { | ||
methodSpec | ||
.addStatement("if (stream == null) throw new IllegalArgumentException(\"stream is null\")") | ||
.addStatement("if (readers != null && readers.length > 0) throw new IllegalArgumentException()"); | ||
} | ||
|
||
private void addMethodBody(MethodSpec.Builder methodSpec) { | ||
methodSpec.addStatement("return $T.values()[$T.readByte(stream)]", enumElement, Streamer.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ava/com/strategyobject/substrateclient/scale/codegen/writer/ScaleWriterAnnotatedEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.strategyobject.substrateclient.scale.codegen.writer; | ||
|
||
import com.squareup.javapoet.*; | ||
import com.strategyobject.substrateclient.common.codegen.ProcessingException; | ||
import com.strategyobject.substrateclient.common.codegen.ProcessorContext; | ||
import com.strategyobject.substrateclient.scale.ScaleWriter; | ||
import com.strategyobject.substrateclient.scale.annotation.AutoRegister; | ||
import com.strategyobject.substrateclient.scale.codegen.ScaleProcessorHelper; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.val; | ||
|
||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.element.TypeElement; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
@RequiredArgsConstructor | ||
public class ScaleWriterAnnotatedEnum { | ||
private static final String WRITERS_ARG = "writers"; | ||
|
||
private final @NonNull TypeElement enumElement; | ||
|
||
public void generateWriter(@NonNull ProcessorContext context) throws IOException, ProcessingException { | ||
val writerName = ScaleProcessorHelper.getWriterName(enumElement.getSimpleName().toString()); | ||
val enumType = TypeName.get(enumElement.asType()); | ||
|
||
val typeSpecBuilder = TypeSpec.classBuilder(writerName) | ||
.addAnnotation(AnnotationSpec.builder(AutoRegister.class) | ||
.addMember("types", "{$L.class}", enumElement.getQualifiedName().toString()) | ||
.build()) | ||
.addModifiers(Modifier.PUBLIC) | ||
.addSuperinterface(ParameterizedTypeName.get(ClassName.get(ScaleWriter.class), enumType)) | ||
.addMethod(generateWriteMethod(enumType)); | ||
|
||
JavaFile.builder( | ||
context.getPackageName(enumElement), | ||
typeSpecBuilder.build() | ||
).build().writeTo(context.getFiler()); | ||
} | ||
|
||
private MethodSpec generateWriteMethod(TypeName classWildcardTyped) { | ||
val methodSpec = MethodSpec.methodBuilder("write") | ||
.addAnnotation(Override.class) | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(TypeName.VOID) | ||
.addParameter(classWildcardTyped, "value") | ||
.addParameter(OutputStream.class, "stream") | ||
.addParameter(ArrayTypeName.of( | ||
ParameterizedTypeName.get( | ||
ClassName.get(ScaleWriter.class), | ||
WildcardTypeName.subtypeOf(Object.class))), | ||
WRITERS_ARG) | ||
.varargs(true) | ||
.addException(IOException.class); | ||
|
||
addValidationRules(methodSpec); | ||
addMethodBody(methodSpec); | ||
return methodSpec.build(); | ||
} | ||
|
||
private void addValidationRules(MethodSpec.Builder methodSpec) { | ||
methodSpec.addStatement("if (stream == null) throw new IllegalArgumentException(\"stream is null\")"); | ||
methodSpec.addStatement("if (value == null) throw new IllegalArgumentException(\"value is null\")"); | ||
methodSpec.addStatement("if (writers != null && writers.length > 0) throw new IllegalArgumentException()"); | ||
} | ||
|
||
private void addMethodBody(MethodSpec.Builder methodSpec) { | ||
methodSpec.addStatement("stream.write(value.ordinal())"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.strategyobject.substrateclient.scale; | ||
|
||
import com.strategyobject.substrateclient.scale.annotation.ScaleReader; | ||
import com.strategyobject.substrateclient.scale.annotation.ScaleWriter; | ||
|
||
@ScaleReader | ||
@ScaleWriter | ||
public enum Enum { | ||
MONDAY, | ||
TUESDAY, | ||
WEDNESDAY, | ||
THURSDAY, | ||
FRIDAY, | ||
SATURDAY, | ||
SUNDAY | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters