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