Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/java/lang/invoke/lambda/LambdaClassLoaderSerialization.java
48795 views
/*1* Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800497026* @summary Lambda serialization in the presence of class loaders27* @library /lib/testlibrary28* @build jdk.testlibrary.IOUtils29* @run main LambdaClassLoaderSerialization30* @author Peter Levart31*/3233import java.io.ByteArrayInputStream;34import java.io.ByteArrayOutputStream;35import java.io.IOException;36import java.io.InputStream;37import java.io.ObjectInputStream;38import java.io.ObjectOutputStream;39import java.io.Serializable;40import java.util.Arrays;4142import jdk.testlibrary.IOUtils;4344public class LambdaClassLoaderSerialization {4546public interface SerializableRunnable extends Runnable, Serializable {}4748public static class MyCode implements SerializableRunnable {4950private byte[] serialize(Object o) {51ByteArrayOutputStream baos;52try (53ObjectOutputStream oos =54new ObjectOutputStream(baos = new ByteArrayOutputStream())55) {56oos.writeObject(o);57}58catch (IOException e) {59throw new RuntimeException(e);60}61return baos.toByteArray();62}6364private <T> T deserialize(byte[] bytes) {65try (66ObjectInputStream ois =67new ObjectInputStream(new ByteArrayInputStream(bytes))68) {69return (T) ois.readObject();70}71catch (IOException | ClassNotFoundException e) {72throw new RuntimeException(e);73}74}7576@Override77public void run() {78System.out.println(" this: " + this);7980SerializableRunnable deSerializedThis = deserialize(serialize(this));81System.out.println(" deSerializedThis: " + deSerializedThis);8283SerializableRunnable runnable = () -> {System.out.println("HELLO");};84System.out.println(" runnable: " + runnable);8586SerializableRunnable deSerializedRunnable = deserialize(serialize(runnable));87System.out.println("deSerializedRunnable: " + deSerializedRunnable);88}89}9091public static void main(String[] args) throws Exception {92ClassLoader myCl = new MyClassLoader(93LambdaClassLoaderSerialization.class.getClassLoader()94);95Class<?> myCodeClass = Class.forName(96LambdaClassLoaderSerialization.class.getName() + "$MyCode",97true,98myCl99);100Runnable myCode = (Runnable) myCodeClass.newInstance();101myCode.run();102}103104static class MyClassLoader extends ClassLoader {105MyClassLoader(ClassLoader parent) {106super(parent);107}108109@Override110protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {111if (name.indexOf('.') < 0) {112synchronized (getClassLoadingLock(name)) {113Class<?> c = findLoadedClass(name);114if (c == null) {115c = findClass(name);116}117if (resolve) {118resolveClass(c);119}120return c;121}122} else {123return super.loadClass(name, resolve);124}125}126127@Override128protected Class<?> findClass(String name) throws ClassNotFoundException {129String path = name.replace('.', '/').concat(".class");130try (InputStream is = getResourceAsStream(path)) {131if (is != null) {132byte[] bytes = IOUtils.readFully(is);133return defineClass(name, bytes, 0, bytes.length);134} else {135throw new ClassNotFoundException(name);136}137}138catch (IOException e) {139throw new ClassNotFoundException(name, e);140}141}142}143}144145146