jdk/src/share/classes/javax/management/MBeanConstructorInfo.java
author emcmanus
Wed, 21 Oct 2009 17:33:18 +0200
changeset 4156 acaa49a2768a
parent 1714 3eeb90939fe7
child 5506 202f599c92aa
permissions -rw-r--r--
6851617: Remove JSR 255 (JMX API 2.0) from JDK 7 Summary: See http://weblogs.java.net/blog/2009/06/16/jsr-255-jmx-api-20-postponed Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1510
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.mbeanserver.Introspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.annotation.Annotation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Describes a constructor exposed by an MBean.  Instances of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * class are immutable.  Subclasses may be mutable but this is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class MBeanConstructorInfo extends MBeanFeatureInfo implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static final long serialVersionUID = 4433990064191844427L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static final MBeanConstructorInfo[] NO_CONSTRUCTORS =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        new MBeanConstructorInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** @see MBeanInfo#arrayGettersSafe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private final transient boolean arrayGettersSafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @serial The signature of the method, that is, the class names of the arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final MBeanParameterInfo[] signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Constructs an <CODE>MBeanConstructorInfo</CODE> object.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * {@link Descriptor} of the constructed object will include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * fields contributed by any annotations on the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Constructor} object that contain the {@link DescriptorKey}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * meta-annotation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param description A human readable description of the operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param constructor The <CODE>java.lang.reflect.Constructor</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * object describing the MBean constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
    67
    public MBeanConstructorInfo(String description, Constructor<?> constructor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        this(constructor.getName(), description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
             constructorSignature(constructor),
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1714
diff changeset
    70
             Introspector.descriptorForElement(constructor));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Constructs an <CODE>MBeanConstructorInfo</CODE> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param name The name of the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param signature <CODE>MBeanParameterInfo</CODE> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * describing the parameters(arguments) of the constructor.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * may be null with the same effect as a zero-length array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param description A human readable description of the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public MBeanConstructorInfo(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                MBeanParameterInfo[] signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this(name, description, signature, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Constructs an <CODE>MBeanConstructorInfo</CODE> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param name The name of the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param signature <CODE>MBeanParameterInfo</CODE> objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * describing the parameters(arguments) of the constructor.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * may be null with the same effect as a zero-length array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param description A human readable description of the constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param descriptor The descriptor for the constructor.  This may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * which is equivalent to an empty descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public MBeanConstructorInfo(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                                String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                MBeanParameterInfo[] signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                Descriptor descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        super(name, description, descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (signature == null || signature.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            signature = MBeanParameterInfo.NO_PARAMS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            signature = signature.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        this.signature = signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        this.arrayGettersSafe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            MBeanInfo.arrayGettersSafe(this.getClass(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                       MBeanConstructorInfo.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <p>Returns a shallow clone of this instance.  The clone is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * obtained by simply calling <tt>super.clone()</tt>, thus calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * the default native shallow cloning mechanism implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * <tt>Object.clone()</tt>.  No deeper cloning of any internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * field is made.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <p>Since this class is immutable, cloning is chiefly of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * interest to subclasses.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     public Object clone () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
             return super.clone() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
             // should not happen as this class is cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
             return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <p>Returns the list of parameters for this constructor.  Each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * parameter is described by an <CODE>MBeanParameterInfo</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * object.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p>The returned array is a shallow copy of the internal array,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * which means that it is a copy of the internal array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * references to the <CODE>MBeanParameterInfo</CODE> objects but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * that each referenced <CODE>MBeanParameterInfo</CODE> object is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * not copied.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @return  An array of <CODE>MBeanParameterInfo</CODE> objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public MBeanParameterInfo[] getSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (signature.length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return signature.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private MBeanParameterInfo[] fastGetSignature() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (arrayGettersSafe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            return getSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            getClass().getName() + "[" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            "description=" + getDescription() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            "name=" + getName() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            "signature=" + Arrays.asList(fastGetSignature()) + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            "descriptor=" + getDescriptor() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Compare this MBeanConstructorInfo to another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param o the object to compare to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return true if and only if <code>o</code> is an MBeanConstructorInfo such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * that its {@link #getName()}, {@link #getDescription()},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * {@link #getSignature()}, and {@link #getDescriptor()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * values are equal (not necessarily
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * identical) to those of this MBeanConstructorInfo.  Two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * signature arrays are equal if their elements are pairwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * equal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (!(o instanceof MBeanConstructorInfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        MBeanConstructorInfo p = (MBeanConstructorInfo) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        return (p.getName().equals(getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                p.getDescription().equals(getDescription()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                Arrays.equals(p.fastGetSignature(), fastGetSignature()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                p.getDescriptor().equals(getDescriptor()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /* Unlike attributes and operations, it's quite likely we'll have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
       more than one constructor with the same name and even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
       description, so we include the parameter array in the hashcode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
       We don't include the description, though, because it could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
       quite long and yet the same between constructors.  Likewise for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
       the descriptor.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        int hash = getName().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        MBeanParameterInfo[] sig = fastGetSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        for (int i = 0; i < sig.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            hash ^= sig[i].hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   213
    private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   214
        final Class<?>[] classes = cn.getParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        final Annotation[][] annots = cn.getParameterAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return MBeanOperationInfo.parameters(classes, annots);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
}