Search in sources :

Example 1 with TypeElementVisitor

use of io.micronaut.inject.visitor.TypeElementVisitor in project micronaut-core by micronaut-projects.

the class TypeElementVisitorProcessor method findCoreTypeElementVisitors.

@NonNull
private static Collection<TypeElementVisitor> findCoreTypeElementVisitors(SoftServiceLoader<TypeElementVisitor> serviceLoader, @Nullable Set<String> warnings) {
    Map<String, TypeElementVisitor> typeElementVisitors = new HashMap<>(10);
    for (ServiceDefinition<TypeElementVisitor> definition : SERVICE_LOADER) {
        if (definition.isPresent()) {
            TypeElementVisitor visitor;
            try {
                visitor = definition.load();
            } catch (Throwable e) {
                if (warnings != null) {
                    warnings.add("TypeElementVisitor [" + definition.getName() + "] will be ignored due to loading error: " + e.getMessage());
                }
                continue;
            }
            if (visitor == null || !visitor.isEnabled()) {
                continue;
            }
            final Requires requires = visitor.getClass().getAnnotation(Requires.class);
            if (requires != null) {
                final Requires.Sdk sdk = requires.sdk();
                if (sdk == Requires.Sdk.MICRONAUT) {
                    final String version = requires.version();
                    if (StringUtils.isNotEmpty(version) && !VersionUtils.isAtLeastMicronautVersion(version)) {
                        try {
                            if (warnings != null) {
                                warnings.add("TypeElementVisitor [" + definition.getName() + "] will be ignored because Micronaut version [" + VersionUtils.MICRONAUT_VERSION + "] must be at least " + version);
                            }
                            continue;
                        } catch (IllegalArgumentException e) {
                        // shouldn't happen, thrown when invalid version encountered
                        }
                    }
                }
            }
            typeElementVisitors.put(definition.getName(), visitor);
        }
    }
    return typeElementVisitors.values();
}
Also used : Requires(io.micronaut.context.annotation.Requires) TypeElementVisitor(io.micronaut.inject.visitor.TypeElementVisitor) NonNull(io.micronaut.core.annotation.NonNull)

Example 2 with TypeElementVisitor

use of io.micronaut.inject.visitor.TypeElementVisitor 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

Requires (io.micronaut.context.annotation.Requires)2 NonNull (io.micronaut.core.annotation.NonNull)2 TypeElementVisitor (io.micronaut.inject.visitor.TypeElementVisitor)2 LoadedVisitor (io.micronaut.annotation.processing.visitor.LoadedVisitor)1 Introduction (io.micronaut.aop.Introduction)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 Generated (io.micronaut.core.annotation.Generated)1 Introspected (io.micronaut.core.annotation.Introspected)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 JavaModelUtils (io.micronaut.inject.processing.JavaModelUtils)1 VisitorContext (io.micronaut.inject.visitor.VisitorContext)1 AbstractBeanDefinitionBuilder (io.micronaut.inject.writer.AbstractBeanDefinitionBuilder)1 IOException (java.io.IOException)1