Merge pull request #77 from kemitix/java-9-compatibility

Java 9 compatibility
This commit is contained in:
Paul Campbell 2018-03-11 09:37:27 +00:00 committed by GitHub
commit bc53254cd2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 33 additions and 69 deletions

View file

@ -31,6 +31,7 @@
<map-builder.version>1.0.0</map-builder.version> <map-builder.version>1.0.0</map-builder.version>
<mockito.version>2.13.0</mockito.version> <mockito.version>2.13.0</mockito.version>
<assertj.version>3.9.0</assertj.version> <assertj.version>3.9.0</assertj.version>
<fast-classpath-scanner.version>2.18.1</fast-classpath-scanner.version>
<maven-checkstyle-plugin.version>3.0.0</maven-checkstyle-plugin.version> <maven-checkstyle-plugin.version>3.0.0</maven-checkstyle-plugin.version>
<kemitix.checkstyle.ruleset.version>${project.version}</kemitix.checkstyle.ruleset.version> <kemitix.checkstyle.ruleset.version>${project.version}</kemitix.checkstyle.ruleset.version>
@ -58,6 +59,11 @@
<version>${lombok.version}</version> <version>${lombok.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>io.github.lukehutch</groupId>
<artifactId>fast-classpath-scanner</artifactId>
<version>${fast-classpath-scanner.version}</version>
</dependency>
<dependency> <dependency>
<groupId>org.mockito</groupId> <groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>

View file

@ -21,15 +21,12 @@
package net.kemitix.checkstyle.ruleset.builder; package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -41,18 +38,6 @@ import java.util.stream.Stream;
@Configuration @Configuration
public class BuilderConfiguration { 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. * A Map of rules for each RuleSource.
* *
@ -63,24 +48,11 @@ public class BuilderConfiguration {
@Bean @Bean
public Map<RuleSource, List<String>> checkClasses(final PackageScanner packageScanner) { public Map<RuleSource, List<String>> checkClasses(final PackageScanner packageScanner) {
return Stream.of(RuleSource.values()) return Stream.of(RuleSource.values())
.map(toRuleSourceEntry(packageScanner)) .map(source -> entry(source, packageScanner.apply(source)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} }
private static Function<RuleSource, AbstractMap.SimpleEntry<RuleSource, List<String>>> toRuleSourceEntry( private static <K, V> AbstractMap.SimpleEntry<K, V> entry(final K key, final V value) {
final PackageScanner packageScanner return new AbstractMap.SimpleEntry<>(key, value);
) {
return source -> new AbstractMap.SimpleEntry<>(
source,
classesInSource(source, packageScanner)
);
}
private static List<String> classesInSource(
final RuleSource source,
final PackageScanner packageScanner
) {
return packageScanner.apply(source)
.collect(Collectors.toList());
} }
} }

View file

@ -21,11 +21,11 @@
package net.kemitix.checkstyle.ruleset.builder; package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath; import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.stream.Stream; import java.util.List;
import java.util.stream.Collectors;
/** /**
* Default implementation of {@link PackageScanner}. * Default implementation of {@link PackageScanner}.
@ -33,15 +33,16 @@ import java.util.stream.Stream;
* @author Paul Campbell (pcampbell@kemitix.net). * @author Paul Campbell (pcampbell@kemitix.net).
*/ */
@Service @Service
@RequiredArgsConstructor
public class DefaultPackageScanner implements PackageScanner { public class DefaultPackageScanner implements PackageScanner {
private final ClassPath classPath;
@Override @Override
public final Stream<String> apply(final RuleSource ruleSource) { public final List<String> apply(final RuleSource ruleSource) {
return classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage()) final String basePackage = ruleSource.getBasePackage();
.stream() return new FastClasspathScanner(basePackage)
.map(ClassPath.ClassInfo::getName); .scan()
.getNamesOfAllStandardClasses()
.stream()
.filter(packageName -> packageName.startsWith(basePackage))
.collect(Collectors.toList());
} }
} }

View file

@ -21,14 +21,14 @@
package net.kemitix.checkstyle.ruleset.builder; package net.kemitix.checkstyle.ruleset.builder;
import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Stream;
/** /**
* Scans a package for all classes. * Scans a package for all classes.
* *
* @author Paul Campbell (pcampbell@kemitix.net). * @author Paul Campbell (pcampbell@kemitix.net).
*/ */
interface PackageScanner extends Function<RuleSource, Stream<String>> { interface PackageScanner extends Function<RuleSource, List<String>> {
} }

View file

@ -9,6 +9,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Stream; import java.util.stream.Stream;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -20,28 +21,20 @@ import static org.mockito.Mockito.mock;
*/ */
public class BuilderConfigurationTest { public class BuilderConfigurationTest {
@Test
public void canGetClassPath() throws IOException {
//when
final ClassPath classPath = new BuilderConfiguration().classPath();
//then
assertThat(classPath).isNotNull();
}
@Test @Test
public void canGetCheckClasses() { public void canGetCheckClasses() {
//given //given
final PackageScanner packageScanner = mock(PackageScanner.class); final PackageScanner packageScanner = mock(PackageScanner.class);
final String checkstyleClass = "checkstyle 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"; final String sevntuClass = "sevntu class";
given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(Stream.of(sevntuClass)); given(packageScanner.apply(RuleSource.SEVNTU)).willReturn(singletonList(sevntuClass));
//when //when
final Map<RuleSource, List<String>> checkClasses = new BuilderConfiguration().checkClasses(packageScanner); final Map<RuleSource, List<String>> checkClasses = new BuilderConfiguration().checkClasses(packageScanner);
//then //then
assertThat(checkClasses).containsOnlyKeys(RuleSource.values()); assertThat(checkClasses).containsOnlyKeys(RuleSource.values());
assertThat(checkClasses) assertThat(checkClasses)
.containsEntry(RuleSource.CHECKSTYLE, Collections.singletonList(checkstyleClass)) .containsEntry(RuleSource.CHECKSTYLE, singletonList(checkstyleClass))
.containsEntry(RuleSource.SEVNTU, Collections.singletonList(sevntuClass)); .containsEntry(RuleSource.SEVNTU, singletonList(sevntuClass));
} }
} }

View file

@ -1,10 +1,8 @@
package net.kemitix.checkstyle.ruleset.builder; package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import java.io.IOException; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -16,27 +14,21 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class DefaultPackageScannerTest { public class DefaultPackageScannerTest {
private PackageScanner scanner; private PackageScanner scanner = new DefaultPackageScanner();
@Before
public void setUp() throws Exception {
final ClassPath classPath = ClassPath.from(getClass().getClassLoader());
scanner = new DefaultPackageScanner(classPath);
}
@Test @Test
public void canScanCheckstylePackage() throws IOException { public void canScanCheckstylePackage() {
//when //when
final Stream<String> result = scanner.apply(RuleSource.CHECKSTYLE); final List<String> result = scanner.apply(RuleSource.CHECKSTYLE);
//then //then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.CHECKSTYLE.getBasePackage())) assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.CHECKSTYLE.getBasePackage()))
.contains("com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck"); .contains("com.puppycrawl.tools.checkstyle.checks.sizes.FileLengthCheck");
} }
@Test @Test
public void canScanSevntuPackage() throws IOException { public void canScanSevntuPackage() {
//when //when
final Stream<String> result = scanner.apply(RuleSource.SEVNTU); final List<String> result = scanner.apply(RuleSource.SEVNTU);
//then //then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.SEVNTU.getBasePackage())) assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.SEVNTU.getBasePackage()))
.contains("com.github.sevntu.checkstyle.checks.design.NestedSwitchCheck"); .contains("com.github.sevntu.checkstyle.checks.design.NestedSwitchCheck");