|
| 1 | +package com.telerik.metadata.parsing.kotlin.classes |
| 2 | + |
| 3 | +import com.telerik.metadata.parsing.kotlin.metadata.MetadataAnnotation |
| 4 | +import org.apache.bcel.classfile.ClassParser |
| 5 | +import org.apache.bcel.classfile.JavaClass |
| 6 | +import org.junit.Assert.assertEquals |
| 7 | +import org.junit.Assert.assertTrue |
| 8 | +import org.junit.Assert.fail |
| 9 | +import org.junit.Test |
| 10 | +import kotlin.metadata.jvm.KotlinClassMetadata |
| 11 | +import kotlin.metadata.jvm.Metadata |
| 12 | + |
| 13 | +class KotlinClassDescriptorMetadataVersionTest { |
| 14 | + |
| 15 | + companion object { |
| 16 | + // Newer than any Kotlin release the bundled kotlin-metadata-jvm knows about, so the test |
| 17 | + // stays meaningful after the library is upgraded. |
| 18 | + private val FUTURE_METADATA_VERSION = intArrayOf(2, 9, 0) |
| 19 | + } |
| 20 | + |
| 21 | + private val fixture = KotlinMetadataVersionFixture::class.java |
| 22 | + |
| 23 | + @Test |
| 24 | + fun `reads class metadata written by a newer Kotlin compiler`() { |
| 25 | + val metadata = descriptorFor(fixture, FUTURE_METADATA_VERSION).kotlinMetadata |
| 26 | + |
| 27 | + assertTrue("Expected readable class metadata, got $metadata", metadata is KotlinClassMetadata.Class) |
| 28 | + assertTrue((metadata as KotlinClassMetadata.Class).kmClass.name.endsWith("KotlinMetadataVersionFixture")) |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `exposes members of a class compiled with a newer Kotlin compiler`() { |
| 33 | + val descriptor = descriptorFor(fixture, FUTURE_METADATA_VERSION) |
| 34 | + |
| 35 | + assertTrue("Class visibility should come from Kotlin metadata", descriptor.isPublic) |
| 36 | + assertTrue("Expected the 'counter' property", descriptor.properties.any { it.name == "counter" }) |
| 37 | + assertTrue("Expected the 'greet' method", descriptor.methods.any { it.name == "greet" }) |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + fun `falls back to bytecode parsing when metadata cannot be read at all`() { |
| 42 | + val descriptor = KotlinClassDescriptor(bcelClass(fixture), corruptAnnotation(), false) |
| 43 | + |
| 44 | + assertEquals(null, descriptor.kotlinMetadata) |
| 45 | + assertTrue("Bytecode-derived visibility should still be available", descriptor.isPublic) |
| 46 | + assertTrue("Bytecode-derived methods should still be available", descriptor.methods.any { it.name == "greet" }) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `strict reading is what rejects newer metadata`() { |
| 51 | + val annotation = annotationWithVersion(fixture, FUTURE_METADATA_VERSION) |
| 52 | + |
| 53 | + try { |
| 54 | + KotlinClassMetadata.readStrict(toMetadata(annotation)) |
| 55 | + fail("readStrict was expected to reject metadata version 2.9.0") |
| 56 | + } catch (e: IllegalArgumentException) { |
| 57 | + assertTrue(e.message!!.contains("version")) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private fun descriptorFor(clazz: Class<*>, metadataVersion: IntArray) = |
| 62 | + KotlinClassDescriptor(bcelClass(clazz), annotationWithVersion(clazz, metadataVersion), false) |
| 63 | + |
| 64 | + private fun bcelClass(clazz: Class<*>): JavaClass { |
| 65 | + val resource = clazz.name.replace('.', '/') + ".class" |
| 66 | + return clazz.classLoader.getResourceAsStream(resource).use { |
| 67 | + ClassParser(it, resource).parse() |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private fun annotationWithVersion(clazz: Class<*>, metadataVersion: IntArray): MetadataAnnotation { |
| 72 | + val real = clazz.getAnnotation(kotlin.Metadata::class.java) |
| 73 | + return object : MetadataAnnotation { |
| 74 | + override val kind = real.kind |
| 75 | + override val metadataVersion = metadataVersion |
| 76 | + override val bytecodeVersion = intArrayOf() |
| 77 | + override val data1 = real.data1 |
| 78 | + override val data2 = real.data2 |
| 79 | + override val extraString = real.extraString |
| 80 | + override val packageName = real.packageName |
| 81 | + override val extraInt = real.extraInt |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private fun corruptAnnotation(): MetadataAnnotation = object : MetadataAnnotation { |
| 86 | + override val kind = 1 |
| 87 | + override val metadataVersion = intArrayOf() |
| 88 | + override val bytecodeVersion = intArrayOf() |
| 89 | + override val data1 = emptyArray<String>() |
| 90 | + override val data2 = emptyArray<String>() |
| 91 | + override val extraString = "" |
| 92 | + override val packageName = "" |
| 93 | + override val extraInt = 0 |
| 94 | + } |
| 95 | + |
| 96 | + private fun toMetadata(annotation: MetadataAnnotation) = Metadata( |
| 97 | + annotation.kind, |
| 98 | + annotation.metadataVersion, |
| 99 | + annotation.data1, |
| 100 | + annotation.data2, |
| 101 | + annotation.extraString, |
| 102 | + annotation.packageName, |
| 103 | + annotation.extraInt) |
| 104 | +} |
0 commit comments