From defa19a14383377c6d18b3a12725a6935eac577e Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Fri, 9 Mar 2018 19:52:10 +0000 Subject: [PATCH 1/9] builder: use BuilderConfiguration to provide the classLoader --- .../ruleset/builder/BuilderConfiguration.java | 10 ++++++++-- .../ruleset/builder/BuilderConfigurationTest.java | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) 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..727e3ba 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 @@ -47,10 +47,16 @@ public class BuilderConfiguration { * @return the ClassPath * * @throws IOException if there is an error + * @param classLoader */ @Bean - public ClassPath classPath() throws IOException { - return ClassPath.from(getClass().getClassLoader()); + public ClassPath classPath(final ClassLoader classLoader) throws IOException { + return ClassPath.from(classLoader); + } + + @Bean + public ClassLoader classLoader() { + return BuilderConfiguration.class.getClassLoader(); } /** 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..63eb6c2 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 @@ -23,7 +23,8 @@ public class BuilderConfigurationTest { @Test public void canGetClassPath() throws IOException { //when - final ClassPath classPath = new BuilderConfiguration().classPath(); + final BuilderConfiguration builderConfiguration = new BuilderConfiguration(); + final ClassPath classPath = builderConfiguration.classPath(builderConfiguration.getClass().getClassLoader()); //then assertThat(classPath).isNotNull(); } From 7ae239809d0bc5d71f5d6ad23f11d1b0edfaf934 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 10 Mar 2018 22:51:10 +0000 Subject: [PATCH 2/9] builder: add dependency on conditional --- builder/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/builder/pom.xml b/builder/pom.xml index 24c374f..574713a 100644 --- a/builder/pom.xml +++ b/builder/pom.xml @@ -31,6 +31,7 @@ 1.0.0 2.13.0 3.9.0 + 0.3.0 3.0.0 ${project.version} @@ -58,6 +59,11 @@ ${lombok.version} provided + + net.kemitix + conditional + ${conditional.version} + org.mockito mockito-core From f333d47613337d52005e90446397c082b12cffaf Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 10 Mar 2018 22:52:08 +0000 Subject: [PATCH 3/9] builder: split Java 8 and 9 class path scanning - java 9 unsupported --- .../builder/DefaultPackageScanner.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) 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..1e7c246 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 @@ -23,8 +23,11 @@ package net.kemitix.checkstyle.ruleset.builder; import com.google.common.reflect.ClassPath; import lombok.RequiredArgsConstructor; +import net.kemitix.conditional.Value; import org.springframework.stereotype.Service; +import java.net.URLClassLoader; +import java.util.function.Supplier; import java.util.stream.Stream; /** @@ -40,8 +43,24 @@ public class DefaultPackageScanner implements PackageScanner { @Override public final Stream apply(final RuleSource ruleSource) { - return classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage()) - .stream() - .map(ClassPath.ClassInfo::getName); + return Value.>where(isJava8()) + .then(scanPackageByClassPath(ruleSource)) + .otherwise(scanPackageByModulePath()); + } + + private boolean isJava8() { + return getClass().getClassLoader() instanceof URLClassLoader; + } + + private Supplier> scanPackageByClassPath(final RuleSource ruleSource) { + return () -> classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage()) + .stream() + .map(ClassPath.ClassInfo::getName); + } + + private Supplier> scanPackageByModulePath() { + return () -> { + throw new UnsupportedOperationException("Java 9 Module Path in unsupported"); + }; } } From 5b28ecf7628748a6880ac36bfa5559c1b31f350f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 10 Mar 2018 23:39:59 +0000 Subject: [PATCH 4/9] builder: add dependency on fast-classpath-scanner --- builder/pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/builder/pom.xml b/builder/pom.xml index 574713a..73d39c3 100644 --- a/builder/pom.xml +++ b/builder/pom.xml @@ -32,6 +32,7 @@ 2.13.0 3.9.0 0.3.0 + 2.18.1 3.0.0 ${project.version} @@ -64,6 +65,11 @@ conditional ${conditional.version} + + io.github.lukehutch + fast-classpath-scanner + ${fast-classpath-scanner.version} + org.mockito mockito-core From 7ba2b557e34fa422c68bc5fc32d69dc557c4e040 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 10 Mar 2018 23:41:35 +0000 Subject: [PATCH 5/9] builder: scan class path in java 9 --- .../builder/DefaultPackageScanner.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) 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 1e7c246..795271a 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 @@ -22,6 +22,7 @@ package net.kemitix.checkstyle.ruleset.builder; import com.google.common.reflect.ClassPath; +import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; import lombok.RequiredArgsConstructor; import net.kemitix.conditional.Value; import org.springframework.stereotype.Service; @@ -44,23 +45,25 @@ public class DefaultPackageScanner implements PackageScanner { @Override public final Stream apply(final RuleSource ruleSource) { return Value.>where(isJava8()) - .then(scanPackageByClassPath(ruleSource)) - .otherwise(scanPackageByModulePath()); + .then(scanPackageByClassPath(ruleSource.getBasePackage())) + .otherwise(scanPackageByModulePath(ruleSource.getBasePackage())); } private boolean isJava8() { return getClass().getClassLoader() instanceof URLClassLoader; } - private Supplier> scanPackageByClassPath(final RuleSource ruleSource) { - return () -> classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage()) + private Supplier> scanPackageByClassPath(final String basePackage) { + return () -> classPath.getTopLevelClassesRecursive(basePackage) .stream() .map(ClassPath.ClassInfo::getName); } - private Supplier> scanPackageByModulePath() { - return () -> { - throw new UnsupportedOperationException("Java 9 Module Path in unsupported"); - }; + private Supplier> scanPackageByModulePath(final String basePackage) { + return () -> new FastClasspathScanner(basePackage) + .scan() + .getNamesOfAllStandardClasses() + .stream() + .filter(packageName -> packageName.startsWith(basePackage)); } } From d59cb7b87931604570b1be0eaec445eae9020e0b Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 11 Mar 2018 08:39:20 +0000 Subject: [PATCH 6/9] builder: use FastClasspathScanner for Java 8 too --- builder/pom.xml | 6 ----- .../builder/DefaultPackageScanner.java | 27 ++----------------- .../builder/DefaultPackageScannerTest.java | 15 +++-------- 3 files changed, 5 insertions(+), 43 deletions(-) diff --git a/builder/pom.xml b/builder/pom.xml index 73d39c3..18f07d5 100644 --- a/builder/pom.xml +++ b/builder/pom.xml @@ -31,7 +31,6 @@ 1.0.0 2.13.0 3.9.0 - 0.3.0 2.18.1 3.0.0 @@ -60,11 +59,6 @@ ${lombok.version} provided - - net.kemitix - conditional - ${conditional.version} - io.github.lukehutch fast-classpath-scanner 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 795271a..0628f4d 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,14 +21,9 @@ package net.kemitix.checkstyle.ruleset.builder; -import com.google.common.reflect.ClassPath; import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner; -import lombok.RequiredArgsConstructor; -import net.kemitix.conditional.Value; import org.springframework.stereotype.Service; -import java.net.URLClassLoader; -import java.util.function.Supplier; import java.util.stream.Stream; /** @@ -37,30 +32,12 @@ 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 Value.>where(isJava8()) - .then(scanPackageByClassPath(ruleSource.getBasePackage())) - .otherwise(scanPackageByModulePath(ruleSource.getBasePackage())); - } - - private boolean isJava8() { - return getClass().getClassLoader() instanceof URLClassLoader; - } - - private Supplier> scanPackageByClassPath(final String basePackage) { - return () -> classPath.getTopLevelClassesRecursive(basePackage) - .stream() - .map(ClassPath.ClassInfo::getName); - } - - private Supplier> scanPackageByModulePath(final String basePackage) { - return () -> new FastClasspathScanner(basePackage) + final String basePackage = ruleSource.getBasePackage(); + return new FastClasspathScanner(basePackage) .scan() .getNamesOfAllStandardClasses() .stream() 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..92ca00b 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,7 @@ 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.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -16,16 +13,10 @@ 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); //then @@ -34,7 +25,7 @@ public class DefaultPackageScannerTest { } @Test - public void canScanSevntuPackage() throws IOException { + public void canScanSevntuPackage() { //when final Stream result = scanner.apply(RuleSource.SEVNTU); //then From 359d15b78f9852b56b1f474f2282bddadb074849 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 11 Mar 2018 08:51:03 +0000 Subject: [PATCH 7/9] builder: Clean up Configuration --- .../ruleset/builder/BuilderConfiguration.java | 43 +++---------------- 1 file changed, 7 insertions(+), 36 deletions(-) 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 727e3ba..f14e09a 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,24 +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 - * @param classLoader - */ - @Bean - public ClassPath classPath(final ClassLoader classLoader) throws IOException { - return ClassPath.from(classLoader); - } - - @Bean - public ClassLoader classLoader() { - return BuilderConfiguration.class.getClassLoader(); - } - /** * A Map of rules for each RuleSource. * @@ -69,24 +48,16 @@ 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.toList()))) .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 AbstractMap.SimpleEntry> entry( + final RuleSource key, + final List value + ) { + return new AbstractMap.SimpleEntry<>(key, value); } - private static List classesInSource( - final RuleSource source, - final PackageScanner packageScanner - ) { - return packageScanner.apply(source) - .collect(Collectors.toList()); - } } From 4de6b398215eeb8a2532bb1e77e72735af2fa1d5 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 11 Mar 2018 08:54:03 +0000 Subject: [PATCH 8/9] builder: PackageScanner now returns a List rather than a Stream --- .../ruleset/builder/BuilderConfiguration.java | 9 ++------- .../ruleset/builder/DefaultPackageScanner.java | 8 +++++--- .../ruleset/builder/PackageScanner.java | 4 ++-- .../builder/BuilderConfigurationTest.java | 18 +++++------------- .../builder/DefaultPackageScannerTest.java | 5 +++-- 5 files changed, 17 insertions(+), 27 deletions(-) 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 f14e09a..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 @@ -48,16 +48,11 @@ public class BuilderConfiguration { @Bean public Map> checkClasses(final PackageScanner packageScanner) { return Stream.of(RuleSource.values()) - .map(source -> entry(source, packageScanner.apply(source) - .collect(Collectors.toList()))) + .map(source -> entry(source, packageScanner.apply(source))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } - private static AbstractMap.SimpleEntry> entry( - final RuleSource key, - final List value - ) { + 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 0628f4d..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 @@ -24,7 +24,8 @@ package net.kemitix.checkstyle.ruleset.builder; 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}. @@ -35,12 +36,13 @@ import java.util.stream.Stream; public class DefaultPackageScanner implements PackageScanner { @Override - public final Stream apply(final RuleSource ruleSource) { + public final List apply(final RuleSource ruleSource) { final String basePackage = ruleSource.getBasePackage(); return new FastClasspathScanner(basePackage) .scan() .getNamesOfAllStandardClasses() .stream() - .filter(packageName -> packageName.startsWith(basePackage)); + .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 63eb6c2..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,29 +21,20 @@ import static org.mockito.Mockito.mock; */ public class BuilderConfigurationTest { - @Test - public void canGetClassPath() throws IOException { - //when - final BuilderConfiguration builderConfiguration = new BuilderConfiguration(); - final ClassPath classPath = builderConfiguration.classPath(builderConfiguration.getClass().getClassLoader()); - //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 92ca00b..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 @@ -2,6 +2,7 @@ package net.kemitix.checkstyle.ruleset.builder; import org.junit.Test; +import java.util.List; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; @@ -18,7 +19,7 @@ public class DefaultPackageScannerTest { @Test 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"); @@ -27,7 +28,7 @@ public class DefaultPackageScannerTest { @Test 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"); From 5146b0e3cd42f909b59674269e107395dca549e1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 11 Mar 2018 09:09:32 +0000 Subject: [PATCH 9/9] Fix root pom artifactId --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c070c77..6cbff27 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 net.kemitix.checkstyle - root + kemitix-checkstyle-ruleset 4.1.0-SNAPSHOT pom