test/tools/javac/lambda/SerializedLambdaInInit.java

changeset 0
959103a6100f
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
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 */
23
24 /*
25 @test
26 @bug 8009742
27 @summary Bad method name: Serialized lambda in a constructor or class init
28 */
29
30 import java.io.*;
31 import java.lang.reflect.Method;
32
33 public class SerializedLambdaInInit {
34 static int assertionCount = 0;
35
36 static void assertTrue(boolean cond) {
37 assertionCount++;
38 if (!cond)
39 throw new AssertionError();
40 }
41
42 static LSI cvisi = z -> "[" + z + "]";
43 static LSI cisi;
44
45 static {
46 cisi = z -> z + z;
47 }
48
49 LSI ivsi = z -> "blah";
50 LSI iisi;
51
52 SerializedLambdaInInit() {
53 iisi = z -> "*" + z;
54 }
55
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();
62
63 write(out, cvisi );
64 write(out, cisi );
65 write(out, slii.ivsi );
66 write(out, slii.iisi );
67 out.flush();
68 out.close();
69
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();
79
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 }
90
91 static void write(ObjectOutput out, LSI lamb) throws IOException {
92 out.writeObject(lamb);
93 }
94
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 }
101
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 }
115
116 interface LSI extends Serializable {
117 String convert(String x);
118 }

mercurial