test/tools/javac/lambda/LambdaConv01.java

Thu, 27 Jun 2013 17:45:56 -0400

author
emc
date
Thu, 27 Jun 2013 17:45:56 -0400
changeset 1869
5c548a8542b8
parent 1415
01c9d4161882
child 2227
998b10c43157
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

mcimadamore@1415 1 /*
mcimadamore@1415 2 * Copyright (c) 2011, 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 * basic test for lambda conversion
mcimadamore@1415 29 * @author Brian Goetz
mcimadamore@1415 30 * @author Maurizio Cimadamore
mcimadamore@1415 31 * @run main LambdaConv01
mcimadamore@1415 32 */
mcimadamore@1415 33
mcimadamore@1415 34 public class LambdaConv01 {
mcimadamore@1415 35
mcimadamore@1415 36 static int assertionCount = 0;
mcimadamore@1415 37
mcimadamore@1415 38 static void assertTrue(boolean cond) {
mcimadamore@1415 39 assertionCount++;
mcimadamore@1415 40 if (!cond)
mcimadamore@1415 41 throw new AssertionError();
mcimadamore@1415 42 }
mcimadamore@1415 43
mcimadamore@1415 44 interface IntToInt {
mcimadamore@1415 45 public int foo(int x);
mcimadamore@1415 46 }
mcimadamore@1415 47
mcimadamore@1415 48 interface IntToVoid {
mcimadamore@1415 49 public void foo(int x);
mcimadamore@1415 50 }
mcimadamore@1415 51
mcimadamore@1415 52 interface VoidToInt {
mcimadamore@1415 53 public int foo();
mcimadamore@1415 54 }
mcimadamore@1415 55
mcimadamore@1415 56 interface TU<T, U> {
mcimadamore@1415 57 public T foo(U u);
mcimadamore@1415 58 }
mcimadamore@1415 59
mcimadamore@1415 60 public static <T, U> T exec(TU<T, U> lambda, U x) {
mcimadamore@1415 61 return lambda.foo(x);
mcimadamore@1415 62 }
mcimadamore@1415 63
mcimadamore@1415 64 static {
mcimadamore@1415 65 //Assignment conversion:
mcimadamore@1415 66 VoidToInt f1 = ()-> 3;
mcimadamore@1415 67 assertTrue(3 == f1.foo());
mcimadamore@1415 68 //Covariant returns:
mcimadamore@1415 69 TU<Number, Integer> f2 = (Integer x) -> x;
emc@1869 70 assertTrue(3 == f2.foo(3).intValue());
mcimadamore@1415 71 //Method resolution with boxing:
mcimadamore@1415 72 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
mcimadamore@1415 73 assertTrue(3 == res);
mcimadamore@1415 74 //Runtime exception transparency:
mcimadamore@1415 75 try {
mcimadamore@1415 76 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
mcimadamore@1415 77 }
mcimadamore@1415 78 catch (RuntimeException e) {
mcimadamore@1415 79 assertTrue(true);
mcimadamore@1415 80 }
mcimadamore@1415 81 }
mcimadamore@1415 82
mcimadamore@1415 83 {
mcimadamore@1415 84 //Assignment conversion:
mcimadamore@1415 85 VoidToInt f1 = ()-> 3;
mcimadamore@1415 86 assertTrue(3 == f1.foo());
mcimadamore@1415 87 //Covariant returns:
mcimadamore@1415 88 TU<Number, Integer> f2 = (Integer x) -> x;
emc@1869 89 assertTrue(3 == f2.foo(3).intValue());
mcimadamore@1415 90 //Method resolution with boxing:
mcimadamore@1415 91 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
mcimadamore@1415 92 assertTrue(3 == res);
mcimadamore@1415 93 //Runtime exception transparency:
mcimadamore@1415 94 try {
mcimadamore@1415 95 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
mcimadamore@1415 96 }
mcimadamore@1415 97 catch (RuntimeException e) {
mcimadamore@1415 98 assertTrue(true);
mcimadamore@1415 99 }
mcimadamore@1415 100 }
mcimadamore@1415 101
mcimadamore@1415 102 public static void test1() {
mcimadamore@1415 103 //Assignment conversion:
mcimadamore@1415 104 VoidToInt f1 = ()-> 3;
mcimadamore@1415 105 assertTrue(3 == f1.foo());
mcimadamore@1415 106 //Covariant returns:
mcimadamore@1415 107 TU<Number, Integer> f2 = (Integer x) -> x;
emc@1869 108 assertTrue(3 == f2.foo(3).intValue());
mcimadamore@1415 109 //Method resolution with boxing:
mcimadamore@1415 110 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
mcimadamore@1415 111 assertTrue(3 == res);
mcimadamore@1415 112 //Runtime exception transparency:
mcimadamore@1415 113 try {
mcimadamore@1415 114 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
mcimadamore@1415 115 }
mcimadamore@1415 116 catch (RuntimeException e) {
mcimadamore@1415 117 assertTrue(true);
mcimadamore@1415 118 }
mcimadamore@1415 119 }
mcimadamore@1415 120
mcimadamore@1415 121 public void test2() {
mcimadamore@1415 122 //Assignment conversion:
mcimadamore@1415 123 VoidToInt f1 = ()-> 3;
mcimadamore@1415 124 assertTrue(3 == f1.foo());
mcimadamore@1415 125 //Covariant returns:
mcimadamore@1415 126 TU<Number, Integer> f2 = (Integer x) -> x;
emc@1869 127 assertTrue(3 == f2.foo(3).intValue());
mcimadamore@1415 128 //Method resolution with boxing:
mcimadamore@1415 129 int res = LambdaConv01.<Integer,Integer>exec((Integer x) -> x, 3);
mcimadamore@1415 130 assertTrue(3 == res);
mcimadamore@1415 131 //Runtime exception transparency:
mcimadamore@1415 132 try {
mcimadamore@1415 133 LambdaConv01.<Integer,Object>exec((Object x) -> x.hashCode(), null);
mcimadamore@1415 134 }
mcimadamore@1415 135 catch (RuntimeException e) {
mcimadamore@1415 136 assertTrue(true);
mcimadamore@1415 137 }
mcimadamore@1415 138 }
mcimadamore@1415 139
mcimadamore@1415 140 public static void main(String[] args) {
mcimadamore@1415 141 test1();
mcimadamore@1415 142 new LambdaConv01().test2();
mcimadamore@1415 143 assertTrue(assertionCount == 16);
mcimadamore@1415 144 }
mcimadamore@1415 145 }

mercurial