Tue, 14 May 2013 11:11:09 -0700
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
rfield@1422 | 1 | /* |
katleman@1448 | 2 | * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
rfield@1422 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
rfield@1422 | 4 | * |
rfield@1422 | 5 | * This code is free software; you can redistribute it and/or modify it |
rfield@1422 | 6 | * under the terms of the GNU General Public License version 2 only, as |
rfield@1422 | 7 | * published by the Free Software Foundation. Oracle designates this |
rfield@1422 | 8 | * particular file as subject to the "Classpath" exception as provided |
rfield@1422 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
rfield@1422 | 10 | * |
rfield@1422 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
rfield@1422 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
rfield@1422 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
rfield@1422 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
rfield@1422 | 15 | * accompanied this code). |
rfield@1422 | 16 | * |
rfield@1422 | 17 | * You should have received a copy of the GNU General Public License version |
rfield@1422 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
rfield@1422 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
rfield@1422 | 20 | * |
rfield@1422 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
rfield@1422 | 22 | * or visit www.oracle.com if you need additional information or have any |
rfield@1422 | 23 | * questions. |
rfield@1422 | 24 | */ |
rfield@1422 | 25 | |
rfield@1422 | 26 | /** |
rfield@1422 | 27 | * @test |
rfield@1422 | 28 | * @bug 8003639 |
rfield@1422 | 29 | * @summary convert lambda testng tests to jtreg and add them |
rfield@1422 | 30 | * @run testng MethodReferenceTestKinds |
rfield@1422 | 31 | */ |
rfield@1422 | 32 | |
rfield@1422 | 33 | import org.testng.annotations.Test; |
rfield@1422 | 34 | |
rfield@1422 | 35 | import static org.testng.Assert.assertEquals; |
rfield@1422 | 36 | |
rfield@1422 | 37 | /** |
rfield@1422 | 38 | * @author Robert Field |
rfield@1422 | 39 | */ |
rfield@1422 | 40 | |
rfield@1422 | 41 | @Test |
rfield@1422 | 42 | public class MethodReferenceTestKinds extends MethodReferenceTestKindsSup { |
rfield@1422 | 43 | |
rfield@1422 | 44 | interface S0 { String get(); } |
rfield@1422 | 45 | interface S1 { String get(MethodReferenceTestKinds x); } |
rfield@1422 | 46 | interface S2 { String get(MethodReferenceTestKinds x, MethodReferenceTestKinds y); } |
rfield@1422 | 47 | |
rfield@1422 | 48 | interface SXN0 { MethodReferenceTestKindsBase make(MethodReferenceTestKinds x); } |
rfield@1422 | 49 | interface SXN1 { MethodReferenceTestKindsBase make(MethodReferenceTestKinds x, String str); } |
rfield@1422 | 50 | |
rfield@1422 | 51 | interface SN0 { MethodReferenceTestKindsBase make(); } |
rfield@1422 | 52 | interface SN1 { MethodReferenceTestKindsBase make(String x); } |
rfield@1422 | 53 | |
rfield@1422 | 54 | class In extends MethodReferenceTestKindsBase { |
rfield@1422 | 55 | In(String val) { |
rfield@1422 | 56 | this.val = val; |
rfield@1422 | 57 | } |
rfield@1422 | 58 | |
rfield@1422 | 59 | In() { |
rfield@1422 | 60 | this("blank"); |
rfield@1422 | 61 | } |
rfield@1422 | 62 | } |
rfield@1422 | 63 | |
rfield@1422 | 64 | String instanceMethod0() { return "IM:0-" + this; } |
rfield@1422 | 65 | String instanceMethod1(MethodReferenceTestKinds x) { return "IM:1-" + this + x; } |
rfield@1422 | 66 | |
rfield@1422 | 67 | static String staticMethod0() { return "SM:0"; } |
rfield@1422 | 68 | static String staticMethod1(MethodReferenceTestKinds x) { return "SM:1-" + x; } |
rfield@1422 | 69 | |
rfield@1422 | 70 | MethodReferenceTestKinds(String val) { |
rfield@1422 | 71 | super(val); |
rfield@1422 | 72 | } |
rfield@1422 | 73 | |
rfield@1422 | 74 | MethodReferenceTestKinds() { |
rfield@1422 | 75 | super("blank"); |
rfield@1422 | 76 | } |
rfield@1422 | 77 | |
rfield@1422 | 78 | MethodReferenceTestKinds inst(String val) { |
rfield@1422 | 79 | return new MethodReferenceTestKinds(val); |
rfield@1422 | 80 | } |
rfield@1422 | 81 | |
rfield@1422 | 82 | public void testMRBound() { |
rfield@1422 | 83 | S0 var = this::instanceMethod0; |
rfield@1422 | 84 | assertEquals(var.get(), "IM:0-MethodReferenceTestKinds(blank)"); |
rfield@1422 | 85 | } |
rfield@1422 | 86 | |
rfield@1422 | 87 | public void testMRBoundArg() { |
rfield@1422 | 88 | S1 var = this::instanceMethod1; |
rfield@1422 | 89 | assertEquals(var.get(inst("arg")), "IM:1-MethodReferenceTestKinds(blank)MethodReferenceTestKinds(arg)"); |
rfield@1422 | 90 | } |
rfield@1422 | 91 | |
rfield@1422 | 92 | public void testMRUnbound() { |
rfield@1422 | 93 | S1 var = MethodReferenceTestKinds::instanceMethod0; |
rfield@1422 | 94 | assertEquals(var.get(inst("rcvr")), "IM:0-MethodReferenceTestKinds(rcvr)"); |
rfield@1422 | 95 | } |
rfield@1422 | 96 | |
rfield@1422 | 97 | public void testMRUnboundArg() { |
rfield@1422 | 98 | S2 var = MethodReferenceTestKinds::instanceMethod1; |
rfield@1422 | 99 | assertEquals(var.get(inst("rcvr"), inst("arg")), "IM:1-MethodReferenceTestKinds(rcvr)MethodReferenceTestKinds(arg)"); |
rfield@1422 | 100 | } |
rfield@1422 | 101 | |
rfield@1422 | 102 | public void testMRSuper() { |
rfield@1422 | 103 | S0 var = super::instanceMethod0; |
rfield@1422 | 104 | assertEquals(var.get(), "SIM:0-MethodReferenceTestKinds(blank)"); |
rfield@1422 | 105 | } |
rfield@1422 | 106 | |
rfield@1422 | 107 | public void testMRSuperArg() { |
rfield@1422 | 108 | S1 var = super::instanceMethod1; |
rfield@1422 | 109 | assertEquals(var.get(inst("arg")), "SIM:1-MethodReferenceTestKinds(blank)MethodReferenceTestKinds(arg)"); |
rfield@1422 | 110 | } |
rfield@1422 | 111 | |
rfield@1422 | 112 | public void testMRStatic() { |
rfield@1422 | 113 | S0 var = MethodReferenceTestKinds::staticMethod0; |
rfield@1422 | 114 | assertEquals(var.get(), "SM:0"); |
rfield@1422 | 115 | } |
rfield@1422 | 116 | |
rfield@1422 | 117 | public void testMRStaticArg() { |
rfield@1422 | 118 | S1 var = MethodReferenceTestKinds::staticMethod1; |
rfield@1422 | 119 | assertEquals(var.get(inst("arg")), "SM:1-MethodReferenceTestKinds(arg)"); |
rfield@1422 | 120 | } |
rfield@1422 | 121 | |
rfield@1422 | 122 | public void testMRTopLevel() { |
rfield@1422 | 123 | SN0 var = MethodReferenceTestKindsBase::new; |
rfield@1422 | 124 | assertEquals(var.make().toString(), "MethodReferenceTestKindsBase(blank)"); |
rfield@1422 | 125 | } |
rfield@1422 | 126 | |
rfield@1422 | 127 | public void testMRTopLevelArg() { |
rfield@1422 | 128 | SN1 var = MethodReferenceTestKindsBase::new; |
rfield@1422 | 129 | assertEquals(var.make("name").toString(), "MethodReferenceTestKindsBase(name)"); |
rfield@1422 | 130 | } |
rfield@1422 | 131 | |
rfield@1422 | 132 | public void testMRImplicitInner() { |
rfield@1422 | 133 | SN0 var = MethodReferenceTestKinds.In::new; |
rfield@1422 | 134 | assertEquals(var.make().toString(), "In(blank)"); |
rfield@1422 | 135 | } |
rfield@1422 | 136 | |
rfield@1422 | 137 | public void testMRImplicitInnerArg() { |
rfield@1422 | 138 | SN1 var = MethodReferenceTestKinds.In::new; |
rfield@1422 | 139 | assertEquals(var.make("name").toString(), "In(name)"); |
rfield@1422 | 140 | } |
rfield@1422 | 141 | |
rfield@1422 | 142 | } |
rfield@1422 | 143 | |
rfield@1422 | 144 | |
rfield@1422 | 145 | class MethodReferenceTestKindsBase { |
rfield@1422 | 146 | String val = "unset"; |
rfield@1422 | 147 | |
rfield@1422 | 148 | public String toString() { |
rfield@1422 | 149 | return getClass().getSimpleName() + "(" + val + ")"; |
rfield@1422 | 150 | } |
rfield@1422 | 151 | |
rfield@1422 | 152 | MethodReferenceTestKindsBase(String val) { |
rfield@1422 | 153 | this.val = val; |
rfield@1422 | 154 | } |
rfield@1422 | 155 | |
rfield@1422 | 156 | MethodReferenceTestKindsBase() { |
rfield@1422 | 157 | this("blank"); |
rfield@1422 | 158 | } |
rfield@1422 | 159 | |
rfield@1422 | 160 | } |
rfield@1422 | 161 | |
rfield@1422 | 162 | class MethodReferenceTestKindsSup extends MethodReferenceTestKindsBase { |
rfield@1422 | 163 | String instanceMethod0() { return "SIM:0-" + this; } |
rfield@1422 | 164 | String instanceMethod1(MethodReferenceTestKinds x) { return "SIM:1-" + this + x; } |
rfield@1422 | 165 | |
rfield@1422 | 166 | MethodReferenceTestKindsSup(String val) { |
rfield@1422 | 167 | super(val); |
rfield@1422 | 168 | } |
rfield@1422 | 169 | |
rfield@1422 | 170 | } |