Minor refactoring and style fixes in JarContentTest
Inline unnecessary members, remove commented code, refactor according to the style guide, some other minor improvements
This commit is contained in:
@@ -7,63 +7,54 @@ package kotlinx.metadata.test
|
|||||||
|
|
||||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmNameResolver
|
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmNameResolver
|
||||||
import org.jetbrains.org.objectweb.asm.ClassReader
|
import org.jetbrains.org.objectweb.asm.ClassReader
|
||||||
|
import org.jetbrains.org.objectweb.asm.ClassReader.SKIP_DEBUG
|
||||||
import org.jetbrains.org.objectweb.asm.ClassReader.SKIP_FRAMES
|
import org.jetbrains.org.objectweb.asm.ClassReader.SKIP_FRAMES
|
||||||
import org.jetbrains.org.objectweb.asm.ClassVisitor
|
import org.jetbrains.org.objectweb.asm.ClassVisitor
|
||||||
import org.jetbrains.org.objectweb.asm.MethodVisitor
|
import org.jetbrains.org.objectweb.asm.MethodVisitor
|
||||||
import org.jetbrains.org.objectweb.asm.Opcodes.ASM4
|
import org.jetbrains.org.objectweb.asm.Opcodes.API_VERSION
|
||||||
import org.junit.Assert.assertFalse
|
import org.junit.Assert.*
|
||||||
import org.junit.Assert.assertNull
|
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Path
|
|
||||||
import java.nio.file.Paths
|
|
||||||
import java.util.zip.ZipFile
|
import java.util.zip.ZipFile
|
||||||
import kotlin.text.Charsets.UTF_8
|
import kotlin.text.Charsets.UTF_8
|
||||||
|
|
||||||
class JarContentTest {
|
class JarContentTest {
|
||||||
@Test
|
@Test
|
||||||
fun testJarContents() {
|
fun testJarContents() {
|
||||||
// println("Patterns: $PREDEFINED_STRINGS")
|
val jars = File("build/libs").walk().filter { it.name.endsWith(".jar") }.toList()
|
||||||
LIBS_PATH.toFile().walk().filter { it.name.endsWith(JAR_EXT) }.forEach(::checkJarContents)
|
assertTrue(jars.isNotEmpty())
|
||||||
|
jars.forEach(::checkClassesHasNoSpecificStringConstants)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkJarContents(jar: File) {
|
private fun checkClassesHasNoSpecificStringConstants(jar: File) {
|
||||||
val zipFile = ZipFile(jar, UTF_8)
|
val zipFile = ZipFile(jar, UTF_8)
|
||||||
zipFile.entries().asSequence().filter { it.name.endsWith(CLASS_EXT) }.forEach {
|
for (entry in zipFile.entries()) {
|
||||||
val loadedConstants = mutableListOf<Any>()
|
if (!entry.name.endsWith(".class")) continue
|
||||||
val classReader = ClassReader(zipFile.getInputStream(it))
|
|
||||||
classReader.accept(object : ClassVisitor(ASM4) {
|
val loadedConstants = mutableListOf<String>()
|
||||||
override fun visitMethod(p0: Int, p1: String?, p2: String?, p3: String?, p4: Array<out String>?): MethodVisitor {
|
zipFile.getInputStream(entry).use { stream ->
|
||||||
return object : MethodVisitor(ASM4) {
|
ClassReader(stream).accept(object : ClassVisitor(API_VERSION) {
|
||||||
override fun visitLdcInsn(cst: Any?) {
|
override fun visitMethod(
|
||||||
if (cst != null) loadedConstants.add(cst)
|
access: Int, name: String?, descriptor: String?, signature: String?, exceptions: Array<out String>?
|
||||||
|
): MethodVisitor = object : MethodVisitor(API_VERSION) {
|
||||||
|
override fun visitLdcInsn(value: Any?) {
|
||||||
|
if (value is String && value.isNotEmpty()) loadedConstants.add(value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}, SKIP_DEBUG or SKIP_FRAMES)
|
||||||
}, SKIP_FRAMES)
|
}
|
||||||
|
|
||||||
for (constant in loadedConstants) {
|
for (constant in loadedConstants) {
|
||||||
if (constant !is String || constant.isNullOrEmpty()) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// println("checking: $constant")
|
|
||||||
// Explicitly checking types that are programmatically built don't appear as string constants.
|
// Explicitly checking types that are programmatically built don't appear as string constants.
|
||||||
assertNull("$constant found at ${it.name}", findPattern(constant))
|
assertNull("$constant found at ${entry.name}", PREDEFINED_STRINGS.find { it in constant })
|
||||||
|
|
||||||
// Implicitly checking none of string constants starts with "kotlin/" prefix, just in case.
|
// Implicitly checking none of string constants starts with "kotlin/" prefix, just in case.
|
||||||
assertFalse("$constant found at ${it.name}", constant.startsWith("kotlin/"))
|
assertFalse("$constant found at ${entry.name}", constant.startsWith("kotlin/"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun findPattern(constant: String): String? {
|
|
||||||
return PREDEFINED_STRINGS.find { it in constant }
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val JAR_EXT = ".jar"
|
|
||||||
const val CLASS_EXT = ".class"
|
|
||||||
val LIBS_PATH: Path = Paths.get("build", "libs")
|
|
||||||
|
|
||||||
private val INTERNAL_COMPANIONS =
|
private val INTERNAL_COMPANIONS =
|
||||||
listOf("Char", "Byte", "Short", "Int", "Float", "Long", "Double", "String", "Enum")
|
listOf("Char", "Byte", "Short", "Int", "Float", "Long", "Double", "String", "Enum")
|
||||||
.map { "kotlin/jvm/internal/${it}CompanionObject" }
|
.map { "kotlin/jvm/internal/${it}CompanionObject" }
|
||||||
|
|||||||
Reference in New Issue
Block a user