0N/A/*
2362N/A * Copyright (c) 1999, 2008, 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
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
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/Apackage javax.management;
0N/A
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.security.AccessController;
0N/A
0N/Aimport com.sun.jmx.mbeanserver.GetPropertyAction;
0N/Aimport com.sun.jmx.mbeanserver.Introspector;
0N/A
0N/A
0N/A/**
0N/A * Describes an MBean attribute exposed for management. Instances of
0N/A * this class are immutable. Subclasses may be mutable but this is
0N/A * not recommended.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A@SuppressWarnings("serial") // serialVersionUID not constant
0N/Apublic class MBeanAttributeInfo extends MBeanFeatureInfo implements Cloneable {
0N/A
0N/A /* Serial version */
0N/A private static final long serialVersionUID;
0N/A static {
0N/A /* For complicated reasons, the serialVersionUID changed
0N/A between JMX 1.0 and JMX 1.1, even though JMX 1.1 did not
0N/A have compatibility code for this class. So the
0N/A serialization produced by this class with JMX 1.2 and
0N/A jmx.serial.form=1.0 is not the same as that produced by
0N/A this class with JMX 1.1 and jmx.serial.form=1.0. However,
0N/A the serialization without that property is the same, and
0N/A that is the only form required by JMX 1.2.
0N/A */
0N/A long uid = 8644704819898565848L;
0N/A try {
0N/A GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
0N/A String form = AccessController.doPrivileged(act);
0N/A if ("1.0".equals(form))
0N/A uid = 7043855487133450673L;
0N/A } catch (Exception e) {
0N/A // OK: exception means no compat with 1.0, too bad
0N/A }
0N/A serialVersionUID = uid;
0N/A }
0N/A
0N/A static final MBeanAttributeInfo[] NO_ATTRIBUTES =
0N/A new MBeanAttributeInfo[0];
0N/A
0N/A /**
0N/A * @serial The actual attribute type.
0N/A */
0N/A private final String attributeType;
0N/A
0N/A /**
0N/A * @serial The attribute write right.
0N/A */
0N/A private final boolean isWrite;
0N/A
0N/A /**
0N/A * @serial The attribute read right.
0N/A */
0N/A private final boolean isRead;
0N/A
0N/A /**
0N/A * @serial Indicates if this method is a "is"
0N/A */
0N/A private final boolean is;
0N/A
0N/A
0N/A /**
0N/A * Constructs an <CODE>MBeanAttributeInfo</CODE> object.
0N/A *
0N/A * @param name The name of the attribute.
0N/A * @param type The type or class name of the attribute.
0N/A * @param description A human readable description of the attribute.
0N/A * @param isReadable True if the attribute has a getter method, false otherwise.
0N/A * @param isWritable True if the attribute has a setter method, false otherwise.
0N/A * @param isIs True if this attribute has an "is" getter, false otherwise.
0N/A *
0N/A * @throws IllegalArgumentException if {@code isIs} is true but
0N/A * {@code isReadable} is not, or if {@code isIs} is true and
0N/A * {@code type} is not {@code boolean} or {@code java.lang.Boolean}.
0N/A * (New code should always use {@code boolean} rather than
0N/A * {@code java.lang.Boolean}.)
0N/A */
0N/A public MBeanAttributeInfo(String name,
0N/A String type,
0N/A String description,
0N/A boolean isReadable,
0N/A boolean isWritable,
0N/A boolean isIs) {
0N/A this(name, type, description, isReadable, isWritable, isIs,
0N/A (Descriptor) null);
0N/A }
0N/A
0N/A /**
0N/A * Constructs an <CODE>MBeanAttributeInfo</CODE> object.
0N/A *
0N/A * @param name The name of the attribute.
0N/A * @param type The type or class name of the attribute.
0N/A * @param description A human readable description of the attribute.
0N/A * @param isReadable True if the attribute has a getter method, false otherwise.
0N/A * @param isWritable True if the attribute has a setter method, false otherwise.
0N/A * @param isIs True if this attribute has an "is" getter, false otherwise.
0N/A * @param descriptor The descriptor for the attribute. This may be null
0N/A * which is equivalent to an empty descriptor.
0N/A *
0N/A * @throws IllegalArgumentException if {@code isIs} is true but
0N/A * {@code isReadable} is not, or if {@code isIs} is true and
0N/A * {@code type} is not {@code boolean} or {@code java.lang.Boolean}.
0N/A * (New code should always use {@code boolean} rather than
0N/A * {@code java.lang.Boolean}.)
0N/A *
0N/A * @since 1.6
0N/A */
0N/A public MBeanAttributeInfo(String name,
0N/A String type,
0N/A String description,
0N/A boolean isReadable,
0N/A boolean isWritable,
0N/A boolean isIs,
0N/A Descriptor descriptor) {
0N/A super(name, description, descriptor);
0N/A
0N/A this.attributeType = type;
0N/A this.isRead = isReadable;
0N/A this.isWrite = isWritable;
0N/A if (isIs && !isReadable) {
0N/A throw new IllegalArgumentException("Cannot have an \"is\" getter " +
0N/A "for a non-readable attribute");
0N/A }
0N/A if (isIs && !type.equals("java.lang.Boolean") &&
0N/A !type.equals("boolean")) {
0N/A throw new IllegalArgumentException("Cannot have an \"is\" getter " +
0N/A "for a non-boolean attribute");
0N/A }
0N/A this.is = isIs;
0N/A }
0N/A
0N/A /**
0N/A * <p>This constructor takes the name of a simple attribute, and Method
0N/A * objects for reading and writing the attribute. The {@link Descriptor}
0N/A * of the constructed object will include fields contributed by any
0N/A * annotations on the {@code Method} objects that contain the
0N/A * {@link DescriptorKey} meta-annotation.
0N/A *
0N/A * @param name The programmatic name of the attribute.
0N/A * @param description A human readable description of the attribute.
0N/A * @param getter The method used for reading the attribute value.
0N/A * May be null if the property is write-only.
0N/A * @param setter The method used for writing the attribute value.
0N/A * May be null if the attribute is read-only.
0N/A * @exception IntrospectionException There is a consistency
0N/A * problem in the definition of this attribute.
0N/A */
0N/A public MBeanAttributeInfo(String name,
0N/A String description,
0N/A Method getter,
0N/A Method setter) throws IntrospectionException {
0N/A this(name,
0N/A attributeType(getter, setter),
0N/A description,
0N/A (getter != null),
0N/A (setter != null),
0N/A isIs(getter),
1790N/A ImmutableDescriptor.union(Introspector.descriptorForElement(getter),
1790N/A Introspector.descriptorForElement(setter)));
0N/A }
0N/A
0N/A /**
0N/A * <p>Returns a shallow clone of this instance.
0N/A * The clone is obtained by simply calling <tt>super.clone()</tt>,
0N/A * thus calling the default native shallow cloning mechanism
0N/A * implemented by <tt>Object.clone()</tt>.
0N/A * No deeper cloning of any internal field is made.</p>
0N/A *
0N/A * <p>Since this class is immutable, cloning is chiefly of
0N/A * interest to subclasses.</p>
0N/A */
0N/A public Object clone () {
0N/A try {
0N/A return super.clone() ;
0N/A } catch (CloneNotSupportedException e) {
0N/A // should not happen as this class is cloneable
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the class name of the attribute.
0N/A *
0N/A * @return the class name.
0N/A */
0N/A public String getType() {
0N/A return attributeType;
0N/A }
0N/A
0N/A /**
0N/A * Whether the value of the attribute can be read.
0N/A *
0N/A * @return True if the attribute can be read, false otherwise.
0N/A */
0N/A public boolean isReadable() {
0N/A return isRead;
0N/A }
0N/A
0N/A /**
0N/A * Whether new values can be written to the attribute.
0N/A *
0N/A * @return True if the attribute can be written to, false otherwise.
0N/A */
0N/A public boolean isWritable() {
0N/A return isWrite;
0N/A }
0N/A
0N/A /**
0N/A * Indicates if this attribute has an "is" getter.
0N/A *
0N/A * @return true if this attribute has an "is" getter.
0N/A */
0N/A public boolean isIs() {
0N/A return is;
0N/A }
0N/A
0N/A public String toString() {
0N/A String access;
0N/A if (isReadable()) {
0N/A if (isWritable())
0N/A access = "read/write";
0N/A else
0N/A access = "read-only";
0N/A } else if (isWritable())
0N/A access = "write-only";
0N/A else
0N/A access = "no-access";
0N/A
0N/A return
0N/A getClass().getName() + "[" +
0N/A "description=" + getDescription() + ", " +
0N/A "name=" + getName() + ", " +
0N/A "type=" + getType() + ", " +
0N/A access + ", " +
0N/A (isIs() ? "isIs, " : "") +
0N/A "descriptor=" + getDescriptor() +
0N/A "]";
0N/A }
0N/A
0N/A /**
0N/A * Compare this MBeanAttributeInfo to another.
0N/A *
0N/A * @param o the object to compare to.
0N/A *
0N/A * @return true if and only if <code>o</code> is an MBeanAttributeInfo such
0N/A * that its {@link #getName()}, {@link #getType()}, {@link
0N/A * #getDescription()}, {@link #isReadable()}, {@link
0N/A * #isWritable()}, and {@link #isIs()} values are equal (not
0N/A * necessarily identical) to those of this MBeanAttributeInfo.
0N/A */
0N/A public boolean equals(Object o) {
0N/A if (o == this)
0N/A return true;
0N/A if (!(o instanceof MBeanAttributeInfo))
0N/A return false;
0N/A MBeanAttributeInfo p = (MBeanAttributeInfo) o;
0N/A return (p.getName().equals(getName()) &&
0N/A p.getType().equals(getType()) &&
0N/A p.getDescription().equals(getDescription()) &&
0N/A p.getDescriptor().equals(getDescriptor()) &&
0N/A p.isReadable() == isReadable() &&
0N/A p.isWritable() == isWritable() &&
0N/A p.isIs() == isIs());
0N/A }
0N/A
0N/A /* We do not include everything in the hashcode. We assume that
0N/A if two operations are different they'll probably have different
0N/A names or types. The penalty we pay when this assumption is
0N/A wrong should be less than the penalty we would pay if it were
0N/A right and we needlessly hashed in the description and parameter
0N/A array. */
0N/A public int hashCode() {
0N/A return getName().hashCode() ^ getType().hashCode();
0N/A }
0N/A
0N/A private static boolean isIs(Method getter) {
0N/A return (getter != null &&
0N/A getter.getName().startsWith("is") &&
0N/A (getter.getReturnType().equals(Boolean.TYPE) ||
0N/A getter.getReturnType().equals(Boolean.class)));
0N/A }
0N/A
0N/A /**
0N/A * Finds the type of the attribute.
0N/A */
0N/A private static String attributeType(Method getter, Method setter)
0N/A throws IntrospectionException {
686N/A Class<?> type = null;
0N/A
0N/A if (getter != null) {
0N/A if (getter.getParameterTypes().length != 0) {
0N/A throw new IntrospectionException("bad getter arg count");
0N/A }
0N/A type = getter.getReturnType();
0N/A if (type == Void.TYPE) {
0N/A throw new IntrospectionException("getter " + getter.getName() +
0N/A " returns void");
0N/A }
0N/A }
0N/A
0N/A if (setter != null) {
686N/A Class<?> params[] = setter.getParameterTypes();
0N/A if (params.length != 1) {
0N/A throw new IntrospectionException("bad setter arg count");
0N/A }
0N/A if (type == null)
0N/A type = params[0];
0N/A else if (type != params[0]) {
0N/A throw new IntrospectionException("type mismatch between " +
0N/A "getter and setter");
0N/A }
0N/A }
0N/A
0N/A if (type == null) {
0N/A throw new IntrospectionException("getter and setter cannot " +
0N/A "both be null");
0N/A }
0N/A
0N/A return type.getName();
0N/A }
0N/A
0N/A}