test/tools/javac/lambda/SerializedLambdaInInit.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) 2013, 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 8009742
    27 @summary Bad method name: Serialized lambda in a constructor or class init
    28 */
    30 import java.io.*;
    31 import java.lang.reflect.Method;
    33 public class SerializedLambdaInInit {
    34     static int assertionCount = 0;
    36     static void assertTrue(boolean cond) {
    37         assertionCount++;
    38         if (!cond)
    39             throw new AssertionError();
    40     }
    42     static LSI cvisi = z -> "[" + z + "]";
    43     static LSI cisi;
    45     static {
    46         cisi = z -> z + z;
    47     }
    49     LSI ivsi = z -> "blah";
    50     LSI iisi;
    52     SerializedLambdaInInit() {
    53         iisi = z -> "*" + z;
    54     }
    56     public static void main(String[] args) throws Exception {
    57         try {
    58             // Write lambdas out
    59             ByteArrayOutputStream baos = new ByteArrayOutputStream();
    60             ObjectOutput out = new ObjectOutputStream(baos);
    61             SerializedLambdaInInit slii = new SerializedLambdaInInit();
    63             write(out, cvisi );
    64             write(out, cisi );
    65             write(out, slii.ivsi );
    66             write(out, slii.iisi );
    67             out.flush();
    68             out.close();
    70             // Read them back
    71             ByteArrayInputStream bais =
    72                 new ByteArrayInputStream(baos.toByteArray());
    73             ObjectInputStream in = new ObjectInputStream(bais);
    74             readAssert(in, "[X]");
    75             readAssert(in, "XX");
    76             readAssert(in, "blah");
    77             readAssert(in, "*X");
    78             in.close();
    80             // Reflectively test for valid method names
    81             for (Method meth : slii.getClass().getDeclaredMethods()) {
    82                 checkIdentifier(meth.getName());
    83             }
    84         } catch (IOException e) {
    85             e.printStackTrace();
    86             throw e;
    87         }
    88         assertTrue(assertionCount == 4);
    89     }
    91     static void write(ObjectOutput out, LSI lamb) throws IOException {
    92         out.writeObject(lamb);
    93     }
    95     static void readAssert(ObjectInputStream in, String expected)  throws IOException, ClassNotFoundException {
    96         LSI ls = (LSI) in.readObject();
    97         String result = ls.convert("X");
    98         System.out.printf("Result: %s\n", result);
    99         assertTrue(result.equals(expected));
   100     }
   102     public static void checkIdentifier(String str) throws Exception {
   103         // null and zero length identifers will throw their own exceptions
   104         char[] chars = str.toCharArray();
   105         if (!Character.isJavaIdentifierStart(chars[0])) {
   106             throw new IllegalArgumentException(str + ": bad identifier start character: '" + chars[0] + "'");
   107         }
   108         for (char ch : chars) {
   109             if (!Character.isJavaIdentifierPart(ch)) {
   110                 throw new IllegalArgumentException(str + ": bad identifier character: '" + ch + "'");
   111             }
   112         }
   113     }
   114 }
   116 interface LSI extends Serializable {
   117     String convert(String x);
   118 }

mercurial