Search in sources :

Example 1 with AbstractCodeGenerator

use of io.micronaut.aot.core.codegen.AbstractCodeGenerator in project micronaut-aot by micronaut-projects.

the class AbstractStaticServiceLoaderSourceGenerator method collectServiceImplementations.

private void collectServiceImplementations(String serviceName) {
    context.addDiagnostics(SERVICE_LOADING_CATEGORY, "Starting service discovery for type " + serviceName);
    ClassLoader cl = this.getClass().getClassLoader();
    Set<String> seen = Collections.synchronizedSet(new HashSet<>());
    SoftServiceLoader.ServiceCollector<Class<?>> availableClasses = SoftServiceLoader.newCollector(serviceName, s -> !s.isEmpty(), cl, className -> {
        if (rejectedClasses.test(className) || !seen.add(className)) {
            return null;
        }
        AbstractCodeGenerator substitution = substitutions.get(className);
        if (substitution != null) {
            List<JavaFile> javaFiles = new ArrayList<>();
            AOTContext tracker = new DelegatingSourceGenerationContext(context) {

                @Override
                public void registerGeneratedSourceFile(@NonNull JavaFile javaFile) {
                    super.registerGeneratedSourceFile(javaFile);
                    javaFiles.add(javaFile);
                }
            };
            substitution.generate(tracker);
            javaFiles.forEach(substitute -> substitutes.computeIfAbsent(serviceName, k -> new ArrayList<>()).add(substitute));
            if (!javaFiles.isEmpty()) {
                return null;
            }
        }
        Class<?> clazz;
        try {
            clazz = cl.loadClass(className);
            DeepAnalyzer deepAnalyzer = deepAnalyzerFor(clazz, serviceName);
            boolean available = deepAnalyzer.isAvailable(clazz);
            if (!available && forceInclude.contains(className)) {
                context.addDiagnostics(SERVICE_LOADING_CATEGORY, "Forcing inclusion of " + clazz + " despite it not matching bean requirements");
                available = true;
            }
            if (!available) {
                if (BeanConfiguration.class.isAssignableFrom(clazz)) {
                    disabledConfigurations.add((BeanConfiguration) clazz.getConstructor().newInstance());
                }
                context.addDiagnostics(SERVICE_LOADING_CATEGORY, "Skipping " + clazz + " because it doesn't match bean requirements");
                return null;
            }
        } catch (ClassNotFoundException | NoClassDefFoundError | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
            context.addDiagnostics(SERVICE_LOADING_CATEGORY, "Skipping service " + serviceName + " implementation " + className + " because of missing dependencies: " + e.getMessage());
            return null;
        }
        return clazz;
    });
    List<Class<?>> serviceClasses = new ArrayList<>();
    availableClasses.collect(serviceClasses::add);
    this.serviceClasses.put(serviceName, serviceClasses);
}
Also used : SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) ArrayList(java.util.ArrayList) NonNull(io.micronaut.core.annotation.NonNull) AbstractCodeGenerator(io.micronaut.aot.core.codegen.AbstractCodeGenerator) DelegatingSourceGenerationContext(io.micronaut.aot.core.codegen.DelegatingSourceGenerationContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) JavaFile(com.squareup.javapoet.JavaFile) AOTContext(io.micronaut.aot.core.AOTContext)

Aggregations

JavaFile (com.squareup.javapoet.JavaFile)1 AOTContext (io.micronaut.aot.core.AOTContext)1 AbstractCodeGenerator (io.micronaut.aot.core.codegen.AbstractCodeGenerator)1 DelegatingSourceGenerationContext (io.micronaut.aot.core.codegen.DelegatingSourceGenerationContext)1 NonNull (io.micronaut.core.annotation.NonNull)1 SoftServiceLoader (io.micronaut.core.io.service.SoftServiceLoader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1