test/tools/javac/lambda/MethodReference31.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) 2011, 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.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 /*
    25  * @test
    26  * @bug 8003280
    27  * @summary Add lambda tests
    28  *  check that boxing of return-type works as expected
    29  */
    31 public class MethodReference31 {
    33     static class Success extends RuntimeException { }
    35     static int assertionCount = 0;
    37     static void assertTrue(boolean cond) {
    38         assertionCount++;
    39         if (!cond)
    40             throw new AssertionError();
    41     }
    43     interface SAM<X> {
    44         X m();
    45     }
    47     interface SAM_byte {
    48         byte m();
    49     }
    51     interface SAM_short {
    52         short m();
    53     }
    55     interface SAM_int {
    56         int m();
    57     }
    59     interface SAM_long {
    60         long m();
    61     }
    63     interface SAM_float {
    64         float m();
    65     }
    67     interface SAM_double {
    68         double m();
    69     }
    71     static <Z> Z test() {
    72         assertTrue(true);
    73         throw new Success();
    74     }
    76     static byte test_byte() {
    77         assertTrue(true);
    78         return 0;
    79     }
    81     static short test_short() {
    82         assertTrue(true);
    83         return 0;
    84     }
    86     static int test_int() {
    87         assertTrue(true);
    88         return 0;
    89     }
    91     static long test_long() {
    92         assertTrue(true);
    93         return 0;
    94     }
    96     static float test_float() {
    97         assertTrue(true);
    98         return 0;
    99     }
   101     static double test_double() {
   102         assertTrue(true);
   103         return 0;
   104     }
   106     static void testByte() {
   107         SAM<Byte> s1 = MethodReference31::test_byte;
   108         s1.m();
   109         SAM_byte s2 = MethodReference31::test_byte;
   110         s2.m();
   111         SAM<Byte> s3 = MethodReference31::<Byte>test;
   112         try {
   113             s3.m();
   114         }
   115         catch (RuntimeException ex) { }
   116         SAM_byte s4 = MethodReference31::<Byte>test;
   117         try {
   118             s4.m();
   119         }
   120         catch (RuntimeException ex) { }
   121     }
   123     static void testShort() {
   124         SAM<Short> s1 = MethodReference31::test_short;
   125         s1.m();
   126         SAM_short s2 = MethodReference31::test_short;
   127         s2.m();
   128         SAM<Short> s3 = MethodReference31::<Short>test;
   129         try {
   130             s3.m();
   131         }
   132         catch (RuntimeException ex) { }
   133         SAM_short s4 = MethodReference31::<Short>test;
   134         try {
   135             s4.m();
   136         }
   137         catch (RuntimeException ex) { }
   138     }
   140     static void testInteger() {
   141         SAM<Integer> s1 = MethodReference31::test_int;
   142         s1.m();
   143         SAM_int s2 = MethodReference31::test_int;
   144         s2.m();
   145         SAM<Integer> s3 = MethodReference31::<Integer>test;
   146         try {
   147             s3.m();
   148         }
   149         catch (RuntimeException ex) { }
   150         SAM_int s4 = MethodReference31::<Integer>test;
   151         try {
   152             s4.m();
   153         }
   154         catch (RuntimeException ex) { }
   155     }
   157     static void testLong() {
   158         SAM<Long> s1 = MethodReference31::test_long;
   159         s1.m();
   160         SAM_long s2 = MethodReference31::test_long;
   161         s2.m();
   162         SAM<Long> s3 = MethodReference31::<Long>test;
   163         try {
   164             s3.m();
   165         }
   166         catch (RuntimeException ex) { }
   167         SAM_long s4 = MethodReference31::<Long>test;
   168         try {
   169             s4.m();
   170         }
   171         catch (RuntimeException ex) { }
   172     }
   174     static void testFloat() {
   175         SAM<Float> s1 = MethodReference31::test_float;
   176         s1.m();
   177         SAM_float s2 = MethodReference31::test_float;
   178         s2.m();
   179         SAM<Float> s3 = MethodReference31::<Float>test;
   180         try {
   181             s3.m();
   182         }
   183         catch (RuntimeException ex) { }
   184         SAM_float s4 = MethodReference31::<Float>test;
   185         try {
   186             s4.m();
   187         }
   188         catch (RuntimeException ex) { }
   189     }
   191     static void testDouble() {
   192         SAM<Double> s1 = MethodReference31::test_double;
   193         s1.m();
   194         SAM_double s2 = MethodReference31::test_double;
   195         s2.m();
   196         SAM<Double> s3 = MethodReference31::<Double>test;
   197         try {
   198             s3.m();
   199         }
   200         catch (RuntimeException ex) { }
   201         SAM_double s4 = MethodReference31::<Double>test;
   202         try {
   203             s4.m();
   204         }
   205         catch (RuntimeException ex) { }
   206     }
   208     public static void main(String[] args) {
   209         testByte();
   210         testShort();
   211         testInteger();
   212         testLong();
   213         testFloat();
   214         testDouble();
   215         assertTrue(assertionCount == 24);
   216     }
   217 }

mercurial