use of io.micronaut.annotation.processing.visitor.LoadedVisitor in project micronaut-core by micronaut-projects.
the class AggregatingTypeElementVisitorProcessor method getSupportedAnnotationTypes.
@Override
public Set<String> getSupportedAnnotationTypes() {
if (!hasVisitors()) {
return Collections.emptySet();
}
if (isIncremental(processingEnv)) {
List<LoadedVisitor> loadedVisitors = getLoadedVisitors();
Set<String> annotationNames = new HashSet<>();
// if a visitor is interested in Object than fall back to all
for (LoadedVisitor loadedVisitor : loadedVisitors) {
TypeElementVisitor<?, ?> visitor = loadedVisitor.getVisitor();
Set<String> supportedAnnotationNames = visitor.getSupportedAnnotationNames();
if (supportedAnnotationNames.contains("*")) {
return super.getSupportedAnnotationTypes();
} else {
annotationNames.addAll(supportedAnnotationNames);
}
}
return annotationNames;
} else {
return super.getSupportedAnnotationTypes();
}
}
use of io.micronaut.annotation.processing.visitor.LoadedVisitor in project micronaut-core by micronaut-projects.
the class TypeElementVisitorProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!loadedVisitors.isEmpty() && !(annotations.size() == 1 && Generated.class.getName().equals(annotations.iterator().next().getQualifiedName().toString()))) {
TypeElement groovyObjectTypeElement = elementUtils.getTypeElement("groovy.lang.GroovyObject");
TypeMirror groovyObjectType = groovyObjectTypeElement != null ? groovyObjectTypeElement.asType() : null;
Set<TypeElement> elements = new LinkedHashSet<>();
for (TypeElement annotation : annotations) {
final Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
includeElements(elements, annotatedElements, groovyObjectType);
}
// This call to getRootElements() should be removed in Micronaut 4. It should not be possible
// to process elements without at least one annotation present and this call breaks that assumption.
final Set<? extends Element> rootElements = roundEnv.getRootElements();
includeElements(elements, rootElements, groovyObjectType);
if (!elements.isEmpty()) {
for (LoadedVisitor loadedVisitor : loadedVisitors) {
for (TypeElement typeElement : elements) {
if (!loadedVisitor.matches(typeElement)) {
continue;
}
String className = typeElement.getQualifiedName().toString();
typeElement.accept(new ElementVisitor(typeElement, Collections.singletonList(loadedVisitor)), className);
}
}
}
for (LoadedVisitor loadedVisitor : loadedVisitors) {
try {
loadedVisitor.getVisitor().finish(javaVisitorContext);
} catch (Throwable e) {
error("Error finalizing type visitor [%s]: %s", loadedVisitor.getVisitor(), e.getMessage());
}
}
}
final List<AbstractBeanDefinitionBuilder> beanDefinitionBuilders = javaVisitorContext.getBeanElementBuilders();
if (CollectionUtils.isNotEmpty(beanDefinitionBuilders)) {
try {
AbstractBeanDefinitionBuilder.writeBeanDefinitionBuilders(classWriterOutputVisitor, beanDefinitionBuilders);
} catch (IOException e) {
// raise a compile error
String message = e.getMessage();
error("Unexpected error: %s", message != null ? message : e.getClass().getSimpleName());
}
}
if (roundEnv.processingOver()) {
javaVisitorContext.finish();
writeBeanDefinitionsToMetaInf();
}
return false;
}
use of io.micronaut.annotation.processing.visitor.LoadedVisitor 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