Refactor JvmRuntimeVersionsConsistencyChecker for readability
This commit is contained in:
+51
-32
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.config.LanguageVersion
|
|||||||
import org.jetbrains.kotlin.config.LanguageVersion.KOTLIN_1_0
|
import org.jetbrains.kotlin.config.LanguageVersion.KOTLIN_1_0
|
||||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||||
import org.jetbrains.kotlin.config.MavenComparableVersion
|
import org.jetbrains.kotlin.config.MavenComparableVersion
|
||||||
|
import java.io.IOException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.jar.Attributes
|
import java.util.jar.Attributes
|
||||||
import java.util.jar.Manifest
|
import java.util.jar.Manifest
|
||||||
@@ -102,9 +103,9 @@ object JvmRuntimeVersionsConsistencyChecker {
|
|||||||
.assertNotNull { "$MANIFEST_KOTLIN_RUNTIME_COMPONENT_MAIN not found in kotlinManifest.properties" }
|
.assertNotNull { "$MANIFEST_KOTLIN_RUNTIME_COMPONENT_MAIN not found in kotlinManifest.properties" }
|
||||||
}
|
}
|
||||||
|
|
||||||
private class KotlinLibraryFile(val component: String, val file: VirtualFile, val version: MavenComparableVersion) {
|
private class KotlinLibraryFile(val file: VirtualFile, val version: MavenComparableVersion) {
|
||||||
override fun toString(): String =
|
override fun toString(): String =
|
||||||
"${file.name}:$version ($component)"
|
"${file.name}:$version"
|
||||||
}
|
}
|
||||||
|
|
||||||
private class RuntimeJarsInfo(
|
private class RuntimeJarsInfo(
|
||||||
@@ -219,44 +220,62 @@ object JvmRuntimeVersionsConsistencyChecker {
|
|||||||
val otherLibrariesWithBundledRuntime = ArrayList<VirtualFile>(0)
|
val otherLibrariesWithBundledRuntime = ArrayList<VirtualFile>(0)
|
||||||
|
|
||||||
for (jarRoot in classpathJarRoots) {
|
for (jarRoot in classpathJarRoots) {
|
||||||
val manifest = try {
|
val fileKind = determineFileKind(jarRoot)
|
||||||
jarRoot.findFileByRelativePath(MANIFEST_MF)?.let { Manifest(it.inputStream) }
|
if (fileKind is FileKind.Irrelevant) continue
|
||||||
}
|
|
||||||
catch (e: Exception) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val runtimeComponent = manifest?.mainAttributes?.getValue(KOTLIN_RUNTIME_COMPONENT_ATTRIBUTE)
|
|
||||||
if (runtimeComponent == null) {
|
|
||||||
if (jarRoot.findFileByRelativePath(KOTLIN_STDLIB_MODULE) != null ||
|
|
||||||
jarRoot.findFileByRelativePath(KOTLIN_REFLECT_MODULE) != null) {
|
|
||||||
val jarFile = VfsUtilCore.getVirtualFileForJar(jarRoot) ?: continue
|
|
||||||
if (isGenuineKotlinRuntime(manifest)) {
|
|
||||||
jars.add(KotlinLibraryFile(KOTLIN_RUNTIME_COMPONENT_MAIN, jarFile, MavenComparableVersion(KOTLIN_1_0)))
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
otherLibrariesWithBundledRuntime.add(jarFile)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
val jarFile = VfsUtilCore.getVirtualFileForJar(jarRoot) ?: continue
|
val jarFile = VfsUtilCore.getVirtualFileForJar(jarRoot) ?: continue
|
||||||
val version = manifest.getKotlinLanguageVersion()
|
when (fileKind) {
|
||||||
val file = KotlinLibraryFile(runtimeComponent, jarFile, version)
|
is FileKind.Runtime -> {
|
||||||
|
val file = KotlinLibraryFile(jarFile, fileKind.version)
|
||||||
if (runtimeComponent == KOTLIN_RUNTIME_COMPONENT_CORE) {
|
jars.add(file)
|
||||||
jars.add(file)
|
if (fileKind.isCoreComponent) {
|
||||||
coreJars.add(file)
|
coreJars.add(file)
|
||||||
}
|
}
|
||||||
else if (runtimeComponent == KOTLIN_RUNTIME_COMPONENT_MAIN) {
|
}
|
||||||
jars.add(file)
|
FileKind.OldRuntime -> jars.add(KotlinLibraryFile(jarFile, MavenComparableVersion(KOTLIN_1_0)))
|
||||||
|
FileKind.LibraryWithBundledRuntime -> otherLibrariesWithBundledRuntime.add(jarFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return RuntimeJarsInfo(jars, coreJars, otherLibrariesWithBundledRuntime)
|
return RuntimeJarsInfo(jars, coreJars, otherLibrariesWithBundledRuntime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private sealed class FileKind {
|
||||||
|
class Runtime(val version: MavenComparableVersion, val isCoreComponent: Boolean) : FileKind()
|
||||||
|
|
||||||
|
// Runtime library of Kotlin 1.0
|
||||||
|
object OldRuntime : FileKind()
|
||||||
|
|
||||||
|
object LibraryWithBundledRuntime : FileKind()
|
||||||
|
|
||||||
|
object Irrelevant : FileKind()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun determineFileKind(jarRoot: VirtualFile): FileKind {
|
||||||
|
val manifestFile = jarRoot.findFileByRelativePath(MANIFEST_MF)
|
||||||
|
val manifest = try {
|
||||||
|
manifestFile?.let { Manifest(it.inputStream) }
|
||||||
|
}
|
||||||
|
catch (e: IOException) {
|
||||||
|
return FileKind.Irrelevant
|
||||||
|
}
|
||||||
|
|
||||||
|
val runtimeComponent = manifest?.mainAttributes?.getValue(KOTLIN_RUNTIME_COMPONENT_ATTRIBUTE)
|
||||||
|
return when (runtimeComponent) {
|
||||||
|
KOTLIN_RUNTIME_COMPONENT_MAIN ->
|
||||||
|
FileKind.Runtime(manifest.getKotlinLanguageVersion(), isCoreComponent = false)
|
||||||
|
KOTLIN_RUNTIME_COMPONENT_CORE ->
|
||||||
|
FileKind.Runtime(manifest.getKotlinLanguageVersion(), isCoreComponent = true)
|
||||||
|
null -> when {
|
||||||
|
jarRoot.findFileByRelativePath(KOTLIN_STDLIB_MODULE) == null &&
|
||||||
|
jarRoot.findFileByRelativePath(KOTLIN_REFLECT_MODULE) == null -> FileKind.Irrelevant
|
||||||
|
isGenuineKotlinRuntime(manifest) -> FileKind.OldRuntime
|
||||||
|
else -> FileKind.LibraryWithBundledRuntime
|
||||||
|
}
|
||||||
|
else -> FileKind.Irrelevant
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Returns true if the manifest is from the original Kotlin Runtime jar, false if it's from a library with a bundled runtime
|
// Returns true if the manifest is from the original Kotlin Runtime jar, false if it's from a library with a bundled runtime
|
||||||
private fun isGenuineKotlinRuntime(manifest: Manifest?): Boolean {
|
private fun isGenuineKotlinRuntime(manifest: Manifest?): Boolean {
|
||||||
return manifest != null &&
|
return manifest != null &&
|
||||||
|
|||||||
Reference in New Issue
Block a user