Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/loading/ArrayClassTest.java
38838 views
/*1* Copyright (c) 2004, 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 497491326* @summary Test that array classes can be found in signatures always27* and can be deserialized by the deprecated MBeanServer.deserialize method28* @author Eamonn McManus29* @run clean ArrayClassTest30* @run build ArrayClassTest31* @run main ArrayClassTest32*/3334import java.io.*;35import java.lang.reflect.*;36import java.net.*;37import javax.management.*;38import javax.management.loading.*;3940public class ArrayClassTest {41public static void main(String[] args) throws Exception {42MBeanServer mbs = MBeanServerFactory.createMBeanServer();4344/* If this test isn't loaded by a URLClassLoader we will get45a ClassCastException here, which is good because it means46this test isn't valid. */47URLClassLoader testLoader =48(URLClassLoader) ArrayClassTest.class.getClassLoader();4950// Create an MLet that can load the same class names but51// will produce different results.52ClassLoader loader = new SpyLoader(testLoader.getURLs());53ObjectName loaderName = new ObjectName("test:type=SpyLoader");54mbs.registerMBean(loader, loaderName);5556ObjectName testName = new ObjectName("test:type=Test");57mbs.createMBean(Test.class.getName(), testName, loaderName,58new Object[1], new String[] {X[].class.getName()});59ClassLoader checkLoader = mbs.getClassLoaderFor(testName);60if (checkLoader != loader)61throw new AssertionError("Wrong loader: " + checkLoader);6263mbs.invoke(testName, "ignore", new Object[1],64new String[] {Y[].class.getName()});6566ByteArrayOutputStream bout = new ByteArrayOutputStream();67ObjectOutputStream oout = new ObjectOutputStream(bout);68oout.writeObject(new Z[0]);69oout.close();70byte[] bytes = bout.toByteArray();71ObjectInputStream oin = mbs.deserialize(testName, bytes);72Object zarray = oin.readObject();73String failed = null;74if (zarray instanceof Z[])75failed = "read back a real Z[]";76else if (!zarray.getClass().getName().equals(Z[].class.getName())) {77failed = "returned object of wrong type: " +78zarray.getClass().getName();79} else if (Array.getLength(zarray) != 0)80failed = "returned array of wrong size: " + Array.getLength(zarray);81if (failed != null) {82System.out.println("TEST FAILED: " + failed);83System.exit(1);84}8586System.out.println("Test passed");87}8889public static interface TestMBean {90public void ignore(Y[] ignored);91}9293public static class Test implements TestMBean {94public Test(X[] ignored) {}95public void ignore(Y[] ignored) {}96}9798public static class X {}99public static class Y {}100public static class Z implements Serializable {}101102public static interface SpyLoaderMBean {}103104/* We originally had this extend MLet but for some reason that105stopped the bug from happening. Some side-effect of registering106the MLet in the MBean server caused it not to fail when asked107to load Z[]. */108public static class SpyLoader extends URLClassLoader109implements SpyLoaderMBean, PrivateClassLoader {110public SpyLoader(URL[] urls) {111// important that the parent classloader be null!112// otherwise we can pick up classes from the classpath113super(urls, null);114}115116/*117public Class loadClass(String name) throws ClassNotFoundException {118System.out.println("loadClass: " + name);119return super.loadClass(name);120}121122public Class loadClass(String name, boolean resolve)123throws ClassNotFoundException {124System.out.println("loadClass: " + name + ", " + resolve);125return super.loadClass(name, resolve);126}127*/128129public Class findClass(String name) throws ClassNotFoundException {130System.out.println("findClass: " + name);131if (false)132new Throwable().printStackTrace(System.out);133Class c = super.findClass(name);134System.out.println(" -> " + name + " (" + c.getClassLoader() + ")");135return c;136}137}138}139140141