test/tools/javac/lambda/methodReferenceExecution/MethodReferenceTestFDCCE.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 MethodReferenceTestFDCCE
    31  */
    33 import org.testng.annotations.Test;
    34 import java.lang.reflect.Array;
    36 import static org.testng.Assert.assertEquals;
    37 import static org.testng.Assert.assertTrue;
    38 import static org.testng.Assert.fail;
    40 /**
    41  * Method references and raw types.
    42  * @author Robert Field
    43  */
    45 @Test
    46 @SuppressWarnings({"rawtypes", "unchecked"})
    47 public class MethodReferenceTestFDCCE {
    49     static void assertCCE(Throwable t) {
    50         assertEquals(t.getClass().getName(), "java.lang.ClassCastException");
    51     }
    53     interface Pred<T> { boolean accept(T x); }
    55     interface Ps { boolean accept(short x); }
    57     interface Oo { Object too(int x); }
    59     interface Reto<T> { T m(); }
    61     class A {}
    62     class B extends A {}
    64     static boolean isMinor(int x) {
    65         return x < 18;
    66     }
    68     static boolean tst(A x) {
    69         return true;
    70     }
    72     static Object otst(Object x) {
    73         return x;
    74     }
    76     static boolean stst(Short x) {
    77         return x < 18;
    78     }
    80     static short ritst() {
    81         return 123;
    82     }
    84     public void testMethodReferenceFDPrim1() {
    85         Pred<Byte> p = MethodReferenceTestFDCCE::isMinor;
    86         Pred p2 = p;
    87         assertTrue(p2.accept((Byte)(byte)15));
    88     }
    90     public void testMethodReferenceFDPrim2() {
    91         Pred<Byte> p = MethodReferenceTestFDCCE::isMinor;
    92         Pred p2 = p;
    93         assertTrue(p2.accept((byte)15));
    94     }
    96     public void testMethodReferenceFDPrimICCE() {
    97         Pred<Byte> p = MethodReferenceTestFDCCE::isMinor;
    98         Pred p2 = p;
    99         try {
   100             p2.accept(15); // should throw CCE
   101             fail("Exception should have been thrown");
   102         } catch (Throwable t) {
   103             assertCCE(t);
   104         }
   105     }
   107     public void testMethodReferenceFDPrimOCCE() {
   108         Pred<Byte> p = MethodReferenceTestFDCCE::isMinor;
   109         Pred p2 = p;
   110         try {
   111             p2.accept(new Object()); // should throw CCE
   112             fail("Exception should have been thrown");
   113         } catch (Throwable t) {
   114             assertCCE(t);
   115         }
   116     }
   118     public void testMethodReferenceFDRef() {
   119         Pred<B> p = MethodReferenceTestFDCCE::tst;
   120         Pred p2 = p;
   121         assertTrue(p2.accept(new B()));
   122     }
   124     public void testMethodReferenceFDRefCCE() {
   125         Pred<B> p = MethodReferenceTestFDCCE::tst;
   126         Pred p2 = p;
   127         try {
   128             p2.accept(new A()); // should throw CCE
   129             fail("Exception should have been thrown");
   130         } catch (Throwable t) {
   131             assertCCE(t);
   132         }
   133     }
   135     public void testMethodReferenceFDPrimPrim() {
   136         Ps p = MethodReferenceTestFDCCE::isMinor;
   137         assertTrue(p.accept((byte)15));
   138     }
   140     public void testMethodReferenceFDPrimBoxed() {
   141         Ps p = MethodReferenceTestFDCCE::stst;
   142         assertTrue(p.accept((byte)15));
   143     }
   145     public void testMethodReferenceFDPrimRef() {
   146         Oo p = MethodReferenceTestFDCCE::otst;
   147         assertEquals(p.too(15).getClass().getName(), "java.lang.Integer");
   148     }
   150     public void testMethodReferenceFDRet1() {
   151         Reto<Short> p = MethodReferenceTestFDCCE::ritst;
   152         assertEquals(p.m(), (Short)(short)123);
   153     }
   154 }

mercurial