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

org.jetbrains.kotlin.resolve.constants.constantValues.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.
 */

package org.jetbrains.kotlin.resolve.constants

import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.resolve.descriptorUtil.classValueType
import org.jetbrains.kotlin.types.ErrorUtils
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.utils.sure

abstract class ConstantValue(open val value: T) {
    abstract val type: KotlinType

    abstract fun  accept(visitor: AnnotationArgumentVisitor, data: D): R

    override fun toString() = value.toString()
}

abstract class IntegerValueConstant protected constructor(value: T) : ConstantValue(value)

class AnnotationValue(value: AnnotationDescriptor) : ConstantValue(value) {

    override val type: KotlinType
        get() = value.type

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitAnnotationValue(this, data)
    override fun toString() = value.toString()
}

class ArrayValue(
        value: List>,
        override val type: KotlinType,
        private val builtIns: KotlinBuiltIns
) : ConstantValue>>(value) {

    init {
        assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was " + type + ": " + value }
    }

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitArrayValue(this, data)

    val elementType: KotlinType
        get() = builtIns.getArrayElementType(type)

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        return value == (other as ArrayValue).value
    }

    override fun hashCode() = value.hashCode()
}

class BooleanValue(
        value: Boolean,
        builtIns: KotlinBuiltIns
) : ConstantValue(value) {

    override val type = builtIns.booleanType
    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitBooleanValue(this, data)

}

class ByteValue(
        value: Byte,
        builtIns: KotlinBuiltIns
) : IntegerValueConstant(value) {

    override val type = builtIns.byteType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitByteValue(this, data)
    override fun toString(): String = "$value.toByte()"
}

class CharValue(
        value: Char,
        builtIns: KotlinBuiltIns
) : IntegerValueConstant(value) {

    override val type = builtIns.charType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitCharValue(this, data)

    override fun toString() = "\\u%04X ('%s')".format(value.toInt(), getPrintablePart(value))

    private fun getPrintablePart(c: Char): String {
        when (c) {
            '\b' -> return "\\b"
            '\t' -> return "\\t"
            '\n' -> return "\\n"
        //TODO: KT-8507
            12.toChar() -> return "\\f"
            '\r' -> return "\\r"
            else -> return if (isPrintableUnicode(c)) Character.toString(c) else "?"
        }
    }

    private fun isPrintableUnicode(c: Char): Boolean {
        val t = Character.getType(c).toByte()
        return t != Character.UNASSIGNED &&
               t != Character.LINE_SEPARATOR &&
               t != Character.PARAGRAPH_SEPARATOR &&
               t != Character.CONTROL &&
               t != Character.FORMAT &&
               t != Character.PRIVATE_USE &&
               t != Character.SURROGATE
    }
}

class DoubleValue(
        value: Double,
        builtIns: KotlinBuiltIns
) : ConstantValue(value) {
    override val type = builtIns.doubleType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitDoubleValue(this, data)

    override fun toString() = "$value.toDouble()"
}

class EnumValue(
        value: ClassDescriptor
) : ConstantValue(value) {

    override val type: KotlinType
        get() = value.classValueType.sure { "Enum entry must have a class object type: " + value }

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitEnumValue(this, data)

    override fun toString() = "$type.${value.name}"

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        return value == (other as EnumValue).value
    }

    override fun hashCode() = value.hashCode()
}

abstract class ErrorValue : ConstantValue(Unit) {

    @Deprecated("Should not be called, for this is not a real value, but a indication of an error")
    override val value: Unit
        get() = throw UnsupportedOperationException()

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitErrorValue(this, data)

    class ErrorValueWithMessage(val message: String) : ErrorValue() {

        override val type = ErrorUtils.createErrorType(message)

        override fun toString() = message
    }

    companion object {
        fun create(message: String): ErrorValue {
            return ErrorValueWithMessage(message)
        }
    }
}

class FloatValue(
        value: Float,
        builtIns: KotlinBuiltIns
) : ConstantValue(value) {
    override val type = builtIns.floatType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitFloatValue(this, data)

    override fun toString() = "$value.toFloat()"
}

class IntValue(
        value: Int,
        builtIns: KotlinBuiltIns
) : IntegerValueConstant(value) {

    override val type = builtIns.intType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitIntValue(this, data)

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        val intValue = other as IntValue

        return value == intValue.value
    }

    override fun hashCode() = value
}

class KClassValue(override val type: KotlinType) :
        ConstantValue(type) {
    override val value: KotlinType
        get() = type.arguments.single().type

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitKClassValue(this, data)
}

class LongValue(
        value: Long,
        builtIns: KotlinBuiltIns
) : IntegerValueConstant(value) {

    override val type = builtIns.longType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitLongValue(this, data)

    override fun toString() = "$value.toLong()"
}

class NullValue(
        builtIns: KotlinBuiltIns
) : ConstantValue(null) {

    override val type = builtIns.nullableNothingType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitNullValue(this, data)

    override fun toString() = "null"
}

class ShortValue(
        value: Short,
        builtIns: KotlinBuiltIns
) : IntegerValueConstant(value) {

    override val type = builtIns.shortType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitShortValue(this, data)

    override fun toString() = "$value.toShort()"
}

class StringValue(
        value: String,
        builtIns: KotlinBuiltIns
) : ConstantValue(value) {
    override val type = builtIns.stringType

    override fun  accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitStringValue(this, data)

    override fun toString() = "\"$value\""

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false

        return value != (other as StringValue).value
    }

    override fun hashCode() = value.hashCode()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy