0N/A/*
2362N/A * Copyright (c) 2006, 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/* This test essentially duplicates the functionality of MXBeanTest.java.
0N/A * See LeakTest.java for an explanation.
0N/A */
0N/A
0N/Aimport java.lang.reflect.Field;
0N/Aimport java.lang.reflect.InvocationHandler;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.lang.reflect.Proxy;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.Map;
0N/Aimport javax.management.InstanceAlreadyExistsException;
0N/Aimport javax.management.JMX;
0N/Aimport javax.management.MBeanRegistrationException;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerFactory;
0N/Aimport javax.management.MalformedObjectNameException;
0N/Aimport javax.management.NotCompliantMBeanException;
0N/Aimport javax.management.ObjectName;
0N/A
0N/Apublic class RandomMXBeanTest {
0N/A public static interface StupidMXBean {
0N/A public int ZERO = Integer.parseInt("0");
0N/A public int getZero();
0N/A public int identity(int x);
0N/A }
0N/A
0N/A public static class StupidImpl implements StupidMXBean {
0N/A public int getZero() {
0N/A return 0;
0N/A }
0N/A
0N/A public int identity(int x) {
0N/A return x;
0N/A }
0N/A }
0N/A
0N/A public static interface ReferMXBean {
0N/A public StupidMXBean getStupid();
0N/A }
0N/A
0N/A public static class ReferImpl implements ReferMXBean {
0N/A private final StupidMXBean stupid;
0N/A
0N/A ReferImpl(StupidMXBean stupid) {
0N/A this.stupid = stupid;
0N/A }
0N/A
0N/A public StupidMXBean getStupid() {
0N/A return stupid;
0N/A }
0N/A }
0N/A
0N/A private static class WrapInvocationHandler implements InvocationHandler {
0N/A private final Object wrapped;
0N/A
0N/A WrapInvocationHandler(Object wrapped) {
0N/A this.wrapped = wrapped;
0N/A }
0N/A
0N/A public Object invoke(Object proxy, Method method, Object[] args)
0N/A throws Throwable {
0N/A return method.invoke(wrapped, args);
0N/A }
0N/A }
0N/A
0N/A private static class DullInvocationHandler implements InvocationHandler {
0N/A private static Map<Class<?>, Object> zeroMap =
0N/A new HashMap<Class<?>, Object>();
0N/A static {
0N/A zeroMap.put(byte.class, (byte) 0);
0N/A zeroMap.put(int.class, 0);
0N/A zeroMap.put(short.class, (short) 0);
0N/A zeroMap.put(long.class, 0L);
0N/A zeroMap.put(float.class, 0F);
0N/A zeroMap.put(double.class, 0.0);
0N/A zeroMap.put(boolean.class, false);
0N/A zeroMap.put(char.class, '\0');
0N/A }
0N/A
0N/A public static Object zeroFor(Class<?> c) {
0N/A if (c.isPrimitive())
0N/A return zeroMap.get(c);
0N/A else
0N/A return null;
0N/A }
0N/A
0N/A public Object invoke(Object proxy, Method method, Object[] args)
0N/A throws Throwable {
0N/A Class<?> retType = method.getReturnType();
0N/A if (!retType.isPrimitive())
0N/A return null;
0N/A return zeroMap.get(retType);
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
0N/A ObjectName name = new ObjectName("a:b=c");
0N/A StupidMXBean stupid = new StupidImpl();
0N/A mbs.registerMBean(stupid, name);
0N/A ObjectName referName = new ObjectName("a:c=d");
0N/A mbs.registerMBean(new ReferImpl(stupid), referName);
0N/A System.out.println(mbs.getMBeanInfo(name));
0N/A StupidMXBean stupid2 = (StupidMXBean)
0N/A Proxy.newProxyInstance(StupidMXBean.class.getClassLoader(),
0N/A new Class<?>[] {StupidMXBean.class},
0N/A new WrapInvocationHandler(stupid));
0N/A ObjectName stupidName2 = new ObjectName("a:d=e");
0N/A mbs.registerMBean(stupid2, stupidName2);
0N/A Field zero = StupidMXBean.class.getField("ZERO");
0N/A System.out.println("Zero field = " + zero.get(null));
0N/A test(mbs, MerlinMXBean.class);
0N/A test(mbs, TigerMXBean.class);
0N/A
0N/A StupidMXBean proxy = JMX.newMXBeanProxy(mbs, name, StupidMXBean.class);
0N/A System.out.println("Zero = " + proxy.getZero());
0N/A System.out.println("One = " + proxy.identity(1));
0N/A ReferMXBean referProxy =
0N/A JMX.newMXBeanProxy(mbs, referName, ReferMXBean.class);
0N/A StupidMXBean stupidProxy2 = referProxy.getStupid();
0N/A System.out.println("Same proxy: " + (proxy == stupidProxy2));
0N/A Method[] methods = StupidMXBean.class.getMethods();
0N/A for (Method method : methods) {
0N/A if (method.getParameterTypes().length == 0)
0N/A method.invoke(proxy, new Object[0]);
0N/A }
0N/A }
0N/A
0N/A private static <T> void test(MBeanServer mbs, Class<T> c) throws Exception {
0N/A System.out.println("Testing " + c.getName());
0N/A T merlin = c.cast(
0N/A Proxy.newProxyInstance(c.getClassLoader(),
0N/A new Class<?>[] {c},
0N/A new DullInvocationHandler()));
0N/A ObjectName merlinName = new ObjectName("a:type=" + c.getName());
0N/A mbs.registerMBean(merlin, merlinName);
0N/A System.out.println(mbs.getMBeanInfo(merlinName));
0N/A T merlinProxy = JMX.newMXBeanProxy(mbs, merlinName, c);
0N/A Method[] merlinMethods = c.getMethods();
0N/A for (Method m : merlinMethods) {
0N/A Class<?>[] types = m.getParameterTypes();
0N/A Object[] params = new Object[types.length];
0N/A for (int i = 0; i < types.length; i++)
0N/A params[i] = DullInvocationHandler.zeroFor(types[i]);
0N/A System.out.println("Invoking " + m.getName());
0N/A m.invoke(merlinProxy, (Object[]) params);
0N/A }
0N/A }
0N/A}