test/tools/javac/lambda/methodReferenceExecution/MethodReferenceTestKinds.java

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

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 1448
7d34e91f66bb
child 2525
2eb010b6cb22
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

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

mercurial