KT-4107 Data objects
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
// LANGUAGE: +DataObjects
|
||||
// WITH_STDLIB
|
||||
|
||||
package com.example
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
data object DataObject
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(DataObject, DataObject)
|
||||
assertFalse(DataObject == null)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
// LANGUAGE: +DataObjects
|
||||
// WITH_STDLIB
|
||||
|
||||
package com.example
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
data object DataObject {
|
||||
data object Inner
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(DataObject.hashCode(), DataObject.hashCode())
|
||||
assertNotEquals(DataObject.hashCode(), DataObject.Inner.hashCode())
|
||||
assertNotEquals(0, DataObject.hashCode())
|
||||
assertNotEquals(0, DataObject.Inner.hashCode())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
// LANGUAGE: +DataObjects
|
||||
// WITH_STDLIB
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
data object DataObject
|
||||
|
||||
val doppelganger = DataObject::class.java.declaredConstructors[0].apply { isAccessible = true }.newInstance()
|
||||
|
||||
fun box(): String {
|
||||
assertFalse(DataObject === doppelganger)
|
||||
assertEquals(DataObject, doppelganger)
|
||||
assertEquals(DataObject.hashCode(), DataObject::class.java.cast(doppelganger).hashCode())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// LANGUAGE: +DataObjects
|
||||
// WITH_STDLIB
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
// FULL_JDK
|
||||
// CHECK_BYTECODE_LISTING
|
||||
|
||||
import java.io.*
|
||||
|
||||
data object NonSerializableDataObject
|
||||
|
||||
data object SerializableDataObject: Serializable
|
||||
|
||||
fun box(): String {
|
||||
ByteArrayOutputStream().use { baos ->
|
||||
ObjectOutputStream(baos).use { oos -> oos.writeObject(SerializableDataObject) }
|
||||
ByteArrayInputStream(baos.toByteArray()).use { bais -> assert(java.io.ObjectInputStream(bais).readObject() === SerializableDataObject) }
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
@kotlin.Metadata
|
||||
public final class NonSerializableDataObject {
|
||||
// source: 'serialization.kt'
|
||||
public final static @org.jetbrains.annotations.NotNull field INSTANCE: NonSerializableDataObject
|
||||
static method <clinit>(): void
|
||||
private method <init>(): void
|
||||
public method equals(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
|
||||
public method hashCode(): int
|
||||
public @org.jetbrains.annotations.NotNull method toString(): java.lang.String
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class SerializableDataObject {
|
||||
// source: 'serialization.kt'
|
||||
public final static @org.jetbrains.annotations.NotNull field INSTANCE: SerializableDataObject
|
||||
static method <clinit>(): void
|
||||
private method <init>(): void
|
||||
public method equals(@org.jetbrains.annotations.Nullable p0: java.lang.Object): boolean
|
||||
public method hashCode(): int
|
||||
private final method readResolve(): java.lang.Object
|
||||
public @org.jetbrains.annotations.NotNull method toString(): java.lang.String
|
||||
}
|
||||
|
||||
@kotlin.Metadata
|
||||
public final class SerializationKt {
|
||||
// source: 'serialization.kt'
|
||||
public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// LANGUAGE: +DataObjects
|
||||
// WITH_STDLIB
|
||||
|
||||
package com.example
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
data object DataObject {
|
||||
data object Nested
|
||||
}
|
||||
|
||||
class Foo {
|
||||
data object Inner
|
||||
}
|
||||
|
||||
data object Declared {
|
||||
override fun toString() = "Overriden"
|
||||
}
|
||||
|
||||
open class WithFinalToString {
|
||||
final override fun toString() = "FinalToString"
|
||||
}
|
||||
|
||||
data object InheritedFromClassWithFinalToString: WithFinalToString()
|
||||
|
||||
open class WithOpenToString {
|
||||
override fun toString() = "OpenToString"
|
||||
}
|
||||
|
||||
data object InheritedFromClassWithOpenToString: WithOpenToString()
|
||||
|
||||
abstract class WithAbstractToString {
|
||||
abstract override fun toString(): String
|
||||
}
|
||||
|
||||
data object InheritedFromClassWithAbstractToString : WithAbstractToString()
|
||||
|
||||
class C {
|
||||
companion object CC
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals("DataObject", DataObject.toString())
|
||||
assertEquals("Nested", DataObject.Nested.toString())
|
||||
assertEquals("Inner", Foo.Inner.toString())
|
||||
assertEquals("Overriden", Declared.toString())
|
||||
assertEquals("FinalToString", InheritedFromClassWithFinalToString.toString())
|
||||
assertEquals("InheritedFromClassWithOpenToString", InheritedFromClassWithOpenToString.toString())
|
||||
assertEquals("InheritedFromClassWithAbstractToString", InheritedFromClassWithAbstractToString.toString())
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// FIR_IDENTICAL
|
||||
// LANGUAGE: +DataObjects
|
||||
|
||||
class C {
|
||||
companion <!WRONG_MODIFIER_TARGET!>data<!> object Object
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package
|
||||
|
||||
public final class C {
|
||||
public constructor C()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
|
||||
public data companion object Object {
|
||||
private constructor Object()
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
// FIR_IDENTICAL
|
||||
<!WRONG_MODIFIER_TARGET!>data<!> object Object<!CONSTRUCTOR_IN_OBJECT!>(val x: Int, val y: Int)<!>
|
||||
@@ -1,13 +0,0 @@
|
||||
package
|
||||
|
||||
public data object Object {
|
||||
private constructor Object(/*0*/ x: kotlin.Int, /*1*/ y: kotlin.Int)
|
||||
public final val x: kotlin.Int
|
||||
public final val y: kotlin.Int
|
||||
public final operator /*synthesized*/ fun component1(): kotlin.Int
|
||||
public final operator /*synthesized*/ fun component2(): kotlin.Int
|
||||
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Int = ..., /*1*/ y: kotlin.Int = ...): Object
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// LANGUAGE: -DataObjects
|
||||
<!WRONG_MODIFIER_TARGET!>data<!> object Object
|
||||
@@ -0,0 +1,2 @@
|
||||
// LANGUAGE: -DataObjects
|
||||
<!UNSUPPORTED_FEATURE!>data<!> object Object
|
||||
@@ -0,0 +1,9 @@
|
||||
package
|
||||
|
||||
public data object Object {
|
||||
private constructor Object()
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
// LANGUAGE: +DataObjects
|
||||
// FIR_IDENTICAL
|
||||
data object Object
|
||||
@@ -0,0 +1,9 @@
|
||||
package
|
||||
|
||||
public data object Object {
|
||||
private constructor Object()
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// FIR_IDENTICAL
|
||||
// LANGUAGE: +DataObjects
|
||||
|
||||
interface I {
|
||||
fun foo()
|
||||
}
|
||||
|
||||
val o = <!UNRESOLVED_REFERENCE!>data<!><!SYNTAX!><!> object<!SYNTAX!><!>: I {
|
||||
override fun foo() {}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package
|
||||
|
||||
public val o: [Error type: Not found recorded type for data]
|
||||
|
||||
public object `<no name provided>` : I {
|
||||
private constructor `<no name provided>`()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ fun foo(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public interface I {
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public abstract fun foo(): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
@@ -3,8 +3,8 @@
|
||||
B(2)
|
||||
}
|
||||
|
||||
<!WRONG_MODIFIER_TARGET!>data<!> object <!DATA_CLASS_WITHOUT_PARAMETERS!>Second<!>
|
||||
<!UNSUPPORTED_FEATURE!>data<!> object Second
|
||||
|
||||
<!WRONG_MODIFIER_TARGET!>data<!> interface <!DATA_CLASS_WITHOUT_PARAMETERS!>Third<!>
|
||||
<!WRONG_MODIFIER_TARGET!>data<!> interface Third
|
||||
|
||||
<!WRONG_MODIFIER_TARGET!>data<!> annotation class Fourth(val x: Int)
|
||||
<!WRONG_MODIFIER_TARGET!>data<!> annotation class Fourth(val x: Int)
|
||||
|
||||
@@ -11,8 +11,6 @@ public final data enum class First : kotlin.Enum<First> {
|
||||
public final val x: kotlin.Int
|
||||
protected final override /*1*/ /*fake_override*/ fun clone(): kotlin.Any
|
||||
public final override /*1*/ /*fake_override*/ fun compareTo(/*0*/ other: First): kotlin.Int
|
||||
public final operator /*synthesized*/ fun component1(): kotlin.Int
|
||||
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Int = ...): First
|
||||
public final override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
protected/*protected and package*/ final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun finalize(): kotlin.Unit
|
||||
public final override /*1*/ /*fake_override*/ /*isHiddenForResolutionEverywhereBesideSupercalls*/ fun getDeclaringClass(): java.lang.Class<First!>!
|
||||
@@ -27,8 +25,6 @@ public final data enum class First : kotlin.Enum<First> {
|
||||
public final data annotation class Fourth : kotlin.Annotation {
|
||||
public constructor Fourth(/*0*/ x: kotlin.Int)
|
||||
public final val x: kotlin.Int
|
||||
public final operator /*synthesized*/ fun component1(): kotlin.Int
|
||||
public final /*synthesized*/ fun copy(/*0*/ x: kotlin.Int = ...): Fourth
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
@@ -36,7 +32,6 @@ public final data annotation class Fourth : kotlin.Annotation {
|
||||
|
||||
public data object Second {
|
||||
private constructor Second()
|
||||
public final /*synthesized*/ fun copy(): Second
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
@@ -47,3 +42,4 @@ public data interface Third {
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
// FIR_IDENTICAL
|
||||
// LANGUAGE: +DataObjects
|
||||
|
||||
data object Override {
|
||||
<!DATA_OBJECT_CUSTOM_EQUALS_OR_HASH_CODE!>override<!> fun equals(other: Any?): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
<!DATA_OBJECT_CUSTOM_EQUALS_OR_HASH_CODE!>override<!> fun hashCode(): Int {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
open class Base {
|
||||
open fun hashCode(x: Int) = x
|
||||
}
|
||||
|
||||
data object NoOverride: Base() {
|
||||
fun equals(other: Any?, tag: Int): Boolean {
|
||||
return true
|
||||
}
|
||||
|
||||
fun hashCode(param: String): Int {
|
||||
return 1
|
||||
}
|
||||
|
||||
override fun hashCode(x: Int) = x + 1
|
||||
}
|
||||
|
||||
open class Super {
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return super.equals(other)
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
data object OverridenInSuper: Super() {
|
||||
<!DATA_OBJECT_CUSTOM_EQUALS_OR_HASH_CODE!>override<!> fun equals(other: Any?): Boolean {
|
||||
return super.equals(other)
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package
|
||||
|
||||
public open class Base {
|
||||
public constructor Base()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open fun hashCode(/*0*/ x: kotlin.Int): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public data object NoOverride : Base {
|
||||
private constructor NoOverride()
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public final fun equals(/*0*/ other: kotlin.Any?, /*1*/ tag: kotlin.Int): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ fun hashCode(/*0*/ x: kotlin.Int): kotlin.Int
|
||||
public final fun hashCode(/*0*/ param: kotlin.String): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public data object Override {
|
||||
private constructor Override()
|
||||
public open override /*1*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public data object OverridenInSuper : Super {
|
||||
private constructor OverridenInSuper()
|
||||
public open override /*1*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public open class Super {
|
||||
public constructor Super()
|
||||
public open override /*1*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// !LANGUAGE: +DataObjects
|
||||
// TARGET_BACKEND: JVM_IR
|
||||
|
||||
import java.io.Serializable
|
||||
|
||||
data <!CONFLICTING_JVM_DECLARATIONS!>object A<!> : Serializable {
|
||||
<!CONFLICTING_JVM_DECLARATIONS!>private fun readResolve(): Any<!> = this
|
||||
}
|
||||
|
||||
data object B : Serializable {
|
||||
private fun readResolve(): B = this
|
||||
}
|
||||
|
||||
data object C {
|
||||
private fun readResolve(): Any = this
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package
|
||||
|
||||
public data object A : java.io.Serializable {
|
||||
private constructor A()
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
private final fun readResolve(): kotlin.Any
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public data object B : java.io.Serializable {
|
||||
private constructor B()
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
private final fun readResolve(): B
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
public data object C {
|
||||
private constructor C()
|
||||
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
|
||||
private final fun readResolve(): kotlin.Any
|
||||
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
|
||||
}
|
||||
|
||||
+1
@@ -56,3 +56,4 @@ data class Test<T : Any?> {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user