summaryrefslogtreecommitdiff
path: root/test/files/run/t9375.scala
blob: 58893c963b2bc02d9d9e09763ada8c98e7e20b22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.io._

object SerDes {
  def serialize(obj: AnyRef): Array[Byte] = {
    val buffer = new ByteArrayOutputStream
    val out = new ObjectOutputStream(buffer)
    out.writeObject(obj)
    buffer.toByteArray
  }

  def deserialize(a: Array[Byte]): AnyRef = {
    val in = new ObjectInputStream(new ByteArrayInputStream(a))
    in.readObject
  }

  def serializeDeserialize[T <: AnyRef](obj: T) = deserialize(serialize(obj)).asInstanceOf[T]
}

import SerDes._

// tests to make sure that deserializing an object does not run its constructor

trait S extends Serializable {
  println("  konstruktor: " + this.getClass)
}

trait SE extends S {
  def outer: Object
}

class A extends S {
  object O extends SE { def outer = A.this }
  private[this] object Op extends SE { def outer = A.this }
  def P: SE = Op

  object N extends S {
    object O extends SE { def outer = N }
    private[this] object Op extends SE { def outer = N }
    def P: SE = Op
  }

  class A extends S {
    object O extends SE { def outer = A.this }
    private[this] object Op extends SE { def outer = A.this }
    def P: SE = Op
  }

  trait T extends S {
    object O extends SE { def outer = T.this }
    private[this] object Op extends SE { def outer = T.this }
    def P: SE = Op
  }
  class C extends T

  def u: SE = {
    object O extends SE { def outer = A.this }
    O
  }

  val v: SE = {
    object O extends SE { def outer = A.this }
    O
  }

  val f: () => SE = () => {
    object O extends SE { def outer = A.this }
    O
  }

  trait GetObj { def O: SE; def P: SE }
  val a: GetObj = new GetObj with S {
    def anonThis = this
    object O extends SE { def outer = anonThis }
    private[this] object Op extends SE { def outer = anonThis }
    def P: SE = Op
  }
}

trait T extends S {
  object O extends SE { def outer = T.this }
  private[this] object Op extends SE { def outer = T.this }
  def P: SE = Op

  object N extends S {
    object O extends SE { def outer = N }
    private[this] object Op extends SE { def outer = N }
    def P: SE = Op
  }

  class A extends S {
    object O extends SE { def outer = A.this }
    private[this] object Op extends SE { def outer = A.this }
    def P: SE = Op
  }

  trait T extends S {
    object O extends SE { def outer = T.this }
    private[this] object Op extends SE { def outer = T.this }
    def P: SE = Op
  }
  class C extends T

  def u: SE = {
    object O extends SE { def outer = T.this }
    O
  }

  val v: SE = {
    object O extends SE { def outer = T.this }
    O
  }

  val f: () => SE = () => {
    object O extends SE { def outer = T.this }
    O
  }

  trait GetObj { def O: SE; def P: SE }
  val a: GetObj = new GetObj with S {
    def anonThis = this
    object O extends SE { def outer = anonThis }
    private[this] object Op extends SE { def outer = anonThis }
    def P: SE = Op
  }
}

class C extends T

object DeserializeModuleNoConstructor {
  def t(): Unit = {
    val a = new A
    val aa = new a.A
    val ac = new a.C

    val c = new C
    val ca = new c.A
    val cc = new c.C

    val outers: List[Object] = List(
      a, a.N, aa, ac, a.a,
      c, c.N, ca, cc, c.a
    )

    println("serializing outer objects should not initialize any nested objects")

    val serANotInit = serialize(a)
    outers foreach serializeDeserialize

    println("now initializing nested objects")

    val os: List[(SE, Object)] = List(
      a.O   -> a,
      a.P   -> a,
      a.N.O -> a.N,
      a.N.P -> a.N,
      aa.O  -> aa,
      aa.P  -> aa,
      ac.O  -> ac,
      ac.P  -> ac,
      a.u   -> a,
      a.v   -> a,
      a.f() -> a,
      a.a.O -> a.a,
      a.a.P -> a.a,

      c.O   -> c,
      c.P   -> c,
      c.N.O -> c.N,
      c.N.P -> c.N,
      ca.O  -> ca,
      ca.P  -> ca,
      cc.O  -> cc,
      cc.P  -> cc,
      c.u   -> c,
      c.v   -> c,
      c.f() -> c,
      c.a.O -> c.a,
      c.a.P -> c.a
    )

    println("no object konstruktors called when serializing / deserializing objects (starting at the outer or the object itself)")

    for ((obj, outer) <- os) {
      assert(obj.outer eq outer, s"${obj.outer} of $obj -- $outer")
      serializeDeserialize(obj)
      serializeDeserialize(outer)
    }

    println("deserializing outer objects with non-initialized inners again")
    val aNotInit = deserialize(serANotInit).asInstanceOf[A]

    println("accessing modules triggers initialization")
    aNotInit.O
    aNotInit.P
    aNotInit.N.O
    aNotInit.N.P

    println("deserializing creates a new object graph, including new scala 'object' instances, no matter where serialization starts")
    val deserializedAs: List[A] = List(
      serializeDeserialize(a),
      serializeDeserialize(a.O).outer.asInstanceOf[A],
      serializeDeserialize(a.P).outer.asInstanceOf[A],
      serializeDeserialize(a.v).outer.asInstanceOf[A]
    )
    for (aSD <- deserializedAs) {
      assert(aSD ne a)
      assert(aSD.O ne a.O)
      assert(aSD.P ne a.P)
      assert(aSD.N ne a.N)
      assert(aSD.N.O ne a.N.O)
      assert(aSD.N.P ne a.N.P)
      assert(aSD.v ne a.v)
      assert(aSD.a.O ne a.a.O)
      assert(aSD.a.P ne a.a.P)
    }
  }
}

// tests for serializing / deserializing static modules

object M extends S {
  object O extends S

  def u: S = {
    object O extends S
    O
  }

  val v: S = {
    object O extends S
    O
  }

  lazy val w: S = {
    object O extends S
    O
  }

  val f: () => S = () => {
    object O extends S
    O
  }
}

object SerializingStaticModules {
  def t(): Unit = {
    println("init static module M and field v")
    M

    println("serDeser does not initialize nested static modules")
    assert(serializeDeserialize(M) eq M)

    println("init M.O")
    M.O

    println("serDeser nested static module")
    assert(serializeDeserialize(M.O) eq M.O)

    println("objects declared in field decls are not static modules, so they deserialize to new instances")
    assert(serializeDeserialize(M.v) ne M.v)

    println("init lazy val M.w")

    println("objects declared in lazy val are not static modules either")
    assert(serializeDeserialize(M.w) ne M.w)

    println("object declared in a function: new instance created on each invocation")
    assert(M.f() ne M.f())
  }
}


object Test extends App {
  DeserializeModuleNoConstructor.t()
  SerializingStaticModules.t()
}