From 0369de86c7124c7a959641e47900168c423bc76a Mon Sep 17 00:00:00 2001 From: "Pavel V. Talanov" Date: Tue, 30 Jun 2015 15:57:54 +0300 Subject: [PATCH] Convert compile constant classes to kotlin: j2k --- .../kotlin/resolve/AnnotationResolver.java | 1 - .../kotlin/resolve/AnnotationTargetChecker.kt | 2 +- .../kotlin/resolve/AnnotationUtil.kt | 2 +- .../evaluate/ConstantExpressionEvaluator.kt | 12 +-- .../serialization/AnnotationSerializer.kt | 24 ++--- .../AbstractEvaluateExpressionTest.kt | 2 +- .../kotlin/renderer/DescriptorRendererImpl.kt | 6 +- .../resolve/constants/AnnotationValue.kt | 42 +++------ .../kotlin/resolve/constants/ArrayValue.kt | 85 ++++++----------- .../kotlin/resolve/constants/BooleanValue.kt | 32 +++---- .../kotlin/resolve/constants/ByteValue.kt | 31 +++---- .../kotlin/resolve/constants/CharValue.kt | 67 ++++++-------- .../resolve/constants/CompileTimeConstant.kt | 76 +++++++-------- .../kotlin/resolve/constants/DoubleValue.kt | 31 +++---- .../kotlin/resolve/constants/EnumValue.kt | 69 +++++--------- .../kotlin/resolve/constants/ErrorValue.kt | 66 +++++-------- .../kotlin/resolve/constants/FloatValue.kt | 31 +++---- .../kotlin/resolve/constants/IntValue.kt | 55 +++++------ .../resolve/constants/IntegerValueConstant.kt | 9 +- .../constants/IntegerValueTypeConstant.kt | 81 +++++++--------- .../constants/IntegerValueTypeConstructor.kt | 92 ++++++++----------- .../kotlin/resolve/constants/KClassValue.kt | 7 +- .../kotlin/resolve/constants/LongValue.kt | 31 +++---- .../kotlin/resolve/constants/NullValue.kt | 33 +++---- .../kotlin/resolve/constants/ShortValue.kt | 31 +++---- .../kotlin/resolve/constants/StringValue.kt | 55 +++++------ .../ConvertToStringTemplateIntention.kt | 2 +- .../quickfix/DeprecatedSymbolUsageFixBase.kt | 2 +- .../js/resolve/diagnostics/JsCallChecker.kt | 2 +- 29 files changed, 377 insertions(+), 602 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java index 792730b3a45..06567399a62 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationResolver.java @@ -63,7 +63,6 @@ import java.util.List; import java.util.Map; import static org.jetbrains.kotlin.diagnostics.Errors.NOT_AN_ANNOTATION_CLASS; -import static org.jetbrains.kotlin.resolve.BindingContext.ANNOTATION_DESCRIPTOR_TO_PSI_ELEMENT; import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE; public class AnnotationResolver { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationTargetChecker.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationTargetChecker.kt index 307be5f064d..6878b9d8088 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationTargetChecker.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationTargetChecker.kt @@ -99,7 +99,7 @@ public object AnnotationTargetChecker { ?: return DEFAULT_TARGET_LIST val valueArguments = targetEntryDescriptor.getAllValueArguments() val valueArgument = valueArguments.entrySet().firstOrNull()?.getValue() as? ArrayValue ?: return DEFAULT_TARGET_LIST - return valueArgument.getValue().filterIsInstance().map { it.getValue().getName().asString() } + return valueArgument.value.filterIsInstance().map { it.value.getName().asString() } } private fun checkAnnotationEntry(entry: JetAnnotationEntry, actualTargets: List, trace: BindingTrace) { diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt index 390c2556b0a..94fbbebc1e6 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/AnnotationUtil.kt @@ -59,5 +59,5 @@ private fun CallableDescriptor.isPlatformStaticIn(predicate: (DeclarationDescrip public fun AnnotationDescriptor.argumentValue(parameterName: String): Any? { return getAllValueArguments().entrySet() .singleOrNull { it.key.getName().asString() == parameterName } - ?.value?.getValue() + ?.value?.value } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt index 0110b9b7b4d..0b947d2ade8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/constants/evaluate/ConstantExpressionEvaluator.kt @@ -151,7 +151,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT else { if (!constant.canBeUsedInAnnotations()) canBeUsedInAnnotation = false if (constant.usesVariableAsConstant()) usesVariableAsConstant = true - sb.append(constant.getValue()) + sb.append(constant.value) } } return if (!interupted) @@ -180,8 +180,8 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT val rightConstant = evaluate(rightExpression, booleanType) if (rightConstant == null) return null - val leftValue = leftConstant.getValue() - val rightValue = rightConstant.getValue() + val leftValue = leftConstant.value + val rightValue = rightConstant.value if (leftValue !is Boolean || rightValue !is Boolean) return null val result = when(operationToken) { @@ -341,7 +341,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT if (compileTimeConstant is IntegerValueTypeConstant) compileTimeConstant.getValue(expectedType ?: TypeUtils.NO_EXPECTED_TYPE) else - compileTimeConstant.getValue() + compileTimeConstant.value return createCompileTimeConstant(value, expectedType, isPure = false, canBeUsedInAnnotation = isPropertyCompileTimeConstant(callableDescriptor), usesVariableAsConstant = true) @@ -462,7 +462,7 @@ public class ConstantExpressionEvaluator private constructor(val trace: BindingT return OperationArgument(evaluationResultWithNewType, compileTimeType, expression) } - val evaluationResult = evaluatedConstant.getValue() + val evaluationResult = evaluatedConstant.value if (evaluationResult == null) return null return OperationArgument(evaluationResult, compileTimeType, expression) @@ -580,7 +580,7 @@ private fun createStringConstant(value: CompileTimeConstant<*>?): StringValue? { is CharValue, is DoubleValue, is FloatValue, is BooleanValue, - is NullValue -> StringValue("${value.getValue()}", value.canBeUsedInAnnotations(), value.usesVariableAsConstant()) + is NullValue -> StringValue("${value.value}", value.canBeUsedInAnnotations(), value.usesVariableAsConstant()) else -> null } } diff --git a/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt b/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt index 0a23ec43013..6e6d7416e23 100644 --- a/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt +++ b/compiler/serialization/src/org/jetbrains/kotlin/serialization/AnnotationSerializer.kt @@ -52,39 +52,39 @@ public object AnnotationSerializer { constant.accept(object : AnnotationArgumentVisitor { override fun visitAnnotationValue(value: AnnotationValue, data: Unit) { setType(Type.ANNOTATION) - setAnnotation(serializeAnnotation(value.getValue(), nameTable)) + setAnnotation(serializeAnnotation(value.value, nameTable)) } override fun visitArrayValue(value: ArrayValue, data: Unit) { setType(Type.ARRAY) - for (element in value.getValue()) { + for (element in value.value) { addArrayElement(valueProto(element, KotlinBuiltIns.getInstance().getArrayElementType(type), nameTable).build()) } } override fun visitBooleanValue(value: BooleanValue, data: Unit) { setType(Type.BOOLEAN) - setIntValue(if (value.getValue()!!) 1 else 0) + setIntValue(if (value.value) 1 else 0) } override fun visitByteValue(value: ByteValue, data: Unit) { setType(Type.BYTE) - setIntValue(value.getValue()!!.toLong()) + setIntValue(value.value.toLong()) } override fun visitCharValue(value: CharValue, data: Unit) { setType(Type.CHAR) - setIntValue(value.getValue()!!.toLong()) + setIntValue(value.value.toLong()) } override fun visitDoubleValue(value: DoubleValue, data: Unit) { setType(Type.DOUBLE) - setDoubleValue(value.getValue()!!) + setDoubleValue(value.value) } override fun visitEnumValue(value: EnumValue, data: Unit) { setType(Type.ENUM) - val enumEntry = value.getValue() + val enumEntry = value.value setClassId(nameTable.getFqNameIndex(enumEntry.getContainingDeclaration() as ClassDescriptor)) setEnumValueId(nameTable.getSimpleNameIndex(enumEntry.getName())) } @@ -95,12 +95,12 @@ public object AnnotationSerializer { override fun visitFloatValue(value: FloatValue, data: Unit) { setType(Type.FLOAT) - setFloatValue(value.getValue()!!) + setFloatValue(value.value) } override fun visitIntValue(value: IntValue, data: Unit) { setType(Type.INT) - setIntValue(value.getValue()!!.toLong()) + setIntValue(value.value.toLong()) } override fun visitKClassValue(value: KClassValue?, data: Unit?) { @@ -110,7 +110,7 @@ public object AnnotationSerializer { override fun visitLongValue(value: LongValue, data: Unit) { setType(Type.LONG) - setIntValue(value.getValue()!!) + setIntValue(value.value) } override fun visitNullValue(value: NullValue, data: Unit) { @@ -135,12 +135,12 @@ public object AnnotationSerializer { override fun visitShortValue(value: ShortValue, data: Unit) { setType(Type.SHORT) - setIntValue(value.getValue()!!.toLong()) + setIntValue(value.value.toLong()) } override fun visitStringValue(value: StringValue, data: Unit) { setType(Type.STRING) - setStringValue(nameTable.getStringIndex(value.getValue()!!)) + setStringValue(nameTable.getStringIndex(value.value)) } }, Unit) diff --git a/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt b/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt index 36ff026c07e..21b3da3518c 100644 --- a/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/resolve/constants/evaluate/AbstractEvaluateExpressionTest.kt @@ -36,7 +36,7 @@ public abstract class AbstractEvaluateExpressionTest : AbstractAnnotationDescrip property, context -> val compileTimeConstant = property.getCompileTimeInitializer() if (compileTimeConstant is StringValue) { - "\\\"${compileTimeConstant.getValue()}\\\"" + "\\\"${compileTimeConstant.value}\\\"" } else { "$compileTimeConstant" } diff --git a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt index dd48739459f..24970e499a8 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt @@ -386,9 +386,9 @@ internal class DescriptorRendererImpl( private fun renderConstant(value: CompileTimeConstant<*>): String { return when (value) { - is ArrayValue -> value.getValue().map { renderConstant(it) }.joinToString(", ", "{", "}") - is AnnotationValue -> renderAnnotation(value.getValue()) - is KClassValue -> renderType(value.getValue()) + "::class" + is ArrayValue -> value.value.map { renderConstant(it) }.joinToString(", ", "{", "}") + is AnnotationValue -> renderAnnotation(value.value) + is KClassValue -> renderType(value.value) + "::class" else -> value.toString() } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt index 551fc128936..f56c108ba1d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/AnnotationValue.kt @@ -14,41 +14,23 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor +import org.jetbrains.kotlin.types.JetType -public class AnnotationValue extends CompileTimeConstant { - - public AnnotationValue(@NotNull AnnotationDescriptor value) { - super(value, true, false, false); +public class AnnotationValue(value: AnnotationDescriptor) : CompileTimeConstant(value, true, false, false) { + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return value.getType() } - @NotNull - @Override - public AnnotationDescriptor getValue() { - AnnotationDescriptor value = super.getValue(); - assert value != null : "Guaranteed by constructor"; - return value; + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitAnnotationValue(this, data) } - @Override - @NotNull - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return value.getType(); - } - - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitAnnotationValue(this, data); - } - - @Override - public String toString() { - return value.toString(); + override fun toString(): String { + return value.toString() } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt index afae9e14363..fe16daf7fc9 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ArrayValue.kt @@ -14,83 +14,58 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -import java.util.List; +public class ArrayValue(value: List>, private val type: JetType, canBeUsedInAnnotations: Boolean, usesVariableAsConstant: Boolean) : CompileTimeConstant>>(value, canBeUsedInAnnotations, false, usesVariableAsConstant) { -public class ArrayValue extends CompileTimeConstant>> { - - private final JetType type; - - public ArrayValue(@NotNull List> value, - @NotNull JetType type, - boolean canBeUsedInAnnotations, - boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, false, usesVariableAsConstant); - assert KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type) - : "Type should be an array, but was " + type + ": " + value; - this.type = type; + init { + assert(KotlinBuiltIns.isArray(type) || KotlinBuiltIns.isPrimitiveArray(type)) { "Type should be an array, but was " + type + ": " + value } } - @NotNull - @Override - public List> getValue() { - List> value = super.getValue(); - assert value != null : "Guaranteed by constructor"; - return value; + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return type } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return type; + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitArrayValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitArrayValue(this, data); + override fun toString(): String { + return value.toString() } - @Override - public String toString() { - return value.toString(); - } + override fun equals(o: Any?): Boolean { + if (this === o) return true + if (o == null || javaClass != o.javaClass) return false - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ArrayValue that = (ArrayValue) o; + val that = o as? ArrayValue ?: return false if (value == null) { - return that.value == null; + return that.value == null } - int i = 0; - for (Object thisObject : value) { - if (!thisObject.equals(that.value.get(i))) { - return false; + var i = 0 + for (thisObject in value) { + if (thisObject != that.value.get(i)) { + return false } - i++; + i++ } - return true; + return true } - @Override - public int hashCode() { - int hashCode = 0; - if (value == null) return hashCode; - for (Object o : value) { - hashCode += o.hashCode(); + override fun hashCode(): Int { + var hashCode = 0 + if (value == null) return hashCode + for (o in value) { + hashCode += o.hashCode() } - return hashCode; + return hashCode } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt index ce55cd6e45b..410b580b059 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/BooleanValue.kt @@ -14,33 +14,23 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class BooleanValue extends CompileTimeConstant { +public class BooleanValue(value: Boolean, canBeUseInAnnotation: Boolean, usesVariableAsConstant: Boolean) : CompileTimeConstant(value, canBeUseInAnnotation, false, usesVariableAsConstant) { - public BooleanValue(boolean value, boolean canBeUseInAnnotation, boolean usesVariableAsConstant) { - super(value, canBeUseInAnnotation, false, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getBooleanType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getBooleanType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitBooleanValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitBooleanValue(this, data); + override fun toString(): String { + return "$value" } - - @Override - public String toString() { - return String.valueOf(value); - } - } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt index 26d036e42e8..7cb040452bb 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ByteValue.kt @@ -14,32 +14,23 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class ByteValue extends IntegerValueConstant { +public class ByteValue(value: Byte, canBeUsedInAnnotations: Boolean, pure: Boolean, usesVaraiableAsConstant: Boolean) : IntegerValueConstant(value, canBeUsedInAnnotations, pure, usesVaraiableAsConstant) { - public ByteValue(byte value, boolean canBeUsedInAnnotations, boolean pure, boolean usesVaraiableAsConstant) { - super(value, canBeUsedInAnnotations, pure, usesVaraiableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getByteType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getByteType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitByteValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitByteValue(this, data); - } - - @Override - public String toString() { - return value + ".toByte()"; + override fun toString(): String { + return "$value.toByte()" } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt index 1ae5504827d..9bdf3e62a7d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CharValue.kt @@ -14,55 +14,44 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class CharValue extends IntegerValueConstant { +public class CharValue(value: Char, canBeUsedInAnnotations: Boolean, pure: Boolean, usesVariableAsConstant: Boolean) : IntegerValueConstant(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) { - public CharValue(char value, boolean canBeUsedInAnnotations, boolean pure, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, pure, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getCharType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getCharType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitCharValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitCharValue(this, data); - } + override fun toString() = "\\u%04X ('%s')".format(value.toInt(), getPrintablePart(value)) - @Override - public String toString() { - return String.format("\\u%04X ('%s')", (int) value, getPrintablePart(value)); - } - - private static String getPrintablePart(char c) { - switch (c) { - case '\b': - return "\\b"; - case '\t': - return "\\t"; - case '\n': - return "\\n"; - case '\f': - return "\\f"; - case '\r': - return "\\r"; - default: - return isPrintableUnicode(c) ? Character.toString(c) : "?"; + private fun getPrintablePart(c: Char): String { + when (c) { + '\b' -> return "\\b" + '\t' -> return "\\t" + '\n' -> return "\\n" + //TODO_R: can't escape form feed in Kotlin + 12.toChar() -> return "\\f" + '\r' -> return "\\r" + else -> return if (isPrintableUnicode(c)) Character.toString(c) else "?" } } - private static boolean isPrintableUnicode(char c) { - int t = Character.getType(c); - 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; + 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 } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt index f51c8ef0e5f..05732ace9fa 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/CompileTimeConstant.kt @@ -14,57 +14,45 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public abstract class CompileTimeConstant { - protected final T value; - private final int flags; +public abstract class CompileTimeConstant protected constructor(public open val value: T, canBeUsedInAnnotations: Boolean, isPure: Boolean, usesVariableAsConstant: Boolean) { + private val flags: Int - /* + init { + flags = (if (isPure) IS_PURE_MASK else 0) or (if (canBeUsedInAnnotations) CAN_BE_USED_IN_ANNOTATIONS_MASK else 0) or (if (usesVariableAsConstant) USES_VARIABLE_AS_CONSTANT_MASK else 0) + } + + public fun canBeUsedInAnnotations(): Boolean { + return (flags and CAN_BE_USED_IN_ANNOTATIONS_MASK) != 0 + } + + public fun isPure(): Boolean { + return (flags and IS_PURE_MASK) != 0 + } + + public fun usesVariableAsConstant(): Boolean { + return (flags and USES_VARIABLE_AS_CONSTANT_MASK) != 0 + } + + public abstract fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType + + public abstract fun accept(visitor: AnnotationArgumentVisitor, data: D): R + + companion object { + + /* * if is pure is false then constant type cannot be changed * ex1. val a: Long = 1.toInt() (TYPE_MISMATCH error, 1.toInt() isn't pure) * ex2. val b: Int = a (TYPE_MISMATCH error, a isn't pure) * */ - private static final int IS_PURE_MASK = 1; - private static final int CAN_BE_USED_IN_ANNOTATIONS_MASK = 1 << 1; - private static final int USES_VARIABLE_AS_CONSTANT_MASK = 1 << 2; - - protected CompileTimeConstant(T value, - boolean canBeUsedInAnnotations, - boolean isPure, - boolean usesVariableAsConstant) { - this.value = value; - flags = (isPure ? IS_PURE_MASK : 0) | - (canBeUsedInAnnotations ? CAN_BE_USED_IN_ANNOTATIONS_MASK : 0) | - (usesVariableAsConstant ? USES_VARIABLE_AS_CONSTANT_MASK : 0); + private val IS_PURE_MASK = 1 + private val CAN_BE_USED_IN_ANNOTATIONS_MASK = 1 shl 1 + private val USES_VARIABLE_AS_CONSTANT_MASK = 1 shl 2 } - - public boolean canBeUsedInAnnotations() { - return (flags & CAN_BE_USED_IN_ANNOTATIONS_MASK) != 0; - } - - public boolean isPure() { - return (flags & IS_PURE_MASK) != 0; - } - - public boolean usesVariableAsConstant() { - return (flags & USES_VARIABLE_AS_CONSTANT_MASK) != 0; - } - - @Nullable - public T getValue() { - return value; - } - - @NotNull - public abstract JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns); - - public abstract R accept(AnnotationArgumentVisitor visitor, D data); } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt index f6a13def8ec..3cc03222e55 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/DoubleValue.kt @@ -14,32 +14,23 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class DoubleValue extends CompileTimeConstant { +public class DoubleValue(value: Double, canBeUsedInAnnotations: Boolean, usesVariableAsConstant: Boolean) : CompileTimeConstant(value, canBeUsedInAnnotations, false, usesVariableAsConstant) { - public DoubleValue(double value, boolean canBeUsedInAnnotations, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, false, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getDoubleType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getDoubleType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitDoubleValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitDoubleValue(this, data); - } - - @Override - public String toString() { - return value + ".toDouble()"; + override fun toString(): String { + return "$value.toDouble()" } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt index 05a092cf9bc..ccca617b354 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/EnumValue.kt @@ -14,64 +14,43 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.ClassDescriptor; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.resolve.descriptorUtil.classObjectType +import org.jetbrains.kotlin.types.JetType +import org.jetbrains.kotlin.utils.sure -import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage.getClassObjectType; +public class EnumValue(value: ClassDescriptor, usesVariableAsConstant: Boolean) : CompileTimeConstant(value, true, false, usesVariableAsConstant) { -public class EnumValue extends CompileTimeConstant { - - public EnumValue(@NotNull ClassDescriptor value, boolean usesVariableAsConstant) { - super(value, true, false, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return getType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return getType(); + private fun getType(): JetType { + val type = value.classObjectType + return type.sure { "Enum entry must have a class object type: " + value } } - @NotNull - private JetType getType() { - JetType type = getClassObjectType(value); - assert type != null : "Enum entry must have a class object type: " + value; - return type; + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitEnumValue(this, data) } - @NotNull - @Override - public ClassDescriptor getValue() { - ClassDescriptor value = super.getValue(); - assert value != null : "Guaranteed by constructor"; - return value; + override fun toString(): String { + return "${getType()}.${value.getName()}" } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitEnumValue(this, data); + override fun equals(o: Any?): Boolean { + if (this === o) return true + if (o == null || javaClass != o.javaClass) return false + + return value == (o as? EnumValue)?.value } - @Override - public String toString() { - return getType() + "." + value.getName(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - return value.equals(((EnumValue) o).value); - } - - @Override - public int hashCode() { - return value.hashCode(); + override fun hashCode(): Int { + return value.hashCode() } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt index 980988ef74f..5d0444504e7 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ErrorValue.kt @@ -14,56 +14,40 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.ErrorUtils; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.ErrorUtils +import org.jetbrains.kotlin.types.JetType -public abstract class ErrorValue extends CompileTimeConstant { +public abstract class ErrorValue : CompileTimeConstant(Unit, true, false, false) { - public ErrorValue() { - super(null, true, false, false); - } - - @Override - @Deprecated // Should not be called, for this is not a real value, but a indication of an error - public Void getValue() { - throw new UnsupportedOperationException(); - } - - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitErrorValue(this, data); - } - - @NotNull - public static ErrorValue create(@NotNull String message) { - return new ErrorValueWithMessage(message); - } - - public static class ErrorValueWithMessage extends ErrorValue { - private final String message; - - public ErrorValueWithMessage(@NotNull String message) { - this.message = message; + 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() } - public String getMessage() { - return message; + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitErrorValue(this, data) + } + + public class ErrorValueWithMessage(public val message: String) : ErrorValue() { + + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return ErrorUtils.createErrorType(message) } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return ErrorUtils.createErrorType(message); + override fun toString(): String { + return message } + } - @Override - public String toString() { - return getMessage(); + companion object { + + public fun create(message: String): ErrorValue { + return ErrorValueWithMessage(message) } } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt index b541ecf0246..7da1f85633d 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/FloatValue.kt @@ -14,32 +14,23 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class FloatValue extends CompileTimeConstant { +public class FloatValue(value: Float, canBeUsedInAnnotations: Boolean, usesVariableAsConstant: Boolean) : CompileTimeConstant(value, canBeUsedInAnnotations, false, usesVariableAsConstant) { - public FloatValue(float value, boolean canBeUsedInAnnotations, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, false, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getFloatType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getFloatType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitFloatValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitFloatValue(this, data); - } - - @Override - public String toString() { - return value + ".toFloat()"; + override fun toString(): String { + return "$value.toFloat()" } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt index cb390098600..5e2ca89cafe 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntValue.kt @@ -14,49 +14,38 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class IntValue extends IntegerValueConstant { +public class IntValue(value: Int, canBeUsedInAnnotations: Boolean, pure: Boolean, usesVariableAsConstant: Boolean) : IntegerValueConstant(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) { - public IntValue(int value, boolean canBeUsedInAnnotations, boolean pure, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, pure, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getIntType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getIntType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitIntValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitIntValue(this, data); + override fun toString(): String { + return value.toString() } - @Override - public String toString() { - return value.toString(); + override fun equals(o: Any?): Boolean { + if (this === o) return true + if (o == null || javaClass != o.javaClass) return false + + val intValue = o as? IntValue ?: return false + + if (value !== intValue.value) return false + + return true } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - IntValue intValue = (IntValue) o; - - if (value != intValue.value) return false; - - return true; - } - - @Override - public int hashCode() { - return value; + override fun hashCode(): Int { + return value } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt index 7c3b9de2e65..220a174d3ab 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueConstant.kt @@ -14,11 +14,6 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -public abstract class IntegerValueConstant extends CompileTimeConstant { - - protected IntegerValueConstant(T value, boolean canBeUsedInAnnotations, boolean pure, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, pure, usesVariableAsConstant); - } -} +public abstract class IntegerValueConstant protected constructor(value: T, canBeUsedInAnnotations: Boolean, pure: Boolean, usesVariableAsConstant: Boolean) : CompileTimeConstant(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt index 682a70b9265..04778e7a327 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstant.kt @@ -14,74 +14,59 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.descriptors.annotations.Annotations; -import org.jetbrains.kotlin.types.*; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.types.* -import java.util.Collections; +import java.util.Collections -public class IntegerValueTypeConstant extends IntegerValueConstant { +public class IntegerValueTypeConstant(value: Number, canBeUsedInAnnotations: Boolean, usesVariableAsConstant: Boolean) : IntegerValueConstant(value, canBeUsedInAnnotations, true, usesVariableAsConstant) { - private final IntegerValueTypeConstructor typeConstructor; + private val typeConstructor: IntegerValueTypeConstructor - public IntegerValueTypeConstant(@NotNull Number value, boolean canBeUsedInAnnotations, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, true, usesVariableAsConstant); - this.typeConstructor = new IntegerValueTypeConstructor(value.longValue()); + init { + this.typeConstructor = IntegerValueTypeConstructor(value.toLong()) } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return new JetTypeImpl( - Annotations.EMPTY, typeConstructor, - false, Collections.emptyList(), - ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true)); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return JetTypeImpl(Annotations.EMPTY, typeConstructor, false, emptyList(), ErrorUtils.createErrorScope("Scope for number value type (" + typeConstructor.toString() + ")", true)) } - @Nullable - @Override - @Deprecated - public Number getValue() { - throw new UnsupportedOperationException("Use IntegerValueTypeConstant.getValue(expectedType) instead"); + deprecated("") + override val value: Number + get() = throw UnsupportedOperationException("Use IntegerValueTypeConstant.getValue(expectedType) instead") + + public fun getType(expectedType: JetType): JetType { + return TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType) } - @NotNull - public JetType getType(@NotNull JetType expectedType) { - return TypeUtils.getPrimitiveNumberType(typeConstructor, expectedType); - } + public fun getValue(expectedType: JetType): Number { + val numberValue = typeConstructor.getValue() + val builtIns = KotlinBuiltIns.getInstance() - @NotNull - public Number getValue(@NotNull JetType expectedType) { - Number numberValue = typeConstructor.getValue(); - KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance(); - - JetType valueType = getType(expectedType); - if (valueType.equals(builtIns.getIntType())) { - return numberValue.intValue(); + val valueType = getType(expectedType) + if (valueType == builtIns.getIntType()) { + return numberValue.toInt() } - else if (valueType.equals(builtIns.getByteType())) { - return numberValue.byteValue(); + else if (valueType == builtIns.getByteType()) { + return numberValue.toByte() } - else if (valueType.equals(builtIns.getShortType())) { - return numberValue.shortValue(); + else if (valueType == builtIns.getShortType()) { + return numberValue.toShort() } else { - return numberValue.longValue(); + return numberValue.toLong() } } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitNumberTypeValue(this, data); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitNumberTypeValue(this, data) } - @Override - public String toString() { - return typeConstructor.toString(); + override fun toString(): String { + return typeConstructor.toString() } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstructor.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstructor.kt index aaec9522e06..93a736afdb4 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstructor.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/IntegerValueTypeConstructor.kt @@ -14,83 +14,65 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.ClassifierDescriptor; -import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor; -import org.jetbrains.kotlin.descriptors.annotations.Annotations; -import org.jetbrains.kotlin.types.JetType; -import org.jetbrains.kotlin.types.TypeConstructor; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.ClassifierDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor +import org.jetbrains.kotlin.descriptors.annotations.Annotations +import org.jetbrains.kotlin.types.JetType +import org.jetbrains.kotlin.types.TypeConstructor -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.ArrayList +import java.util.Collections -public class IntegerValueTypeConstructor implements TypeConstructor { - private final long value; - private final Collection supertypes = new ArrayList(4); +public class IntegerValueTypeConstructor(private val value: Long) : TypeConstructor { + private val supertypes = ArrayList(4) - public IntegerValueTypeConstructor(long value) { - // order of types matters - // 'getPrimitiveNumberType' returns first of supertypes that is a subtype of expected type - // for expected type 'Any' result type 'Int' should be returned - this.value = value; - checkBoundsAndAddSuperType(value, (long) Integer.MIN_VALUE, (long) Integer.MAX_VALUE, KotlinBuiltIns.getInstance().getIntType()); - checkBoundsAndAddSuperType(value, (long) Byte.MIN_VALUE, (long) Byte.MAX_VALUE, KotlinBuiltIns.getInstance().getByteType()); - checkBoundsAndAddSuperType(value, (long) Short.MIN_VALUE, (long) Short.MAX_VALUE, KotlinBuiltIns.getInstance().getShortType()); - supertypes.add(KotlinBuiltIns.getInstance().getLongType()); - } + init { + checkBoundsAndAddSuperType(value, Integer.MIN_VALUE.toLong(), Integer.MAX_VALUE.toLong(), KotlinBuiltIns.getInstance().getIntType()) + checkBoundsAndAddSuperType(value, java.lang.Byte.MIN_VALUE.toLong(), java.lang.Byte.MAX_VALUE.toLong(), KotlinBuiltIns.getInstance().getByteType()) + checkBoundsAndAddSuperType(value, java.lang.Short.MIN_VALUE.toLong(), java.lang.Short.MAX_VALUE.toLong(), KotlinBuiltIns.getInstance().getShortType()) + supertypes.add(KotlinBuiltIns.getInstance().getLongType()) + }// order of types matters + // 'getPrimitiveNumberType' returns first of supertypes that is a subtype of expected type + // for expected type 'Any' result type 'Int' should be returned - private void checkBoundsAndAddSuperType(long value, long minValue, long maxValue, JetType kotlinType) { + private fun checkBoundsAndAddSuperType(value: Long, minValue: Long, maxValue: Long, kotlinType: JetType) { if (value >= minValue && value <= maxValue) { - supertypes.add(kotlinType); + supertypes.add(kotlinType) } } - @NotNull - @Override - public Collection getSupertypes() { - return supertypes; + override fun getSupertypes(): Collection { + return supertypes } - @NotNull - @Override - public List getParameters() { - return Collections.emptyList(); + override fun getParameters(): List { + return emptyList() } - @Override - public boolean isFinal() { - return false; + override fun isFinal(): Boolean { + return false } - @Override - public boolean isDenotable() { - return false; + override fun isDenotable(): Boolean { + return false } - @Nullable - @Override - public ClassifierDescriptor getDeclarationDescriptor() { - return null; + override fun getDeclarationDescriptor(): ClassifierDescriptor? { + return null } - @NotNull - @Override - public Annotations getAnnotations() { - return Annotations.EMPTY; + override fun getAnnotations(): Annotations { + return Annotations.EMPTY } - public Long getValue() { - return value; + public fun getValue(): Long { + return value } - @Override - public String toString() { - return "IntegerValueType(" + value + ")"; + override fun toString(): String { + return "IntegerValueType(" + value + ")" } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt index 563f1b680bb..ec518f99fbd 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/KClassValue.kt @@ -21,9 +21,10 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor import org.jetbrains.kotlin.types.JetType -public class KClassValue(type: JetType) : CompileTimeConstant(type, true, false, false) { - override fun getType(kotlinBuiltIns: KotlinBuiltIns) = value - override fun getValue(): JetType = value.getArguments().single().getType() +public class KClassValue(private val type: JetType) : CompileTimeConstant(type, true, false, false) { + override fun getType(kotlinBuiltIns: KotlinBuiltIns) = type + override val value: JetType + get() = type.getArguments().single().getType() override fun accept(visitor: AnnotationArgumentVisitor, data: D) = visitor.visitKClassValue(this, data) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt index 448dd6fab72..b0f803bf7e2 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/LongValue.kt @@ -14,32 +14,23 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class LongValue extends IntegerValueConstant { +public class LongValue(value: Long, canBeUsedInAnnotations: Boolean, pure: Boolean, usesVariableAsConstant: Boolean) : IntegerValueConstant(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) { - public LongValue(long value, boolean canBeUsedInAnnotations, boolean pure, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, pure, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getLongType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getLongType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitLongValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitLongValue(this, data); - } - - @Override - public String toString() { - return value + ".toLong()"; + override fun toString(): String { + return "$value.toLong()" } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt index a4ec8773aa7..35fccaf9a49 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/NullValue.kt @@ -14,34 +14,27 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class NullValue extends CompileTimeConstant { +public class NullValue private constructor() : CompileTimeConstant(null, false, false, false) { - public static final NullValue NULL = new NullValue(); - - private NullValue() { - super(null, false, false, false); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getNullableNothingType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getNullableNothingType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitNullValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitNullValue(this, data); + override fun toString(): String { + return "null" } - @Override - public String toString() { - return "null"; + companion object { + public val NULL: NullValue = NullValue() } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt index b32fcc2ceb1..45294ca2d27 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/ShortValue.kt @@ -14,33 +14,24 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class ShortValue extends IntegerValueConstant { +public class ShortValue(value: Short, canBeUsedInAnnotations: Boolean, pure: Boolean, usesVariableAsConstant: Boolean) : IntegerValueConstant(value, canBeUsedInAnnotations, pure, usesVariableAsConstant) { - public ShortValue(short value, boolean canBeUsedInAnnotations, boolean pure, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, pure, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getShortType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getShortType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitShortValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitShortValue(this, data); - } - - @Override - public String toString() { - return value + ".toShort()"; + override fun toString(): String { + return "$value.toShort()" } } diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt index d3367beb420..e1cb3feac4f 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/constants/StringValue.kt @@ -14,49 +14,38 @@ * limitations under the License. */ -package org.jetbrains.kotlin.resolve.constants; +package org.jetbrains.kotlin.resolve.constants -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.builtins.KotlinBuiltIns; -import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor; -import org.jetbrains.kotlin.types.JetType; +import org.jetbrains.kotlin.builtins.KotlinBuiltIns +import org.jetbrains.kotlin.descriptors.annotations.AnnotationArgumentVisitor +import org.jetbrains.kotlin.types.JetType -public class StringValue extends CompileTimeConstant { +public class StringValue(value: String, canBeUsedInAnnotations: Boolean, usesVariableAsConstant: Boolean) : CompileTimeConstant(value, canBeUsedInAnnotations, false, usesVariableAsConstant) { - public StringValue(String value, boolean canBeUsedInAnnotations, boolean usesVariableAsConstant) { - super(value, canBeUsedInAnnotations, false, usesVariableAsConstant); + override fun getType(kotlinBuiltIns: KotlinBuiltIns): JetType { + return kotlinBuiltIns.getStringType() } - @NotNull - @Override - public JetType getType(@NotNull KotlinBuiltIns kotlinBuiltIns) { - return kotlinBuiltIns.getStringType(); + override fun accept(visitor: AnnotationArgumentVisitor, data: D): R { + return visitor.visitStringValue(this, data) } - @Override - public R accept(AnnotationArgumentVisitor visitor, D data) { - return visitor.visitStringValue(this, data); + override fun toString(): String { + return "\"" + value + "\"" } - @Override - public String toString() { - return "\"" + value + "\""; + override fun equals(o: Any?): Boolean { + if (this === o) return true + if (o == null || javaClass != o.javaClass) return false + + val that = o as? StringValue ?: return false + + if (if (value != null) value != that.value else that.value != null) return false + + return true } - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - StringValue that = (StringValue) o; - - if (value != null ? !value.equals(that.value) : that.value != null) return false; - - return true; - } - - @Override - public int hashCode() { - return value != null ? value.hashCode() : 0; + override fun hashCode(): Int { + return if (value != null) value.hashCode() else 0 } } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt index 52f70e28e34..01afc5eb16e 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/ConvertToStringTemplateIntention.kt @@ -95,7 +95,7 @@ public class ConvertToStringTemplateIntention : JetSelfTargetingOffsetIndependen constant.getValue(type).toString() } else { - constant?.getValue().toString() + constant?.value.toString() } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt index 23f76759d79..1be97fe23b2 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/DeprecatedSymbolUsageFixBase.kt @@ -112,7 +112,7 @@ public abstract class DeprecatedSymbolUsageFixBase( if (pattern.isEmpty()) return null val importValues = replaceWithValue.argumentValue("imports"/*TODO: kotlin.ReplaceWith::imports.name*/) as? List<*> ?: return null if (importValues.any { it !is StringValue }) return null - val imports = importValues.map { (it as StringValue).getValue()!! } + val imports = importValues.map { (it as StringValue).value } // should not be available for descriptors with optional parameters if we cannot fetch default values for them (currently for library with no sources) if (descriptor is CallableDescriptor && diff --git a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt index 9a4cf2dd0d5..de5fabeb1e3 100644 --- a/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt +++ b/js/js.frontend/src/org/jetbrains/kotlin/js/resolve/diagnostics/JsCallChecker.kt @@ -84,7 +84,7 @@ public class JsCallChecker : CallChecker { return } - val code = evaluationResult.getValue() as String + val code = evaluationResult.value as String val errorReporter = JsCodeErrorReporter(argument, code, context.trace) try {