@@ -3,62 +3,70 @@ import scala.language.unsafeNulls
3
3
4
4
import java .lang .invoke ._ , MethodType .methodType
5
5
6
- object Test extends Foo :
7
- def main (args : Array [String ]): Unit = ()
8
-
9
- class Foo {
6
+ class Foo :
10
7
def neg (x : Int ): Int = - x
11
8
def rev (s : String ): String = s.reverse
12
9
def over (l : Long ): String = " long"
13
10
def over (i : Int ): String = " int"
14
11
def unit (s : String ): Unit = ()
15
12
def obj (s : String ): Object = s
16
- def id [T ](x : T ): T = x
17
13
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 ]))
27
25
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 ))
30
28
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 ))
33
31
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 ))
36
34
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 ))
39
37
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)
43
43
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)
47
49
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 ))
52
54
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
59
68
60
69
def expectWrongMethod (op : => Any ) = try {
61
70
op
62
71
throw new AssertionError (" expected operation to fail but it didn't" )
63
72
} catch case expected : WrongMethodTypeException => ()
64
- }
0 commit comments