Search in sources :

Example 1 with AutoProtoSchemaBuilder

use of org.infinispan.protostream.annotations.AutoProtoSchemaBuilder in project protostream by infinispan.

the class AutoProtoSchemaBuilderAnnotationProcessor method processDependencies.

// todo [anistor] we do not support yet dependencies on packages, only on types
private ProcessorContext processDependencies(RoundEnvironment roundEnv, SerializationContext serCtx, Element annotatedElement, AutoProtoSchemaBuilder builderAnnotation) throws IOException {
    List<? extends TypeMirror> dependencies = DangerousActions.getTypeMirrors(builderAnnotation, AutoProtoSchemaBuilder::dependsOn);
    ProcessorContext processorContext = new ProcessorContext();
    // register internal known types
    processorContext.marshalledClasses.put(typeFactory.fromClass(WrappedMessage.class), WrappedMessage.PROTO_FILE);
    for (TypeMirror dependencyType : dependencies) {
        TypeElement dependencyElement = (TypeElement) types.asElement(dependencyType);
        String dependencyFQN = dependencyElement.getQualifiedName().toString();
        AutoProtoSchemaBuilder dependencyAnnotation = getBuilderAnnotation(dependencyElement);
        if (dependencyAnnotation == null) {
            throw new AnnotationProcessingException(annotatedElement, "Dependency %s is not annotated with @AutoProtoSchemaBuilder annotation", dependencyFQN);
        }
        if (!processedElementsFQN.add(dependencyFQN)) {
            throw new AnnotationProcessingException(annotatedElement, "Illegal recursive dependency on %s", dependencyFQN);
        }
        // disable source file emission for dependencies because they are already compiled
        boolean wasGenerationEnabled = generatedFilesWriter.isEnabled();
        generatedFilesWriter.setEnabled(false);
        processElement(roundEnv, serCtx, dependencyElement, dependencyAnnotation, processorContext);
        // restore previous state of source file emission
        generatedFilesWriter.setEnabled(wasGenerationEnabled);
        processedElementsFQN.remove(dependencyFQN);
    }
    return processorContext;
}
Also used : WrappedMessage(org.infinispan.protostream.WrappedMessage) AutoProtoSchemaBuilder(org.infinispan.protostream.annotations.AutoProtoSchemaBuilder) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement)

Example 2 with AutoProtoSchemaBuilder

use of org.infinispan.protostream.annotations.AutoProtoSchemaBuilder in project protostream by infinispan.

the class AutoProtoSchemaBuilderAnnotationProcessor method process.

// todo [anistor] check RoundEnvironment.errorRaised() and do not write any more output files if errors are present
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (isDebugEnabled) {
        logDebug("AutoProtoSchemaBuilderAnnotationProcessor annotations=%s, rootElements=%s", annotations, roundEnv.getRootElements());
    }
    Optional<? extends TypeElement> claimedAnnotation = annotations.stream().filter(a -> a.getQualifiedName().contentEquals(ANNOTATION_NAME)).findAny();
    try {
        if (claimedAnnotation.isPresent()) {
            for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(claimedAnnotation.get())) {
                AutoProtoSchemaBuilder builderAnnotation = getBuilderAnnotation(annotatedElement);
                SerializationContext serCtx = ProtobufUtil.newSerializationContext();
                try {
                    processElement(roundEnv, serCtx, annotatedElement, builderAnnotation, new ProcessorContext());
                } catch (ProtoSchemaBuilderException | DescriptorParserException e) {
                    throw new AnnotationProcessingException(e, annotatedElement, "%s", getStackTraceAsString(e));
                }
            }
        }
        if (roundEnv.processingOver()) {
            serviceLoaderFileGenerator.writeServiceFile(filer);
        }
    } catch (AnnotationProcessingException e) {
        // this is caused by the user supplying incorrect data in the annotation or related classes
        if (isDebugEnabled) {
            logDebug("@AutoProtoSchemaBuilder processor threw an exception: %s", getStackTraceAsString(e));
        }
        reportError(e);
    } catch (Exception e) {
        // this may be a fatal programming error in the annotation processor itself
        reportError(null, "@AutoProtoSchemaBuilder processor threw a fatal exception: %s", getStackTraceAsString(e));
    }
    return claimedAnnotation.isPresent();
}
Also used : AbstractProcessor(javax.annotation.processing.AbstractProcessor) SupportedOptions(javax.annotation.processing.SupportedOptions) Modifier(javax.lang.model.element.Modifier) TypeElement(javax.lang.model.element.TypeElement) SupportedAnnotationTypes(javax.annotation.processing.SupportedAnnotationTypes) Elements(javax.lang.model.util.Elements) Version(org.infinispan.protostream.Version) Generated(javax.annotation.Generated) Matcher(java.util.regex.Matcher) Diagnostic(javax.tools.Diagnostic) Map(java.util.Map) NestingKind(javax.lang.model.element.NestingKind) XClass(org.infinispan.protostream.annotations.impl.types.XClass) Messager(javax.annotation.processing.Messager) PrintWriter(java.io.PrintWriter) Collection(java.util.Collection) GeneratedSchema(org.infinispan.protostream.GeneratedSchema) Set(java.util.Set) Element(javax.lang.model.element.Element) ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) Processor(javax.annotation.processing.Processor) Types(javax.lang.model.util.Types) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) SourceVersion(javax.lang.model.SourceVersion) List(java.util.List) Filer(javax.annotation.processing.Filer) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) IndentWriter(org.infinispan.protostream.annotations.impl.IndentWriter) PackageElement(javax.lang.model.element.PackageElement) ProtobufUtil(org.infinispan.protostream.ProtobufUtil) HashMap(java.util.HashMap) XMethod(org.infinispan.protostream.annotations.impl.types.XMethod) HashSet(java.util.HashSet) AutoProtoSchemaBuilder(org.infinispan.protostream.annotations.AutoProtoSchemaBuilder) SerializationContextInitializer(org.infinispan.protostream.SerializationContextInitializer) LinkedHashSet(java.util.LinkedHashSet) Name(javax.lang.model.element.Name) ElementKind(javax.lang.model.element.ElementKind) HasModelElement(org.infinispan.protostream.annotations.impl.processor.types.HasModelElement) StringWriter(java.io.StringWriter) IOException(java.io.IOException) WrappedMessage(org.infinispan.protostream.WrappedMessage) TypeMirror(javax.lang.model.type.TypeMirror) RoundEnvironment(javax.annotation.processing.RoundEnvironment) AutoService(com.google.auto.service.AutoService) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) MirrorTypeFactory(org.infinispan.protostream.annotations.impl.processor.types.MirrorTypeFactory) SerializationContext(org.infinispan.protostream.SerializationContext) SerializationContext(org.infinispan.protostream.SerializationContext) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) AutoProtoSchemaBuilder(org.infinispan.protostream.annotations.AutoProtoSchemaBuilder) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) PackageElement(javax.lang.model.element.PackageElement) HasModelElement(org.infinispan.protostream.annotations.impl.processor.types.HasModelElement) ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) DescriptorParserException(org.infinispan.protostream.DescriptorParserException) IOException(java.io.IOException)

Aggregations

TypeElement (javax.lang.model.element.TypeElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 WrappedMessage (org.infinispan.protostream.WrappedMessage)2 AutoProtoSchemaBuilder (org.infinispan.protostream.annotations.AutoProtoSchemaBuilder)2 AutoService (com.google.auto.service.AutoService)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Generated (javax.annotation.Generated)1 AbstractProcessor (javax.annotation.processing.AbstractProcessor)1