jdk/src/share/classes/javax/management/MBeanAttributeInfo.java
author jfdenise
Tue, 09 Dec 2008 15:36:14 +0100
changeset 1699 3611e5fd6da5
parent 1510 e747d3193ef2
child 1714 3eeb90939fe7
permissions -rw-r--r--
6250014: MBeanOperationInfo Descriptor field for exceptions Reviewed-by: emcmanus
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
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 java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.jmx.mbeanserver.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.jmx.mbeanserver.Introspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Describes an MBean attribute exposed for management.  Instances of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * this class are immutable.  Subclasses may be mutable but this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * not recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
@SuppressWarnings("serial")  // serialVersionUID not constant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class MBeanAttributeInfo extends MBeanFeatureInfo implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /* Serial version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final long serialVersionUID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        /* For complicated reasons, the serialVersionUID changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
           between JMX 1.0 and JMX 1.1, even though JMX 1.1 did not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
           have compatibility code for this class.  So the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
           serialization produced by this class with JMX 1.2 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
           jmx.serial.form=1.0 is not the same as that produced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
           this class with JMX 1.1 and jmx.serial.form=1.0.  However,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
           the serialization without that property is the same, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
           that is the only form required by JMX 1.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        long uid = 8644704819898565848L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            String form = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            if ("1.0".equals(form))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                uid = 7043855487133450673L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            // OK: exception means no compat with 1.0, too bad
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        serialVersionUID = uid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static final MBeanAttributeInfo[] NO_ATTRIBUTES =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        new MBeanAttributeInfo[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @serial The actual attribute type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private final String attributeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @serial The attribute write right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final boolean isWrite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @serial The attribute read right.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final boolean isRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @serial Indicates if this method is a "is"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private final boolean is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Constructs an <CODE>MBeanAttributeInfo</CODE> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param name The name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param type The type or class name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param description A human readable description of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param isReadable True if the attribute has a getter method, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param isWritable True if the attribute has a setter method, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param isIs True if this attribute has an "is" getter, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @throws IllegalArgumentException if {@code isIs} is true but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * {@code isReadable} is not, or if {@code isIs} is true and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * {@code type} is not {@code boolean} or {@code java.lang.Boolean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * (New code should always use {@code boolean} rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * {@code java.lang.Boolean}.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public MBeanAttributeInfo(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                              String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                              String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                              boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                              boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                              boolean isIs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        this(name, type, description, isReadable, isWritable, isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
             (Descriptor) null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Constructs an <CODE>MBeanAttributeInfo</CODE> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param name The name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param type The type or class name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param description A human readable description of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param isReadable True if the attribute has a getter method, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param isWritable True if the attribute has a setter method, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @param isIs True if this attribute has an "is" getter, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param descriptor The descriptor for the attribute.  This may be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * which is equivalent to an empty descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @throws IllegalArgumentException if {@code isIs} is true but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * {@code isReadable} is not, or if {@code isIs} is true and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * {@code type} is not {@code boolean} or {@code java.lang.Boolean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * (New code should always use {@code boolean} rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * {@code java.lang.Boolean}.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public MBeanAttributeInfo(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                              String type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                              String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                              boolean isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                              boolean isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                              boolean isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                              Descriptor descriptor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        super(name, description, descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        this.attributeType = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        this.isRead = isReadable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        this.isWrite = isWritable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (isIs && !isReadable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IllegalArgumentException("Cannot have an \"is\" getter " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                               "for a non-readable attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (isIs && !type.equals("java.lang.Boolean") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                !type.equals("boolean")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalArgumentException("Cannot have an \"is\" getter " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                               "for a non-boolean attribute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.is = isIs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p>This constructor takes the name of a simple attribute, and Method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * objects for reading and writing the attribute.  The {@link Descriptor}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * of the constructed object will include fields contributed by any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * annotations on the {@code Method} objects that contain the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * {@link DescriptorKey} meta-annotation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @param name The programmatic name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param description A human readable description of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @param getter The method used for reading the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *          May be null if the property is write-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param setter The method used for writing the attribute value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *          May be null if the attribute is read-only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @exception IntrospectionException There is a consistency
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * problem in the definition of this attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public MBeanAttributeInfo(String name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                              String description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                              Method getter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                              Method setter) throws IntrospectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        this(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
             attributeType(getter, setter),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
             description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
             (getter != null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
             (setter != null),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
             isIs(getter),
1699
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1510
diff changeset
   189
             ImmutableDescriptor.union(Introspector.
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1510
diff changeset
   190
             descriptorForElement(getter, false),
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1510
diff changeset
   191
                                   Introspector.descriptorForElement(setter,
3611e5fd6da5 6250014: MBeanOperationInfo Descriptor field for exceptions
jfdenise
parents: 1510
diff changeset
   192
                                   true)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p>Returns a shallow clone of this instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * The clone is obtained by simply calling <tt>super.clone()</tt>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * thus calling the default native shallow cloning mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * implemented by <tt>Object.clone()</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * No deeper cloning of any internal field is made.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p>Since this class is immutable, cloning is chiefly of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * interest to subclasses.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     public Object clone () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
             return super.clone() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
             // should not happen as this class is cloneable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
             return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * Returns the class name of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @return the class name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public String getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return attributeType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Whether the value of the attribute can be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return True if the attribute can be read, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    public boolean isReadable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return isRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Whether new values can be written to the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return True if the attribute can be written to, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public boolean isWritable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return isWrite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Indicates if this attribute has an "is" getter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @return true if this attribute has an "is" getter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public boolean isIs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        String access;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (isReadable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (isWritable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                access = "read/write";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                access = "read-only";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else if (isWritable())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            access = "write-only";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            access = "no-access";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            getClass().getName() + "[" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            "description=" + getDescription() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            "name=" + getName() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            "type=" + getType() + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            access + ", " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            (isIs() ? "isIs, " : "") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            "descriptor=" + getDescriptor() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Compare this MBeanAttributeInfo to another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @param o the object to compare to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @return true if and only if <code>o</code> is an MBeanAttributeInfo such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * that its {@link #getName()}, {@link #getType()}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * #getDescription()}, {@link #isReadable()}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * #isWritable()}, and {@link #isIs()} values are equal (not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * necessarily identical) to those of this MBeanAttributeInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public boolean equals(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (o == this)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        if (!(o instanceof MBeanAttributeInfo))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        MBeanAttributeInfo p = (MBeanAttributeInfo) o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return (p.getName().equals(getName()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                p.getType().equals(getType()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                p.getDescription().equals(getDescription()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                p.getDescriptor().equals(getDescriptor()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                p.isReadable() == isReadable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                p.isWritable() == isWritable() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                p.isIs() == isIs());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /* We do not include everything in the hashcode.  We assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
       if two operations are different they'll probably have different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
       names or types.  The penalty we pay when this assumption is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
       wrong should be less than the penalty we would pay if it were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
       right and we needlessly hashed in the description and parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
       array.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return getName().hashCode() ^ getType().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private static boolean isIs(Method getter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return (getter != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                getter.getName().startsWith("is") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                (getter.getReturnType().equals(Boolean.TYPE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                 getter.getReturnType().equals(Boolean.class)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * Finds the type of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static String attributeType(Method getter, Method setter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throws IntrospectionException {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   321
        Class<?> type = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (getter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (getter.getParameterTypes().length != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                throw new IntrospectionException("bad getter arg count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            type = getter.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if (type == Void.TYPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                throw new IntrospectionException("getter " + getter.getName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                                 " returns void");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (setter != null) {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 2
diff changeset
   335
            Class<?> params[] = setter.getParameterTypes();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (params.length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                throw new IntrospectionException("bad setter arg count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (type == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                type = params[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            else if (type != params[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                throw new IntrospectionException("type mismatch between " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                                 "getter and setter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (type == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throw new IntrospectionException("getter and setter cannot " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                             "both be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return type.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
}