package javax.management;
import java.lang.reflect.Method;
import java.io.Serializable;
import org.jboss.mx.util.Serialization;
public class MBeanAttributeInfo extends MBeanFeatureInfo
implements Serializable, Cloneable
{
private static final long serialVersionUID;
static
{
switch (Serialization.version)
{
case Serialization.V1R0:
serialVersionUID = 7043855487133450673L;
break;
default:
serialVersionUID = 8644704819898565848L;
}
}
private String attributeType = null;
private boolean isRead = false;
private boolean isWrite = false;
private boolean is = false;
private transient String cacheString;
private transient int cacheHashCode;
public MBeanAttributeInfo(String name, String type, String description,
boolean isReadable, boolean isWritable, boolean isIs)
throws IllegalArgumentException
{
super(name, description);
if (isIs && type.equals(Boolean.TYPE.getName()) == false && type.equals(Boolean.class.getName()) == false)
throw new IllegalArgumentException("Cannot have isIs for a non boolean/Boolean type");
this.attributeType = type;
this.isRead = isReadable;
this.isWrite = isWritable;
this.is = isIs;
}
public MBeanAttributeInfo(String name, String description, Method getter, Method setter)
throws IntrospectionException
{
super(name, description);
if (getter != null)
{
if (getter.getParameterTypes().length != 0)
throw new IntrospectionException("Expecting getter method to be of the form 'AttributeType getAttributeName()': found getter with " + getter.getParameterTypes().length + " parameters.");
if (getter.getReturnType() == Void.TYPE)
throw new IntrospectionException("Expecting getter method to be of the form 'AttributeType getAttributeName()': found getter with void return type.");
this.isRead = true;
if (getter.getName().startsWith("is"))
this.is = true;
this.attributeType = getter.getReturnType().getName();
}
if (setter != null)
{
if (setter.getParameterTypes().length != 1)
throw new IntrospectionException("Expecting the setter method to be of the form 'void setAttributeName(AttributeType value)': found setter with " + setter.getParameterTypes().length + " parameters.");
if (setter.getReturnType() != Void.TYPE)
throw new IntrospectionException("Expecting the setter method to be of the form 'void setAttributeName(AttributeType value)': found setter with " + setter.getReturnType() + " return type.");
this.isWrite = true;
if (attributeType == null)
{
try
{
attributeType = setter.getParameterTypes() [0].getName();
}
catch (ArrayIndexOutOfBoundsException e)
{
throw new IntrospectionException("Attribute setter is lacking type: " + name);
}
}
if (!(attributeType.equals(setter.getParameterTypes() [0].getName())))
throw new IntrospectionException("Attribute type mismatch: " + name);
}
}
public synchronized Object clone()
{
MBeanAttributeInfo clone = null;
try
{
clone = (MBeanAttributeInfo) super.clone();
clone.attributeType = this.attributeType;
clone.isRead = this.isRead;
clone.isWrite = this.isWrite;
clone.is = this.is;
}
catch(CloneNotSupportedException e)
{
}
return clone;
}
public String getType()
{
return attributeType;
}
public boolean isReadable()
{
return isRead;
}
public boolean isWritable()
{
return isWrite;
}
public boolean isIs()
{
return is;
}
public boolean equals(Object object)
{
if (this == object)
return true;
if (object == null || (object instanceof MBeanAttributeInfo) == false)
return false;
MBeanAttributeInfo other = (MBeanAttributeInfo) object;
if (super.equals(other) == false)
return false;
if (this.getType().equals(other.getType()) == false)
return false;
if (this.isReadable() != other.isReadable())
return false;
if (this.isWritable() != other.isWritable())
return false;
if (this.isIs() != other.isIs())
return false;
return true;
}
public int hashCode()
{
if (cacheHashCode == 0)
{
cacheHashCode = super.hashCode();
cacheHashCode += getType().hashCode();
}
return cacheHashCode;
}
public String toString()
{
if (cacheString == null)
{
StringBuffer buffer = new StringBuffer(100);
buffer.append(getClass().getName()).append(":");
buffer.append(" name=").append(getName());
buffer.append(" description=").append(getDescription());
buffer.append(" type=").append(getType());
buffer.append(" Readable=").append(isReadable());
buffer.append(" Writable=").append(isWritable());
buffer.append(" isIs=").append(isIs());
cacheString = buffer.toString();
}
return cacheString;
}
}