builder: split Java 8 and 9 class path scanning - java 9 unsupported

This commit is contained in:
Paul Campbell 2018-03-10 22:52:08 +00:00
parent 7ae239809d
commit f333d47613

View file

@ -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<String> apply(final RuleSource ruleSource) {
return classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage())
return Value.<Stream<String>>where(isJava8())
.then(scanPackageByClassPath(ruleSource))
.otherwise(scanPackageByModulePath());
}
private boolean isJava8() {
return getClass().getClassLoader() instanceof URLClassLoader;
}
private Supplier<Stream<String>> scanPackageByClassPath(final RuleSource ruleSource) {
return () -> classPath.getTopLevelClassesRecursive(ruleSource.getBasePackage())
.stream()
.map(ClassPath.ClassInfo::getName);
}
private Supplier<Stream<String>> scanPackageByModulePath() {
return () -> {
throw new UnsupportedOperationException("Java 9 Module Path in unsupported");
};
}
}