test/tools/javac/lambda/typeInference/InferenceTest5.java

Tue, 14 May 2013 11:11:09 -0700

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 1415
01c9d4161882
permissions
-rw-r--r--

8012556: Implement lambda methods on interfaces as static
8006140: Javac NPE compiling Lambda expression on initialization expression of static field in interface
Summary: Lambdas occurring in static contexts or those not needing instance information should be generated into static methods. This has long been the case for classes. However, as a work-around to the lack of support for statics on interfaces, interface lambda methods have been generated into default methods. For lambdas in interface static contexts (fields and static methods) this causes an NPE in javac because there is no 'this'. MethodHandles now support static methods on interfaces. This changeset allows lambda methods to be generated as static interface methods. An existing bug in Hotspot (8013875) is exposed in a test when the "-esa" flag is used. This test and another test that already exposed this bug have been marked with @ignore.
Reviewed-by: 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 * This test is for overloaded methods, verify that the specific method is
mcimadamore@1415 29 selected when type inference occurs
mcimadamore@1415 30 * @compile InferenceTest5.java
mcimadamore@1415 31 * @run main InferenceTest5
mcimadamore@1415 32 */
mcimadamore@1415 33
mcimadamore@1415 34 import java.util.List;
mcimadamore@1415 35 import java.io.File;
mcimadamore@1415 36
mcimadamore@1415 37 public class InferenceTest5 {
mcimadamore@1415 38
mcimadamore@1415 39 private static void assertTrue(boolean b) {
mcimadamore@1415 40 if(!b)
mcimadamore@1415 41 throw new AssertionError();
mcimadamore@1415 42 }
mcimadamore@1415 43
mcimadamore@1415 44 public static void main(String[] args) {
mcimadamore@1415 45 InferenceTest5 test = new InferenceTest5();
mcimadamore@1415 46 int n = test.method1((a, b) -> {} );
mcimadamore@1415 47 assertTrue(n == 1);
mcimadamore@1415 48
mcimadamore@1415 49 n = test.method1(() -> null);
mcimadamore@1415 50 assertTrue(n == 2);
mcimadamore@1415 51
mcimadamore@1415 52 n = test.method1(a -> null);
mcimadamore@1415 53 assertTrue(n == 3);
mcimadamore@1415 54
mcimadamore@1415 55 n = test.method1(a -> {});
mcimadamore@1415 56 assertTrue(n == 4);
mcimadamore@1415 57
mcimadamore@1415 58 n = test.method1(() -> {});
mcimadamore@1415 59 assertTrue(n == 5);
mcimadamore@1415 60
mcimadamore@1415 61 n = test.method1((a, b) -> 0);
mcimadamore@1415 62 assertTrue(n == 6);
mcimadamore@1415 63
mcimadamore@1415 64 n = test.method1((a, b) -> null);
mcimadamore@1415 65 assertTrue(n == 6);
mcimadamore@1415 66
mcimadamore@1415 67 n = test.method1((a, b) -> null, (a, b) -> null);
mcimadamore@1415 68 assertTrue(n == 7);
mcimadamore@1415 69 }
mcimadamore@1415 70
mcimadamore@1415 71 int method1(SAM1<String> s) {
mcimadamore@1415 72 return 1;
mcimadamore@1415 73 }
mcimadamore@1415 74
mcimadamore@1415 75 int method1(SAM2 s) {
mcimadamore@1415 76 return 2;
mcimadamore@1415 77 }
mcimadamore@1415 78
mcimadamore@1415 79 int method1(SAM3 s) {
mcimadamore@1415 80 return 3;
mcimadamore@1415 81 }
mcimadamore@1415 82
mcimadamore@1415 83 int method1(SAM4 s) {
mcimadamore@1415 84 return 4;
mcimadamore@1415 85 }
mcimadamore@1415 86
mcimadamore@1415 87 int method1(SAM5 s) {
mcimadamore@1415 88 return 5;
mcimadamore@1415 89 }
mcimadamore@1415 90
mcimadamore@1415 91 int method1(SAM6<?, ? super Integer> s) {
mcimadamore@1415 92 return 6;
mcimadamore@1415 93 }
mcimadamore@1415 94
mcimadamore@1415 95 int method1(SAM6<?, ?>... s) {
mcimadamore@1415 96 return 7;
mcimadamore@1415 97 }
mcimadamore@1415 98
mcimadamore@1415 99 static interface SAM1<T> {
mcimadamore@1415 100 void foo(List<T> a, List<T> b);
mcimadamore@1415 101 }
mcimadamore@1415 102
mcimadamore@1415 103 static interface SAM2 {
mcimadamore@1415 104 List<String> foo();
mcimadamore@1415 105 }
mcimadamore@1415 106
mcimadamore@1415 107 static interface SAM3 {
mcimadamore@1415 108 String foo(int a);
mcimadamore@1415 109 }
mcimadamore@1415 110
mcimadamore@1415 111 static interface SAM4 {
mcimadamore@1415 112 void foo(List<File> a);
mcimadamore@1415 113 }
mcimadamore@1415 114
mcimadamore@1415 115 static interface SAM5 {
mcimadamore@1415 116 void foo();
mcimadamore@1415 117 }
mcimadamore@1415 118
mcimadamore@1415 119 static interface SAM6<T, V> {
mcimadamore@1415 120 V get(T t, T t2);
mcimadamore@1415 121 }
mcimadamore@1415 122 }

mercurial