builder: scan class path in java 9

This commit is contained in:
Paul Campbell 2018-03-10 23:41:35 +00:00
parent 5b28ecf762
commit 7ba2b557e3

View file

@ -22,6 +22,7 @@
package net.kemitix.checkstyle.ruleset.builder; package net.kemitix.checkstyle.ruleset.builder;
import com.google.common.reflect.ClassPath; import com.google.common.reflect.ClassPath;
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.kemitix.conditional.Value; import net.kemitix.conditional.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -44,23 +45,25 @@ public class DefaultPackageScanner implements PackageScanner {
@Override @Override
public final Stream<String> apply(final RuleSource ruleSource) { public final Stream<String> apply(final RuleSource ruleSource) {
return Value.<Stream<String>>where(isJava8()) return Value.<Stream<String>>where(isJava8())
.then(scanPackageByClassPath(ruleSource)) .then(scanPackageByClassPath(ruleSource.getBasePackage()))
.otherwise(scanPackageByModulePath()); .otherwise(scanPackageByModulePath(ruleSource.getBasePackage()));
} }
private boolean isJava8() { private boolean isJava8() {
return getClass().getClassLoader() instanceof URLClassLoader; return getClass().getClassLoader() instanceof URLClassLoader;
} }
private Supplier<Stream<String>> scanPackageByClassPath(final RuleSource ruleSource) { private Supplier<Stream<String>> scanPackageByClassPath(final String basePackage) {
return () -> classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage()) return () -> classPath.getTopLevelClassesRecursive(basePackage)
.stream() .stream()
.map(ClassPath.ClassInfo::getName); .map(ClassPath.ClassInfo::getName);
} }
private Supplier<Stream<String>> scanPackageByModulePath() { private Supplier<Stream<String>> scanPackageByModulePath(final String basePackage) {
return () -> { return () -> new FastClasspathScanner(basePackage)
throw new UnsupportedOperationException("Java 9 Module Path in unsupported"); .scan()
}; .getNamesOfAllStandardClasses()
.stream()
.filter(packageName -> packageName.startsWith(basePackage));
} }
} }