test/tools/javac/lambda/SerializedLambdaInInit.java

changeset 0
959103a6100f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/lambda/SerializedLambdaInInit.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,118 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 +@test
    1.29 +@bug 8009742
    1.30 +@summary Bad method name: Serialized lambda in a constructor or class init
    1.31 +*/
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.lang.reflect.Method;
    1.35 +
    1.36 +public class SerializedLambdaInInit {
    1.37 +    static int assertionCount = 0;
    1.38 +
    1.39 +    static void assertTrue(boolean cond) {
    1.40 +        assertionCount++;
    1.41 +        if (!cond)
    1.42 +            throw new AssertionError();
    1.43 +    }
    1.44 +
    1.45 +    static LSI cvisi = z -> "[" + z + "]";
    1.46 +    static LSI cisi;
    1.47 +
    1.48 +    static {
    1.49 +        cisi = z -> z + z;
    1.50 +    }
    1.51 +
    1.52 +    LSI ivsi = z -> "blah";
    1.53 +    LSI iisi;
    1.54 +
    1.55 +    SerializedLambdaInInit() {
    1.56 +        iisi = z -> "*" + z;
    1.57 +    }
    1.58 +
    1.59 +    public static void main(String[] args) throws Exception {
    1.60 +        try {
    1.61 +            // Write lambdas out
    1.62 +            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    1.63 +            ObjectOutput out = new ObjectOutputStream(baos);
    1.64 +            SerializedLambdaInInit slii = new SerializedLambdaInInit();
    1.65 +
    1.66 +            write(out, cvisi );
    1.67 +            write(out, cisi );
    1.68 +            write(out, slii.ivsi );
    1.69 +            write(out, slii.iisi );
    1.70 +            out.flush();
    1.71 +            out.close();
    1.72 +
    1.73 +            // Read them back
    1.74 +            ByteArrayInputStream bais =
    1.75 +                new ByteArrayInputStream(baos.toByteArray());
    1.76 +            ObjectInputStream in = new ObjectInputStream(bais);
    1.77 +            readAssert(in, "[X]");
    1.78 +            readAssert(in, "XX");
    1.79 +            readAssert(in, "blah");
    1.80 +            readAssert(in, "*X");
    1.81 +            in.close();
    1.82 +
    1.83 +            // Reflectively test for valid method names
    1.84 +            for (Method meth : slii.getClass().getDeclaredMethods()) {
    1.85 +                checkIdentifier(meth.getName());
    1.86 +            }
    1.87 +        } catch (IOException e) {
    1.88 +            e.printStackTrace();
    1.89 +            throw e;
    1.90 +        }
    1.91 +        assertTrue(assertionCount == 4);
    1.92 +    }
    1.93 +
    1.94 +    static void write(ObjectOutput out, LSI lamb) throws IOException {
    1.95 +        out.writeObject(lamb);
    1.96 +    }
    1.97 +
    1.98 +    static void readAssert(ObjectInputStream in, String expected)  throws IOException, ClassNotFoundException {
    1.99 +        LSI ls = (LSI) in.readObject();
   1.100 +        String result = ls.convert("X");
   1.101 +        System.out.printf("Result: %s\n", result);
   1.102 +        assertTrue(result.equals(expected));
   1.103 +    }
   1.104 +
   1.105 +    public static void checkIdentifier(String str) throws Exception {
   1.106 +        // null and zero length identifers will throw their own exceptions
   1.107 +        char[] chars = str.toCharArray();
   1.108 +        if (!Character.isJavaIdentifierStart(chars[0])) {
   1.109 +            throw new IllegalArgumentException(str + ": bad identifier start character: '" + chars[0] + "'");
   1.110 +        }
   1.111 +        for (char ch : chars) {
   1.112 +            if (!Character.isJavaIdentifierPart(ch)) {
   1.113 +                throw new IllegalArgumentException(str + ": bad identifier character: '" + ch + "'");
   1.114 +            }
   1.115 +        }
   1.116 +    }
   1.117 +}
   1.118 +
   1.119 +interface LSI extends Serializable {
   1.120 +    String convert(String x);
   1.121 +}

mercurial