55
66package org.jetbrains.kotlin.backend.common.diagnostics
77
8+ import org.jetbrains.kotlin.backend.common.diagnostics.LibrarySpecialCompatibilityChecker.Companion.KLIB_JAR_MANIFEST_FILE
89import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
910import org.jetbrains.kotlin.cli.common.messages.MessageCollector
1011import org.jetbrains.kotlin.config.KotlinCompilerVersion
1112import org.jetbrains.kotlin.config.LanguageVersion
1213import org.jetbrains.kotlin.config.MavenComparableVersion
14+ import org.jetbrains.kotlin.library.KlibComponent
15+ import org.jetbrains.kotlin.library.KlibComponentLayout
16+ import org.jetbrains.kotlin.library.KlibLayoutReader
1317import org.jetbrains.kotlin.library.KotlinLibrary
14- import org.jetbrains.kotlin.library.impl.KotlinLibraryImpl
1518import java.io.ByteArrayInputStream
16- import java.io.IOException
1719import java.util.jar.Manifest
20+ import org.jetbrains.kotlin.konan.file.File as KlibFile
1821
1922/* * See KT-68322 for details. */
2023abstract class LibrarySpecialCompatibilityChecker {
@@ -41,7 +44,7 @@ abstract class LibrarySpecialCompatibilityChecker {
4144 // We use `substringBefore('-')` to cut off irrelevant part of the version string.
4245 // Ex: 2.0.255-SNAPSHOT -> 2.0.255, 2.0.20-dev-12345 -> 2.0.20
4346 MavenComparableVersion (rawVersion.substringBefore(' -' ))
44- } catch (e : Exception ) {
47+ } catch (_ : Exception ) {
4548 return null
4649 }
4750
@@ -58,7 +61,7 @@ abstract class LibrarySpecialCompatibilityChecker {
5861
5962 for (library in libraries) {
6063 if (shouldCheckLibrary(library)) {
61- val jarManifest = library.getJarManifest() ? : continue
64+ val jarManifest = library.getComponent( JarManifestComponent . Kind )?.jarManifest ? : continue
6265 val libraryVersion = Version .parseVersion(jarManifest.mainAttributes.getValue(KLIB_JAR_LIBRARY_VERSION )) ? : continue
6366
6467 val messageToReport = getMessageToReport(compilerVersion, libraryVersion, library)
@@ -69,18 +72,6 @@ abstract class LibrarySpecialCompatibilityChecker {
6972 }
7073 }
7174
72- private fun KotlinLibrary.getJarManifest (): Manifest ? =
73- (this as KotlinLibraryImpl ).base.access.inPlace { layout ->
74- val jarManifestFile = layout.libFile.child(KLIB_JAR_MANIFEST_FILE )
75- if (! jarManifestFile.isFile) return @inPlace null
76-
77- try {
78- ByteArrayInputStream (jarManifestFile.readBytes()).use { Manifest (it) }
79- } catch (_: IOException ) {
80- null
81- }
82- }
83-
8475 private fun getRawCompilerVersion (): String? {
8576 return customCompilerVersionForTest?.let { return it.version } ? : KotlinCompilerVersion .getVersion()
8677 }
@@ -107,3 +98,24 @@ abstract class LibrarySpecialCompatibilityChecker {
10798 const val KLIB_JAR_LIBRARY_VERSION = " Implementation-Version"
10899 }
109100}
101+
102+ private class JarManifestComponent (
103+ private val layoutReader : KlibLayoutReader <JarManifestComponentLayout >
104+ ) : KlibComponent {
105+ val jarManifest: Manifest ?
106+ get() = layoutReader.readInPlaceOrFallback(null ) {
107+ ByteArrayInputStream (it.jarManifestFile.readBytes()).use(::Manifest )
108+ }
109+
110+ object Kind : KlibComponent.Kind<JarManifestComponent, JarManifestComponentLayout> {
111+ override fun createLayout (root : KlibFile ) = JarManifestComponentLayout (root)
112+
113+ override fun createComponentIfDataInKlibIsAvailable (layoutReader : KlibLayoutReader <JarManifestComponentLayout >) =
114+ if (layoutReader.readInPlaceOrFallback(false ) { it.jarManifestFile.isFile }) JarManifestComponent (layoutReader) else null
115+ }
116+ }
117+
118+ private class JarManifestComponentLayout (root : KlibFile ) : KlibComponentLayout(root) {
119+ val jarManifestFile: KlibFile
120+ get() = root.child(KLIB_JAR_MANIFEST_FILE )
121+ }
0 commit comments