Refactor API version inference/checking code
The behavior is not changed in this commit
This commit is contained in:
+28
-35
@@ -102,33 +102,38 @@ object JvmRuntimeVersionsConsistencyChecker {
|
|||||||
if (runtimeJarsInfo.jars.isEmpty()) return
|
if (runtimeJarsInfo.jars.isEmpty()) return
|
||||||
|
|
||||||
val languageVersionSettings = configuration.languageVersionSettings
|
val languageVersionSettings = configuration.languageVersionSettings
|
||||||
val apiVersion = languageVersionSettings.apiVersion.version
|
val currentApi = languageVersionSettings.apiVersion
|
||||||
|
|
||||||
val consistency = checkCompilerClasspathConsistency(messageCollector, apiVersion, runtimeJarsInfo)
|
val consistency = checkCompilerClasspathConsistency(messageCollector, currentApi.version, runtimeJarsInfo)
|
||||||
if (consistency is ClasspathConsistency.InconsistentWithApiVersion) {
|
if (consistency is ClasspathConsistency.InconsistentWithApiVersion) {
|
||||||
val actualRuntimeVersion = consistency.actualRuntimeVersion
|
val actualRuntimeVersion = consistency.actualRuntimeVersion
|
||||||
messageCollector.issue(
|
messageCollector.issue(
|
||||||
null,
|
null,
|
||||||
"Runtime JAR files in the classpath have the version $actualRuntimeVersion, " +
|
"Runtime JAR files in the classpath have the version $actualRuntimeVersion, " +
|
||||||
"which is older than the API version $apiVersion. " +
|
"which is older than the API version ${currentApi.version}. " +
|
||||||
"Consider using the runtime of version $apiVersion, or pass '-api-version $actualRuntimeVersion' explicitly to " +
|
"Consider using the runtime of version ${currentApi.version}, or pass '-api-version $actualRuntimeVersion' " +
|
||||||
"restrict the available APIs to the runtime of version $actualRuntimeVersion. " +
|
"explicitly to restrict the available APIs to the runtime of version $actualRuntimeVersion. " +
|
||||||
"You can also pass '-language-version $actualRuntimeVersion' instead, which will restrict " +
|
"You can also pass '-language-version $actualRuntimeVersion' instead, which will restrict " +
|
||||||
"not only the APIs to the specified version, but also the language features"
|
"not only the APIs to the specified version, but also the language features"
|
||||||
)
|
)
|
||||||
|
|
||||||
val actualApi = ApiVersion.parse(actualRuntimeVersion.toString())
|
for (jar in consistency.incompatibleJars) {
|
||||||
if (actualApi != null) {
|
messageCollector.issue(
|
||||||
val inferredApiVersion =
|
jar.file,
|
||||||
if (configuration.getBoolean(CLIConfigurationKeys.IS_API_VERSION_EXPLICIT))
|
"Runtime JAR file has version ${jar.version} which is older than required for API version ${currentApi.version}"
|
||||||
languageVersionSettings.apiVersion
|
)
|
||||||
else
|
}
|
||||||
// "minOf" is needed in case when API version was inferred from language version and it's older than actualApi.
|
|
||||||
// For example, in "kotlinc-1.2 -language-version 1.0 -cp kotlin-runtime-1.1.jar" we should still infer API = 1.0
|
|
||||||
minOf(languageVersionSettings.apiVersion, actualApi)
|
|
||||||
|
|
||||||
|
val actualApi = ApiVersion.parse(actualRuntimeVersion.toString())
|
||||||
|
if (actualApi == null) {
|
||||||
|
messageCollector.issue(null, "Could not parse runtime JAR version: $actualRuntimeVersion")
|
||||||
|
} else if (!configuration.getBoolean(CLIConfigurationKeys.IS_API_VERSION_EXPLICIT) && actualApi < currentApi) {
|
||||||
|
// If there's no explicit "-api-version" AND there's an old stdlib in the classpath (older than the default value of API),
|
||||||
|
// we infer API = the version of that stdlib.
|
||||||
|
// Note that "no explicit -api-version" requirement is necessary because for example, in
|
||||||
|
// "kotlinc-1.2 -language-version 1.0 -cp kotlin-runtime-1.1.jar" we should still infer API = 1.0
|
||||||
val newSettings = object : LanguageVersionSettings by languageVersionSettings {
|
val newSettings = object : LanguageVersionSettings by languageVersionSettings {
|
||||||
override val apiVersion: ApiVersion get() = inferredApiVersion
|
override val apiVersion: ApiVersion get() = actualApi
|
||||||
}
|
}
|
||||||
|
|
||||||
messageCollector.issue(null, "Old runtime has been found in the classpath. " +
|
messageCollector.issue(null, "Old runtime has been found in the classpath. " +
|
||||||
@@ -137,9 +142,6 @@ object JvmRuntimeVersionsConsistencyChecker {
|
|||||||
|
|
||||||
configuration.languageVersionSettings = newSettings
|
configuration.languageVersionSettings = newSettings
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
messageCollector.issue(null, "Could not parse runtime JAR version: $actualRuntimeVersion")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (consistency != ClasspathConsistency.Consistent) {
|
else if (consistency != ClasspathConsistency.Consistent) {
|
||||||
messageCollector.issue(
|
messageCollector.issue(
|
||||||
@@ -165,7 +167,10 @@ object JvmRuntimeVersionsConsistencyChecker {
|
|||||||
|
|
||||||
private sealed class ClasspathConsistency {
|
private sealed class ClasspathConsistency {
|
||||||
object Consistent : ClasspathConsistency()
|
object Consistent : ClasspathConsistency()
|
||||||
class InconsistentWithApiVersion(val actualRuntimeVersion: MavenComparableVersion) : ClasspathConsistency()
|
class InconsistentWithApiVersion(
|
||||||
|
val actualRuntimeVersion: MavenComparableVersion,
|
||||||
|
val incompatibleJars: List<KotlinLibraryFile>
|
||||||
|
) : ClasspathConsistency()
|
||||||
object InconsistentWithCompilerVersion : ClasspathConsistency()
|
object InconsistentWithCompilerVersion : ClasspathConsistency()
|
||||||
object InconsistentBecauseOfRuntimesWithDifferentVersions : ClasspathConsistency()
|
object InconsistentBecauseOfRuntimesWithDifferentVersions : ClasspathConsistency()
|
||||||
}
|
}
|
||||||
@@ -189,9 +194,10 @@ object JvmRuntimeVersionsConsistencyChecker {
|
|||||||
val runtimeVersion = checkMatchingVersionsAndGetRuntimeVersion(messageCollector, jars)
|
val runtimeVersion = checkMatchingVersionsAndGetRuntimeVersion(messageCollector, jars)
|
||||||
?: return ClasspathConsistency.InconsistentBecauseOfRuntimesWithDifferentVersions
|
?: return ClasspathConsistency.InconsistentBecauseOfRuntimesWithDifferentVersions
|
||||||
|
|
||||||
if (jars.map {
|
val jarsIncompatibleWithApiVersion = jars.filter { it.version < apiVersion }
|
||||||
checkCompatibleWithApiVersion(messageCollector, it, apiVersion)
|
if (jarsIncompatibleWithApiVersion.isNotEmpty()) {
|
||||||
}.any { it }) return ClasspathConsistency.InconsistentWithApiVersion(runtimeVersion)
|
return ClasspathConsistency.InconsistentWithApiVersion(runtimeVersion, jarsIncompatibleWithApiVersion)
|
||||||
|
}
|
||||||
|
|
||||||
return ClasspathConsistency.Consistent
|
return ClasspathConsistency.Consistent
|
||||||
}
|
}
|
||||||
@@ -208,19 +214,6 @@ object JvmRuntimeVersionsConsistencyChecker {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkCompatibleWithApiVersion(
|
|
||||||
messageCollector: MessageCollector, jar: KotlinLibraryFile, apiVersion: MavenComparableVersion
|
|
||||||
): Boolean {
|
|
||||||
if (jar.version < apiVersion) {
|
|
||||||
messageCollector.issue(
|
|
||||||
jar.file,
|
|
||||||
"Runtime JAR file has version ${jar.version} which is older than required for API version $apiVersion"
|
|
||||||
)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the version if it's the same across all jars, or null if versions of some jars differ.
|
// Returns the version if it's the same across all jars, or null if versions of some jars differ.
|
||||||
private fun checkMatchingVersionsAndGetRuntimeVersion(
|
private fun checkMatchingVersionsAndGetRuntimeVersion(
|
||||||
messageCollector: MessageCollector,
|
messageCollector: MessageCollector,
|
||||||
|
|||||||
Reference in New Issue
Block a user