-
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.
There is added code generation for RPC data transfer objects: RpcEncoderProcessor, RpcDecoderProcessor. RPC interfaces call generated encoders/decoders. Redundant mocks were removed. Proper types are annotated with RpcEncoder/RpcDecoder/ScaleWriter/ScaleReader. Generated ScaleWriters/ScaleReaders use getters/setters instead of fields. RpcCallProcessor and RpcSubscriptionProcessor are generalized using parent abstract class. Scale/ScaleGeneric annotations support parameters and methods(for return value). ProcessorContext and ProcessingException became common for all generators. Type(Read/Write)Generator(s) were renamed to (Reader/Writer)Compositor(s). ScaleReaderNotFoundException and ScaleWriterNotFoundException were removed to support recursive traversing of both types Scale and RPC in parallel and avoid visiting ahead. Auto registration is not supported more in terms were supposed. All annotated encoders/decoders/writers/readers prefixed the project name (including the company name) are still auto registrable. To auto register external classes should use the following API, e.g.: `ScaleReaderRegistry.getInstance().registerAnnotatedFrom("com.my-company.my-project")`. TypeTraverser has started to support primitives. TypeTraverser hasn't been visiting wild card types if generic is Self(Encodable/Writable).
- Loading branch information
Showing
140 changed files
with
3,998 additions
and
1,252 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
dependencies { | ||
implementation 'org.reflections:reflections:0.9.12' | ||
implementation 'org.reflections:reflections:0.10.1' | ||
implementation 'com.squareup:javapoet:1.13.0' | ||
} |
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
79 changes: 79 additions & 0 deletions
79
common/src/main/java/com/strategyobject/substrateclient/common/codegen/ProcessorContext.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,79 @@ | ||
package com.strategyobject.substrateclient.common.codegen; | ||
|
||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import javax.annotation.processing.Filer; | ||
import javax.annotation.processing.Messager; | ||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.TypeElement; | ||
import javax.lang.model.type.PrimitiveType; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
import javax.tools.Diagnostic; | ||
|
||
@RequiredArgsConstructor | ||
@Getter | ||
public class ProcessorContext { | ||
private final @NonNull Types typeUtils; | ||
private final @NonNull Elements elementUtils; | ||
private final @NonNull Filer filer; | ||
private final @NonNull Messager messager; | ||
|
||
public String getPackageName(@NonNull TypeElement classElement) { | ||
return elementUtils.getPackageOf(classElement).getQualifiedName().toString(); | ||
} | ||
|
||
public boolean isSubtypeOf(@NonNull TypeMirror candidate, @NonNull TypeMirror supertype) { | ||
return typeUtils.isAssignable(candidate, supertype); | ||
} | ||
|
||
public boolean isGeneric(@NonNull TypeMirror type) { | ||
return ((TypeElement) typeUtils.asElement(type)) | ||
.getTypeParameters() | ||
.size() > 0; | ||
} | ||
|
||
public TypeMirror erasure(@NonNull TypeMirror type) { | ||
return typeUtils.erasure(type); | ||
} | ||
|
||
public TypeMirror getBoxed(@NonNull TypeMirror type) { | ||
return type instanceof PrimitiveType ? | ||
typeUtils.boxedClass((PrimitiveType) type).asType() : | ||
type; | ||
} | ||
|
||
public TypeMirror getType(Class<?> clazz) { | ||
return elementUtils.getTypeElement(clazz.getCanonicalName()).asType(); | ||
} | ||
|
||
public boolean isSameType(@NonNull TypeMirror left, @NonNull TypeMirror right) { | ||
return typeUtils.isSameType(left, right); | ||
} | ||
|
||
public void error(Exception exception) { | ||
messager.printMessage( | ||
Diagnostic.Kind.ERROR, | ||
exception.getMessage() | ||
); | ||
} | ||
|
||
public void error(Element e, Exception exception) { | ||
messager.printMessage( | ||
Diagnostic.Kind.ERROR, | ||
exception.getMessage(), | ||
e | ||
); | ||
} | ||
|
||
public void error(Element e, String message, Object... args) { | ||
messager.printMessage( | ||
Diagnostic.Kind.ERROR, | ||
String.format(message, args), | ||
e | ||
); | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
common/src/main/java/com/strategyobject/substrateclient/common/codegen/TypeUtils.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,32 @@ | ||
package com.strategyobject.substrateclient.common.codegen; | ||
|
||
import lombok.val; | ||
|
||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.type.TypeKind; | ||
|
||
import static com.strategyobject.substrateclient.common.utils.StringUtils.capitalize; | ||
|
||
public class TypeUtils { | ||
private static final String SETTER_PREFIX = "set"; | ||
private static final String DEFAULT_GETTER_PREFIX = "get"; | ||
private static final String BOOLEAN_GETTER_PREFIX = "is"; | ||
|
||
public static String getSetterName(String field) { | ||
return SETTER_PREFIX + capitalize(field); | ||
} | ||
|
||
public static String getGetterName(VariableElement field) { | ||
val isPrimitiveBoolean = field.asType().getKind() == TypeKind.BOOLEAN; | ||
val fieldName = field.getSimpleName().toString(); | ||
if (isPrimitiveBoolean && fieldName.startsWith(BOOLEAN_GETTER_PREFIX)) { | ||
return fieldName; | ||
} | ||
|
||
val prefix = isPrimitiveBoolean ? | ||
BOOLEAN_GETTER_PREFIX : | ||
DEFAULT_GETTER_PREFIX; | ||
|
||
return prefix + capitalize(fieldName); | ||
} | ||
} |
26 changes: 19 additions & 7 deletions
26
common/src/main/java/com/strategyobject/substrateclient/common/reflection/Scanner.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 |
---|---|---|
@@ -1,21 +1,33 @@ | ||
package com.strategyobject.substrateclient.common.reflection; | ||
|
||
import lombok.NonNull; | ||
import org.reflections.Reflections; | ||
import org.reflections.scanners.SubTypesScanner; | ||
import org.reflections.util.ClasspathHelper; | ||
import org.reflections.util.ConfigurationBuilder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public final class Scanner { | ||
private static final Reflections reflections; | ||
private final Reflections reflections; | ||
|
||
static { | ||
reflections = new Reflections("", new SubTypesScanner()); | ||
private Scanner(String[] prefixes) { | ||
reflections = new Reflections( | ||
new ConfigurationBuilder() | ||
.setUrls( | ||
Arrays.stream(prefixes) | ||
.flatMap(p -> ClasspathHelper.forPackage(p).stream()) | ||
.collect(Collectors.toCollection(ArrayList::new))) | ||
); | ||
} | ||
|
||
public static <T> Set<Class<? extends T>> getSubTypesOf(Class<T> clazz) { | ||
return reflections.getSubTypesOf(clazz); | ||
public static Scanner forPrefixes(@NonNull String... prefixes){ | ||
return new Scanner(prefixes); | ||
} | ||
|
||
private Scanner() { | ||
public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> clazz) { | ||
return reflections.getSubTypesOf(clazz); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,18 @@ | ||
dependencies { | ||
implementation project(':common') | ||
implementation project(':rpc:core') | ||
implementation project(':scale') | ||
implementation project(':scale:scale-codegen') | ||
implementation project(':transport') | ||
|
||
compileOnly 'com.google.auto.service:auto-service-annotations:1.0' | ||
annotationProcessor 'com.google.auto.service:auto-service:1.0' | ||
|
||
implementation 'com.squareup:javapoet:1.13.0' | ||
testImplementation 'com.google.testing.compile:compile-testing:0.19' | ||
testImplementation 'com.google.code.gson:gson:2.8.8' | ||
|
||
testCompileOnly project(':rpc:codegen') | ||
testAnnotationProcessor project(':rpc:codegen') | ||
testAnnotationProcessor project(':scale:scale-codegen') | ||
} |
Oops, something went wrong.