Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom rulesets and reporters on Java 9+ #573

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.pinterest.ktlint.core.ParseException
import com.pinterest.ktlint.core.Reporter
import com.pinterest.ktlint.core.ReporterProvider
import com.pinterest.ktlint.core.RuleExecutionException
import com.pinterest.ktlint.core.RuleSet
import com.pinterest.ktlint.core.RuleSetProvider
import com.pinterest.ktlint.internal.ApplyToIDEAGloballySubCommand
import com.pinterest.ktlint.internal.GitPreCommitHookSubCommand
Expand Down Expand Up @@ -224,7 +225,7 @@ class KtlintCommandLine {
val start = System.currentTimeMillis()

// load 3rd party ruleset(s) (if any)
if (rulesets.isNotEmpty()) loadJARs(rulesets)
if (rulesets.isNotEmpty()) loadJARs(rulesets, RuleSet::class.java.classLoader)

// Detect custom rulesets that have not been moved to the new package
if (ServiceLoader.load(com.github.shyiko.ktlint.core.RuleSetProvider::class.java).any()) {
Expand Down Expand Up @@ -370,7 +371,7 @@ class KtlintCommandLine {
.mapNotNull { it.artifact }
.distinct()
if (missingReporters.isNotEmpty()) {
loadJARs(missingReporters)
loadJARs(missingReporters, Reporter::class.java.classLoader)
reporterLoader.reload()
reporterLoader.associateBy { it.id }
}
Expand Down Expand Up @@ -478,8 +479,7 @@ class KtlintCommandLine {

private fun <T> List<T>.head(limit: Int) = if (limit == size) this else this.subList(0, limit)

// fixme: isn't going to work on JDK 9
private fun loadJARs(artifacts: List<String>) {
private fun loadJARs(artifacts: List<String>, classLoader: ClassLoader) {
val jarUrls = artifacts
.map {
val artifactFile = File(expandTilde(it))
Expand All @@ -490,8 +490,8 @@ class KtlintCommandLine {
artifactFile.toURI().toURL()
}

val classLoader = ClassLoader.getSystemClassLoader() as URLClassLoader
classLoader.addURLs(jarUrls)
val urlClassLoader = URLClassLoader(jarUrls.toTypedArray(), classLoader)
urlClassLoader.addURLs(jarUrls)
}

private fun parseQuery(query: String) =
Expand Down