xref: /aosp_15_r20/tools/metalava/metalava-testing/src/main/java/com/android/tools/metalava/testing/KotlinTestUtils.kt (revision 115816f9299ab6ddd6b9673b81f34e707f6bacab)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.tools.metalava.testing
18 
19 import java.io.File
20 
21 private const val DOT_KT = ".kt"
22 
23 /** Get Kotlin stdlib paths. */
getKotlinStdlibPathsnull24 fun getKotlinStdlibPaths(): MutableList<File> {
25     val classPath: String = System.getProperty("java.class.path")
26     val paths = mutableListOf<File>()
27     for (path in classPath.split(':')) {
28         val file = File(path)
29         val name = file.name
30         if (name.startsWith("kotlin-stdlib") || name.startsWith("kotlin-script-runtime")) {
31             paths.add(file)
32         }
33     }
34     if (paths.isEmpty()) {
35         error("Did not find kotlin-stdlib-jre8 in classpath: $classPath")
36     }
37     return paths
38 }
39 
40 /** Get the Kotlin stdlib paths if needed for [sources]. */
findKotlinStdlibPathsnull41 fun findKotlinStdlibPaths(sources: Array<String>): List<File> {
42     val paths = getKotlinStdlibPaths()
43     return if (sources.asSequence().any { it.endsWith(DOT_KT) }) {
44         paths
45     } else {
46         emptyList()
47     }
48 }
49