001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008:
009: package javax.management;
010:
011: import java.io.Serializable;
012: import java.lang.reflect.Method;
013:
014: import mx4j.util.Utils;
015:
016: /**
017: * @version $Revision: 1.14 $
018: */
019: // Change not needed, workaround to a TCK bug only to achieve TCK compliance
020: // public class MBeanAttributeInfo extends MBeanFeatureInfo implements Cloneable
021: public class MBeanAttributeInfo extends MBeanFeatureInfo implements
022: Cloneable, Serializable {
023: private static final long serialVersionUID = 8644704819898565848L;
024:
025: /**
026: * @serial The full qualified class name of the attribute's type
027: */
028: private String attributeType;
029: /**
030: * @serial The readability of the attribute
031: */
032: private boolean isRead;
033: /**
034: * @serial The writability of the attribute
035: */
036: private boolean isWrite;
037: /**
038: * @serial The boolean flag that says the attribute's type is boolean
039: */
040: private boolean is;
041:
042: /**
043: * Creates a new MBeanAttributeInfo
044: *
045: * @param name The attribute name
046: * @param description The attribute description
047: * @param getter The getter method, or null if write only
048: * @param setter The setter method, or null if read only
049: * @throws IntrospectionException If the introspection of the attribute fails
050: */
051: public MBeanAttributeInfo(String name, String description,
052: Method getter, Method setter) throws IntrospectionException {
053: super (name, description);
054:
055: String getterType = null;
056: String setterType = null;
057:
058: if (getter != null) {
059: if (Utils.isAttributeGetter(getter)) {
060: this .isRead = true;
061: if (getter.getName().startsWith("is"))
062: this .is = true;
063: getterType = getter.getReturnType().getName();
064: } else
065: throw new IntrospectionException("Bad getter method");
066: }
067: if (setter != null) {
068: if (Utils.isAttributeSetter(setter)) {
069: this .isWrite = true;
070: setterType = setter.getParameterTypes()[0].getName();
071: } else
072: throw new IntrospectionException("Bad setter method");
073: }
074:
075: this .attributeType = reconcileAttributeType(getterType,
076: setterType);
077: }
078:
079: /**
080: * Creates a new MBeanAttributeInfo
081: *
082: * @param name The attribute name
083: * @param description The attribute description
084: * @param isReadable The attribute's readability
085: * @param isWritable The attribute's writability
086: * @param isIs The flag if the attribute's type is boolean
087: */
088: public MBeanAttributeInfo(String name, String className,
089: String description, boolean isReadable, boolean isWritable,
090: boolean isIs) {
091: super (name, description);
092:
093: this .attributeType = className;
094: this .isRead = isReadable;
095: this .isWrite = isWritable;
096: this .is = isIs;
097: }
098:
099: public Object clone() {
100: try {
101: return super .clone();
102: } catch (CloneNotSupportedException ignored) {
103: return null;
104: }
105: }
106:
107: public String getType() {
108: return attributeType;
109: }
110:
111: public boolean isReadable() {
112: return isRead;
113: }
114:
115: public boolean isWritable() {
116: return isWrite;
117: }
118:
119: public boolean isIs() {
120: return is;
121: }
122:
123: public int hashCode() {
124: int hash = super .hashCode();
125:
126: String t = getType();
127: if (t != null)
128: hash = 29 * hash + t.hashCode();
129:
130: hash = 29
131: * hash
132: + 3
133: * (isReadable() ? Boolean.TRUE.hashCode()
134: : Boolean.FALSE.hashCode());
135: hash = 29
136: * hash
137: + 5
138: * (isWritable() ? Boolean.TRUE.hashCode()
139: : Boolean.FALSE.hashCode());
140: hash = 29
141: * hash
142: + 7
143: * (isIs() ? Boolean.TRUE.hashCode() : Boolean.FALSE
144: .hashCode());
145:
146: return hash;
147: }
148:
149: public boolean equals(Object obj) {
150: if (!super .equals(obj))
151: return false;
152: if (!(obj instanceof MBeanAttributeInfo))
153: return false;
154:
155: MBeanAttributeInfo other = (MBeanAttributeInfo) obj;
156:
157: String this Type = getType();
158: String otherType = other.getType();
159: if (this Type != null ? !this Type.equals(otherType)
160: : otherType != null)
161: return false;
162:
163: if (isReadable() ^ other.isReadable())
164: return false;
165: if (isWritable() ^ other.isWritable())
166: return false;
167: if (isIs() ^ other.isIs())
168: return false;
169:
170: return true;
171: }
172:
173: private String reconcileAttributeType(String getterType,
174: String setterType) throws IntrospectionException {
175: String result = null; // only unchanged if getterType == null && setterType == null
176:
177: if (getterType == null && setterType != null) {
178: result = setterType;
179: } else if (getterType != null && setterType == null) {
180: result = getterType;
181: } else if (getterType != null && setterType != null) {
182: if (getterType.compareToIgnoreCase(setterType) == 0) {
183: result = getterType;
184: } else {
185: throw new IntrospectionException(
186: "Attribute setter/getter types don't match");
187: }
188: }
189:
190: return result;
191: }
192: }
|