ClassScanner.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.util.spring;

  27. import java.lang.annotation.Annotation;
  28. import javax.annotation.Nonnull;
  29. import java.util.ArrayList;
  30. import java.util.Collection;
  31. import java.util.List;
  32. import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
  33. import org.springframework.core.type.filter.AnnotationTypeFilter;
  34. import org.springframework.core.type.filter.TypeFilter;
  35. import org.springframework.util.ClassUtils;
  36. import it.tidalwave.role.spring.RoleSpringConfiguration;
  37. import lombok.Setter;

  38. /***************************************************************************************************************************************************************
  39.  *
  40.  * A utility for scanning classes in the classpath with some criteria.
  41.  *
  42.  * @author  Fabrizio Giudici
  43.  *
  44.  **************************************************************************************************************************************************************/
  45. public class ClassScanner
  46.   {
  47.     @Setter
  48.     private static String basePackages = System.getProperty(
  49.             ClassScanner.class.getCanonicalName() + ".basePackages", RoleSpringConfiguration.getBasePackages());

  50.     private final ClassPathScanningCandidateComponentProvider scanner =
  51.             new ClassPathScanningCandidateComponentProvider(false);

  52.     /***********************************************************************************************************************************************************
  53.      * Scans for classes and returns them.
  54.      *
  55.      * @return  the collection of scanned classes
  56.      **********************************************************************************************************************************************************/
  57.     @Nonnull
  58.     public final Collection<Class<?>> findClasses()
  59.       {
  60.         final List<Class<?>> classes = new ArrayList<>();

  61.         for (final var basePackage : basePackages.split(":"))
  62.           {
  63.             for (final var candidate : scanner.findCandidateComponents(basePackage))
  64.               {
  65.                 classes.add(ClassUtils.resolveClassName(candidate.getBeanClassName(),
  66.                             ClassUtils.getDefaultClassLoader()));
  67.               }
  68.           }

  69.         return classes;
  70.       }

  71.     /***********************************************************************************************************************************************************
  72.      * Adds an "include" filter.
  73.      *
  74.      * @param  filter   the filter
  75.      * @return          itself for method chaining
  76.      **********************************************************************************************************************************************************/
  77.     @Nonnull
  78.     public ClassScanner withIncludeFilter (@Nonnull final TypeFilter filter)
  79.       {
  80.         scanner.addIncludeFilter(filter);
  81.         return this;
  82.       }

  83.     /***********************************************************************************************************************************************************
  84.      * Adds a filter for an annotation.
  85.      *
  86.      * @param  annotationClass  the annotation class
  87.      * @return                  itself for method chaining
  88.      **********************************************************************************************************************************************************/
  89.     @Nonnull
  90.     public ClassScanner withAnnotationFilter (@Nonnull final Class<? extends Annotation> annotationClass)
  91.       {
  92.         return withIncludeFilter(new AnnotationTypeFilter(annotationClass));
  93.       }
  94.   }