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