Path: blob/master/test/jdk/javax/management/mxbean/RandomMXBeanTest.java
41149 views
/*1* Copyright (c) 2006, 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/* This test essentially duplicates the functionality of MXBeanTest.java.24* See LeakTest.java for an explanation.25*/2627import java.lang.reflect.Field;28import java.lang.reflect.InvocationHandler;29import java.lang.reflect.InvocationTargetException;30import java.lang.reflect.Method;31import java.lang.reflect.Proxy;32import java.util.HashMap;33import java.util.Map;34import javax.management.InstanceAlreadyExistsException;35import javax.management.JMX;36import javax.management.MBeanRegistrationException;37import javax.management.MBeanServer;38import javax.management.MBeanServerFactory;39import javax.management.MalformedObjectNameException;40import javax.management.NotCompliantMBeanException;41import javax.management.ObjectName;4243public class RandomMXBeanTest {44public static interface StupidMXBean {45public int ZERO = Integer.parseInt("0");46public int getZero();47public int identity(int x);48}4950public static class StupidImpl implements StupidMXBean {51public int getZero() {52return 0;53}5455public int identity(int x) {56return x;57}58}5960public static interface ReferMXBean {61public StupidMXBean getStupid();62}6364public static class ReferImpl implements ReferMXBean {65private final StupidMXBean stupid;6667ReferImpl(StupidMXBean stupid) {68this.stupid = stupid;69}7071public StupidMXBean getStupid() {72return stupid;73}74}7576private static class WrapInvocationHandler implements InvocationHandler {77private final Object wrapped;7879WrapInvocationHandler(Object wrapped) {80this.wrapped = wrapped;81}8283public Object invoke(Object proxy, Method method, Object[] args)84throws Throwable {85return method.invoke(wrapped, args);86}87}8889private static class DullInvocationHandler implements InvocationHandler {90private static Map<Class<?>, Object> zeroMap =91new HashMap<Class<?>, Object>();92static {93zeroMap.put(byte.class, (byte) 0);94zeroMap.put(int.class, 0);95zeroMap.put(short.class, (short) 0);96zeroMap.put(long.class, 0L);97zeroMap.put(float.class, 0F);98zeroMap.put(double.class, 0.0);99zeroMap.put(boolean.class, false);100zeroMap.put(char.class, '\0');101}102103public static Object zeroFor(Class<?> c) {104if (c.isPrimitive())105return zeroMap.get(c);106else107return null;108}109110public Object invoke(Object proxy, Method method, Object[] args)111throws Throwable {112Class<?> retType = method.getReturnType();113if (!retType.isPrimitive())114return null;115return zeroMap.get(retType);116}117}118119public static void main(String[] args) throws Exception {120MBeanServer mbs = MBeanServerFactory.newMBeanServer();121ObjectName name = new ObjectName("a:b=c");122StupidMXBean stupid = new StupidImpl();123mbs.registerMBean(stupid, name);124ObjectName referName = new ObjectName("a:c=d");125mbs.registerMBean(new ReferImpl(stupid), referName);126System.out.println(mbs.getMBeanInfo(name));127StupidMXBean stupid2 = (StupidMXBean)128Proxy.newProxyInstance(StupidMXBean.class.getClassLoader(),129new Class<?>[] {StupidMXBean.class},130new WrapInvocationHandler(stupid));131ObjectName stupidName2 = new ObjectName("a:d=e");132mbs.registerMBean(stupid2, stupidName2);133Field zero = StupidMXBean.class.getField("ZERO");134System.out.println("Zero field = " + zero.get(null));135test(mbs, MerlinMXBean.class);136test(mbs, TigerMXBean.class);137138StupidMXBean proxy = JMX.newMXBeanProxy(mbs, name, StupidMXBean.class);139System.out.println("Zero = " + proxy.getZero());140System.out.println("One = " + proxy.identity(1));141ReferMXBean referProxy =142JMX.newMXBeanProxy(mbs, referName, ReferMXBean.class);143StupidMXBean stupidProxy2 = referProxy.getStupid();144System.out.println("Same proxy: " + (proxy == stupidProxy2));145Method[] methods = StupidMXBean.class.getMethods();146for (Method method : methods) {147if (method.getParameterTypes().length == 0)148method.invoke(proxy, new Object[0]);149}150}151152private static <T> void test(MBeanServer mbs, Class<T> c) throws Exception {153System.out.println("Testing " + c.getName());154T merlin = c.cast(155Proxy.newProxyInstance(c.getClassLoader(),156new Class<?>[] {c},157new DullInvocationHandler()));158ObjectName merlinName = new ObjectName("a:type=" + c.getName());159mbs.registerMBean(merlin, merlinName);160System.out.println(mbs.getMBeanInfo(merlinName));161T merlinProxy = JMX.newMXBeanProxy(mbs, merlinName, c);162Method[] merlinMethods = c.getMethods();163for (Method m : merlinMethods) {164Class<?>[] types = m.getParameterTypes();165Object[] params = new Object[types.length];166for (int i = 0; i < types.length; i++)167params[i] = DullInvocationHandler.zeroFor(types[i]);168System.out.println("Invoking " + m.getName());169m.invoke(merlinProxy, (Object[]) params);170}171}172}173174175