-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
jdkUtls.kt
31 lines (26 loc) · 1.2 KB
/
jdkUtls.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* Copyright 2020-2022 JetBrains s.r.o. and respective authors and developers.
* Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE.txt file.
*/
package org.jetbrains.compose.test.utils
import java.io.File
const val JDK_11_BYTECODE_VERSION = 55
fun readClassFileVersion(classFile: File): Int {
val url = classFile.toURI().toURL().toExternalForm()
val javapResult = runJavaTool("javap", "-verbose", url)
val majorVersionRegex = "major version: (\\d+)".toRegex()
val bytecode = javapResult.out
val match = majorVersionRegex.find(bytecode)
?: error(buildString {
appendLine("Could not find 'major version' in '$classFile' bytecode:")
appendLine(bytecode)
})
return match.groupValues[1].toInt()
}
fun runJavaTool(toolName: String, vararg args: String): ProcessRunResult {
val javaHome = File(System.getProperty("java.home"))
val toolExecutableName = if (isWindows) "$toolName.exe" else toolName
val executable = javaHome.resolve("bin/$toolExecutableName")
check(executable.isFile) { "Could not find tool '$toolName' at specified path: $executable" }
return runProcess(executable, args.toList())
}