sashapolo commented on a change in pull request #130: URL: https://github.com/apache/ignite-3/pull/130#discussion_r638720258
########## File path: modules/network-annotation-processor/src/main/java/org/apache/ignite/network/processor/internal/AutoSerializableProcessor.java ########## @@ -0,0 +1,248 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.network.processor.internal; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringJoiner; +import java.util.stream.Collectors; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.Element; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.Modifier; +import javax.lang.model.element.Name; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic; +import com.squareup.javapoet.ClassName; +import com.squareup.javapoet.JavaFile; +import com.squareup.javapoet.MethodSpec; +import com.squareup.javapoet.TypeName; +import com.squareup.javapoet.TypeSpec; +import org.apache.ignite.network.NetworkMessage; +import org.apache.ignite.network.processor.annotations.AutoSerializable; +import org.apache.ignite.network.serialization.MessageDeserializer; +import org.apache.ignite.network.serialization.MessageSerializationFactory; +import org.apache.ignite.network.serialization.MessageSerializationRegistry; +import org.apache.ignite.network.serialization.MessageSerializer; + +/** + * Annotation processor for generating (de-)serializers for network messages marked with the {@link AutoSerializable} + * annotation. + */ +@SupportedSourceVersion(SourceVersion.RELEASE_11) +public class AutoSerializableProcessor extends AbstractProcessor { + /** {@inheritDoc} */ + @Override public Set<String> getSupportedAnnotationTypes() { + return Set.of(AutoSerializable.class.getName()); + } + + /** {@inheritDoc} */ + @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { + Set<TypeElement> annotatedElements = annotations.stream() + .map(roundEnv::getElementsAnnotatedWith) + .flatMap(Collection::stream) + .map(TypeElement.class::cast) + .collect(Collectors.toUnmodifiableSet()); + + if (annotatedElements.isEmpty()) { + return true; + } + + try { + generateSources(annotatedElements); + } catch (IOException e) { + throw new IllegalStateException("IO exception during annotation processing", e); + } + + return true; + } + + /** + * Generates serialization-related classes for the given elements. + */ + private void generateSources(Set<TypeElement> annotatedElements) throws IOException { + var factories = new HashMap<TypeElement, TypeSpec>(); + + for (var messageClass : annotatedElements) { + try { + if (isValidElement(messageClass)) { + String packageName = ClassName.get(messageClass).packageName(); + + TypeSpec serializer = generateSerializer(messageClass); + writeToFile(packageName, serializer); + + TypeSpec deserializer = generateDeseralizer(messageClass); + writeToFile(packageName, deserializer); + + TypeSpec factory = generateFactory(messageClass, serializer, deserializer); + writeToFile(packageName, factory); + + factories.put(messageClass, factory); + } else { + processingEnv.getMessager().printMessage( + Diagnostic.Kind.ERROR, + String.format( + "%s annotation must only be present on interfaces that extend %s", + AutoSerializable.class, NetworkMessage.class + ), + messageClass + ); + } + } catch (ProcessingException e) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, e.getMessage(), messageClass); + } + } + + TypeSpec registryInitializer = generateRegistryInitializer(factories); + writeToFile(getParentPackage(annotatedElements), registryInitializer); Review comment: we can have multiple factories in the same module, so I don't think it is possible in the general case -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
