Search in sources :

Example 1 with Generated

use of io.micronaut.core.annotation.Generated in project micronaut-aot by micronaut-projects.

the class AbstractStaticServiceLoaderSourceGenerator method generate.

@Override
public void generate(@NonNull AOTContext context) {
    this.context = context;
    if (serviceNames == null) {
        serviceNames = context.getConfiguration().stringList(findOption(this.getClass(), SERVICE_TYPES).key());
    }
    if (substitutions == null) {
        Set<String> resourceNames = new LinkedHashSet<>();
        resourceNames.add("application");
        context.getAnalyzer().getEnvironmentNames().stream().map(env -> "application-" + env).forEach(resourceNames::add);
        context.getConfiguration().stringList(Environments.POSSIBLE_ENVIRONMENTS_NAMES).stream().filter(env -> !"default".equals(env)).map(env -> "application-" + env).forEach(resourceNames::add);
        substitutions = new HashMap<>();
        if (context.getConfiguration().isFeatureEnabled(YamlPropertySourceGenerator.ID)) {
            YamlPropertySourceGenerator yaml = new YamlPropertySourceGenerator(resourceNames);
            yaml.generate(context);
            if (MetadataUtils.isEnabledOn(context.getRuntime(), yaml)) {
                LOGGER.debug("Substituting {} with {}", PropertySourceLoader.class.getName(), yaml.getClass().getName());
                substitutions.put(YamlPropertySourceLoader.class.getName(), yaml);
            }
        }
    }
    if (metadataProviderPredicate == null) {
        metadataProviderPredicate = context.getAnalyzer().getAnnotationMetadataPredicate();
    }
    if (rejectedClasses == null) {
        List<String> strings = context.getConfiguration().stringList(findOption(this.getClass(), REJECTED_CLASSES).key());
        Set<String> rejected = strings.isEmpty() ? Collections.emptySet() : new HashSet<>(strings);
        rejectedClasses = rejected::contains;
    }
    if (forceInclude == null) {
        forceInclude = new HashSet<>(context.getConfiguration().stringList(findOption(this.getClass(), FORCE_INCLUDE).key()));
    }
    for (String serviceName : serviceNames) {
        LOGGER.debug("Processing service type {}", serviceName);
        collectServiceImplementations(serviceName);
    }
    context.put(Substitutes.class, substitutes);
    for (BeanConfiguration beanConfiguration : disabledConfigurations) {
        for (List<Class<?>> classList : serviceClasses.values()) {
            for (Class<?> clazz : classList) {
                if (beanConfiguration.isWithin(clazz)) {
                    context.addDiagnostics(SERVICE_LOADING_CATEGORY, "Disabling " + clazz.getName() + " because it belongs to " + beanConfiguration.getName() + " which is disabled (" + beanConfiguration.getClass() + ")");
                    disabledServices.add(clazz);
                }
            }
        }
    }
    generateServiceLoader();
    LOGGER.debug("Generated static service loader classes: {}", staticServiceClasses.keySet());
    LOGGER.debug("Generated static {} service loader substitutions", substitutes.values().size());
    staticServiceClasses.values().stream().map(context::javaFile).forEach(context::registerGeneratedSourceFile);
    context.registerStaticOptimization("StaticServicesLoader", SoftServiceLoader.Optimizations.class, this::buildOptimization);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Environments(io.micronaut.aot.core.Environments) AnnotationMetadataProvider(io.micronaut.core.annotation.AnnotationMetadataProvider) LoggerFactory(org.slf4j.LoggerFactory) ClassName(com.squareup.javapoet.ClassName) WildcardTypeName(com.squareup.javapoet.WildcardTypeName) HashMap(java.util.HashMap) DelegatingSourceGenerationContext(io.micronaut.aot.core.codegen.DelegatingSourceGenerationContext) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) Map(java.util.Map) MetadataUtils(io.micronaut.aot.core.config.MetadataUtils) CodeBlock(com.squareup.javapoet.CodeBlock) LinkedHashSet(java.util.LinkedHashSet) Logger(org.slf4j.Logger) PropertySourceLoader(io.micronaut.context.env.PropertySourceLoader) Predicate(java.util.function.Predicate) Collection(java.util.Collection) PUBLIC(javax.lang.model.element.Modifier.PUBLIC) Set(java.util.Set) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeSpec(com.squareup.javapoet.TypeSpec) AbstractCodeGenerator(io.micronaut.aot.core.codegen.AbstractCodeGenerator) BeanConfiguration(io.micronaut.inject.BeanConfiguration) MetadataUtils.findOption(io.micronaut.aot.core.config.MetadataUtils.findOption) InvocationTargetException(java.lang.reflect.InvocationTargetException) JavaFile(com.squareup.javapoet.JavaFile) NonNull(io.micronaut.core.annotation.NonNull) List(java.util.List) Stream(java.util.stream.Stream) YamlPropertySourceLoader(io.micronaut.context.env.yaml.YamlPropertySourceLoader) Generated(io.micronaut.core.annotation.Generated) AOTContext(io.micronaut.aot.core.AOTContext) Collections(java.util.Collections) PropertySourceLoader(io.micronaut.context.env.PropertySourceLoader) YamlPropertySourceLoader(io.micronaut.context.env.yaml.YamlPropertySourceLoader) SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) YamlPropertySourceLoader(io.micronaut.context.env.yaml.YamlPropertySourceLoader) BeanConfiguration(io.micronaut.inject.BeanConfiguration)

Example 2 with Generated

use of io.micronaut.core.annotation.Generated in project micronaut-core by micronaut-projects.

the class ServiceDescriptionProcessor method processGeneratedAnnotation.

private boolean processGeneratedAnnotation(List<io.micronaut.inject.ast.Element> originatingElements, Element element, TypeElement typeElement, String name) {
    Generated generated = element.getAnnotation(Generated.class);
    if (generated != null) {
        String serviceName = generated.service();
        if (StringUtils.isNotEmpty(serviceName)) {
            serviceDescriptors.computeIfAbsent(serviceName, s1 -> new HashSet<>()).add(name);
            originatingElements.add(new JavaClassElement(typeElement, AnnotationMetadata.EMPTY_METADATA, null));
        }
        return true;
    }
    return false;
}
Also used : SupportedOptions(javax.annotation.processing.SupportedOptions) ContextConfigurer(io.micronaut.context.annotation.ContextConfigurer) Set(java.util.Set) HashMap(java.util.HashMap) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) ArrayList(java.util.ArrayList) JavaClassElement(io.micronaut.annotation.processing.visitor.JavaClassElement) HashSet(java.util.HashSet) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) TypeMirror(javax.lang.model.type.TypeMirror) RoundEnvironment(javax.annotation.processing.RoundEnvironment) Map(java.util.Map) DeclaredType(javax.lang.model.type.DeclaredType) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Generated(io.micronaut.core.annotation.Generated) Optional(java.util.Optional) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) ApplicationContextConfigurer(io.micronaut.context.ApplicationContextConfigurer) Collections(java.util.Collections) ContextConfigurerVisitor(io.micronaut.context.visitor.ContextConfigurerVisitor) Generated(io.micronaut.core.annotation.Generated) JavaClassElement(io.micronaut.annotation.processing.visitor.JavaClassElement) HashSet(java.util.HashSet)

Aggregations

Generated (io.micronaut.core.annotation.Generated)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 ClassName (com.squareup.javapoet.ClassName)1 CodeBlock (com.squareup.javapoet.CodeBlock)1 JavaFile (com.squareup.javapoet.JavaFile)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 WildcardTypeName (com.squareup.javapoet.WildcardTypeName)1 JavaClassElement (io.micronaut.annotation.processing.visitor.JavaClassElement)1 AOTContext (io.micronaut.aot.core.AOTContext)1 Environments (io.micronaut.aot.core.Environments)1 AbstractCodeGenerator (io.micronaut.aot.core.codegen.AbstractCodeGenerator)1 DelegatingSourceGenerationContext (io.micronaut.aot.core.codegen.DelegatingSourceGenerationContext)1 MetadataUtils (io.micronaut.aot.core.config.MetadataUtils)1