kotlin.reflect.jvm.internal.impl.resolve.constants.constantValues.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* 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 kotlin.reflect.jvm.internal.impl.resolve.constants
import kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns
import kotlin.reflect.jvm.internal.impl.builtins.StandardNames
import kotlin.reflect.jvm.internal.impl.descriptors.ClassDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.ModuleDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.TypeParameterDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.AnnotationArgumentVisitor
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.AnnotationDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.Annotations
import kotlin.reflect.jvm.internal.impl.descriptors.findClassAcrossModuleDependencies
import kotlin.reflect.jvm.internal.impl.name.ClassId
import kotlin.reflect.jvm.internal.impl.name.Name
import kotlin.reflect.jvm.internal.impl.resolve.DescriptorUtils
import kotlin.reflect.jvm.internal.impl.resolve.descriptorUtil.classId
import kotlin.reflect.jvm.internal.impl.types.*
import kotlin.reflect.jvm.internal.impl.types.typeUtil.replaceArgumentsWithStarProjections
abstract class ConstantValue(open val value: T) {
abstract fun getType(module: ModuleDescriptor): KotlinType
abstract fun accept(visitor: AnnotationArgumentVisitor, data: D): R
override fun equals(other: Any?): Boolean = this === other || value == (other as? ConstantValue<*>)?.value
override fun hashCode(): Int = value?.hashCode() ?: 0
override fun toString(): String = value.toString()
open fun boxedValue(): Any? = value
}
abstract class IntegerValueConstant protected constructor(value: T) : ConstantValue(value)
abstract class UnsignedValueConstant protected constructor(value: T) : ConstantValue(value)
class AnnotationValue(value: AnnotationDescriptor) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor): KotlinType = value.type
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitAnnotationValue(this, data)
}
open class ArrayValue(
value: List>,
private val computeType: (ModuleDescriptor) -> KotlinType
) : ConstantValue>>(value) {
override fun getType(module: ModuleDescriptor): KotlinType = computeType(module).also { type ->
assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type) || KotlinBuiltIns.isUnsignedArrayType(type)) {
"Type should be an array, but was $type: $value"
}
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitArrayValue(this, data)
}
class BooleanValue(value: Boolean) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.booleanType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitBooleanValue(this, data)
}
class ByteValue(value: Byte) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.byteType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitByteValue(this, data)
override fun toString(): String = "$value.toByte()"
}
class CharValue(value: Char) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.charType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitCharValue(this, data)
override fun toString() = "\\u%04X ('%s')".format(value.code, getPrintablePart(value))
private fun getPrintablePart(c: Char): String = when (c) {
'\b' -> "\\b"
'\t' -> "\\t"
'\n' -> "\\n"
//TODO: KT-8507
12.toChar() -> "\\f"
'\r' -> "\\r"
else -> if (isPrintableUnicode(c)) c.toString() 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) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.doubleType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitDoubleValue(this, data)
override fun toString() = "$value.toDouble()"
}
class EnumValue(val enumClassId: ClassId, val enumEntryName: Name) : ConstantValue>(enumClassId to enumEntryName) {
override fun getType(module: ModuleDescriptor): KotlinType =
module.findClassAcrossModuleDependencies(enumClassId)?.takeIf(DescriptorUtils::isEnumClass)?.defaultType
?: ErrorUtils.createErrorType("Containing class for error-class based enum entry $enumClassId.$enumEntryName")
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitEnumValue(this, data)
override fun toString() = "${enumClassId.shortClassName}.$enumEntryName"
}
abstract class ErrorValue : ConstantValue(Unit) {
init {
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 fun getType(module: ModuleDescriptor) = ErrorUtils.createErrorType(message)
override fun toString() = message
}
companion object {
fun create(message: String): ErrorValue {
return ErrorValueWithMessage(message)
}
}
}
class FloatValue(value: Float) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.floatType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitFloatValue(this, data)
override fun toString() = "$value.toFloat()"
}
class IntValue(value: Int) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.intType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitIntValue(this, data)
}
class KClassValue(value: Value) : ConstantValue(value) {
sealed class Value {
data class NormalClass(val value: ClassLiteralValue) : Value() {
val classId: ClassId get() = value.classId
val arrayDimensions: Int get() = value.arrayNestedness
}
data class LocalClass(val type: KotlinType) : Value()
}
constructor(value: ClassLiteralValue) : this(Value.NormalClass(value))
constructor(classId: ClassId, arrayDimensions: Int) : this(ClassLiteralValue(classId, arrayDimensions))
override fun getType(module: ModuleDescriptor): KotlinType =
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, module.builtIns.kClass, listOf(TypeProjectionImpl(getArgumentType(module))))
fun getArgumentType(module: ModuleDescriptor): KotlinType {
when (value) {
is Value.LocalClass -> return value.type
is Value.NormalClass -> {
val (classId, arrayDimensions) = value.value
val descriptor = module.findClassAcrossModuleDependencies(classId)
?: return ErrorUtils.createErrorType("Unresolved type: $classId (arrayDimensions=$arrayDimensions)")
// If this value refers to a class named test.Foo.Bar where both Foo and Bar have generic type parameters,
// we're constructing a type `test.Foo<*>.Bar<*>` below
var type = descriptor.defaultType.replaceArgumentsWithStarProjections()
repeat(arrayDimensions) {
type = module.builtIns.getArrayType(Variance.INVARIANT, type)
}
return type
}
}
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitKClassValue(this, data)
companion object {
fun create(argumentType: KotlinType): ConstantValue<*>? {
if (argumentType.isError) return null
var type = argumentType
var arrayDimensions = 0
while (KotlinBuiltIns.isArray(type)) {
type = type.arguments.single().type
arrayDimensions++
}
return when (val descriptor = type.constructor.declarationDescriptor) {
is ClassDescriptor -> {
val classId = descriptor.classId ?: return KClassValue(KClassValue.Value.LocalClass(argumentType))
KClassValue(classId, arrayDimensions)
}
is TypeParameterDescriptor -> {
// This is possible before 1.4 if a reified type parameter is used in annotation on a local class / anonymous object.
// In JVM class file, we can't represent such literal properly, so we're writing java.lang.Object instead.
// This has no effect on the compiler front-end or other back-ends, so we use kotlin.Any for simplicity here.
// See LanguageFeature.ProhibitTypeParametersInClassLiteralsInAnnotationArguments
KClassValue(ClassId.topLevel(StandardNames.FqNames.any.toSafe()), 0)
}
else -> null
}
}
}
}
class LongValue(value: Long) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.longType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitLongValue(this, data)
override fun toString() = "$value.toLong()"
}
class NullValue : ConstantValue(null) {
override fun getType(module: ModuleDescriptor) = module.builtIns.nullableNothingType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitNullValue(this, data)
}
class ShortValue(value: Short) : IntegerValueConstant(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.shortType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitShortValue(this, data)
override fun toString() = "$value.toShort()"
}
class StringValue(value: String) : ConstantValue(value) {
override fun getType(module: ModuleDescriptor) = module.builtIns.stringType
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitStringValue(this, data)
override fun toString() = "\"$value\""
}
class UByteValue(byteValue: Byte) : UnsignedValueConstant(byteValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(StandardNames.FqNames.uByte)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type UByte not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D): R = visitor.visitUByteValue(this, data)
override fun toString() = "$value.toUByte()"
override fun boxedValue(): Any = value.toUByte()
}
class UShortValue(shortValue: Short) : UnsignedValueConstant(shortValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(StandardNames.FqNames.uShort)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type UShort not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D): R = visitor.visitUShortValue(this, data)
override fun toString() = "$value.toUShort()"
override fun boxedValue(): Any = value.toUShort()
}
class UIntValue(intValue: Int) : UnsignedValueConstant(intValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(StandardNames.FqNames.uInt)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type UInt not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitUIntValue(this, data)
override fun toString() = "$value.toUInt()"
override fun boxedValue(): Any = value.toUInt()
}
class ULongValue(longValue: Long) : UnsignedValueConstant(longValue) {
override fun getType(module: ModuleDescriptor): KotlinType {
return module.findClassAcrossModuleDependencies(StandardNames.FqNames.uLong)?.defaultType
?: ErrorUtils.createErrorType("Unsigned type ULong not found")
}
override fun accept(visitor: AnnotationArgumentVisitor, data: D): R = visitor.visitULongValue(this, data)
override fun toString() = "$value.toULong()"
override fun boxedValue(): Any = value.toULong()
}