| MxOperationInfo.java |
package org.jfox.mx;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
*
* @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
*
* Describes a management operation exposed by an MBean. Instances of
* this class are immutable. Subclasses may be mutable but this is
* not recommended.
*/
public class MxOperationInfo extends MxFeatureInfo implements java.io.Serializable, Cloneable {
/* Serial version */
static final long serialVersionUID = -6178860474881375330L;
static final MxOperationInfo[] NO_OPERATIONS =
new MxOperationInfo[0];
/**
* Indicates that the operation is read-like,
* it basically returns information.
*/
private boolean isInfo = false;
/**
* @serial The method's return value.
*/
private final String type;
/**
* @serial The signature of the method, that is, the class names
* of the arguments.
*/
private final MxParameterInfo[] signature;
/** @see org.jfox.mx.MxInfo#immutable */
private final transient boolean immutable;
/**
* Constructs an <CODE>MxOperationInfo</CODE> object.
*
* @param method The <CODE>java.lang.reflect.Method</CODE> object
* describing the MBean operation.
* @param description A human readable description of the operation.
*
* @exception IllegalArgumentException if the name of
* <code>method</code> is not a valid Java identifier, or if the
* name of its return type or one of its parameter types is not a
* syntactically legal Java type name. Java reserved words are
* not considered illegal here. If <code>method</code> comes from
* a class compiled from Java, this exception cannot happen.
*/
public MxOperationInfo(String description,
Method method)
throws IllegalArgumentException {
this(method.getName(),
description,
methodSignature(method),
method.getReturnType().getName(),
(method.getParameterTypes().length ==0 && !method.getReturnType().equals(void.class) ? true : false));
}
/**
* Constructs an <CODE>MxOperationInfo</CODE> object.
*
* @param name The name of the method.
* @param description A human readable description of the operation.
* @param signature <CODE>MxParameterInfo</CODE> objects
* describing the parameters(arguments) of the method. This may be
* null with the same effect as a zero-length array.
* @param type The type of the method's return value.
* @param isInfo
* ACTION, ACTION_INFO, UNKNOWN</CODE>.
*
* @exception IllegalArgumentException if <code>name</code> is not
* a valid Java identifier or if <code>type</code> is not a
* syntactically legal Java type name. Java reserved words are not
* considered illegal here.
*/
private MxOperationInfo(String name,
String description,
MxParameterInfo[] signature,
String type,
boolean isInfo)
throws IllegalArgumentException {
super(name, description);
MxInfo.mustBeValidJavaIdentifier(name);
MxInfo.mustBeValidJavaTypeName(type);
if (signature == null || signature.length == 0)
signature = MxParameterInfo.NO_PARAMS;
else
signature = (MxParameterInfo[]) signature.clone();
this.signature = signature;
this.type = type;
this.isInfo = isInfo;
this.immutable =
MxInfo.isImmutableClass(this.getClass(),
MxOperationInfo.class);
}
/**
* <p>Returns a shallow clone of this instance.
* The clone is obtained by simply calling <tt>super.clone()</tt>,
* thus calling the default native shallow cloning mechanism
* implemented by <tt>Object.clone()</tt>.
* No deeper cloning of any internal field is made.</p>
*
* <p>Since this class is immutable, cloning is chiefly of interest
* to subclasses.</p>
*/
public Object clone () {
try {
return super.clone() ;
} catch (CloneNotSupportedException e) {
// should not happen as this class is cloneable
return null;
}
}
/**
* Returns the type of the method's return value.
*
* @return the return type.
*/
public String getReturnType() {
return type;
}
/**
* <p>Returns the list of parameters for this operation. Each
* parameter is described by an <CODE>MxParameterInfo</CODE>
* object.</p>
*
* <p>The returned array is a shallow copy of the internal array,
* which means that it is a copy of the internal array of
* references to the <CODE>MxParameterInfo</CODE> objects but
* that each referenced <CODE>MxParameterInfo</CODE> object is
* not copied.</p>
*
* @return An array of <CODE>MxParameterInfo</CODE> objects.
*/
public MxParameterInfo[] getSignature() {
if (signature.length == 0)
return signature;
else
return (MxParameterInfo[]) signature.clone();
}
private MxParameterInfo[] fastGetSignature() {
if (immutable)
return signature;
else
return getSignature();
}
/**
* 该方法是否是一个 info 的方法, info 方法将在控制台上打印出结果
* @return
*/
public boolean isInfoOperation() {
return isInfo;
}
/**
* Compare this MxOperationInfo to another.
*
* @param o the object to compare to.
*
* @return true iff <code>o</code> is an MxOperationInfo such
* that its {@link #getName()}, {@link #getReturnType()}, {@link
* #getDescription()}, {@link #isInfoOperation()}, and {@link
* #getSignature()} values are equal (not necessarily identical)
* to those of this MxConstructorInfo. Two signature arrays
* are equal if their elements are pairwise equal.
*/
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof MxOperationInfo))
return false;
MxOperationInfo p = (MxOperationInfo) o;
return (p.getName().equals(getName()) &&
p.getReturnType().equals(getReturnType()) &&
p.getDescription().equals(getDescription()) &&
p.isInfoOperation() == isInfoOperation() &&
Arrays.equals(p.fastGetSignature(), fastGetSignature()));
}
/* We do not include everything in the hashcode. We assume that
if two operations are different they'll probably have different
names or types. The penalty we pay when this assumption is
wrong should be less than the penalty we would pay if it were
right and we needlessly hashed in the description and the
parameter array. */
public int hashCode() {
return getName().hashCode() ^ getReturnType().hashCode();
}
private static MxParameterInfo[] methodSignature(Method method)
throws IllegalArgumentException {
final Class[] classes = method.getParameterTypes();
final MxParameterInfo[] params =
new MxParameterInfo[classes.length];
for (int i = 0; i < classes.length; i++) {
final String pn = "p" + (i + 1);
params[i] = new MxParameterInfo(pn, classes[i].getName(), "");
}
return params;
}
}