Path: blob/master/src/java.management/share/classes/javax/management/MBeanAttributeInfo.java
41154 views
/*1* Copyright (c) 1999, 2021, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.management;2627import java.lang.reflect.Method;28import java.security.AccessController;2930import com.sun.jmx.mbeanserver.GetPropertyAction;31import com.sun.jmx.mbeanserver.Introspector;32import java.util.Objects;333435/**36* Describes an MBean attribute exposed for management. Instances of37* this class are immutable. Subclasses may be mutable but this is38* not recommended.39*40* @since 1.541*/42@SuppressWarnings("serial") // serialVersionUID not constant43public class MBeanAttributeInfo extends MBeanFeatureInfo implements Cloneable {4445/* Serial version */46private static final long serialVersionUID;47static {48/* For complicated reasons, the serialVersionUID changed49between JMX 1.0 and JMX 1.1, even though JMX 1.1 did not50have compatibility code for this class. So the51serialization produced by this class with JMX 1.2 and52jmx.serial.form=1.0 is not the same as that produced by53this class with JMX 1.1 and jmx.serial.form=1.0. However,54the serialization without that property is the same, and55that is the only form required by JMX 1.2.56*/57long uid = 8644704819898565848L;58try {59GetPropertyAction act = new GetPropertyAction("jmx.serial.form");60@SuppressWarnings("removal")61String form = AccessController.doPrivileged(act);62if ("1.0".equals(form))63uid = 7043855487133450673L;64} catch (Exception e) {65// OK: exception means no compat with 1.0, too bad66}67serialVersionUID = uid;68}6970static final MBeanAttributeInfo[] NO_ATTRIBUTES =71new MBeanAttributeInfo[0];7273/**74* @serial The actual attribute type.75*/76private final String attributeType;7778/**79* @serial The attribute write right.80*/81private final boolean isWrite;8283/**84* @serial The attribute read right.85*/86private final boolean isRead;8788/**89* @serial Indicates if this method is a "is"90*/91private final boolean is;929394/**95* Constructs an {@code MBeanAttributeInfo} object.96*97* @param name The name of the attribute.98* @param type The type or class name of the attribute.99* @param description A human readable description of the attribute.100* @param isReadable True if the attribute has a getter method, false otherwise.101* @param isWritable True if the attribute has a setter method, false otherwise.102* @param isIs True if this attribute has an "is" getter, false otherwise.103*104* @throws IllegalArgumentException if {@code isIs} is true but105* {@code isReadable} is not, or if {@code isIs} is true and106* {@code type} is not {@code boolean} or {@code java.lang.Boolean}.107* (New code should always use {@code boolean} rather than108* {@code java.lang.Boolean}.)109*/110public MBeanAttributeInfo(String name,111String type,112String description,113boolean isReadable,114boolean isWritable,115boolean isIs) {116this(name, type, description, isReadable, isWritable, isIs,117(Descriptor) null);118}119120/**121* Constructs an {@code MBeanAttributeInfo} object.122*123* @param name The name of the attribute.124* @param type The type or class name of the attribute.125* @param description A human readable description of the attribute.126* @param isReadable True if the attribute has a getter method, false otherwise.127* @param isWritable True if the attribute has a setter method, false otherwise.128* @param isIs True if this attribute has an "is" getter, false otherwise.129* @param descriptor The descriptor for the attribute. This may be null130* which is equivalent to an empty descriptor.131*132* @throws IllegalArgumentException if {@code isIs} is true but133* {@code isReadable} is not, or if {@code isIs} is true and134* {@code type} is not {@code boolean} or {@code java.lang.Boolean}.135* (New code should always use {@code boolean} rather than136* {@code java.lang.Boolean}.)137*138* @since 1.6139*/140public MBeanAttributeInfo(String name,141String type,142String description,143boolean isReadable,144boolean isWritable,145boolean isIs,146Descriptor descriptor) {147super(name, description, descriptor);148149this.attributeType = type;150this.isRead = isReadable;151this.isWrite = isWritable;152if (isIs && !isReadable) {153throw new IllegalArgumentException("Cannot have an \"is\" getter " +154"for a non-readable attribute");155}156if (isIs && !type.equals("java.lang.Boolean") &&157!type.equals("boolean")) {158throw new IllegalArgumentException("Cannot have an \"is\" getter " +159"for a non-boolean attribute");160}161this.is = isIs;162}163164/**165* <p>This constructor takes the name of a simple attribute, and Method166* objects for reading and writing the attribute. The {@link Descriptor}167* of the constructed object will include fields contributed by any168* annotations on the {@code Method} objects that contain the169* {@link DescriptorKey} meta-annotation.170*171* @param name The programmatic name of the attribute.172* @param description A human readable description of the attribute.173* @param getter The method used for reading the attribute value.174* May be null if the property is write-only.175* @param setter The method used for writing the attribute value.176* May be null if the attribute is read-only.177* @exception IntrospectionException There is a consistency178* problem in the definition of this attribute.179*/180public MBeanAttributeInfo(String name,181String description,182Method getter,183Method setter) throws IntrospectionException {184this(name,185attributeType(getter, setter),186description,187(getter != null),188(setter != null),189isIs(getter),190ImmutableDescriptor.union(Introspector.descriptorForElement(getter),191Introspector.descriptorForElement(setter)));192}193194/**195* <p>Returns a shallow clone of this instance.196* The clone is obtained by simply calling {@code super.clone()},197* thus calling the default native shallow cloning mechanism198* implemented by {@code Object.clone()}.199* No deeper cloning of any internal field is made.</p>200*201* <p>Since this class is immutable, cloning is chiefly of202* interest to subclasses.</p>203*/204public Object clone () {205try {206return super.clone() ;207} catch (CloneNotSupportedException e) {208// should not happen as this class is cloneable209return null;210}211}212213/**214* Returns the class name of the attribute.215*216* @return the class name.217*/218public String getType() {219return attributeType;220}221222/**223* Whether the value of the attribute can be read.224*225* @return True if the attribute can be read, false otherwise.226*/227public boolean isReadable() {228return isRead;229}230231/**232* Whether new values can be written to the attribute.233*234* @return True if the attribute can be written to, false otherwise.235*/236public boolean isWritable() {237return isWrite;238}239240/**241* Indicates if this attribute has an "is" getter.242*243* @return true if this attribute has an "is" getter.244*/245public boolean isIs() {246return is;247}248249public String toString() {250String access;251if (isReadable()) {252if (isWritable())253access = "read/write";254else255access = "read-only";256} else if (isWritable())257access = "write-only";258else259access = "no-access";260261return262getClass().getName() + "[" +263"description=" + getDescription() + ", " +264"name=" + getName() + ", " +265"type=" + getType() + ", " +266access + ", " +267(isIs() ? "isIs, " : "") +268"descriptor=" + getDescriptor() +269"]";270}271272/**273* Compare this MBeanAttributeInfo to another.274*275* @param o the object to compare to.276*277* @return true if and only if {@code o} is an MBeanAttributeInfo such278* that its {@link #getName()}, {@link #getType()}, {@link279* #getDescription()}, {@link #isReadable()}, {@link280* #isWritable()}, and {@link #isIs()} values are equal (not281* necessarily identical) to those of this MBeanAttributeInfo.282*/283public boolean equals(Object o) {284if (o == this)285return true;286if (!(o instanceof MBeanAttributeInfo))287return false;288MBeanAttributeInfo p = (MBeanAttributeInfo) o;289return (Objects.equals(p.getName(), getName()) &&290Objects.equals(p.getType(), getType()) &&291Objects.equals(p.getDescription(), getDescription()) &&292Objects.equals(p.getDescriptor(), getDescriptor()) &&293p.isReadable() == isReadable() &&294p.isWritable() == isWritable() &&295p.isIs() == isIs());296}297298/* We do not include everything in the hashcode. We assume that299if two operations are different they'll probably have different300names or types. The penalty we pay when this assumption is301wrong should be less than the penalty we would pay if it were302right and we needlessly hashed in the description and parameter303array. */304public int hashCode() {305return Objects.hash(getName(), getType());306}307308private static boolean isIs(Method getter) {309return (getter != null &&310getter.getName().startsWith("is") &&311(getter.getReturnType().equals(Boolean.TYPE) ||312getter.getReturnType().equals(Boolean.class)));313}314315/**316* Finds the type of the attribute.317*/318private static String attributeType(Method getter, Method setter)319throws IntrospectionException {320Class<?> type = null;321322if (getter != null) {323if (getter.getParameterTypes().length != 0) {324throw new IntrospectionException("bad getter arg count");325}326type = getter.getReturnType();327if (type == Void.TYPE) {328throw new IntrospectionException("getter " + getter.getName() +329" returns void");330}331}332333if (setter != null) {334Class<?> params[] = setter.getParameterTypes();335if (params.length != 1) {336throw new IntrospectionException("bad setter arg count");337}338if (type == null)339type = params[0];340else if (type != params[0]) {341throw new IntrospectionException("type mismatch between " +342"getter and setter");343}344}345346if (type == null) {347throw new IntrospectionException("getter and setter cannot " +348"both be null");349}350351return type.getName();352}353354}355356357