summaryrefslogtreecommitdiff
path: root/test/junit/scala/issues/RunTest.scala
blob: 3ebdc8a72ff6b6d8aa281ea1a32ec1f508d25d97 (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
package scala.issues

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.{AfterClass, BeforeClass, Test}
import org.junit.Assert._

import scala.reflect.runtime._
import scala.tools.reflect.ToolBox
import scala.tools.testing.ClearAfterClass

object RunTest {
  class VC(val x: Any) extends AnyVal
  class VCI(val x: Int) extends AnyVal { override def toString = "" + x }
}

@RunWith(classOf[JUnit4])
class RunTest extends ClearAfterClass {
  val toolBox = cached("toolbox", () => universe.runtimeMirror(getClass.getClassLoader).mkToolBox())

  def run[T](code: String): T = {
    toolBox.eval(toolBox.parse(code)).asInstanceOf[T]
  }

  @Test
  def classOfValueClassAlias(): Unit = {
    val code =
      """import scala.issues.RunTest.VC
        |type aVC = VC
        |type aInt = Int
        |type aInteger = Integer
        |classOf[VC] == classOf[aVC] &&
        |  classOf[aInt] == classOf[Int] &&
        |  classOf[aInteger] == classOf[Integer] &&
        |  classOf[aInt] != classOf[aInteger]
      """.stripMargin
    assertTrue(run[Boolean](code))
  }

  @Test
  def classOfFinalVal(): Unit = {
    val code =
      """class C {
        |  final val a1 = classOf[Int]
        |  final val b1 = classOf[List[_]]
        |  final val c1 = classOf[List[String]]
        |  final val d1 = classOf[Array[Int]]
        |  final val e1 = classOf[Array[List[_]]]
        |  final val f1 = classOf[Array[_]]
        |
        |  val a2 = classOf[Int]
        |  val b2 = classOf[List[_]]
        |  val c2 = classOf[List[String]]
        |  val d2 = classOf[Array[Int]]
        |  val e2 = classOf[Array[List[_]]]
        |  val f2 = classOf[Array[_]]
        |
        |  val listC = Class.forName("scala.collection.immutable.List")
        |
        |  val compare = List(
        |    (a1, a2, Integer.TYPE),
        |    (b1, b2, listC),
        |    (c1, c2, listC),
        |    (d1, d2, Array(1).getClass),
        |    (e1, e2, Array(List()).getClass),
        |    (f1, f2, new Object().getClass))
        |}
        |(new C).compare
      """.stripMargin
    type K = Class[_]
    val cs = run[List[(K, K, K)]](code)
    for ((x, y, z) <- cs) {
      assertEquals(x, y)
      assertEquals(x, z)
    }
  }

  @Test
  def t9702(): Unit = {
    val code =
      """import javax.annotation.Resource
        |import scala.issues.RunTest.VC
        |class C {
        |  type aList[K] = List[K]
        |  type aVC = VC
        |  type aInt = Int
        |  type aInteger = Integer
        |  @Resource(`type` = classOf[List[Int]])      def a = 0
        |  @Resource(`type` = classOf[List[_]])        def b = 0
        |  @Resource(`type` = classOf[aList[_]])       def c = 0
        |  @Resource(`type` = classOf[Int])            def d = 0
        |  @Resource(`type` = classOf[aInt])           def e = 0
        |  @Resource(`type` = classOf[Integer])        def f = 0
        |  @Resource(`type` = classOf[aInteger])       def g = 0
        |  @Resource(`type` = classOf[VC])             def h = 0
        |  @Resource(`type` = classOf[aVC])            def i = 0
        |  @Resource(`type` = classOf[Array[Int]])     def j = 0
        |  @Resource(`type` = classOf[Array[List[_]]]) def k = 0
        |}
        |val c = classOf[C]
        |def typeArg(meth: String) = c.getDeclaredMethod(meth).getDeclaredAnnotation(classOf[Resource]).`type`
        |('a' to 'k').toList.map(_.toString).map(typeArg)
      """.stripMargin

    val l = Class.forName("scala.collection.immutable.List")
    val i = Integer.TYPE
    val ig = new Integer(1).getClass
    val v = new RunTest.VC(1).getClass
    val ai = Array(1).getClass
    val al = Array(List()).getClass

    // sanity checks
    assertEquals(i, classOf[Int])
    assertNotEquals(i, ig)

    assertEquals(run[List[Class[_]]](code),
      List(l, l, l, i, i, ig, ig, v, v, ai, al))
  }

  @Test
  def annotationInfoNotErased(): Unit = {
    val code =
      """import javax.annotation.Resource
        |import scala.annotation.meta.getter
        |class C {
        |  type Rg = Resource @getter
        |  @(Resource @getter)(`type` = classOf[Int]) def a = 0
        |  @Rg(`type` = classOf[Int])                 def b = 0
        |}
        |val c = classOf[C]
        |def typeArg(meth: String) = c.getDeclaredMethod(meth).getDeclaredAnnotation(classOf[Resource]).`type`
        |List("a", "b") map typeArg
        |""".stripMargin

    val i = Integer.TYPE
    assertEquals(run[List[Class[_]]](code), List(i, i))
  }

  @Test
  def invocationReceivers(): Unit = {
    import invocationReceiversTestCode._
    assertEquals(run[String](definitions("Object") + runCode), "hi" * 9)
    assertEquals(run[String](definitions("String") + runCode), "hi" * 9) // bridge method for clone generated
  }

  @Test
  def classOfUnitConstant(): Unit = {
    val code =
      """abstract class A { def f: Class[_] }
        |class C extends A { final val f = classOf[Unit] }
        |val c = new C
        |(c.f, (c: A).f)
      """.stripMargin
    val u = Void.TYPE
    assertEquals(run[(Class[_], Class[_])](code), (u, u))
  }

  @Test
  def t9671(): Unit = {
    val code =
      """import scala.issues.RunTest.VCI
        |
        |def f1(a: Any) = "" + a
        |def f2(a: AnyVal) = "" + a
        |def f3[T](a: T) = "" + a
        |def f4(a: Int) = "" + a
        |def f5(a: VCI) = "" + a
        |def f6(u: Unit) = "" + u
        |
        |def n1: AnyRef = null
        |def n2: Null = null
        |def n3: Any = null
        |def n4[T]: T = null.asInstanceOf[T]
        |
        |def npe(s: => String) = try { s; throw new Error() } catch { case _: NullPointerException => "npe" }
        |
        |    f1(null.asInstanceOf[Int])  +
        |    f1(  n1.asInstanceOf[Int])  +
        |    f1(  n2.asInstanceOf[Int])  +
        |    f1(  n3.asInstanceOf[Int])  +
        |    f1(               n4[Int])  + // "null"
        |"-"                             +
        |    f1(null.asInstanceOf[VCI])  +
        |npe(f1(  n1.asInstanceOf[VCI])) + // SI-8097
        |    f1(  n2.asInstanceOf[VCI])  +
        |npe(f1(  n3.asInstanceOf[VCI])) + // SI-8097
        |    f1(               n4[VCI])  + // "null"
        |"-"                             +
        |    f1(null.asInstanceOf[Unit]) +
        |    f1(  n1.asInstanceOf[Unit]) +
        |    f1(  n2.asInstanceOf[Unit]) +
        |    f1(  n3.asInstanceOf[Unit]) +
        |    f1(               n4[Unit]) + // "null"
        |"-"                             +
        |    f2(null.asInstanceOf[Int])  +
        |    f2(  n1.asInstanceOf[Int])  +
        |    f2(  n2.asInstanceOf[Int])  +
        |    f2(  n3.asInstanceOf[Int])  +
        |    f2(               n4[Int])  + // "null"
        |"-"                             +
        |    f2(null.asInstanceOf[VCI])  +
        |npe(f2(  n1.asInstanceOf[VCI])) + // SI-8097
        |    f2(  n2.asInstanceOf[VCI])  +
        |npe(f2(  n3.asInstanceOf[VCI])) + // SI-8097
        |    f2(               n4[VCI])  + // "null"
        |"-"                             +
        |    f2(null.asInstanceOf[Unit]) +
        |    f2(  n1.asInstanceOf[Unit]) +
        |    f2(  n2.asInstanceOf[Unit]) +
        |    f2(  n3.asInstanceOf[Unit]) +
        |    f2(               n4[Unit]) + // "null"
        |"-"                             +
        |    f3(null.asInstanceOf[Int])  +
        |    f3(  n1.asInstanceOf[Int])  +
        |    f3(  n2.asInstanceOf[Int])  +
        |    f3(  n3.asInstanceOf[Int])  +
        |    f3(               n4[Int])  + // "null"
        |"-"                             +
        |    f3(null.asInstanceOf[VCI])  +
        |npe(f3(  n1.asInstanceOf[VCI])) + // SI-8097
        |    f3(  n2.asInstanceOf[VCI])  +
        |npe(f3(  n3.asInstanceOf[VCI])) + // SI-8097
        |    f3(               n4[VCI])  + // "null"
        |"-"                             +
        |    f3(null.asInstanceOf[Unit]) +
        |    f3(  n1.asInstanceOf[Unit]) +
        |    f3(  n2.asInstanceOf[Unit]) +
        |    f3(  n3.asInstanceOf[Unit]) +
        |    f3(               n4[Unit]) + // "null"
        |"-"                             +
        |    f4(null.asInstanceOf[Int])  +
        |    f4(  n1.asInstanceOf[Int])  +
        |    f4(  n2.asInstanceOf[Int])  +
        |    f4(  n3.asInstanceOf[Int])  +
        |    f4(               n4[Int])  +
        |"-"                             +
        |    f5(null.asInstanceOf[VCI])  +
        |npe(f5(  n1.asInstanceOf[VCI])) + // SI-8097
        |    f5(  n2.asInstanceOf[VCI])  +
        |npe(f5(  n3.asInstanceOf[VCI])) + // SI-8097
        |npe(f5(               n4[VCI])) + // SI-8097
        |"-"                             +
        |    f6(null.asInstanceOf[Unit]) +
        |    f6(  n1.asInstanceOf[Unit]) +
        |    f6(  n2.asInstanceOf[Unit]) +
        |    f6(  n3.asInstanceOf[Unit]) +
        |    f6(               n4[Unit])   // "null"
      """.stripMargin

    assertEquals(run[String](code),
      "0000null-0npe0npenull-()()()()null-0000null-0npe0npenull-()()()()null-0000null-0npe0npenull-()()()()null-00000-0npe0npenpe-()()()()null")
  }
}