File: TestSerializedLambdaNameStability.java

package info (click to toggle)
libnb-javaparser-java 9%2B2018-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 65,320 kB
  • sloc: java: 440,096; xml: 6,359; sh: 865; makefile: 314
file content (173 lines) | stat: -rw-r--r-- 6,896 bytes parent folder | download | duplicates (18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8023668
 * @summary Desugar serializable lambda bodies using more robust naming scheme
 * @library /tools/lib
 * @modules jdk.compiler/com.sun.tools.javac.api
 *          jdk.compiler/com.sun.tools.javac.main
 *          jdk.jdeps/com.sun.tools.javap
 * @build toolbox.ToolBox toolbox.JavacTask
 * @run main TestSerializedLambdaNameStability
 */

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.*;

import toolbox.JavacTask;
import toolbox.ToolBox;

public class TestSerializedLambdaNameStability {

    final ClassLoader writingClassLoader;
    final ClassLoader clonedClassLoader;
    final ClassLoader checkingClassLoader;

    TestSerializedLambdaNameStability()  {
        writingClassLoader = new TestClassLoader("before");
        clonedClassLoader = new TestClassLoader("before");
        checkingClassLoader = new TestClassLoader("after");
    }

    public static void main(String... args) throws Exception {
        new TestSerializedLambdaNameStability().doit("NameOfCapturedArgs", true);
        new TestSerializedLambdaNameStability().doit("TypesOfCapturedArgs", true);
        new TestSerializedLambdaNameStability().doit("OrderOfCapturedArgs", true);
        new TestSerializedLambdaNameStability().doit("VariableAssignmentTarget", false);
        new TestSerializedLambdaNameStability().doit("TargetName", true);
        new TestSerializedLambdaNameStability().doit("TargetType", true);
    }

    public void doit(String name, boolean expectFail) throws Exception {
        String iName = "I" + name;
        String testName = "TEST" + name;
        Class<?> kw = writingClassLoader.loadClass(testName);
        Object instw = getInstance(kw);
        Method mw = getMethod(kw, "write", ObjectOutput.class);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (ObjectOutput out = new ObjectOutputStream(baos)) {
            mw.invoke(instw, out);
        }
        byte[] ser = baos.toByteArray();

        // Read and check clone
        readCheck(iName, testName, clonedClassLoader, ser);
        System.err.printf("cloned test readCheck %s\n", testName);

        // Read and check other
        if (expectFail) {
            try {
                readCheck(iName, testName, checkingClassLoader, ser);
            } catch (InvocationTargetException ite) {
                Throwable underlying = ite;
                while (underlying != null && !(underlying instanceof IllegalArgumentException)) {
                    underlying = underlying.getCause();
                }
                if (underlying != null) {
                    if (underlying.getMessage().contains("deserialization")) {
                        System.err.printf("PASS: other test %s got expected exception %s\n", testName, underlying);
                        return;
                    }
                }
                System.err.printf("FAIL: other test %s got unexpected exception %s\n", testName, ite);
                throw new Exception("unexpected exception ", ite);
            }
            System.err.printf("FAIL: other test %s expected an exception", testName);
            throw new Exception("expected an exception" + testName);
        } else {
            readCheck(iName, testName, checkingClassLoader, ser);
            System.err.printf("PASS: other test %s readCheck\n", testName);
        }
    }

    void readCheck(String iName, String testName, ClassLoader loader, byte[] ser) throws Exception {
        Class<?> k = loader.loadClass(testName);
        Object inst = getInstance(k);
        Method mrc = getMethod(k, "readCheck", ObjectInput.class);
        ByteArrayInputStream bais = new ByteArrayInputStream(ser);
        try (ObjectInput in = new ObjectInputStream(bais)) {
            mrc.invoke(inst, in);
        }
    }

    Method getMethod(Class<?> k, String name, Class<?> argTypes) throws Exception {
        Method meth = k.getDeclaredMethod(name, argTypes);
        meth.setAccessible(true);
        return meth;
    }

    Object getInstance(Class<?> k) throws Exception {
        Constructor<?> cons = k.getConstructors()[0];
        cons.setAccessible(true);
        return cons.newInstance();
    }

    static class TestClassLoader extends ClassLoader  {
        static final String compiledDir = System.getProperty("user.dir");
        static final String sourceBaseDir = System.getProperty("test.src");

        final ToolBox tb = new ToolBox();
        final String context;

        public TestClassLoader(String context) {
            super();
            this.context = context;
        }

        @Override
        public Class findClass(String name) throws ClassNotFoundException {
            byte[] b;

            try {
                b = loadClassData(name);
            } catch (Throwable th) {
                // th.printStackTrace();
                throw new ClassNotFoundException("Loading error", th);
            }
            return defineClass(name, b, 0, b.length);
        }

        private byte[] loadClassData(String name) throws Exception {
            String srcName;
            if (name.startsWith("TEST"))
                srcName = name;
            else if (name.startsWith("I"))
                srcName = "TEST" + name.substring(1);
            else
                throw new Exception("Did not expect to load " + name);
            Path srcFile = Paths.get(sourceBaseDir, context, srcName + ".java");
            new JavacTask(tb)
                    .outdir(compiledDir)
                    .files(srcFile)
                    .run();
            Path cfFile = Paths.get(compiledDir, name + ".class");
            byte[] bytes = Files.readAllBytes(cfFile);
            return bytes;
        }
    }
}