Search in sources :

Example 1 with LoadedVisitor

use of io.micronaut.annotation.processing.visitor.LoadedVisitor in project micronaut-core by micronaut-projects.

the class AggregatingTypeElementVisitorProcessor method getSupportedAnnotationTypes.

@Override
public Set<String> getSupportedAnnotationTypes() {
    if (!hasVisitors()) {
        return Collections.emptySet();
    }
    if (isIncremental(processingEnv)) {
        List<LoadedVisitor> loadedVisitors = getLoadedVisitors();
        Set<String> annotationNames = new HashSet<>();
        // if a visitor is interested in Object than fall back to all
        for (LoadedVisitor loadedVisitor : loadedVisitors) {
            TypeElementVisitor<?, ?> visitor = loadedVisitor.getVisitor();
            Set<String> supportedAnnotationNames = visitor.getSupportedAnnotationNames();
            if (supportedAnnotationNames.contains("*")) {
                return super.getSupportedAnnotationTypes();
            } else {
                annotationNames.addAll(supportedAnnotationNames);
            }
        }
        return annotationNames;
    } else {
        return super.getSupportedAnnotationTypes();
    }
}
Also used : LoadedVisitor(io.micronaut.annotation.processing.visitor.LoadedVisitor) HashSet(java.util.HashSet)

Example 2 with LoadedVisitor

use of io.micronaut.annotation.processing.visitor.LoadedVisitor in project micronaut-core by micronaut-projects.

the class TypeElementVisitorProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    if (!loadedVisitors.isEmpty() && !(annotations.size() == 1 && Generated.class.getName().equals(annotations.iterator().next().getQualifiedName().toString()))) {
        TypeElement groovyObjectTypeElement = elementUtils.getTypeElement("groovy.lang.GroovyObject");
        TypeMirror groovyObjectType = groovyObjectTypeElement != null ? groovyObjectTypeElement.asType() : null;
        Set<TypeElement> elements = new LinkedHashSet<>();
        for (TypeElement annotation : annotations) {
            final Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
            includeElements(elements, annotatedElements, groovyObjectType);
        }
        // This call to getRootElements() should be removed in Micronaut 4. It should not be possible
        // to process elements without at least one annotation present and this call breaks that assumption.
        final Set<? extends Element> rootElements = roundEnv.getRootElements();
        includeElements(elements, rootElements, groovyObjectType);
        if (!elements.isEmpty()) {
            for (LoadedVisitor loadedVisitor : loadedVisitors) {
                for (TypeElement typeElement : elements) {
                    if (!loadedVisitor.matches(typeElement)) {
                        continue;
                    }
                    String className = typeElement.getQualifiedName().toString();
                    typeElement.accept(new ElementVisitor(typeElement, Collections.singletonList(loadedVisitor)), className);
                }
            }
        }
        for (LoadedVisitor loadedVisitor : loadedVisitors) {
            try {
                loadedVisitor.getVisitor().finish(javaVisitorContext);
            } catch (Throwable e) {
                error("Error finalizing type visitor [%s]: %s", loadedVisitor.getVisitor(), e.getMessage());
            }
        }
    }
    final List<AbstractBeanDefinitionBuilder> beanDefinitionBuilders = javaVisitorContext.getBeanElementBuilders();
    if (CollectionUtils.isNotEmpty(beanDefinitionBuilders)) {
        try {
            AbstractBeanDefinitionBuilder.writeBeanDefinitionBuilders(classWriterOutputVisitor, beanDefinitionBuilders);
        } catch (IOException e) {
            // raise a compile error
            String message = e.getMessage();
            error("Unexpected error: %s", message != null ? message : e.getClass().getSimpleName());
        }
    }
    if (roundEnv.processingOver()) {
        javaVisitorContext.finish();
        writeBeanDefinitionsToMetaInf();
    }
    return false;
}
Also used : Generated(io.micronaut.core.annotation.Generated) TypeElement(javax.lang.model.element.TypeElement) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) AbstractBeanDefinitionBuilder(io.micronaut.inject.writer.AbstractBeanDefinitionBuilder) IOException(java.io.IOException) LoadedVisitor(io.micronaut.annotation.processing.visitor.LoadedVisitor) TypeMirror(javax.lang.model.type.TypeMirror)

Example 3 with LoadedVisitor

use of io.micronaut.annotation.processing.visitor.LoadedVisitor in project micronaut-core by micronaut-projects.

the class TypeElementVisitorProcessor method init.

@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
    super.init(processingEnv);
    this.typeElementVisitors = findTypeElementVisitors();
    // set supported options as system properties to keep compatibility
    // in particular for micronaut-openapi
    processingEnv.getOptions().entrySet().stream().filter(entry -> entry.getKey() != null && entry.getKey().startsWith(VisitorContext.MICRONAUT_BASE_OPTION_NAME)).forEach(entry -> System.setProperty(entry.getKey(), entry.getValue() == null ? "" : entry.getValue()));
    this.loadedVisitors = new ArrayList<>(typeElementVisitors.size());
    for (TypeElementVisitor<?, ?> visitor : typeElementVisitors) {
        TypeElementVisitor.VisitorKind visitorKind = visitor.getVisitorKind();
        TypeElementVisitor.VisitorKind incrementalProcessorKind = getIncrementalProcessorKind();
        if (incrementalProcessorKind == visitorKind) {
            try {
                loadedVisitors.add(new LoadedVisitor(visitor, javaVisitorContext, genericUtils, processingEnv));
            } catch (TypeNotPresentException | NoClassDefFoundError e) {
            // ignored, means annotations referenced are not on the classpath
            }
        }
    }
    OrderUtil.reverseSort(loadedVisitors);
    for (LoadedVisitor loadedVisitor : loadedVisitors) {
        try {
            loadedVisitor.getVisitor().start(javaVisitorContext);
        } catch (Throwable e) {
            error("Error initializing type visitor [%s]: %s", loadedVisitor.getVisitor(), e.getMessage());
        }
    }
}
Also used : JavaModelUtils(io.micronaut.inject.processing.JavaModelUtils) OrderUtil(io.micronaut.core.order.OrderUtil) java.util(java.util) SupportedOptions(javax.annotation.processing.SupportedOptions) VariableElement(javax.lang.model.element.VariableElement) TypeElement(javax.lang.model.element.TypeElement) SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) VersionUtils(io.micronaut.core.version.VersionUtils) Introduction(io.micronaut.aop.Introduction) Nullable(io.micronaut.core.annotation.Nullable) DeclaredType(javax.lang.model.type.DeclaredType) LoadedVisitor(io.micronaut.annotation.processing.visitor.LoadedVisitor) Requires(io.micronaut.context.annotation.Requires) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy) ExecutableElement(javax.lang.model.element.ExecutableElement) FIELD(javax.lang.model.element.ElementKind.FIELD) ServiceDefinition(io.micronaut.core.io.service.ServiceDefinition) Element(javax.lang.model.element.Element) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Introspected(io.micronaut.core.annotation.Introspected) StringUtils(io.micronaut.core.util.StringUtils) NonNull(io.micronaut.core.annotation.NonNull) VisitorContext(io.micronaut.inject.visitor.VisitorContext) TypeMirror(javax.lang.model.type.TypeMirror) ElementScanner8(javax.lang.model.util.ElementScanner8) Stream(java.util.stream.Stream) RoundEnvironment(javax.annotation.processing.RoundEnvironment) CollectionUtils(io.micronaut.core.util.CollectionUtils) Generated(io.micronaut.core.annotation.Generated) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) AbstractBeanDefinitionBuilder(io.micronaut.inject.writer.AbstractBeanDefinitionBuilder) LoadedVisitor(io.micronaut.annotation.processing.visitor.LoadedVisitor) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor)

Aggregations

LoadedVisitor (io.micronaut.annotation.processing.visitor.LoadedVisitor)3 Generated (io.micronaut.core.annotation.Generated)2 TypeElementVisitor (io.micronaut.inject.visitor.TypeElementVisitor)2 AbstractBeanDefinitionBuilder (io.micronaut.inject.writer.AbstractBeanDefinitionBuilder)2 IOException (java.io.IOException)2 TypeElement (javax.lang.model.element.TypeElement)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Introduction (io.micronaut.aop.Introduction)1 Requires (io.micronaut.context.annotation.Requires)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 Introspected (io.micronaut.core.annotation.Introspected)1 NonNull (io.micronaut.core.annotation.NonNull)1 Nullable (io.micronaut.core.annotation.Nullable)1 ServiceDefinition (io.micronaut.core.io.service.ServiceDefinition)1 SoftServiceLoader (io.micronaut.core.io.service.SoftServiceLoader)1 OrderUtil (io.micronaut.core.order.OrderUtil)1 CollectionUtils (io.micronaut.core.util.CollectionUtils)1 StringUtils (io.micronaut.core.util.StringUtils)1 VersionUtils (io.micronaut.core.version.VersionUtils)1 AnnotationMetadataHierarchy (io.micronaut.inject.annotation.AnnotationMetadataHierarchy)1