Skip to content

Commit 2acea19

Browse files
authored
Merge pull request scala#7754 from ashawley/issue-9306
Test defect for reflection of private fields
2 parents 229257c + 57e6e2e commit 2acea19

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

test/junit/scala/collection/mutable/OpenHashMapTest.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class OpenHashMapTest {
1515
val m = OpenHashMap.empty[Int, Int]
1616

1717
// Reflect to get the private `deleted` field's value, which should be zero.
18-
19-
/* TODO Doesn't work, due to scala/bug#9306.
18+
// Was broken, see scala/bug#9306.
2019
import scala.reflect.runtime.{universe => ru}
2120

2221
val mirror = ru.runtimeMirror(m.getClass.getClassLoader)
@@ -27,7 +26,7 @@ class OpenHashMapTest {
2726
.head.asTerm
2827

2928
val fieldMirror = mirror.reflect(m).reflectField(termSym)
30-
*/
29+
3130
// Use Java reflection instead for now.
3231
val field =
3332
try { // Name may or not be mangled, depending on what the compiler authors are doing.
@@ -43,7 +42,7 @@ class OpenHashMapTest {
4342
assertEquals(1, field.getInt(m))
4443

4544
m.put(0, 0) // Add an entry with the same key
46-
// TODO assertEquals(0, fieldMirror.get.asInstanceOf[Int])
45+
assertEquals(0, fieldMirror.get.asInstanceOf[Int])
4746
assertEquals(0, field.getInt(m))
4847
}
4948

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package scala.reflect
2+
3+
import org.junit.Assert._
4+
import org.junit.Ignore
5+
import org.junit.Test
6+
7+
class FieldAccessTest {
8+
9+
class TestClass {
10+
private val x = 123
11+
// Uncommenting the following line would make the test fail
12+
() => x
13+
}
14+
15+
/** scala/bug#9306 */
16+
@Test
17+
def testFieldAccess(): Unit = {
18+
import scala.reflect.runtime.{universe => ru}
19+
import scala.reflect.runtime.currentMirror
20+
val obj = new TestClass
21+
val objType = currentMirror.reflect(obj).symbol.toType
22+
val objFields = objType.members.collect { case ms: ru.MethodSymbol if ms.isGetter => ms }
23+
assertEquals(123, currentMirror.reflect(obj).reflectField(objFields.head).get)
24+
}
25+
}

0 commit comments

Comments
 (0)