TestSerializedLambdaNameStability.java revision 3294:9adfb22ff08f
1/*
2 * Copyright (c) 2013, 2015, 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 8023668
27 * @summary Desugar serializable lambda bodies using more robust naming scheme
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.file
31 *          jdk.compiler/com.sun.tools.javac.main
32 *          jdk.jdeps/com.sun.tools.javap
33 * @build ToolBox
34 * @run main TestSerializedLambdaNameStability
35 */
36
37import java.io.*;
38import java.lang.reflect.Constructor;
39import java.lang.reflect.InvocationTargetException;
40import java.lang.reflect.Method;
41import java.nio.file.*;
42
43public class TestSerializedLambdaNameStability {
44
45    final ClassLoader writingClassLoader;
46    final ClassLoader clonedClassLoader;
47    final ClassLoader checkingClassLoader;
48
49    TestSerializedLambdaNameStability()  {
50        writingClassLoader = new TestClassLoader("before");
51        clonedClassLoader = new TestClassLoader("before");
52        checkingClassLoader = new TestClassLoader("after");
53    }
54
55    public static void main(String... args) throws Exception {
56        new TestSerializedLambdaNameStability().doit("NameOfCapturedArgs", true);
57        new TestSerializedLambdaNameStability().doit("TypesOfCapturedArgs", true);
58        new TestSerializedLambdaNameStability().doit("OrderOfCapturedArgs", true);
59        new TestSerializedLambdaNameStability().doit("VariableAssignmentTarget", false);
60        new TestSerializedLambdaNameStability().doit("TargetName", true);
61        new TestSerializedLambdaNameStability().doit("TargetType", true);
62    }
63
64    public void doit(String name, boolean expectFail) throws Exception {
65        String iName = "I" + name;
66        String testName = "TEST" + name;
67        Class<?> kw = writingClassLoader.loadClass(testName);
68        Object instw = getInstance(kw);
69        Method mw = getMethod(kw, "write", ObjectOutput.class);
70        ByteArrayOutputStream baos = new ByteArrayOutputStream();
71        try (ObjectOutput out = new ObjectOutputStream(baos)) {
72            mw.invoke(instw, out);
73        }
74        byte[] ser = baos.toByteArray();
75
76        // Read and check clone
77        readCheck(iName, testName, clonedClassLoader, ser);
78        System.err.printf("cloned test readCheck %s\n", testName);
79
80        // Read and check other
81        if (expectFail) {
82            try {
83                readCheck(iName, testName, checkingClassLoader, ser);
84            } catch (InvocationTargetException ite) {
85                Throwable underlying = ite;
86                while (underlying != null && !(underlying instanceof IllegalArgumentException)) {
87                    underlying = underlying.getCause();
88                }
89                if (underlying != null) {
90                    if (underlying.getMessage().contains("deserialization")) {
91                        System.err.printf("PASS: other test %s got expected exception %s\n", testName, underlying);
92                        return;
93                    }
94                }
95                System.err.printf("FAIL: other test %s got unexpected exception %s\n", testName, ite);
96                throw new Exception("unexpected exception ", ite);
97            }
98            System.err.printf("FAIL: other test %s expected an exception", testName);
99            throw new Exception("expected an exception" + testName);
100        } else {
101            readCheck(iName, testName, checkingClassLoader, ser);
102            System.err.printf("PASS: other test %s readCheck\n", testName);
103        }
104    }
105
106    void readCheck(String iName, String testName, ClassLoader loader, byte[] ser) throws Exception {
107        Class<?> k = loader.loadClass(testName);
108        Object inst = getInstance(k);
109        Method mrc = getMethod(k, "readCheck", ObjectInput.class);
110        ByteArrayInputStream bais = new ByteArrayInputStream(ser);
111        try (ObjectInput in = new ObjectInputStream(bais)) {
112            mrc.invoke(inst, in);
113        }
114    }
115
116    Method getMethod(Class<?> k, String name, Class<?> argTypes) throws Exception {
117        Method meth = k.getDeclaredMethod(name, argTypes);
118        meth.setAccessible(true);
119        return meth;
120    }
121
122    Object getInstance(Class<?> k) throws Exception {
123        Constructor<?> cons = k.getConstructors()[0];
124        cons.setAccessible(true);
125        return cons.newInstance();
126    }
127
128    static class TestClassLoader extends ClassLoader  {
129        static final String compiledDir = System.getProperty("user.dir");
130        static final String sourceBaseDir = System.getProperty("test.src");
131
132        final ToolBox tb = new ToolBox();
133        final String context;
134
135        public TestClassLoader(String context) {
136            super();
137            this.context = context;
138        }
139
140        @Override
141        public Class findClass(String name) throws ClassNotFoundException {
142            byte[] b;
143
144            try {
145                b = loadClassData(name);
146            } catch (Throwable th) {
147                // th.printStackTrace();
148                throw new ClassNotFoundException("Loading error", th);
149            }
150            return defineClass(name, b, 0, b.length);
151        }
152
153        private byte[] loadClassData(String name) throws Exception {
154            String srcName;
155            if (name.startsWith("TEST"))
156                srcName = name;
157            else if (name.startsWith("I"))
158                srcName = "TEST" + name.substring(1);
159            else
160                throw new Exception("Did not expect to load " + name);
161            Path srcFile = Paths.get(sourceBaseDir, context, srcName + ".java");
162            tb.new JavacTask()
163                    .outdir(compiledDir)
164                    .files(srcFile)
165                    .run();
166            Path cfFile = Paths.get(compiledDir, name + ".class");
167            byte[] bytes = Files.readAllBytes(cfFile);
168            return bytes;
169        }
170    }
171}
172