langtools/test/tools/javac/lambda/LambdaConv01.java
author emc
Thu, 27 Jun 2013 17:45:56 -0400
changeset 18662 1cac45e71eb9
parent 14547 86d8d242b0c4
child 22448 a85fbad9d687
permissions -rw-r--r--
8013357: javac accepts erroneous binary comparison operations Summary: javac does not report type errors on illegal Object == primitive comparisons Reviewed-by: abuckley, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     1
/*
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     4
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     8
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    13
 * accompanied this code).
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    14
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    18
 *
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    21
 * questions.
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    22
 */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    23
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    24
/*
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    25
 * @test
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    26
 * @bug 8003280
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    27
 * @summary Add lambda tests
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    28
 *  basic test for lambda conversion
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    29
 * @author  Brian Goetz
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    30
 * @author  Maurizio Cimadamore
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    31
 * @run main LambdaConv01
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    32
 */
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    33
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    34
public class LambdaConv01 {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    35
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    36
    static int assertionCount = 0;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    37
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    38
    static void assertTrue(boolean cond) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    39
        assertionCount++;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    40
        if (!cond)
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    41
            throw new AssertionError();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    42
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    43
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    44
    interface IntToInt {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    45
      public int foo(int x);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    46
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    47
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    48
    interface IntToVoid {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    49
      public void foo(int x);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    50
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    51
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    52
    interface VoidToInt {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    53
      public int foo();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    54
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    55
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    56
    interface TU<T, U> {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    57
      public T foo(U u);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    58
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    59
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    60
    public static <T, U> T exec(TU<T, U> lambda, U x) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    61
        return lambda.foo(x);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    62
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    63
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    64
    static {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    65
        //Assignment conversion:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    66
        VoidToInt f1 = ()-> 3;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    67
        assertTrue(3 == f1.foo());
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    68
        //Covariant returns:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    69
        TU<Number, Integer> f2 = (Integer x) -> x;
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 14547
diff changeset
    70
        assertTrue(3 == f2.foo(3).intValue());
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    71
        //Method resolution with boxing:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    72
        int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    73
        assertTrue(3 == res);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    74
        //Runtime exception transparency:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    75
        try {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    76
            LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    77
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    78
        catch (RuntimeException e) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    79
            assertTrue(true);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    80
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    81
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    82
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    83
    {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    84
        //Assignment conversion:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    85
        VoidToInt f1 = ()-> 3;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    86
        assertTrue(3 == f1.foo());
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    87
        //Covariant returns:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    88
        TU<Number, Integer> f2 = (Integer x) -> x;
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 14547
diff changeset
    89
        assertTrue(3 == f2.foo(3).intValue());
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    90
        //Method resolution with boxing:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    91
        int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    92
        assertTrue(3 == res);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    93
        //Runtime exception transparency:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    94
        try {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    95
            LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    96
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    97
        catch (RuntimeException e) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    98
            assertTrue(true);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
    99
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   100
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   101
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   102
    public static void test1() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   103
        //Assignment conversion:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   104
        VoidToInt f1 = ()-> 3;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   105
        assertTrue(3 == f1.foo());
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   106
        //Covariant returns:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   107
        TU<Number, Integer> f2 = (Integer x) -> x;
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 14547
diff changeset
   108
        assertTrue(3 == f2.foo(3).intValue());
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   109
        //Method resolution with boxing:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   110
        int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   111
        assertTrue(3 == res);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   112
        //Runtime exception transparency:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   113
        try {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   114
            LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   115
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   116
        catch (RuntimeException e) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   117
            assertTrue(true);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   118
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   119
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   120
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   121
    public void test2() {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   122
        //Assignment conversion:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   123
        VoidToInt f1 = ()-> 3;
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   124
        assertTrue(3 == f1.foo());
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   125
        //Covariant returns:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   126
        TU<Number, Integer> f2 = (Integer x) -> x;
18662
1cac45e71eb9 8013357: javac accepts erroneous binary comparison operations
emc
parents: 14547
diff changeset
   127
        assertTrue(3 == f2.foo(3).intValue());
14547
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   128
        //Method resolution with boxing:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   129
        int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   130
        assertTrue(3 == res);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   131
        //Runtime exception transparency:
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   132
        try {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   133
            LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   134
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   135
        catch (RuntimeException e) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   136
            assertTrue(true);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   137
        }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   138
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   139
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   140
    public static void main(String[] args) {
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   141
        test1();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   142
        new LambdaConv01().test2();
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   143
        assertTrue(assertionCount == 16);
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   144
    }
86d8d242b0c4 8003280: Add lambda tests
mcimadamore
parents:
diff changeset
   145
}