6250014: MBeanOperationInfo Descriptor field for exceptions
authorjfdenise
Tue, 09 Dec 2008 15:36:14 +0100
changeset 1699 3611e5fd6da5
parent 1698 2f0b565a475e
child 1700 4506662fb2ee
6250014: MBeanOperationInfo Descriptor field for exceptions Reviewed-by: emcmanus
jdk/src/share/classes/com/sun/jmx/mbeanserver/ConvertingMethod.java
jdk/src/share/classes/com/sun/jmx/mbeanserver/Introspector.java
jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanIntrospector.java
jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanIntrospector.java
jdk/src/share/classes/javax/management/Descriptor.java
jdk/src/share/classes/javax/management/JMX.java
jdk/src/share/classes/javax/management/MBeanAttributeInfo.java
jdk/src/share/classes/javax/management/MBeanConstructorInfo.java
jdk/src/share/classes/javax/management/MBeanOperationInfo.java
jdk/test/javax/management/Introspector/ExceptionsDescriptorTest.java
--- a/jdk/src/share/classes/com/sun/jmx/mbeanserver/ConvertingMethod.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/com/sun/jmx/mbeanserver/ConvertingMethod.java	Tue Dec 09 15:36:14 2008 +0100
@@ -53,7 +53,7 @@
     }
 
     Descriptor getDescriptor() {
-        return Introspector.descriptorForElement(method);
+        return Introspector.descriptorForElement(method, false);
     }
 
     Type getGenericReturnType() {
--- a/jdk/src/share/classes/com/sun/jmx/mbeanserver/Introspector.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/com/sun/jmx/mbeanserver/Introspector.java	Tue Dec 09 15:36:14 2008 +0100
@@ -63,7 +63,10 @@
 import java.beans.PropertyDescriptor;
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
+import java.util.LinkedHashSet;
+import java.util.Set;
 import javax.management.AttributeNotFoundException;
+import javax.management.JMX;
 import javax.management.openmbean.CompositeData;
 import javax.management.openmbean.MXBeanMappingFactory;
 
@@ -462,11 +465,31 @@
         return null;
     }
 
-    public static Descriptor descriptorForElement(final AnnotatedElement elmt) {
+    public static Descriptor descriptorForElement(final AnnotatedElement elmt,
+            boolean isSetter) {
         if (elmt == null)
             return ImmutableDescriptor.EMPTY_DESCRIPTOR;
         final Annotation[] annots = elmt.getAnnotations();
-        return descriptorForAnnotations(annots);
+        Descriptor descr = descriptorForAnnotations(annots);
+        String[] exceptions = {};
+        if(elmt instanceof Method)
+            exceptions = getAllExceptions(((Method) elmt).getExceptionTypes());
+        else
+            if(elmt instanceof Constructor<?>)
+                exceptions = getAllExceptions(((Constructor<?>) elmt).
+                        getExceptionTypes());
+
+        if(exceptions.length > 0 ) {
+            String fieldName = isSetter ? JMX.SET_EXCEPTIONS_FIELD :
+                JMX.EXCEPTIONS_FIELD;
+
+            String[] fieldNames = {fieldName};
+            Object[] fieldValues = {exceptions};
+            descr = ImmutableDescriptor.union(descr,
+                    new ImmutableDescriptor(fieldNames, fieldValues));
+        }
+
+        return descr;
     }
 
     public static Descriptor descriptorForAnnotation(Annotation annot) {
@@ -489,6 +512,20 @@
             return new ImmutableDescriptor(descriptorMap);
     }
 
+    /**
+     * Array of thrown excepions.
+     * @param exceptions can be null;
+     * @return An Array of Exception class names. Size is 0 if method is null.
+     */
+    private static String[] getAllExceptions(Class<?>[] exceptions) {
+        Set<String> set = new LinkedHashSet<String>();
+        for(Class<?>ex : exceptions)
+            set.add(ex.getName());
+
+        String[] arr = new String[set.size()];
+        return set.toArray(arr);
+    }
+
     private static void addDescriptorFieldsToMap(
             Map<String, Object> descriptorMap, DescriptorFields df) {
         for (String field : df.value()) {
--- a/jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanIntrospector.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanIntrospector.java	Tue Dec 09 15:36:14 2008 +0100
@@ -403,7 +403,7 @@
                     new ImmutableDescriptor(interfaceClassName);
             final Descriptor mbeanDescriptor = getBasicMBeanDescriptor();
             final Descriptor annotatedDescriptor =
-                    Introspector.descriptorForElement(mbeanInterface);
+                    Introspector.descriptorForElement(mbeanInterface, false);
             final Descriptor descriptor =
                 DescriptorCache.getInstance().union(
                     classNameDescriptor,
--- a/jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanIntrospector.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanIntrospector.java	Tue Dec 09 15:36:14 2008 +0100
@@ -292,7 +292,7 @@
         Descriptor descriptor =
             typeDescriptor(returnType, originalReturnType);
         descriptor = ImmutableDescriptor.union(descriptor,
-                Introspector.descriptorForElement(method));
+                Introspector.descriptorForElement(method, false));
         final MBeanOperationInfo oi;
         if (openReturnType && openParameterTypes) {
             /* If the return value and all the parameters can be faithfully
--- a/jdk/src/share/classes/javax/management/Descriptor.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/javax/management/Descriptor.java	Tue Dec 09 15:36:14 2008 +0100
@@ -147,6 +147,14 @@
  * might be disabled if it cannot currently be emitted but could be in
  * other circumstances.</td>
  *
+ * <tr id="exceptions"><td><i>exceptions</i><td>String[]</td>
+ * <td>MBeanAttributeInfo, MBeanConstructorInfo, MBeanOperationInfo</td>
+ *
+ * <td>The class names of the exceptions that can be thrown when invoking a
+ * constructor or operation, or getting an attribute. Exceptions thrown when
+ * setting an attribute are specified by the field
+ * <a href="#setExceptions">{@code setExceptions}</a>.
+ *
  * <tr id="immutableInfo"><td><i>immutableInfo</i><td>String</td>
  * <td>MBeanInfo</td>
  *
@@ -270,6 +278,13 @@
  * href="MXBean.html#type-names">Type Names</a> of the MXBean
  * specification.</p>
  *
+ * <tr id="setExceptions"><td><i>setExceptions</i><td>String[]</td>
+ * <td>MBeanAttributeInfo</td>
+ *
+ * <td>The class names of the exceptions that can be thrown when setting
+ * an attribute. Exceptions thrown when getting an attribute are specified
+ * by the field <a href="#exceptions">{@code exceptions}</a>.
+ *
  * <tr><td>severity</td><td>String<br>Integer</td>
  * <td>MBeanNotificationInfo</td>
  *
--- a/jdk/src/share/classes/javax/management/JMX.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/javax/management/JMX.java	Tue Dec 09 15:36:14 2008 +0100
@@ -76,6 +76,12 @@
             "descriptionResourceKey";
 
     /**
+     * The name of the <a href="Descriptor.html#exceptions">{@code
+     * exceptions}</a> field.
+     */
+    public static final String EXCEPTIONS_FIELD = "exceptions";
+
+    /**
      * The name of the <a href="Descriptor.html#immutableInfo">{@code
      * immutableInfo}</a> field.
      */
@@ -138,6 +144,12 @@
     public static final String ORIGINAL_TYPE_FIELD = "originalType";
 
     /**
+     * The name of the <a href="Descriptor.html#setExceptions">{@code
+     * setExceptions}</a> field.
+     */
+    public static final String SET_EXCEPTIONS_FIELD = "setExceptions";
+
+    /**
      * <p>Options to apply to an MBean proxy or to an instance of {@link
      * StandardMBean}.</p>
      *
--- a/jdk/src/share/classes/javax/management/MBeanAttributeInfo.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/javax/management/MBeanAttributeInfo.java	Tue Dec 09 15:36:14 2008 +0100
@@ -186,8 +186,10 @@
              (getter != null),
              (setter != null),
              isIs(getter),
-             ImmutableDescriptor.union(Introspector.descriptorForElement(getter),
-                                   Introspector.descriptorForElement(setter)));
+             ImmutableDescriptor.union(Introspector.
+             descriptorForElement(getter, false),
+                                   Introspector.descriptorForElement(setter,
+                                   true)));
     }
 
     /**
--- a/jdk/src/share/classes/javax/management/MBeanConstructorInfo.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/javax/management/MBeanConstructorInfo.java	Tue Dec 09 15:36:14 2008 +0100
@@ -67,7 +67,7 @@
     public MBeanConstructorInfo(String description, Constructor<?> constructor) {
         this(constructor.getName(), description,
              constructorSignature(constructor),
-             Introspector.descriptorForElement(constructor));
+             Introspector.descriptorForElement(constructor, false));
     }
 
     /**
--- a/jdk/src/share/classes/javax/management/MBeanOperationInfo.java	Tue Dec 09 14:44:42 2008 +0100
+++ b/jdk/src/share/classes/javax/management/MBeanOperationInfo.java	Tue Dec 09 15:36:14 2008 +0100
@@ -113,7 +113,7 @@
              methodSignature(method),
              method.getReturnType().getName(),
              UNKNOWN,
-             Introspector.descriptorForElement(method));
+             Introspector.descriptorForElement(method, false));
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/management/Introspector/ExceptionsDescriptorTest.java	Tue Dec 09 15:36:14 2008 +0100
@@ -0,0 +1,245 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test %M% %I%
+ * @bug 6250014
+ * @summary Test that Exceptions are added to the MbeanInfo
+ * @author Jean-Francois Denise
+ * @run main/othervm ExceptionsDescriptorTest
+ */
+import java.lang.management.ManagementFactory;
+import java.util.HashSet;
+import java.util.Set;
+import javax.management.Descriptor;
+import javax.management.JMX;
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanConstructorInfo;
+import javax.management.MBeanInfo;
+import javax.management.MBeanOperationInfo;
+import javax.management.ObjectName;
+
+public class ExceptionsDescriptorTest {
+
+    private static final ObjectName OBJECT_NAME = ObjectName.valueOf(":type=Foo");
+    final static String EXCEPTION_NAME = Exception.class.getName();
+    final static String ILLEGAL_ARGUMENT_EXCEPTION_NAME =
+            IllegalArgumentException.class.getName();
+    final static Set<String> ONE_EXCEPTION = new HashSet<String>();
+    final static Set<String> TWO_EXCEPTION = new HashSet<String>();
+    static {
+        ONE_EXCEPTION.add(EXCEPTION_NAME);
+        TWO_EXCEPTION.add(EXCEPTION_NAME);
+        TWO_EXCEPTION.add(ILLEGAL_ARGUMENT_EXCEPTION_NAME);
+    }
+    public interface TestMBean {
+
+        public void doIt();
+
+        public void doIt(String str) throws Exception;
+
+        public void doIt(String str, boolean b) throws Exception,
+                IllegalArgumentException;
+
+        public String getThat();
+
+        public void setThat(String that);
+
+        public String getThe() throws Exception;
+
+        public void setThe(String the);
+
+        public String getThese();
+
+        public void setThese(String the) throws Exception;
+
+        public String getIt() throws Exception;
+
+        public void setIt(String str) throws Exception;
+
+        public String getThis() throws Exception, IllegalArgumentException;
+
+        public void setThose(String str) throws Exception,
+                IllegalArgumentException;
+    }
+
+    public static class Test implements TestMBean {
+
+        public Test() {
+        }
+
+        public Test(int i) throws Exception {
+        }
+
+        public Test(int i, int j) throws Exception, IllegalArgumentException {
+        }
+
+        public void doIt() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void doIt(String str) throws Exception {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void doIt(String str, boolean b) throws Exception, IllegalArgumentException {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public String getThat() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void setThat(String that) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public String getThe() throws Exception {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void setThe(String the) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public String getThese() {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void setThese(String the) throws Exception {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public String getIt() throws Exception {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void setIt(String str) throws Exception {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public String getThis() throws Exception, IllegalArgumentException {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+
+        public void setThose(String str) throws Exception, IllegalArgumentException {
+            throw new UnsupportedOperationException("Not supported yet.");
+        }
+    }
+
+    private static void check(Descriptor d,
+            Set<String> exceptionsExpectedValue,
+            boolean exceptionsExpected,
+            Set<String> setExceptionsExpectedValue,
+            boolean setExceptionsExpected) throws Exception {
+        String[] exceptionsValues = (String[]) d.getFieldValue(JMX.EXCEPTIONS_FIELD);
+        String[] setExceptionsValues = (String[]) d.getFieldValue(JMX.SET_EXCEPTIONS_FIELD);
+
+        if (exceptionsExpected && exceptionsValues == null) {
+            throw new Exception("exceptions is expected but null value");
+        }
+        if (!exceptionsExpected && exceptionsValues != null) {
+            throw new Exception("exceptions is not expected but non null value");
+        }
+        if (setExceptionsExpected && setExceptionsValues == null) {
+            throw new Exception("setExceptions is expected but null value");
+        }
+        if (!setExceptionsExpected && setExceptionsValues != null) {
+            throw new Exception("setExceptions is not expected but non null value");
+        }
+
+        if (exceptionsExpected) {
+            checkValues(exceptionsExpectedValue, exceptionsValues);
+        }
+        if (setExceptionsExpected) {
+            checkValues(setExceptionsExpectedValue, setExceptionsValues);
+        }
+    }
+
+    private static void checkValues(Set<String> expectedValuesSet,
+            String[] realValues) throws Exception {
+
+        Set<String> realValuesSet = new HashSet<String>();
+        for (String ex : realValues) {
+            realValuesSet.add(ex);
+        }
+        if (!realValuesSet.equals(expectedValuesSet)) {
+            throw new Exception("Invalid content for exceptions. Was expecting " +
+                    expectedValuesSet + ". Found " + realValuesSet);
+        }
+    }
+
+    public static void main(String[] args) throws Exception {
+        Test t = new Test();
+        ManagementFactory.getPlatformMBeanServer().registerMBean(t, OBJECT_NAME);
+        MBeanInfo info = ManagementFactory.getPlatformMBeanServer().
+                getMBeanInfo(OBJECT_NAME);
+        //Constructors
+        for (MBeanConstructorInfo ctr : info.getConstructors()) {
+            if (ctr.getSignature().length == 0) {
+                check(ctr.getDescriptor(), null, false, null, false);
+            }
+            if (ctr.getSignature().length == 1) {
+                check(ctr.getDescriptor(), ONE_EXCEPTION, true, null, false);
+            }
+            if (ctr.getSignature().length == 2) {
+                check(ctr.getDescriptor(),TWO_EXCEPTION,true, null, false);
+            }
+        }
+        //Attributes
+        for (MBeanAttributeInfo attr : info.getAttributes()) {
+            if (attr.getName().equals("That")) {
+                check(attr.getDescriptor(), null, false, null, false);
+            }
+            if (attr.getName().equals("The")) {
+                check(attr.getDescriptor(), ONE_EXCEPTION,true,null, false);
+            }
+            if (attr.getName().equals("These")) {
+                check(attr.getDescriptor(), null, false, ONE_EXCEPTION,true);
+            }
+            if (attr.getName().equals("It")) {
+                check(attr.getDescriptor(), ONE_EXCEPTION,true,ONE_EXCEPTION,
+                        true);
+            }
+            if (attr.getName().equals("This")) {
+                check(attr.getDescriptor(), TWO_EXCEPTION,true,null,false);
+            }
+            if (attr.getName().equals("Those")) {
+                check(attr.getDescriptor(), null,false,TWO_EXCEPTION,true);
+            }
+        }
+        //Operations
+        for (MBeanOperationInfo oper : info.getOperations()) {
+            if (oper.getSignature().length == 0) {
+                check(oper.getDescriptor(), null, false, null, false);
+            }
+            if (oper.getSignature().length == 1) {
+                check(oper.getDescriptor(), ONE_EXCEPTION, true, null, false);
+            }
+            if (oper.getSignature().length == 2) {
+                check(oper.getDescriptor(), TWO_EXCEPTION,true, null, false);
+            }
+        }
+        System.out.println("Test passed");
+    }
+}