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

kotlin.jvm.JvmClassMapping.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-Beta1
Show newest version
/*
 * Copyright 2010-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
@file:JvmName("JvmClassMappingKt")
@file:Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST")

package kotlin.jvm

import kotlin.jvm.internal.ClassBasedDeclarationContainer
import kotlin.jvm.internal.Reflection
import kotlin.reflect.KClass

/**
 * Returns a Java [Class] instance corresponding to the given [KClass] instance.
 */
val  KClass.java: Class
    @JvmName("getJavaClass")
    get() = (this as ClassBasedDeclarationContainer).jClass as Class

/**
 * Returns a Java [Class] instance representing the primitive type corresponding to the given [KClass] if it exists.
 */
val  KClass.javaPrimitiveType: Class?
    get() {
        val thisJClass = (this as ClassBasedDeclarationContainer).jClass
        if (thisJClass.isPrimitive) return thisJClass as Class

        return when (thisJClass.canonicalName) {
            "java.lang.Boolean"   -> Boolean::class.java
            "java.lang.Character" -> Char::class.java
            "java.lang.Byte"      -> Byte::class.java
            "java.lang.Short"     -> Short::class.java
            "java.lang.Integer"   -> Int::class.java
            "java.lang.Float"     -> Float::class.java
            "java.lang.Long"      -> Long::class.java
            "java.lang.Double"    -> Double::class.java
            else -> null
        } as Class?
    }

/**
 * Returns a Java [Class] instance corresponding to the given [KClass] instance.
 * In case of primitive types it returns corresponding wrapper classes.
 */
val  KClass.javaObjectType: Class
    get() {
        val thisJClass = (this as ClassBasedDeclarationContainer).jClass
        if (!thisJClass.isPrimitive) return thisJClass as Class

        return when (thisJClass.canonicalName) {
            "boolean" -> java.lang.Boolean::class.java
            "char"    -> java.lang.Character::class.java
            "byte"    -> java.lang.Byte::class.java
            "short"   -> java.lang.Short::class.java
            "int"     -> java.lang.Integer::class.java
            "float"   -> java.lang.Float::class.java
            "long"    -> java.lang.Long::class.java
            "double"  -> java.lang.Double::class.java
            else -> thisJClass
        } as Class
    }

/**
 * Returns a [KClass] instance corresponding to the given Java [Class] instance.
 */
val  Class.kotlin: KClass
    @JvmName("getKotlinClass")
    get() = Reflection.createKotlinClass(this) as KClass


/**
 * Returns the runtime Java class of this object.
 */
val  T.javaClass : Class
    @Suppress("UsePropertyAccessSyntax")
    get() = (this as java.lang.Object).getClass() as Class

@Deprecated("Use 'java' property to get Java class corresponding to this Kotlin class or cast this instance to Any if you really want to get the runtime Java class of this implementation of KClass.", ReplaceWith("(this as Any).javaClass"), level = DeprecationLevel.ERROR)
val  KClass.javaClass: Class>
    @JvmName("getRuntimeClassOfKClassInstance")
    @Suppress("UsePropertyAccessSyntax")
    get() = (this as java.lang.Object).getClass() as Class>

/**
 * Checks if array can contain element of type [T].
 */
@Suppress("REIFIED_TYPE_PARAMETER_NO_INLINE")
fun  Array<*>.isArrayOf(): Boolean =
        T::class.java.isAssignableFrom(this.javaClass.componentType)

/**
 * Returns a [KClass] instance corresponding to the annotation type of this annotation.
 */
val  T.annotationClass: KClass
    get() = (this as java.lang.annotation.Annotation).annotationType().kotlin as KClass




© 2015 - 2024 Weber Informatics LLC | Privacy Policy