0N/A/*
2362N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4974913
0N/A * @summary Test that array classes can be found in signatures always
0N/A * and can be deserialized by the deprecated MBeanServer.deserialize method
0N/A * @author Eamonn McManus
0N/A * @run clean ArrayClassTest
0N/A * @run build ArrayClassTest
0N/A * @run main ArrayClassTest
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.lang.reflect.*;
0N/Aimport java.net.*;
0N/Aimport javax.management.*;
0N/Aimport javax.management.loading.*;
0N/A
0N/Apublic class ArrayClassTest {
0N/A public static void main(String[] args) throws Exception {
0N/A MBeanServer mbs = MBeanServerFactory.createMBeanServer();
0N/A
0N/A /* If this test isn't loaded by a URLClassLoader we will get
0N/A a ClassCastException here, which is good because it means
0N/A this test isn't valid. */
0N/A URLClassLoader testLoader =
0N/A (URLClassLoader) ArrayClassTest.class.getClassLoader();
0N/A
0N/A // Create an MLet that can load the same class names but
0N/A // will produce different results.
0N/A ClassLoader loader = new SpyLoader(testLoader.getURLs());
0N/A ObjectName loaderName = new ObjectName("test:type=SpyLoader");
0N/A mbs.registerMBean(loader, loaderName);
0N/A
0N/A ObjectName testName = new ObjectName("test:type=Test");
0N/A mbs.createMBean(Test.class.getName(), testName, loaderName,
0N/A new Object[1], new String[] {X[].class.getName()});
0N/A ClassLoader checkLoader = mbs.getClassLoaderFor(testName);
0N/A if (checkLoader != loader)
0N/A throw new AssertionError("Wrong loader: " + checkLoader);
0N/A
0N/A mbs.invoke(testName, "ignore", new Object[1],
0N/A new String[] {Y[].class.getName()});
0N/A
0N/A ByteArrayOutputStream bout = new ByteArrayOutputStream();
0N/A ObjectOutputStream oout = new ObjectOutputStream(bout);
0N/A oout.writeObject(new Z[0]);
0N/A oout.close();
0N/A byte[] bytes = bout.toByteArray();
0N/A ObjectInputStream oin = mbs.deserialize(testName, bytes);
0N/A Object zarray = oin.readObject();
0N/A String failed = null;
0N/A if (zarray instanceof Z[])
0N/A failed = "read back a real Z[]";
0N/A else if (!zarray.getClass().getName().equals(Z[].class.getName())) {
0N/A failed = "returned object of wrong type: " +
0N/A zarray.getClass().getName();
0N/A } else if (Array.getLength(zarray) != 0)
0N/A failed = "returned array of wrong size: " + Array.getLength(zarray);
0N/A if (failed != null) {
0N/A System.out.println("TEST FAILED: " + failed);
0N/A System.exit(1);
0N/A }
0N/A
0N/A System.out.println("Test passed");
0N/A }
0N/A
0N/A public static interface TestMBean {
0N/A public void ignore(Y[] ignored);
0N/A }
0N/A
0N/A public static class Test implements TestMBean {
0N/A public Test(X[] ignored) {}
0N/A public void ignore(Y[] ignored) {}
0N/A }
0N/A
0N/A public static class X {}
0N/A public static class Y {}
0N/A public static class Z implements Serializable {}
0N/A
0N/A public static interface SpyLoaderMBean {}
0N/A
0N/A /* We originally had this extend MLet but for some reason that
0N/A stopped the bug from happening. Some side-effect of registering
0N/A the MLet in the MBean server caused it not to fail when asked
0N/A to load Z[]. */
0N/A public static class SpyLoader extends URLClassLoader
0N/A implements SpyLoaderMBean, PrivateClassLoader {
0N/A public SpyLoader(URL[] urls) {
0N/A // important that the parent classloader be null!
0N/A // otherwise we can pick up classes from the classpath
0N/A super(urls, null);
0N/A }
0N/A
0N/A /*
0N/A public Class loadClass(String name) throws ClassNotFoundException {
0N/A System.out.println("loadClass: " + name);
0N/A return super.loadClass(name);
0N/A }
0N/A
0N/A public Class loadClass(String name, boolean resolve)
0N/A throws ClassNotFoundException {
0N/A System.out.println("loadClass: " + name + ", " + resolve);
0N/A return super.loadClass(name, resolve);
0N/A }
0N/A */
0N/A
0N/A public Class findClass(String name) throws ClassNotFoundException {
0N/A System.out.println("findClass: " + name);
0N/A if (false)
0N/A new Throwable().printStackTrace(System.out);
0N/A Class c = super.findClass(name);
0N/A System.out.println(" -> " + name + " (" + c.getClassLoader() + ")");
0N/A return c;
0N/A }
0N/A }
0N/A}