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

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

author
rfield
date
Tue, 14 May 2013 11:11:09 -0700
changeset 1752
c09b7234cded
parent 0
959103a6100f
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 MethodReferenceTestNewInner
    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 MethodReferenceTestNewInner {
    44     String note = "NO NOTE";
    46     interface M0<T> {
    48         T m();
    49     }
    51     interface MP<T> {
    53         T m(MethodReferenceTestNewInner m);
    54     }
    56     class N0 {
    58         N0() {
    59         }
    60     }
    62     interface M1<T> {
    64         T m(Integer a);
    65     }
    67     class N1 {
    69         int i;
    71         N1(int i) {
    72             this.i = i;
    73         }
    74     }
    76     interface M2<T> {
    78         T m(Integer n, String o);
    79     }
    81     class N2 {
    83         Number n;
    84         Object o;
    86         N2(Number n, Object o) {
    87             this.n = n;
    88             this.o = o;
    89         }
    91         public String toString() {
    92             return note + ":N2(" + n + "," + o + ")";
    93         }
    94     }
    96     interface MV {
    98         NV m(Integer ai, int i);
    99     }
   101     class NV {
   103         int i;
   105         NV(int... v) {
   106             i = 0;
   107             for (int x : v) {
   108                 i += x;
   109             }
   110         }
   112         public String toString() {
   113             return note + ":NV(" + i + ")";
   114         }
   115     }
   117 /* unbound constructor case not supported anymore (dropped by EG)
   118     public static void testConstructorReferenceP() {
   119         MP<N0> q;
   121         q = N0::new;
   122         assertEquals(q.m(new MethodReferenceTestNewInner()).getClass().getSimpleName(), "N0");
   123     }
   124 */
   125     public void testConstructorReference0() {
   126         M0<N0> q;
   128         q = N0::new;
   129         assertEquals(q.m().getClass().getSimpleName(), "N0");
   130     }
   132     public void testConstructorReference1() {
   133         M1<N1> q;
   135         q = N1::new;
   136         assertEquals(q.m(14).getClass().getSimpleName(), "N1");
   137     }
   139     public void testConstructorReference2() {
   140         M2<N2> q;
   142         note = "T2";
   143         q = N2::new;
   144         assertEquals(q.m(7, "hi").toString(), "T2:N2(7,hi)");
   145     }
   147     /***
   148     public void testConstructorReferenceVarArgs() {
   149         MV q;
   151         note = "TVA";
   152         q = NV::new;
   153         assertEquals(q.m(5, 45).toString(), "TNV:NV(50)");
   154     }
   155     ***/
   157 }

mercurial