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>
<mockito.version>2.13.0</mockito.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>
<kemitix.checkstyle.ruleset.version>${project.version}</kemitix.checkstyle.ruleset.version>
@ -58,6 +59,11 @@
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.github.lukehutch</groupId>
<artifactId>fast-classpath-scanner</artifactId>
<version>${fast-classpath-scanner.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View file

@ -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<RuleSource, List<String>> 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<RuleSource, AbstractMap.SimpleEntry<RuleSource, List<String>>> toRuleSourceEntry(
final PackageScanner packageScanner
) {
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());
private static <K, V> AbstractMap.SimpleEntry<K, V> entry(final K key, final V value) {
return new AbstractMap.SimpleEntry<>(key, value);
}
}

View file

@ -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<String> apply(final RuleSource ruleSource) {
return classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage())
.stream()
.map(ClassPath.ClassInfo::getName);
public final List<String> apply(final RuleSource ruleSource) {
final String basePackage = ruleSource.getBasePackage();
return new FastClasspathScanner(basePackage)
.scan()
.getNamesOfAllStandardClasses()
.stream()
.filter(packageName -> packageName.startsWith(basePackage))
.collect(Collectors.toList());
}
}

View file

@ -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<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.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<RuleSource, List<String>> 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));
}
}

View file

@ -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<String> result = scanner.apply(RuleSource.CHECKSTYLE);
final List<String> 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<String> result = scanner.apply(RuleSource.SEVNTU);
final List<String> result = scanner.apply(RuleSource.SEVNTU);
//then
assertThat(result).allMatch(cn -> cn.startsWith(RuleSource.SEVNTU.getBasePackage()))
.contains("com.github.sevntu.checkstyle.checks.design.NestedSwitchCheck");