All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.beanmapper.autoconfigure.ApplicationScanner Maven / Gradle / Ivy

The newest version!
package io.beanmapper.autoconfigure;

import static io.beanmapper.utils.Classes.forName;

import java.lang.annotation.Annotation;
import java.util.Collections;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;

import io.beanmapper.annotations.LogicSecuredCheck;
import io.beanmapper.core.collections.CollectionHandler;
import io.beanmapper.core.converter.BeanConverter;
import io.beanmapper.utils.Classes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScanner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.core.type.filter.TypeFilter;

/**
 * Utility that helps searching for beans/classes within the application.
 */
class ApplicationScanner {

    private final Logger log = LoggerFactory.getLogger(ApplicationScanner.class);
    private final EntityScanner entityScanner;
    private final ClassPathScanningCandidateComponentProvider classpathScanner;

    ApplicationScanner(ApplicationContext context) {
        this.entityScanner = new EntityScanner(context);
        this.classpathScanner = new ClassPathScanningCandidateComponentProvider(
                false);
        this.classpathScanner.setEnvironment(context.getEnvironment());
        this.classpathScanner.setResourceLoader(context);
    }

    Optional findApplicationPackage() {
        try {
            Set> appClasses = entityScanner.scan(SpringBootApplication.class);
            Class appClass = appClasses.iterator().next();
            return Optional.of(appClass.getPackage().getName());
        } catch (ClassNotFoundException | NoSuchElementException e) {
            log.error("Cannot find class annotated with SpringBootApplication. ", e);
            return Optional.empty();
        }
    }

    Set> findBeanPairInstructions() {
        Set> foundAnnotations = findBeanPairInstructions(BeanMapFromClass.class);
        foundAnnotations.addAll(findBeanPairInstructions(BeanMapToClass.class));
        return foundAnnotations;
    }

    private Set> findBeanPairInstructions(Class markerAnnotation) {
        try {
            return entityScanner.scan(markerAnnotation);
        } catch (ClassNotFoundException | NoSuchElementException e) {
            return Collections.emptySet();
        }
    }

    Set> findBeanConverterClasses(String basePackage) {
        return findClasses(basePackage, BeanConverter.class);
    }

    Set> findCollectionHandlerClasses(String basePackage) {
        return findClasses(basePackage, CollectionHandler.class);
    }

    Set> findLogicSecuredCheckClasses(String basePackage) {
        return findClasses(basePackage, LogicSecuredCheck.class);
    }

    private  Set> findClasses(String basePackage, Class lookForClass) {
        Set> converterClasses = new HashSet<>();
        classpathScanner.addIncludeFilter(createTypeFilterForClass(lookForClass));
        classpathScanner.findCandidateComponents(basePackage).forEach(
                bd -> converterClasses.add((Class) forName(bd.getBeanClassName())));
        classpathScanner.resetFilters(false);
        return converterClasses;
    }

    private TypeFilter createTypeFilterForClass(Class clazz) {
        return (metadataReader, metadataReaderFactory) -> {
            String className = metadataReader.getClassMetadata().getClassName();
            Class currentClass = Classes.forName(className);
            return clazz.isAssignableFrom(currentClass);
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy