Path: blob/master/test/jdk/java/beans/XMLEncoder/6329581/Test6329581.java
41153 views
/*1* Copyright (c) 2009, 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 632958126* @summary Tests encoding of a class with custom ClassLoader27* @author Sergey Malenkov28*/2930import java.beans.ExceptionListener;31import java.beans.XMLDecoder;32import java.beans.XMLEncoder;33import java.io.ByteArrayInputStream;34import java.io.ByteArrayOutputStream;35import java.net.URL;36import java.net.URLClassLoader;3738public class Test6329581 extends URLClassLoader implements ExceptionListener {39public static final class Bean {40}4142public static void main(String[] args) throws Exception {43new Test6329581().decode(new Test6329581().encode(Bean.class.getName()));44}4546private Test6329581() {47super(new URL[] {48Test6329581.class.getProtectionDomain().getCodeSource().getLocation()49});50}5152@Override53protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException {54Class c = findLoadedClass(name);55if (c == null) {56if (Bean.class.getName().equals(name)) {57c = findClass(name);58}59else try {60c = getParent().loadClass(name);61}62catch (ClassNotFoundException exception) {63c = findClass(name);64}65}66if (resolve) {67resolveClass(c);68}69return c;70}7172public void exceptionThrown(Exception exception) {73throw new Error("unexpected exception", exception);74}7576private void validate(Object object) {77if (!object.getClass().getClassLoader().equals(this)) {78throw new Error("Bean is loaded with unexpected class loader");79}80}8182private byte[] encode(String name) throws Exception {83Object object = loadClass(name).newInstance();84validate(object);85ByteArrayOutputStream out = new ByteArrayOutputStream();86XMLEncoder encoder = new XMLEncoder(out);87encoder.setExceptionListener(this);88encoder.writeObject(object);89encoder.close();90return out.toByteArray();91}9293private Object decode(byte[] array) {94ByteArrayInputStream in = new ByteArrayInputStream(array);95XMLDecoder decoder = new XMLDecoder(in, null, this, this);96Object object = decoder.readObject();97validate(object);98decoder.close();99return object;100}101}102103104