All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fireflysource.common.reflect.KotlinNameResolver.kt Maven / Gradle / Ivy

There is a newer version: 5.0.2
Show newest version
package com.fireflysource.common.reflect

import java.lang.reflect.Modifier

/**
 * Resolves name of java classes.
 *
 * @author Pengtao Qiu
 */
object KotlinNameResolver {
    /**
     * Get class name for function by the package of the function.
     *
     * @param func Get class name for the function.
     */
    fun name(func: () -> Unit): String {
        val name = func.javaClass.name
        return when {
            name.contains("Kt$") -> name.substringBefore("Kt$")
            name.contains("$") -> name.substringBefore("$")
            else -> name
        }
    }

    /**
     * Get class name for java class (that usually represents kotlin class)
     *
     * @param forClass Get the name for the class.
     */
    fun  name(forClass: Class): String = unwrapCompanionClass(forClass).name


    /**
     * unwrap companion class to enclosing class given a Java Class
     */
    private fun  unwrapCompanionClass(clazz: Class): Class<*> {
        if (clazz.enclosingClass != null) {
            try {
                val field = clazz.enclosingClass.getField(clazz.simpleName)
                if (Modifier.isStatic(field.modifiers) && field.type == clazz) {
                    // && field.get(null) === obj
                    // the above might be safer but problematic with initialization order
                    return clazz.enclosingClass
                }
            } catch (e: Exception) {
                //ok, it is not a companion object
            }
        }
        return clazz
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy