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);
}
Aggregations