1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/T8004969.java Thu Feb 21 14:43:51 2013 -0800 1.3 @@ -0,0 +1,103 @@ 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 8004969 1.30 +@summary Lambda deserialization 1.31 + 1.32 +*/ 1.33 + 1.34 +import java.io.*; 1.35 + 1.36 +public class T8004969 implements Serializable { 1.37 + 1.38 + static int assertionCount = 0; 1.39 + 1.40 + static void assertTrue(boolean cond) { 1.41 + assertionCount++; 1.42 + if (!cond) 1.43 + throw new AssertionError(); 1.44 + } 1.45 + 1.46 + static String mm(String s) { return "mref" + s; } 1.47 + 1.48 + String aField = "aF"; 1.49 + 1.50 + public static void main(String[] args) throws Exception { 1.51 + (new T8004969()).doit(); 1.52 + } 1.53 + 1.54 + public void doit() throws Exception { 1.55 + String aLocal = "aL"; 1.56 + int anInt = 99; 1.57 + 1.58 + try { 1.59 + // Write lambdas out 1.60 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1.61 + ObjectOutput out = new ObjectOutputStream(baos); 1.62 + 1.63 + write(out, z -> "[" + z + "]" ); 1.64 + write(out, z -> { String x = z + z; return x + x; } ); 1.65 + write(out, T8004969::mm ); 1.66 + write(out, z -> (new LSI() { public String convert(String x) { return "*"+x; }} ).convert(z) ); 1.67 + write(out, z -> aField + z ); 1.68 + write(out, z -> aLocal + z ); 1.69 + write(out, z -> z + anInt ); 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, "XXXX"); 1.79 + readAssert(in, "mrefX"); 1.80 + readAssert(in, "*X"); 1.81 + readAssert(in, "aFX"); 1.82 + readAssert(in, "aLX"); 1.83 + readAssert(in, "X99"); 1.84 + in.close(); 1.85 + } catch (IOException e) { 1.86 + e.printStackTrace(); 1.87 + throw e; 1.88 + } 1.89 + assertTrue(assertionCount == 7); 1.90 + } 1.91 + 1.92 + static void write(ObjectOutput out, LSI lamb) throws IOException { 1.93 + out.writeObject(lamb); 1.94 + } 1.95 + 1.96 + static void readAssert(ObjectInputStream in, String expected) throws IOException, ClassNotFoundException { 1.97 + LSI ls = (LSI) in.readObject(); 1.98 + String result = ls.convert("X"); 1.99 + System.out.printf("Result: %s\n", result); 1.100 + assertTrue(result.equals(expected)); 1.101 + } 1.102 +} 1.103 + 1.104 +interface LSI extends Serializable { 1.105 + String convert(String x); 1.106 +}