summaryrefslogtreecommitdiff
path: root/plugins/kotlin/gradle/gradle-tooling/src/org/jetbrains/kotlin/idea/gradleTooling/utils.kt
blob: fc673ebf22b89948473217ab988d7e207d59fa5e (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.idea.gradleTooling

import java.util.*

fun Class<*>.getMethodOrNull(name: String, vararg parameterTypes: Class<*>) =
    try {
        getMethod(name, *parameterTypes)
    } catch (e: Exception) {
        null
    }

fun Class<*>.getDeclaredMethodOrNull(name: String, vararg parameterTypes: Class<*>) =
    try {
        getDeclaredMethod(name, *parameterTypes)?.also { it.isAccessible = true }
    } catch (e: Exception) {
        null
    }

fun ClassLoader.loadClassOrNull(name: String): Class<*>? {
    return try {
        loadClass(name)
    } catch (e: LinkageError) {
        return null
    } catch (e: ClassNotFoundException) {
        return null
    }
}

fun compilationFullName(simpleName: String, classifier: String?) =
    if (classifier != null) classifier + simpleName.capitalize() else simpleName

fun String.capitalize(): String {
    /* Default implementation as suggested by 'capitalize' deprecation */
    if (KotlinVersion.CURRENT.isAtLeast(1, 5)) {
        return this.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
    }

    /* Fallback implementation for older Kotlin versions */
    if (this.isEmpty()) return this
    @Suppress("DEPRECATION")
    return this[0].toUpperCase() + this.drop(1)
}