langtools/test/tools/javac/lambda/MethodReferenceGenericTarget.java
changeset 28593 d8bb77bbb041
equal deleted inserted replaced
28592:4f1da6e809b0 28593:d8bb77bbb041
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8046977 8065303
       
    27  * @summary ClassCastException: typing information needed for method reference bridging not preserved
       
    28  * @author Srikanth
       
    29  * @run main MethodReferenceGenericTarget
       
    30  */
       
    31 
       
    32 public class MethodReferenceGenericTarget {
       
    33     static String result = "";
       
    34 
       
    35     interface ISi { int m(Short a); }
       
    36 
       
    37     public static void main(String[] args) {
       
    38       (new MethodReferenceGenericTarget()).testUnboxObjectToNumberWiden();
       
    39       if (!result.equals("7775"))
       
    40           throw new AssertionError("Incorrect result");
       
    41         MethodReferenceTestPrivateTypeConversion.main(null);
       
    42         new InferenceHookTest().test();
       
    43     }
       
    44 
       
    45     void foo(ISi q) {
       
    46         result += q.m((short)75);
       
    47     }
       
    48 
       
    49     public void testUnboxObjectToNumberWiden() {
       
    50         ISi q = (new E<Short>())::xI;
       
    51         result += q.m((short)77);
       
    52         // Verify poly invocation context to confirm we handle
       
    53         // deferred/speculative attribution paths adequately.
       
    54         foo((new E<Short>())::xI);
       
    55     }
       
    56 
       
    57     class E<T> {
       
    58         private T xI(T t) { return t; }
       
    59     }
       
    60 }
       
    61 
       
    62 // snippet from https://bugs.openjdk.java.net/browse/JDK-8065303
       
    63 class MethodReferenceTestPrivateTypeConversion {
       
    64 
       
    65     class MethodReferenceTestTypeConversion_E<T> {
       
    66         private T xI(T t) { return t; }
       
    67     }
       
    68 
       
    69     interface ISi { int m(Short a); }
       
    70 
       
    71     interface ICc { char m(Character a); }
       
    72 
       
    73     public void testUnboxObjectToNumberWiden() {
       
    74         ISi q = (new MethodReferenceTestTypeConversion_E<Short>())::xI;
       
    75         if ((q.m((short)77) != (short)77))
       
    76             throw new AssertionError("Incorrect result");
       
    77     }
       
    78 
       
    79     public void testUnboxObjectToChar() {
       
    80         ICc q = (new MethodReferenceTestTypeConversion_E<Character>())::xI;
       
    81         if (q.m('@') != '@')
       
    82             throw new AssertionError("Incorrect result");
       
    83     }
       
    84 
       
    85     public static void main(String[] args) {
       
    86         new MethodReferenceTestPrivateTypeConversion().testUnboxObjectToNumberWiden();
       
    87         new MethodReferenceTestPrivateTypeConversion().testUnboxObjectToChar();
       
    88     }
       
    89 }
       
    90 
       
    91 class InferenceHookTestBase {
       
    92     <X> X m(Integer i) { return null; }
       
    93 }
       
    94 
       
    95 class InferenceHookTest extends InferenceHookTestBase {
       
    96     interface SAM1<R> {
       
    97         R m(Integer i);
       
    98     }
       
    99 
       
   100     <Z> Z g(SAM1<Z> o) { return null; }
       
   101 
       
   102     void test() {
       
   103         String s = g(super::m);
       
   104         if (s != null)
       
   105             throw new AssertionError("Incorrect result");
       
   106     }
       
   107 }