ClassScanner.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2023 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
  12.  * the License. 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
  17.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  18.  * specific language governing permissions and limitations under the License.
  19.  *
  20.  * *********************************************************************************************************************
  21.  *
  22.  * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.util.spring;

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

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

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

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

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

  72.         return classes;
  73.       }

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

  88.     /*******************************************************************************************************************
  89.      *
  90.      * Adds a filter for an annotation.
  91.      *
  92.      * @param  annotationClass  the annotation class
  93.      * @return                  itself for method chaining
  94.      *
  95.      ******************************************************************************************************************/
  96.     @Nonnull
  97.     public ClassScanner withAnnotationFilter (@Nonnull final Class<? extends Annotation> annotationClass)
  98.       {
  99.         return withIncludeFilter(new AnnotationTypeFilter(annotationClass));
  100.       }
  101.   }