Path: blob/master/src/java.management/share/classes/javax/management/MBeanConstructorInfo.java
41154 views
/*1* Copyright (c) 1999, 2013, 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 com.sun.jmx.mbeanserver.Introspector;28import java.lang.annotation.Annotation;29import java.lang.reflect.Constructor;30import java.util.Arrays;31import java.util.Objects;3233/**34* Describes a constructor exposed by an MBean. Instances of this35* class are immutable. Subclasses may be mutable but this is not36* recommended.37*38* @since 1.539*/40public class MBeanConstructorInfo extends MBeanFeatureInfo implements Cloneable {4142/* Serial version */43static final long serialVersionUID = 4433990064191844427L;4445static final MBeanConstructorInfo[] NO_CONSTRUCTORS =46new MBeanConstructorInfo[0];4748/** @see MBeanInfo#arrayGettersSafe */49private final transient boolean arrayGettersSafe;5051/**52* @serial The signature of the method, that is, the class names of the arguments.53*/54private final MBeanParameterInfo[] signature;5556/**57* Constructs an {@code MBeanConstructorInfo} object. The58* {@link Descriptor} of the constructed object will include59* fields contributed by any annotations on the {@code60* Constructor} object that contain the {@link DescriptorKey}61* meta-annotation.62*63* @param description A human readable description of the operation.64* @param constructor The {@code java.lang.reflect.Constructor}65* object describing the MBean constructor.66*/67public MBeanConstructorInfo(String description, Constructor<?> constructor) {68this(constructor.getName(), description,69constructorSignature(constructor),70Introspector.descriptorForElement(constructor));71}7273/**74* Constructs an {@code MBeanConstructorInfo} object.75*76* @param name The name of the constructor.77* @param signature {@code MBeanParameterInfo} objects78* describing the parameters(arguments) of the constructor. This79* may be null with the same effect as a zero-length array.80* @param description A human readable description of the constructor.81*/82public MBeanConstructorInfo(String name,83String description,84MBeanParameterInfo[] signature) {85this(name, description, signature, null);86}8788/**89* Constructs an {@code MBeanConstructorInfo} object.90*91* @param name The name of the constructor.92* @param signature {@code MBeanParameterInfo} objects93* describing the parameters(arguments) of the constructor. This94* may be null with the same effect as a zero-length array.95* @param description A human readable description of the constructor.96* @param descriptor The descriptor for the constructor. This may be null97* which is equivalent to an empty descriptor.98*99* @since 1.6100*/101public MBeanConstructorInfo(String name,102String description,103MBeanParameterInfo[] signature,104Descriptor descriptor) {105super(name, description, descriptor);106107if (signature == null || signature.length == 0)108signature = MBeanParameterInfo.NO_PARAMS;109else110signature = signature.clone();111this.signature = signature;112this.arrayGettersSafe =113MBeanInfo.arrayGettersSafe(this.getClass(),114MBeanConstructorInfo.class);115}116117118/**119* <p>Returns a shallow clone of this instance. The clone is120* obtained by simply calling {@code super.clone()}, thus calling121* the default native shallow cloning mechanism implemented by122* {@code Object.clone()}. No deeper cloning of any internal123* field is made.</p>124*125* <p>Since this class is immutable, cloning is chiefly of126* interest to subclasses.</p>127*/128public Object clone () {129try {130return super.clone() ;131} catch (CloneNotSupportedException e) {132// should not happen as this class is cloneable133return null;134}135}136137/**138* <p>Returns the list of parameters for this constructor. Each139* parameter is described by an {@code MBeanParameterInfo}140* object.</p>141*142* <p>The returned array is a shallow copy of the internal array,143* which means that it is a copy of the internal array of144* references to the {@code MBeanParameterInfo} objects but145* that each referenced {@code MBeanParameterInfo} object is146* not copied.</p>147*148* @return An array of {@code MBeanParameterInfo} objects.149*/150public MBeanParameterInfo[] getSignature() {151if (signature.length == 0)152return signature;153else154return signature.clone();155}156157private MBeanParameterInfo[] fastGetSignature() {158if (arrayGettersSafe)159return signature;160else161return getSignature();162}163164public String toString() {165return166getClass().getName() + "[" +167"description=" + getDescription() + ", " +168"name=" + getName() + ", " +169"signature=" + Arrays.asList(fastGetSignature()) + ", " +170"descriptor=" + getDescriptor() +171"]";172}173174/**175* Compare this MBeanConstructorInfo to another.176*177* @param o the object to compare to.178*179* @return true if and only if {@code o} is an MBeanConstructorInfo such180* that its {@link #getName()}, {@link #getDescription()},181* {@link #getSignature()}, and {@link #getDescriptor()}182* values are equal (not necessarily183* identical) to those of this MBeanConstructorInfo. Two184* signature arrays are equal if their elements are pairwise185* equal.186*/187public boolean equals(Object o) {188if (o == this)189return true;190if (!(o instanceof MBeanConstructorInfo))191return false;192MBeanConstructorInfo p = (MBeanConstructorInfo) o;193return (Objects.equals(p.getName(), getName()) &&194Objects.equals(p.getDescription(), getDescription()) &&195Arrays.equals(p.fastGetSignature(), fastGetSignature()) &&196Objects.equals(p.getDescriptor(), getDescriptor()));197}198199/* Unlike attributes and operations, it's quite likely we'll have200more than one constructor with the same name and even201description, so we include the parameter array in the hashcode.202We don't include the description, though, because it could be203quite long and yet the same between constructors. Likewise for204the descriptor. */205public int hashCode() {206return Objects.hash(getName()) ^ Arrays.hashCode(fastGetSignature());207}208209private static MBeanParameterInfo[] constructorSignature(Constructor<?> cn) {210final Class<?>[] classes = cn.getParameterTypes();211final Annotation[][] annots = cn.getParameterAnnotations();212return MBeanOperationInfo.parameters(classes, annots);213}214}215216217