@@ -3,62 +3,70 @@ import scala.language.unsafeNulls
33
44import java .lang .invoke ._ , MethodType .methodType
55
6- object Test extends Foo :
7- def main (args : Array [String ]): Unit = ()
8-
9- class Foo {
6+ class Foo :
107 def neg (x : Int ): Int = - x
118 def rev (s : String ): String = s.reverse
129 def over (l : Long ): String = " long"
1310 def over (i : Int ): String = " int"
1411 def unit (s : String ): Unit = ()
1512 def obj (s : String ): Object = s
16- def id [T ](x : T ): T = x
1713
18- val l = MethodHandles .lookup()
19- val self = new Foo ()
20- val mhNeg = l.findVirtual(classOf [Foo ], " neg" , methodType(classOf [Int ], classOf [Int ]))
21- val mhRev = l.findVirtual(classOf [Foo ], " rev" , methodType(classOf [String ], classOf [String ]))
22- val mhOverL = l.findVirtual(classOf [Foo ], " over" , methodType(classOf [String ], classOf [Long ]))
23- val mhOverI = l.findVirtual(classOf [Foo ], " over" , methodType(classOf [String ], classOf [Int ]))
24- val mhUnit = l.findVirtual(classOf [Foo ], " unit" , methodType(classOf [Unit ], classOf [String ]))
25- val mhObj = l.findVirtual(classOf [Foo ], " obj" , methodType(classOf [Any ], classOf [String ]))
26- val mhCL = l.findStatic(classOf [ClassLoader ], " getPlatformClassLoader" , methodType(classOf [ClassLoader ]))
14+ object Test :
15+ def main (args : Array [String ]): Unit =
16+ val l = MethodHandles .lookup()
17+ val self = new Foo ()
18+ val mhNeg = l.findVirtual(classOf [Foo ], " neg" , methodType(classOf [Int ], classOf [Int ]))
19+ val mhRev = l.findVirtual(classOf [Foo ], " rev" , methodType(classOf [String ], classOf [String ]))
20+ val mhOverL = l.findVirtual(classOf [Foo ], " over" , methodType(classOf [String ], classOf [Long ]))
21+ val mhOverI = l.findVirtual(classOf [Foo ], " over" , methodType(classOf [String ], classOf [Int ]))
22+ val mhUnit = l.findVirtual(classOf [Foo ], " unit" , methodType(classOf [Unit ], classOf [String ]))
23+ val mhObj = l.findVirtual(classOf [Foo ], " obj" , methodType(classOf [Any ], classOf [String ]))
24+ val mhCL = l.findStatic(classOf [ClassLoader ], " getPlatformClassLoader" , methodType(classOf [ClassLoader ]))
2725
28- val testNeg1 = assert(- 42 == (mhNeg.invokeExact(self, 42 ): Int ))
29- val testNeg2 = assert(- 33 == (mhNeg.invokeExact(self, 33 ): Int ))
26+ assert(- 42 == (mhNeg.invokeExact(self, 42 ): Int ))
27+ assert(- 33 == (mhNeg.invokeExact(self, 33 ): Int ))
3028
31- val testRev1 = assert(" oof" == (mhRev.invokeExact(self, " foo" ): String ))
32- val testRev2 = assert(" rab" == (mhRev.invokeExact(self, " bar" ): String ))
29+ assert(" oof" == (mhRev.invokeExact(self, " foo" ): String ))
30+ assert(" rab" == (mhRev.invokeExact(self, " bar" ): String ))
3331
34- val testOverL = assert(" long" == (mhOverL.invokeExact(self, 1L ): String ))
35- val testOVerI = assert(" int" == (mhOverI.invokeExact(self, 1 ): String ))
32+ assert(" long" == (mhOverL.invokeExact(self, 1L ): String ))
33+ assert(" int" == (mhOverI.invokeExact(self, 1 ): String ))
3634
37- val testNeg_tvar = assert(- 3 == (id(mhNeg.invokeExact(self, 3 )): Int ))
38- val testNeg_obj = expectWrongMethod(mhNeg.invokeExact(self, 4 ))
35+ assert(- 3 == (id(mhNeg.invokeExact(self, 3 )): Int ))
36+ expectWrongMethod(mhNeg.invokeExact(self, 4 ))
3937
40- val testUnit_exp = { mhUnit.invokeExact(self, " hi" ): Unit ; () }
41- val testUnit_val = { val hi2 : Unit = mhUnit.invokeExact(self, " hi2" ); assert((()) == hi2) }
42- val testUnit_def = { def hi3 : Unit = mhUnit.invokeExact(self, " hi3" ); assert((()) == hi3) }
38+ { mhUnit.invokeExact(self, " hi" ): Unit ; () } // explicit block
39+ val hi2 : Unit = mhUnit.invokeExact(self, " hi2" )
40+ assert((()) == hi2)
41+ def hi3 : Unit = mhUnit.invokeExact(self, " hi3" )
42+ assert((()) == hi3)
4343
44- val testObj_exp = { mhObj.invokeExact(self, " any" ); () }
45- val testObj_val = { val any2 = mhObj.invokeExact(self, " any2" ); assert(" any2" == any2) }
46- val testObj_def = { def any3 = mhObj.invokeExact(self, " any3" ); assert(" any3" == any3) }
44+ { mhObj.invokeExact(self, " any" ); () } // explicit block
45+ val any2 = mhObj.invokeExact(self, " any2" )
46+ assert(" any2" == any2)
47+ def any3 = mhObj.invokeExact(self, " any3" )
48+ assert(" any3" == any3)
4749
48- val testCl_pass = assert(null != (mhCL.invoke(): ClassLoader ))
49- val testCl_cast = assert(null != (mhCL.invoke().asInstanceOf [ClassLoader ]: ClassLoader ))
50- val testCl_passX = assert(null != (mhCL.invokeExact(): ClassLoader ))
51- val testCl_castX = assert(null != (mhCL.invokeExact().asInstanceOf [ClassLoader ]: ClassLoader ))
50+ assert(null != (mhCL.invoke(): ClassLoader ))
51+ assert(null != (mhCL.invoke().asInstanceOf [ClassLoader ]: ClassLoader ))
52+ assert(null != (mhCL.invokeExact(): ClassLoader ))
53+ assert(null != (mhCL.invokeExact().asInstanceOf [ClassLoader ]: ClassLoader ))
5254
53- val testNeg_inline_obj = expectWrongMethod(l
54- .findVirtual(classOf [Foo ], " neg" , methodType(classOf [Int ], classOf [Int ]))
55- .invokeExact(self, 3 ))
56- val testNeg_inline_pass = assert(- 4 == (l
57- .findVirtual(classOf [Foo ], " neg" , methodType(classOf [Int ], classOf [Int ]))
58- .invokeExact(self, 4 ): Int ))
55+ expectWrongMethod {
56+ l // explicit chain method call
57+ .findVirtual(classOf [Foo ], " neg" , methodType(classOf [Int ], classOf [Int ]))
58+ .invokeExact(self, 3 )
59+ }
60+ val res4 = {
61+ l // explicit chain method call
62+ .findVirtual(classOf [Foo ], " neg" , methodType(classOf [Int ], classOf [Int ]))
63+ .invokeExact(self, 4 ): Int
64+ }
65+ assert(- 4 == res4)
66+
67+ def id [T ](x : T ): T = x
5968
6069 def expectWrongMethod (op : => Any ) = try {
6170 op
6271 throw new AssertionError (" expected operation to fail but it didn't" )
6372 } catch case expected : WrongMethodTypeException => ()
64- }
0 commit comments