Skip to content

Package: ClassScanner

ClassScanner

nameinstructionbranchcomplexitylinemethod
ClassScanner()
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
findClasses()
M: 0 C: 46
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
withAnnotationFilter(Class)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
withIncludeFilter(TypeFilter)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

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:
29: import java.lang.annotation.Annotation;
30: import javax.annotation.Nonnull;
31: import java.util.ArrayList;
32: import java.util.Collection;
33: import java.util.List;
34: import org.springframework.beans.factory.config.BeanDefinition;
35: import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
36: import org.springframework.core.type.filter.AnnotationTypeFilter;
37: import org.springframework.core.type.filter.TypeFilter;
38: import org.springframework.util.ClassUtils;
39: import it.tidalwave.role.spring.RoleSpringConfiguration;
40:
41: /***********************************************************************************************************************
42: *
43: * A utility for scanning classes in the classpath with some criteria.
44: *
45: * @author Fabrizio Giudici
46: *
47: **********************************************************************************************************************/
48: public class ClassScanner
49: {
50: private final String basePackages = System.getProperty(
51: ClassScanner.class.getCanonicalName() + ".basePackages", RoleSpringConfiguration.getBasePackages());
52:
53: private final ClassPathScanningCandidateComponentProvider scanner =
54: new ClassPathScanningCandidateComponentProvider(false);
55:
56: /*******************************************************************************************************************
57: *
58: * Scans for classes and returns them.
59: *
60: * @return the collection of scanned classes
61: *
62: ******************************************************************************************************************/
63: @Nonnull
64: public final Collection<Class<?>> findClasses()
65: {
66: final List<Class<?>> classes = new ArrayList<>();
67:
68:• for (final String basePackage : basePackages.split(":"))
69: {
70:• for (final BeanDefinition candidate : scanner.findCandidateComponents(basePackage))
71: {
72: classes.add(ClassUtils.resolveClassName(candidate.getBeanClassName(),
73: ClassUtils.getDefaultClassLoader()));
74: }
75: }
76:
77: return classes;
78: }
79:
80: /*******************************************************************************************************************
81: *
82: * Adds an "include" filter.
83: *
84: * @param filter the filter
85: * @return itself for method chaining
86: *
87: ******************************************************************************************************************/
88: @Nonnull
89: public ClassScanner withIncludeFilter (@Nonnull final TypeFilter filter)
90: {
91: scanner.addIncludeFilter(filter);
92: return this;
93: }
94:
95: /*******************************************************************************************************************
96: *
97: * Adds a filter for an annotation.
98: *
99: * @param annotationClass the annotation class
100: * @return itself for method chaining
101: *
102: ******************************************************************************************************************/
103: @Nonnull
104: public ClassScanner withAnnotationFilter (@Nonnull final Class<? extends Annotation> annotationClass)
105: {
106: return withIncludeFilter(new AnnotationTypeFilter(annotationClass));
107: }
108: }