8009742: Bad lambda name for lambda in a static initializer or ctor

Mon, 11 Mar 2013 10:02:55 -0700

author
rfield
date
Mon, 11 Mar 2013 10:02:55 -0700
changeset 1624
d0ae21e3a382
parent 1623
c61add6bf8ac
child 1625
fbb6e470079d

8009742: Bad lambda name for lambda in a static initializer or ctor
Reviewed-by: mcimadamore

src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java file | annotate | diff | comparison | revisions
test/tools/javac/lambda/SerializedLambdaInInit.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Mon Mar 11 15:35:13 2013 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Mon Mar 11 10:02:55 2013 -0700
     1.3 @@ -1296,7 +1296,12 @@
     1.4          private Name serializedLambdaName(Symbol owner) {
     1.5              StringBuilder buf = new StringBuilder();
     1.6              buf.append(names.lambda);
     1.7 -            buf.append(owner.name);
     1.8 +            String methodName = owner.name.toString();
     1.9 +            if (methodName.equals("<clinit>"))
    1.10 +                methodName = "static";
    1.11 +            else if (methodName.equals("<init>"))
    1.12 +                methodName = "new";
    1.13 +            buf.append(methodName);
    1.14              buf.append('$');
    1.15              int methTypeHash = methodSig(owner.type).hashCode();
    1.16              buf.append(Integer.toHexString(methTypeHash));
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/lambda/SerializedLambdaInInit.java	Mon Mar 11 10:02:55 2013 -0700
     2.3 @@ -0,0 +1,118 @@
     2.4 +/*
     2.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 +@test
    2.29 +@bug 8009742
    2.30 +@summary Bad method name: Serialized lambda in a constructor or class init
    2.31 +*/
    2.32 +
    2.33 +import java.io.*;
    2.34 +import java.lang.reflect.Method;
    2.35 +
    2.36 +public class SerializedLambdaInInit {
    2.37 +    static int assertionCount = 0;
    2.38 +
    2.39 +    static void assertTrue(boolean cond) {
    2.40 +        assertionCount++;
    2.41 +        if (!cond)
    2.42 +            throw new AssertionError();
    2.43 +    }
    2.44 +
    2.45 +    static LSI cvisi = z -> "[" + z + "]";
    2.46 +    static LSI cisi;
    2.47 +
    2.48 +    static {
    2.49 +        cisi = z -> z + z;
    2.50 +    }
    2.51 +
    2.52 +    LSI ivsi = z -> "blah";
    2.53 +    LSI iisi;
    2.54 +
    2.55 +    SerializedLambdaInInit() {
    2.56 +        iisi = z -> "*" + z;
    2.57 +    }
    2.58 +
    2.59 +    public static void main(String[] args) throws Exception {
    2.60 +        try {
    2.61 +            // Write lambdas out
    2.62 +            ByteArrayOutputStream baos = new ByteArrayOutputStream();
    2.63 +            ObjectOutput out = new ObjectOutputStream(baos);
    2.64 +            SerializedLambdaInInit slii = new SerializedLambdaInInit();
    2.65 +
    2.66 +            write(out, cvisi );
    2.67 +            write(out, cisi );
    2.68 +            write(out, slii.ivsi );
    2.69 +            write(out, slii.iisi );
    2.70 +            out.flush();
    2.71 +            out.close();
    2.72 +
    2.73 +            // Read them back
    2.74 +            ByteArrayInputStream bais =
    2.75 +                new ByteArrayInputStream(baos.toByteArray());
    2.76 +            ObjectInputStream in = new ObjectInputStream(bais);
    2.77 +            readAssert(in, "[X]");
    2.78 +            readAssert(in, "XX");
    2.79 +            readAssert(in, "blah");
    2.80 +            readAssert(in, "*X");
    2.81 +            in.close();
    2.82 +
    2.83 +            // Reflectively test for valid method names
    2.84 +            for (Method meth : slii.getClass().getDeclaredMethods()) {
    2.85 +                checkIdentifier(meth.getName());
    2.86 +            }
    2.87 +        } catch (IOException e) {
    2.88 +            e.printStackTrace();
    2.89 +            throw e;
    2.90 +        }
    2.91 +        assertTrue(assertionCount == 4);
    2.92 +    }
    2.93 +
    2.94 +    static void write(ObjectOutput out, LSI lamb) throws IOException {
    2.95 +        out.writeObject(lamb);
    2.96 +    }
    2.97 +
    2.98 +    static void readAssert(ObjectInputStream in, String expected)  throws IOException, ClassNotFoundException {
    2.99 +        LSI ls = (LSI) in.readObject();
   2.100 +        String result = ls.convert("X");
   2.101 +        System.out.printf("Result: %s\n", result);
   2.102 +        assertTrue(result.equals(expected));
   2.103 +    }
   2.104 +
   2.105 +    public static void checkIdentifier(String str) throws Exception {
   2.106 +        // null and zero length identifers will throw their own exceptions
   2.107 +        char[] chars = str.toCharArray();
   2.108 +        if (!Character.isJavaIdentifierStart(chars[0])) {
   2.109 +            throw new IllegalArgumentException(str + ": bad identifier start character: '" + chars[0] + "'");
   2.110 +        }
   2.111 +        for (char ch : chars) {
   2.112 +            if (!Character.isJavaIdentifierPart(ch)) {
   2.113 +                throw new IllegalArgumentException(str + ": bad identifier character: '" + ch + "'");
   2.114 +            }
   2.115 +        }
   2.116 +    }
   2.117 +}
   2.118 +
   2.119 +interface LSI extends Serializable {
   2.120 +    String convert(String x);
   2.121 +}

mercurial