test/tools/javac/lambda/lambdaNaming/TestSerializedLambdaNameStability.java

Mon, 23 Jun 2014 13:14:32 -0700

author
rfield
date
Mon, 23 Jun 2014 13:14:32 -0700
changeset 2528
eb284abd64fe
parent 0
959103a6100f
permissions
-rw-r--r--

8046060: Different results of floating point multiplication for lambda code block
Summary: propogate strictfp into lambda body
Reviewed-by: vromero, jlahoda

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8023668
aoqi@0 27 * @summary Desugar serializable lambda bodies using more robust naming scheme
aoqi@0 28 * @library /tools/javac/lib
aoqi@0 29 * @build ToolBox
aoqi@0 30 * @run main TestSerializedLambdaNameStability
aoqi@0 31 */
aoqi@0 32
aoqi@0 33 import java.io.*;
aoqi@0 34 import java.lang.reflect.Constructor;
aoqi@0 35 import java.lang.reflect.InvocationTargetException;
aoqi@0 36 import java.lang.reflect.Method;
aoqi@0 37 import java.nio.file.*;
aoqi@0 38
aoqi@0 39 public class TestSerializedLambdaNameStability {
aoqi@0 40
aoqi@0 41 final ClassLoader writingClassLoader;
aoqi@0 42 final ClassLoader clonedClassLoader;
aoqi@0 43 final ClassLoader checkingClassLoader;
aoqi@0 44
aoqi@0 45 TestSerializedLambdaNameStability() {
aoqi@0 46 writingClassLoader = new TestClassLoader("before");
aoqi@0 47 clonedClassLoader = new TestClassLoader("before");
aoqi@0 48 checkingClassLoader = new TestClassLoader("after");
aoqi@0 49 }
aoqi@0 50
aoqi@0 51 public static void main(String... args) throws Exception {
aoqi@0 52 new TestSerializedLambdaNameStability().doit("NameOfCapturedArgs", true);
aoqi@0 53 new TestSerializedLambdaNameStability().doit("TypesOfCapturedArgs", true);
aoqi@0 54 new TestSerializedLambdaNameStability().doit("OrderOfCapturedArgs", true);
aoqi@0 55 new TestSerializedLambdaNameStability().doit("VariableAssignmentTarget", false);
aoqi@0 56 new TestSerializedLambdaNameStability().doit("TargetName", true);
aoqi@0 57 new TestSerializedLambdaNameStability().doit("TargetType", true);
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 public void doit(String name, boolean expectFail) throws Exception {
aoqi@0 61 String iName = "I" + name;
aoqi@0 62 String testName = "TEST" + name;
aoqi@0 63 Class<?> kw = writingClassLoader.loadClass(testName);
aoqi@0 64 Object instw = getInstance(kw);
aoqi@0 65 Method mw = getMethod(kw, "write", ObjectOutput.class);
aoqi@0 66 ByteArrayOutputStream baos = new ByteArrayOutputStream();
aoqi@0 67 try (ObjectOutput out = new ObjectOutputStream(baos)) {
aoqi@0 68 mw.invoke(instw, out);
aoqi@0 69 }
aoqi@0 70 byte[] ser = baos.toByteArray();
aoqi@0 71
aoqi@0 72 // Read and check clone
aoqi@0 73 readCheck(iName, testName, clonedClassLoader, ser);
aoqi@0 74 System.err.printf("cloned test readCheck %s\n", testName);
aoqi@0 75
aoqi@0 76 // Read and check other
aoqi@0 77 if (expectFail) {
aoqi@0 78 try {
aoqi@0 79 readCheck(iName, testName, checkingClassLoader, ser);
aoqi@0 80 } catch (InvocationTargetException ite) {
aoqi@0 81 Throwable underlying = ite;
aoqi@0 82 while (underlying != null && !(underlying instanceof IllegalArgumentException)) {
aoqi@0 83 underlying = underlying.getCause();
aoqi@0 84 }
aoqi@0 85 if (underlying != null) {
aoqi@0 86 if (underlying.getMessage().contains("deserialization")) {
aoqi@0 87 System.err.printf("PASS: other test %s got expected exception %s\n", testName, underlying);
aoqi@0 88 return;
aoqi@0 89 }
aoqi@0 90 }
aoqi@0 91 System.err.printf("FAIL: other test %s got unexpected exception %s\n", testName, ite);
aoqi@0 92 throw new Exception("unexpected exception ", ite);
aoqi@0 93 }
aoqi@0 94 System.err.printf("FAIL: other test %s expected an exception", testName);
aoqi@0 95 throw new Exception("expected an exception" + testName);
aoqi@0 96 } else {
aoqi@0 97 readCheck(iName, testName, checkingClassLoader, ser);
aoqi@0 98 System.err.printf("PASS: other test %s readCheck\n", testName);
aoqi@0 99 }
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 void readCheck(String iName, String testName, ClassLoader loader, byte[] ser) throws Exception {
aoqi@0 103 Class<?> k = loader.loadClass(testName);
aoqi@0 104 Object inst = getInstance(k);
aoqi@0 105 Method mrc = getMethod(k, "readCheck", ObjectInput.class);
aoqi@0 106 ByteArrayInputStream bais = new ByteArrayInputStream(ser);
aoqi@0 107 try (ObjectInput in = new ObjectInputStream(bais)) {
aoqi@0 108 mrc.invoke(inst, in);
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111
aoqi@0 112 Method getMethod(Class<?> k, String name, Class<?> argTypes) throws Exception {
aoqi@0 113 Method meth = k.getDeclaredMethod(name, argTypes);
aoqi@0 114 meth.setAccessible(true);
aoqi@0 115 return meth;
aoqi@0 116 }
aoqi@0 117
aoqi@0 118 Object getInstance(Class<?> k) throws Exception {
aoqi@0 119 Constructor<?> cons = k.getConstructors()[0];
aoqi@0 120 cons.setAccessible(true);
aoqi@0 121 return cons.newInstance();
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 static class TestClassLoader extends ClassLoader {
aoqi@0 125 static final String compiledDir = System.getProperty("user.dir");
aoqi@0 126 static final String sourceBaseDir = System.getProperty("test.src");
aoqi@0 127
aoqi@0 128 final String context;
aoqi@0 129
aoqi@0 130 public TestClassLoader(String context) {
aoqi@0 131 super();
aoqi@0 132 this.context = context;
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 @Override
aoqi@0 136 public Class findClass(String name) throws ClassNotFoundException {
aoqi@0 137 byte[] b;
aoqi@0 138
aoqi@0 139 try {
aoqi@0 140 b = loadClassData(name);
aoqi@0 141 } catch (Throwable th) {
aoqi@0 142 // th.printStackTrace();
aoqi@0 143 throw new ClassNotFoundException("Loading error", th);
aoqi@0 144 }
aoqi@0 145 return defineClass(name, b, 0, b.length);
aoqi@0 146 }
aoqi@0 147
aoqi@0 148 private byte[] loadClassData(String name) throws Exception {
aoqi@0 149 String srcName;
aoqi@0 150 if (name.startsWith("TEST"))
aoqi@0 151 srcName = name;
aoqi@0 152 else if (name.startsWith("I"))
aoqi@0 153 srcName = "TEST" + name.substring(1);
aoqi@0 154 else
aoqi@0 155 throw new Exception("Did not expect to load " + name);
aoqi@0 156 Path srcFile = Paths.get(sourceBaseDir, context, srcName + ".java");
aoqi@0 157 String testSource = new String(Files.readAllBytes(srcFile));
aoqi@0 158 ToolBox.JavaToolArgs javacSuccessArgs =
aoqi@0 159 new ToolBox.JavaToolArgs().setSources(testSource);
aoqi@0 160 ToolBox.javac(javacSuccessArgs);
aoqi@0 161 Path cfFile = Paths.get(compiledDir, name + ".class");
aoqi@0 162 byte[] bytes = Files.readAllBytes(cfFile);
aoqi@0 163 return bytes;
aoqi@0 164 }
aoqi@0 165 }
aoqi@0 166 }

mercurial