Tue, 18 Dec 2012 22:16:45 +0000
8005193: New regression test test/tools/javac/lambda/BadMethodCall2.java fails
Summary: Bad golden file in negative test
Reviewed-by: jjh
mcimadamore@1415 | 1 | /* |
mcimadamore@1415 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
mcimadamore@1415 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
mcimadamore@1415 | 4 | * |
mcimadamore@1415 | 5 | * This code is free software; you can redistribute it and/or modify it |
mcimadamore@1415 | 6 | * under the terms of the GNU General Public License version 2 only, as |
mcimadamore@1415 | 7 | * published by the Free Software Foundation. |
mcimadamore@1415 | 8 | * |
mcimadamore@1415 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
mcimadamore@1415 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
mcimadamore@1415 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
mcimadamore@1415 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
mcimadamore@1415 | 13 | * accompanied this code). |
mcimadamore@1415 | 14 | * |
mcimadamore@1415 | 15 | * You should have received a copy of the GNU General Public License version |
mcimadamore@1415 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
mcimadamore@1415 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
mcimadamore@1415 | 18 | * |
mcimadamore@1415 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
mcimadamore@1415 | 20 | * or visit www.oracle.com if you need additional information or have any |
mcimadamore@1415 | 21 | * questions. |
mcimadamore@1415 | 22 | */ |
mcimadamore@1415 | 23 | |
mcimadamore@1415 | 24 | /** |
mcimadamore@1415 | 25 | * @test |
mcimadamore@1415 | 26 | * @bug 8003280 |
mcimadamore@1415 | 27 | * @summary Add lambda tests |
mcimadamore@1415 | 28 | * Test SAM conversion of method references in contexts of assignment, method/constructor argument, |
mcimadamore@1415 | 29 | * return statement, array initializer, lambda expression body, conditional expression and cast. |
mcimadamore@1415 | 30 | * @compile SamConversion.java |
mcimadamore@1415 | 31 | * @run main SamConversion |
mcimadamore@1415 | 32 | */ |
mcimadamore@1415 | 33 | |
mcimadamore@1415 | 34 | public class SamConversion { |
mcimadamore@1415 | 35 | |
mcimadamore@1415 | 36 | static int assertionCount = 0; |
mcimadamore@1415 | 37 | |
mcimadamore@1415 | 38 | static interface Foo { |
mcimadamore@1415 | 39 | Integer m(int i); |
mcimadamore@1415 | 40 | } |
mcimadamore@1415 | 41 | |
mcimadamore@1415 | 42 | static interface Bar { |
mcimadamore@1415 | 43 | int m(Integer i) throws MyException; |
mcimadamore@1415 | 44 | } |
mcimadamore@1415 | 45 | |
mcimadamore@1415 | 46 | private static void assertTrue(boolean b) { |
mcimadamore@1415 | 47 | assertionCount++; |
mcimadamore@1415 | 48 | if(!b) |
mcimadamore@1415 | 49 | throw new AssertionError(); |
mcimadamore@1415 | 50 | } |
mcimadamore@1415 | 51 | |
mcimadamore@1415 | 52 | private static int test1(Foo foo) { |
mcimadamore@1415 | 53 | return foo.m(1); |
mcimadamore@1415 | 54 | } |
mcimadamore@1415 | 55 | |
mcimadamore@1415 | 56 | private static void test2(Bar bar, int result) { |
mcimadamore@1415 | 57 | try { |
mcimadamore@1415 | 58 | assertTrue(bar.m(1) == result); |
mcimadamore@1415 | 59 | } catch (Exception e){ |
mcimadamore@1415 | 60 | assertTrue(false); |
mcimadamore@1415 | 61 | } |
mcimadamore@1415 | 62 | } |
mcimadamore@1415 | 63 | |
mcimadamore@1415 | 64 | private static Bar test3(int i) { |
mcimadamore@1415 | 65 | switch (i) { |
mcimadamore@1415 | 66 | case 0: |
mcimadamore@1415 | 67 | return A::method1; |
mcimadamore@1415 | 68 | case 1: |
mcimadamore@1415 | 69 | return new A()::method2; |
mcimadamore@1415 | 70 | case 2: |
mcimadamore@1415 | 71 | return A::method3; |
mcimadamore@1415 | 72 | case 3: |
mcimadamore@1415 | 73 | return new A()::method4; |
mcimadamore@1415 | 74 | case 4: |
mcimadamore@1415 | 75 | return new A()::method5; |
mcimadamore@1415 | 76 | case 5: |
mcimadamore@1415 | 77 | return A::method6; |
mcimadamore@1415 | 78 | default: |
mcimadamore@1415 | 79 | return null; |
mcimadamore@1415 | 80 | } |
mcimadamore@1415 | 81 | } |
mcimadamore@1415 | 82 | |
mcimadamore@1415 | 83 | /** |
mcimadamore@1415 | 84 | * Test SAM conversion of method reference in assignment context |
mcimadamore@1415 | 85 | */ |
mcimadamore@1415 | 86 | private static void testAssignment() { |
mcimadamore@1415 | 87 | Foo foo = A::method1; //static reference, parameter type matching and return type matching |
mcimadamore@1415 | 88 | assertTrue(foo.m(1) == 2); |
mcimadamore@1415 | 89 | |
mcimadamore@1415 | 90 | foo = new A()::method2; //instance reference, parameter type unboxing and return type boxing |
mcimadamore@1415 | 91 | assertTrue(foo.m(1) == 3); |
mcimadamore@1415 | 92 | |
mcimadamore@1415 | 93 | foo = A::method3; //static reference, parameter type matching and return type boxing |
mcimadamore@1415 | 94 | assertTrue(foo.m(1) == 4); |
mcimadamore@1415 | 95 | |
mcimadamore@1415 | 96 | foo = new A()::method4; //instance reference, parameter type unboxing and return type matching |
mcimadamore@1415 | 97 | assertTrue(foo.m(1) == 5); |
mcimadamore@1415 | 98 | |
mcimadamore@1415 | 99 | foo = new A()::method5; //instance reference, parameter type unboxing and return type matching |
mcimadamore@1415 | 100 | assertTrue(foo.m(1) == 6); |
mcimadamore@1415 | 101 | |
mcimadamore@1415 | 102 | Bar bar = A::method1; |
mcimadamore@1415 | 103 | try { |
mcimadamore@1415 | 104 | assertTrue(bar.m(1) == 2); |
mcimadamore@1415 | 105 | } catch (Exception e) { |
mcimadamore@1415 | 106 | assertTrue(false); |
mcimadamore@1415 | 107 | } |
mcimadamore@1415 | 108 | |
mcimadamore@1415 | 109 | bar = new A()::method2; |
mcimadamore@1415 | 110 | try { |
mcimadamore@1415 | 111 | assertTrue(bar.m(1) == 3); |
mcimadamore@1415 | 112 | } catch (Exception e) { |
mcimadamore@1415 | 113 | assertTrue(false); |
mcimadamore@1415 | 114 | } |
mcimadamore@1415 | 115 | |
mcimadamore@1415 | 116 | bar = A::method3; |
mcimadamore@1415 | 117 | try { |
mcimadamore@1415 | 118 | assertTrue(bar.m(1) == 4); |
mcimadamore@1415 | 119 | } catch (Exception e) { |
mcimadamore@1415 | 120 | assertTrue(false); |
mcimadamore@1415 | 121 | } |
mcimadamore@1415 | 122 | |
mcimadamore@1415 | 123 | bar = new A()::method4; |
mcimadamore@1415 | 124 | try { |
mcimadamore@1415 | 125 | assertTrue(bar.m(1) == 5); |
mcimadamore@1415 | 126 | } catch (Exception e) { |
mcimadamore@1415 | 127 | assertTrue(false); |
mcimadamore@1415 | 128 | } |
mcimadamore@1415 | 129 | |
mcimadamore@1415 | 130 | bar = new A()::method5; |
mcimadamore@1415 | 131 | try { |
mcimadamore@1415 | 132 | assertTrue(bar.m(1) == 6); |
mcimadamore@1415 | 133 | } catch (Exception e) { |
mcimadamore@1415 | 134 | assertTrue(false); |
mcimadamore@1415 | 135 | } |
mcimadamore@1415 | 136 | } |
mcimadamore@1415 | 137 | |
mcimadamore@1415 | 138 | /** |
mcimadamore@1415 | 139 | * Test SAM conversion of method reference in method/constructor argument context |
mcimadamore@1415 | 140 | */ |
mcimadamore@1415 | 141 | private static void testMethodArgument() { |
mcimadamore@1415 | 142 | assertTrue(test1(A::method1) == 2); |
mcimadamore@1415 | 143 | assertTrue(test1(new A()::method2) == 3); |
mcimadamore@1415 | 144 | assertTrue(test1(A::method3) == 4); |
mcimadamore@1415 | 145 | assertTrue(test1(new A()::method4) == 5); |
mcimadamore@1415 | 146 | assertTrue(test1(new A()::method5) == 6); |
mcimadamore@1415 | 147 | test2(A::method1, 2); |
mcimadamore@1415 | 148 | test2(new A()::method2, 3); |
mcimadamore@1415 | 149 | test2(A::method3, 4); |
mcimadamore@1415 | 150 | test2(new A()::method4, 5); |
mcimadamore@1415 | 151 | test2(new A()::method5, 6); |
mcimadamore@1415 | 152 | A a = new A(A::method1); //A(Foo f) called |
mcimadamore@1415 | 153 | assertTrue(a.method2(1) == 11); |
mcimadamore@1415 | 154 | assertTrue(a.method4(1) == 11); |
mcimadamore@1415 | 155 | assertTrue(a.method5(1) == 11); |
mcimadamore@1415 | 156 | A a2 = new A(new A()::method2); //A(Bar b) called |
mcimadamore@1415 | 157 | assertTrue(a2.method2(1) == 12); |
mcimadamore@1415 | 158 | assertTrue(a2.method4(1) == 12); |
mcimadamore@1415 | 159 | assertTrue(a2.method5(1) == 12); |
mcimadamore@1415 | 160 | } |
mcimadamore@1415 | 161 | |
mcimadamore@1415 | 162 | /** |
mcimadamore@1415 | 163 | * Test SAM conversion of method reference in return statement context |
mcimadamore@1415 | 164 | */ |
mcimadamore@1415 | 165 | private static void testReturnStatement() { |
mcimadamore@1415 | 166 | Bar bar = test3(0); |
mcimadamore@1415 | 167 | try { |
mcimadamore@1415 | 168 | assertTrue(bar.m(1) == 2); |
mcimadamore@1415 | 169 | } catch (Exception e) { |
mcimadamore@1415 | 170 | assertTrue(false); |
mcimadamore@1415 | 171 | } |
mcimadamore@1415 | 172 | bar = test3(1); |
mcimadamore@1415 | 173 | try { |
mcimadamore@1415 | 174 | assertTrue(bar.m(1) == 3); |
mcimadamore@1415 | 175 | } catch (Exception e) { |
mcimadamore@1415 | 176 | assertTrue(false); |
mcimadamore@1415 | 177 | } |
mcimadamore@1415 | 178 | bar = test3(2); |
mcimadamore@1415 | 179 | try { |
mcimadamore@1415 | 180 | assertTrue(bar.m(1) == 4); |
mcimadamore@1415 | 181 | } catch (Exception e) { |
mcimadamore@1415 | 182 | assertTrue(false); |
mcimadamore@1415 | 183 | } |
mcimadamore@1415 | 184 | bar = test3(3); |
mcimadamore@1415 | 185 | try { |
mcimadamore@1415 | 186 | assertTrue(bar.m(1) == 5); |
mcimadamore@1415 | 187 | } catch (Exception e) { |
mcimadamore@1415 | 188 | assertTrue(false); |
mcimadamore@1415 | 189 | } |
mcimadamore@1415 | 190 | bar = test3(4); |
mcimadamore@1415 | 191 | try { |
mcimadamore@1415 | 192 | assertTrue(bar.m(1) == 6); |
mcimadamore@1415 | 193 | } catch (Exception e) { |
mcimadamore@1415 | 194 | assertTrue(false); |
mcimadamore@1415 | 195 | } |
mcimadamore@1415 | 196 | bar = test3(5); |
mcimadamore@1415 | 197 | try { |
mcimadamore@1415 | 198 | bar.m(1); |
mcimadamore@1415 | 199 | assertTrue(false); |
mcimadamore@1415 | 200 | } catch (MyException e) { |
mcimadamore@1415 | 201 | } catch (Exception e) { |
mcimadamore@1415 | 202 | assertTrue(false); |
mcimadamore@1415 | 203 | } |
mcimadamore@1415 | 204 | } |
mcimadamore@1415 | 205 | |
mcimadamore@1415 | 206 | /** |
mcimadamore@1415 | 207 | * Test SAM conversion of method reference in cast context |
mcimadamore@1415 | 208 | */ |
mcimadamore@1415 | 209 | private static void testCast() { |
mcimadamore@1415 | 210 | assertTrue(((Foo)A::method1).m(1) == 2); |
mcimadamore@1415 | 211 | try { |
mcimadamore@1415 | 212 | assertTrue(((Bar)new A()::method2).m(1) == 3); |
mcimadamore@1415 | 213 | } catch (Exception e) { |
mcimadamore@1415 | 214 | assertTrue(false); |
mcimadamore@1415 | 215 | } |
mcimadamore@1415 | 216 | } |
mcimadamore@1415 | 217 | |
mcimadamore@1415 | 218 | /** |
mcimadamore@1415 | 219 | * Test SAM conversion of method reference in array initializer context |
mcimadamore@1415 | 220 | */ |
mcimadamore@1415 | 221 | private static void testArrayInitializer() { |
mcimadamore@1415 | 222 | Object[] oarray = {"a", 1, (Foo)A::method3}; //last element need a cast |
mcimadamore@1415 | 223 | Object[] oarray2 = {"a", 1, (Bar)new A()::method4}; //last element need a cast |
mcimadamore@1415 | 224 | Foo[] farray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5}; |
mcimadamore@1415 | 225 | Bar[] barray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5, A::method6}; |
mcimadamore@1415 | 226 | } |
mcimadamore@1415 | 227 | |
mcimadamore@1415 | 228 | /** |
mcimadamore@1415 | 229 | * Test SAM conversion of method reference in conditional expression context |
mcimadamore@1415 | 230 | */ |
mcimadamore@1415 | 231 | private static void testConditionalExpression(boolean b) { |
mcimadamore@1415 | 232 | Foo f = b ? A::method3 : new A()::method5; |
mcimadamore@1415 | 233 | if(b) |
mcimadamore@1415 | 234 | assertTrue(f.m(1) == 4); |
mcimadamore@1415 | 235 | else |
mcimadamore@1415 | 236 | assertTrue(f.m(1) == 6); |
mcimadamore@1415 | 237 | |
mcimadamore@1415 | 238 | Bar bar = b ? A::method1 : A::method6; |
mcimadamore@1415 | 239 | if(b) { |
mcimadamore@1415 | 240 | try { |
mcimadamore@1415 | 241 | assertTrue(bar.m(1) == 2); |
mcimadamore@1415 | 242 | } catch (Exception e) { |
mcimadamore@1415 | 243 | assertTrue(false); |
mcimadamore@1415 | 244 | } |
mcimadamore@1415 | 245 | } |
mcimadamore@1415 | 246 | else { |
mcimadamore@1415 | 247 | try { |
mcimadamore@1415 | 248 | bar.m(1); |
mcimadamore@1415 | 249 | assertTrue(false); |
mcimadamore@1415 | 250 | } catch (MyException e) { |
mcimadamore@1415 | 251 | } catch (Exception e) { |
mcimadamore@1415 | 252 | assertTrue(false); |
mcimadamore@1415 | 253 | } |
mcimadamore@1415 | 254 | } |
mcimadamore@1415 | 255 | } |
mcimadamore@1415 | 256 | |
mcimadamore@1415 | 257 | /** |
mcimadamore@1415 | 258 | * Test SAM conversion of method reference in lambda expression body |
mcimadamore@1415 | 259 | */ |
mcimadamore@1415 | 260 | private static void testLambdaExpressionBody() { |
mcimadamore@1415 | 261 | Foo f = n -> ((Foo)A::method3).m(n); |
mcimadamore@1415 | 262 | assertTrue(f.m(1) == 4); |
mcimadamore@1415 | 263 | |
mcimadamore@1415 | 264 | Bar b = n -> { return ((Foo)new A()::method5).m(n); }; |
mcimadamore@1415 | 265 | try { |
mcimadamore@1415 | 266 | assertTrue(b.m(1) == 6); |
mcimadamore@1415 | 267 | } catch (Exception e) { |
mcimadamore@1415 | 268 | assertTrue(false); |
mcimadamore@1415 | 269 | } |
mcimadamore@1415 | 270 | } |
mcimadamore@1415 | 271 | |
mcimadamore@1415 | 272 | public static void main(String[] args) { |
mcimadamore@1415 | 273 | testAssignment(); |
mcimadamore@1415 | 274 | testMethodArgument(); |
mcimadamore@1415 | 275 | testReturnStatement(); |
mcimadamore@1415 | 276 | testCast(); |
mcimadamore@1415 | 277 | testArrayInitializer(); |
mcimadamore@1415 | 278 | testConditionalExpression(true); |
mcimadamore@1415 | 279 | testConditionalExpression(false); |
mcimadamore@1415 | 280 | testLambdaExpressionBody(); |
mcimadamore@1415 | 281 | |
mcimadamore@1415 | 282 | assertTrue(assertionCount == 38); |
mcimadamore@1415 | 283 | } |
mcimadamore@1415 | 284 | |
mcimadamore@1415 | 285 | static class MyException extends Exception {} |
mcimadamore@1415 | 286 | |
mcimadamore@1415 | 287 | static class A { |
mcimadamore@1415 | 288 | |
mcimadamore@1415 | 289 | int value = 0; |
mcimadamore@1415 | 290 | |
mcimadamore@1415 | 291 | A() { |
mcimadamore@1415 | 292 | } |
mcimadamore@1415 | 293 | |
mcimadamore@1415 | 294 | A(Foo f) { |
mcimadamore@1415 | 295 | value = f.m(9); |
mcimadamore@1415 | 296 | } |
mcimadamore@1415 | 297 | |
mcimadamore@1415 | 298 | A(Bar b) { |
mcimadamore@1415 | 299 | try { |
mcimadamore@1415 | 300 | value = b.m(9); |
mcimadamore@1415 | 301 | } catch (MyException e){} |
mcimadamore@1415 | 302 | } |
mcimadamore@1415 | 303 | |
mcimadamore@1415 | 304 | static Integer method1(int n) { |
mcimadamore@1415 | 305 | return n + 1; |
mcimadamore@1415 | 306 | } |
mcimadamore@1415 | 307 | |
mcimadamore@1415 | 308 | int method2(Integer n) { |
mcimadamore@1415 | 309 | return value == 0 ? n + 2 : n + value; |
mcimadamore@1415 | 310 | } |
mcimadamore@1415 | 311 | |
mcimadamore@1415 | 312 | static int method3(int n) { |
mcimadamore@1415 | 313 | return n + 3; |
mcimadamore@1415 | 314 | } |
mcimadamore@1415 | 315 | |
mcimadamore@1415 | 316 | Integer method4(Integer n) { |
mcimadamore@1415 | 317 | return value == 0 ? n + 4 : n + value; |
mcimadamore@1415 | 318 | } |
mcimadamore@1415 | 319 | |
mcimadamore@1415 | 320 | Integer method5(Integer n) { |
mcimadamore@1415 | 321 | return value == 0 ? new Integer(n + 5) : new Integer(n + value); |
mcimadamore@1415 | 322 | } |
mcimadamore@1415 | 323 | |
mcimadamore@1415 | 324 | static int method6(Integer n) throws MyException{ |
mcimadamore@1415 | 325 | throw new MyException(); |
mcimadamore@1415 | 326 | } |
mcimadamore@1415 | 327 | } |
mcimadamore@1415 | 328 | } |