Search in sources :

Example 1 with MessageSerializationFactory

use of org.apache.ignite.network.serialization.MessageSerializationFactory in project ignite-3 by apache.

the class SerializationFactoryGenerator method generateFactory.

/**
 * Generates a {@link MessageSerializationFactory} class for the given network message type.
 *
 * @param message      network message
 * @param serializer   {@link MessageSerializer}, generated for the given {@code message}
 * @param deserializer {@link MessageDeserializer}, generated for the given {@code message}
 * @return {@code TypeSpec} of the generated {@code MessageSerializationFactory}
 */
public TypeSpec generateFactory(MessageClass message, TypeSpec serializer, TypeSpec deserializer) {
    processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Generating a MessageSerializationFactory", message.element());
    ClassName messageFactoryClassName = messageGroup.messageFactoryClassName();
    FieldSpec messageFactoryField = FieldSpec.builder(messageFactoryClassName, "messageFactory").addModifiers(Modifier.PRIVATE, Modifier.FINAL).build();
    return TypeSpec.classBuilder(message.simpleName() + "SerializationFactory").addModifiers(Modifier.PUBLIC).addSuperinterface(ParameterizedTypeName.get(ClassName.get(MessageSerializationFactory.class), message.className())).addField(messageFactoryField).addMethod(MethodSpec.constructorBuilder().addModifiers(Modifier.PUBLIC).addParameter(messageFactoryClassName, "messageFactory").addStatement("this.$N = messageFactory", messageFactoryField).build()).addMethod(MethodSpec.methodBuilder("createDeserializer").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(ParameterizedTypeName.get(ClassName.get(MessageDeserializer.class), message.className())).addStatement("return new $N($N)", deserializer, messageFactoryField).build()).addMethod(MethodSpec.methodBuilder("createSerializer").addAnnotation(Override.class).addModifiers(Modifier.PUBLIC).returns(ParameterizedTypeName.get(ClassName.get(MessageSerializer.class), message.className())).addStatement("return new $N()", serializer).build()).addOriginatingElement(message.element()).addOriginatingElement(messageGroup.element()).build();
}
Also used : MessageDeserializer(org.apache.ignite.network.serialization.MessageDeserializer) ClassName(com.squareup.javapoet.ClassName) MessageSerializationFactory(org.apache.ignite.network.serialization.MessageSerializationFactory) FieldSpec(com.squareup.javapoet.FieldSpec)

Aggregations

ClassName (com.squareup.javapoet.ClassName)1 FieldSpec (com.squareup.javapoet.FieldSpec)1 MessageDeserializer (org.apache.ignite.network.serialization.MessageDeserializer)1 MessageSerializationFactory (org.apache.ignite.network.serialization.MessageSerializationFactory)1