Thu, 25 Jul 2013 14:51:40 +0100
8020843: javac crashes on accessibility check with method reference with typevar receiver
Summary: method reference overload check doesn't walk through type-variable receivers
Reviewed-by: jjg
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 | } |
mcimadamore@1415 | 153 | |
mcimadamore@1415 | 154 | /** |
mcimadamore@1415 | 155 | * Test SAM conversion of method reference in return statement context |
mcimadamore@1415 | 156 | */ |
mcimadamore@1415 | 157 | private static void testReturnStatement() { |
mcimadamore@1415 | 158 | Bar bar = test3(0); |
mcimadamore@1415 | 159 | try { |
mcimadamore@1415 | 160 | assertTrue(bar.m(1) == 2); |
mcimadamore@1415 | 161 | } catch (Exception e) { |
mcimadamore@1415 | 162 | assertTrue(false); |
mcimadamore@1415 | 163 | } |
mcimadamore@1415 | 164 | bar = test3(1); |
mcimadamore@1415 | 165 | try { |
mcimadamore@1415 | 166 | assertTrue(bar.m(1) == 3); |
mcimadamore@1415 | 167 | } catch (Exception e) { |
mcimadamore@1415 | 168 | assertTrue(false); |
mcimadamore@1415 | 169 | } |
mcimadamore@1415 | 170 | bar = test3(2); |
mcimadamore@1415 | 171 | try { |
mcimadamore@1415 | 172 | assertTrue(bar.m(1) == 4); |
mcimadamore@1415 | 173 | } catch (Exception e) { |
mcimadamore@1415 | 174 | assertTrue(false); |
mcimadamore@1415 | 175 | } |
mcimadamore@1415 | 176 | bar = test3(3); |
mcimadamore@1415 | 177 | try { |
mcimadamore@1415 | 178 | assertTrue(bar.m(1) == 5); |
mcimadamore@1415 | 179 | } catch (Exception e) { |
mcimadamore@1415 | 180 | assertTrue(false); |
mcimadamore@1415 | 181 | } |
mcimadamore@1415 | 182 | bar = test3(4); |
mcimadamore@1415 | 183 | try { |
mcimadamore@1415 | 184 | assertTrue(bar.m(1) == 6); |
mcimadamore@1415 | 185 | } catch (Exception e) { |
mcimadamore@1415 | 186 | assertTrue(false); |
mcimadamore@1415 | 187 | } |
mcimadamore@1415 | 188 | bar = test3(5); |
mcimadamore@1415 | 189 | try { |
mcimadamore@1415 | 190 | bar.m(1); |
mcimadamore@1415 | 191 | assertTrue(false); |
mcimadamore@1415 | 192 | } catch (MyException e) { |
mcimadamore@1415 | 193 | } catch (Exception e) { |
mcimadamore@1415 | 194 | assertTrue(false); |
mcimadamore@1415 | 195 | } |
mcimadamore@1415 | 196 | } |
mcimadamore@1415 | 197 | |
mcimadamore@1415 | 198 | /** |
mcimadamore@1415 | 199 | * Test SAM conversion of method reference in cast context |
mcimadamore@1415 | 200 | */ |
mcimadamore@1415 | 201 | private static void testCast() { |
mcimadamore@1415 | 202 | assertTrue(((Foo)A::method1).m(1) == 2); |
mcimadamore@1415 | 203 | try { |
mcimadamore@1415 | 204 | assertTrue(((Bar)new A()::method2).m(1) == 3); |
mcimadamore@1415 | 205 | } catch (Exception e) { |
mcimadamore@1415 | 206 | assertTrue(false); |
mcimadamore@1415 | 207 | } |
mcimadamore@1415 | 208 | } |
mcimadamore@1415 | 209 | |
mcimadamore@1415 | 210 | /** |
mcimadamore@1415 | 211 | * Test SAM conversion of method reference in array initializer context |
mcimadamore@1415 | 212 | */ |
mcimadamore@1415 | 213 | private static void testArrayInitializer() { |
mcimadamore@1415 | 214 | Object[] oarray = {"a", 1, (Foo)A::method3}; //last element need a cast |
mcimadamore@1415 | 215 | Object[] oarray2 = {"a", 1, (Bar)new A()::method4}; //last element need a cast |
mcimadamore@1415 | 216 | Foo[] farray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5}; |
mcimadamore@1415 | 217 | Bar[] barray = {A::method1, new A()::method2, A::method3, new A()::method4, new A()::method5, A::method6}; |
mcimadamore@1415 | 218 | } |
mcimadamore@1415 | 219 | |
mcimadamore@1415 | 220 | /** |
mcimadamore@1415 | 221 | * Test SAM conversion of method reference in conditional expression context |
mcimadamore@1415 | 222 | */ |
mcimadamore@1415 | 223 | private static void testConditionalExpression(boolean b) { |
mcimadamore@1415 | 224 | Foo f = b ? A::method3 : new A()::method5; |
mcimadamore@1415 | 225 | if(b) |
mcimadamore@1415 | 226 | assertTrue(f.m(1) == 4); |
mcimadamore@1415 | 227 | else |
mcimadamore@1415 | 228 | assertTrue(f.m(1) == 6); |
mcimadamore@1415 | 229 | |
mcimadamore@1415 | 230 | Bar bar = b ? A::method1 : A::method6; |
mcimadamore@1415 | 231 | if(b) { |
mcimadamore@1415 | 232 | try { |
mcimadamore@1415 | 233 | assertTrue(bar.m(1) == 2); |
mcimadamore@1415 | 234 | } catch (Exception e) { |
mcimadamore@1415 | 235 | assertTrue(false); |
mcimadamore@1415 | 236 | } |
mcimadamore@1415 | 237 | } |
mcimadamore@1415 | 238 | else { |
mcimadamore@1415 | 239 | try { |
mcimadamore@1415 | 240 | bar.m(1); |
mcimadamore@1415 | 241 | assertTrue(false); |
mcimadamore@1415 | 242 | } catch (MyException e) { |
mcimadamore@1415 | 243 | } catch (Exception e) { |
mcimadamore@1415 | 244 | assertTrue(false); |
mcimadamore@1415 | 245 | } |
mcimadamore@1415 | 246 | } |
mcimadamore@1415 | 247 | } |
mcimadamore@1415 | 248 | |
mcimadamore@1415 | 249 | /** |
mcimadamore@1415 | 250 | * Test SAM conversion of method reference in lambda expression body |
mcimadamore@1415 | 251 | */ |
mcimadamore@1415 | 252 | private static void testLambdaExpressionBody() { |
mcimadamore@1415 | 253 | Foo f = n -> ((Foo)A::method3).m(n); |
mcimadamore@1415 | 254 | assertTrue(f.m(1) == 4); |
mcimadamore@1415 | 255 | |
mcimadamore@1415 | 256 | Bar b = n -> { return ((Foo)new A()::method5).m(n); }; |
mcimadamore@1415 | 257 | try { |
mcimadamore@1415 | 258 | assertTrue(b.m(1) == 6); |
mcimadamore@1415 | 259 | } catch (Exception e) { |
mcimadamore@1415 | 260 | assertTrue(false); |
mcimadamore@1415 | 261 | } |
mcimadamore@1415 | 262 | } |
mcimadamore@1415 | 263 | |
mcimadamore@1415 | 264 | public static void main(String[] args) { |
mcimadamore@1415 | 265 | testAssignment(); |
mcimadamore@1415 | 266 | testMethodArgument(); |
mcimadamore@1415 | 267 | testReturnStatement(); |
mcimadamore@1415 | 268 | testCast(); |
mcimadamore@1415 | 269 | testArrayInitializer(); |
mcimadamore@1415 | 270 | testConditionalExpression(true); |
mcimadamore@1415 | 271 | testConditionalExpression(false); |
mcimadamore@1415 | 272 | testLambdaExpressionBody(); |
mcimadamore@1415 | 273 | |
mcimadamore@1510 | 274 | assertTrue(assertionCount == 32); |
mcimadamore@1415 | 275 | } |
mcimadamore@1415 | 276 | |
mcimadamore@1415 | 277 | static class MyException extends Exception {} |
mcimadamore@1415 | 278 | |
mcimadamore@1415 | 279 | static class A { |
mcimadamore@1415 | 280 | |
mcimadamore@1415 | 281 | int value = 0; |
mcimadamore@1415 | 282 | |
mcimadamore@1415 | 283 | A() { |
mcimadamore@1415 | 284 | } |
mcimadamore@1415 | 285 | |
mcimadamore@1415 | 286 | A(Foo f) { |
mcimadamore@1415 | 287 | value = f.m(9); |
mcimadamore@1415 | 288 | } |
mcimadamore@1415 | 289 | |
mcimadamore@1415 | 290 | A(Bar b) { |
mcimadamore@1415 | 291 | try { |
mcimadamore@1415 | 292 | value = b.m(9); |
mcimadamore@1415 | 293 | } catch (MyException e){} |
mcimadamore@1415 | 294 | } |
mcimadamore@1415 | 295 | |
mcimadamore@1415 | 296 | static Integer method1(int n) { |
mcimadamore@1415 | 297 | return n + 1; |
mcimadamore@1415 | 298 | } |
mcimadamore@1415 | 299 | |
mcimadamore@1415 | 300 | int method2(Integer n) { |
mcimadamore@1415 | 301 | return value == 0 ? n + 2 : n + value; |
mcimadamore@1415 | 302 | } |
mcimadamore@1415 | 303 | |
mcimadamore@1415 | 304 | static int method3(int n) { |
mcimadamore@1415 | 305 | return n + 3; |
mcimadamore@1415 | 306 | } |
mcimadamore@1415 | 307 | |
mcimadamore@1415 | 308 | Integer method4(Integer n) { |
mcimadamore@1415 | 309 | return value == 0 ? n + 4 : n + value; |
mcimadamore@1415 | 310 | } |
mcimadamore@1415 | 311 | |
mcimadamore@1415 | 312 | Integer method5(Integer n) { |
mcimadamore@1415 | 313 | return value == 0 ? new Integer(n + 5) : new Integer(n + value); |
mcimadamore@1415 | 314 | } |
mcimadamore@1415 | 315 | |
mcimadamore@1415 | 316 | static int method6(Integer n) throws MyException{ |
mcimadamore@1415 | 317 | throw new MyException(); |
mcimadamore@1415 | 318 | } |
mcimadamore@1415 | 319 | } |
mcimadamore@1415 | 320 | } |