diff --git a/builder/pom.xml b/builder/pom.xml
index 24c374f..18f07d5 100644
--- a/builder/pom.xml
+++ b/builder/pom.xml
@@ -31,6 +31,7 @@
1.0.0
2.13.0
3.9.0
+ 2.18.1
3.0.0
${project.version}
@@ -58,6 +59,11 @@
${lombok.version}
provided
+
+ io.github.lukehutch
+ fast-classpath-scanner
+ ${fast-classpath-scanner.version}
+
org.mockito
mockito-core
diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java
index 626c729..d5c7a95 100644
--- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java
+++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfiguration.java
@@ -21,15 +21,12 @@
package net.kemitix.checkstyle.ruleset.builder;
-import com.google.common.reflect.ClassPath;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import java.io.IOException;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
-import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -41,18 +38,6 @@ import java.util.stream.Stream;
@Configuration
public class BuilderConfiguration {
- /**
- * Create the ClassPath used to scan packages.
- *
- * @return the ClassPath
- *
- * @throws IOException if there is an error
- */
- @Bean
- public ClassPath classPath() throws IOException {
- return ClassPath.from(getClass().getClassLoader());
- }
-
/**
* A Map of rules for each RuleSource.
*
@@ -63,24 +48,11 @@ public class BuilderConfiguration {
@Bean
public Map> checkClasses(final PackageScanner packageScanner) {
return Stream.of(RuleSource.values())
- .map(toRuleSourceEntry(packageScanner))
+ .map(source -> entry(source, packageScanner.apply(source)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
- private static Function>> toRuleSourceEntry(
- final PackageScanner packageScanner
- ) {
- return source -> new AbstractMap.SimpleEntry<>(
- source,
- classesInSource(source, packageScanner)
- );
- }
-
- private static List classesInSource(
- final RuleSource source,
- final PackageScanner packageScanner
- ) {
- return packageScanner.apply(source)
- .collect(Collectors.toList());
+ private static AbstractMap.SimpleEntry entry(final K key, final V value) {
+ return new AbstractMap.SimpleEntry<>(key, value);
}
}
diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java
index aea4769..f193f1b 100644
--- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java
+++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScanner.java
@@ -21,11 +21,11 @@
package net.kemitix.checkstyle.ruleset.builder;
-import com.google.common.reflect.ClassPath;
-import lombok.RequiredArgsConstructor;
+import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import org.springframework.stereotype.Service;
-import java.util.stream.Stream;
+import java.util.List;
+import java.util.stream.Collectors;
/**
* Default implementation of {@link PackageScanner}.
@@ -33,15 +33,16 @@ import java.util.stream.Stream;
* @author Paul Campbell (pcampbell@kemitix.net).
*/
@Service
-@RequiredArgsConstructor
public class DefaultPackageScanner implements PackageScanner {
- private final ClassPath classPath;
-
@Override
- public final Stream apply(final RuleSource ruleSource) {
- return classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage())
- .stream()
- .map(ClassPath.ClassInfo::getName);
+ public final List apply(final RuleSource ruleSource) {
+ final String basePackage = ruleSource.getBasePackage();
+ return new FastClasspathScanner(basePackage)
+ .scan()
+ .getNamesOfAllStandardClasses()
+ .stream()
+ .filter(packageName -> packageName.startsWith(basePackage))
+ .collect(Collectors.toList());
}
}
diff --git a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java
index 7cc8c2b..02a59df 100644
--- a/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java
+++ b/builder/src/main/java/net/kemitix/checkstyle/ruleset/builder/PackageScanner.java
@@ -21,14 +21,14 @@
package net.kemitix.checkstyle.ruleset.builder;
+import java.util.List;
import java.util.function.Function;
-import java.util.stream.Stream;
/**
* Scans a package for all classes.
*
* @author Paul Campbell (pcampbell@kemitix.net).
*/
-interface PackageScanner extends Function> {
+interface PackageScanner extends Function> {
}
diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java
index 9279196..f9afaa8 100644
--- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java
+++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/BuilderConfigurationTest.java
@@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
+import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -20,28 +21,20 @@ import static org.mockito.Mockito.mock;
*/
public class BuilderConfigurationTest {
- @Test
- public void canGetClassPath() throws IOException {
- //when
- final ClassPath classPath = new BuilderConfiguration().classPath();
- //then
- assertThat(classPath).isNotNull();
- }
-
@Test
public void canGetCheckClasses() {
//given
final PackageScanner packageScanner = mock(PackageScanner.class);
final String checkstyleClass = "checkstyle class";
- given(packageScanner.apply(RuleSource.CHECKSTYLE)).willReturn(Stream.of(checkstyleClass));
+ given(packageScanner.apply(RuleSource.CHECKSTYLE)).willReturn(singletonList(checkstyleClass));
final String sevntuClass = "sevntu class";
- given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(Stream.of(sevntuClass));
+ given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(singletonList(sevntuClass));
//when
final Map> checkClasses = new BuilderConfiguration().checkClasses(packageScanner);
//then
assertThat(checkClasses).containsOnlyKeys(RuleSource.values());
assertThat(checkClasses)
- .containsEntry(RuleSource.CHECKSTYLE, Collections.singletonList(checkstyleClass))
- .containsEntry(RuleSource.SEVNTU, Collections.singletonList(sevntuClass));
+ .containsEntry(RuleSource.CHECKSTYLE, singletonList(checkstyleClass))
+ .containsEntry(RuleSource.SEVNTU, singletonList(sevntuClass));
}
}
diff --git a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java
index 240d12c..7b5a02e 100644
--- a/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java
+++ b/builder/src/test/java/net/kemitix/checkstyle/ruleset/builder/DefaultPackageScannerTest.java
@@ -1,10 +1,8 @@
package net.kemitix.checkstyle.ruleset.builder;
-import com.google.common.reflect.ClassPath;
-import org.junit.Before;
import org.junit.Test;
-import java.io.IOException;
+import java.util.List;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
@@ -16,27 +14,21 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class DefaultPackageScannerTest {
- private PackageScanner scanner;
-
- @Before
- public void setUp() throws Exception {
- final ClassPath classPath = ClassPath.from(getClass().getClassLoader());
- scanner = new DefaultPackageScanner(classPath);
- }
+ private PackageScanner scanner = new DefaultPackageScanner();
@Test
- public void canScanCheckstylePackage() throws IOException {
+ public void canScanCheckstylePackage() {
//when
- final Stream result = scanner.apply(RuleSource.CHECKSTYLE);
+ final List result = scanner.apply(RuleSource.CHECKSTYLE);
//then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.CHECKSTYLE.getBasePackage()))
.contains("com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck");
}
@Test
- public void canScanSevntuPackage() throws IOException {
+ public void canScanSevntuPackage() {
//when
- final Stream result = scanner.apply(RuleSource.SEVNTU);
+ final List result = scanner.apply(RuleSource.SEVNTU);
//then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.SEVNTU.getBasePackage()))
.contains("com.github.sevntu.checkstyle.checks.design.NestedSwitchCheck");