View Javadoc

1   package ch.elca.el4j.services.monitoring.jmx.util;
2   
3   import java.beans.PropertyDescriptor;
4   import java.lang.reflect.Method;
5   
6   /**
7    * Inspects a PropertyDescriptor.
8    *
9    * @svnLink $Revision: 3883 $;$Date: 2009-08-04 15:35:01 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/jmx/src/main/java/ch/elca/el4j/services/monitoring/jmx/util/PropertyReflector.java $
10   *
11   * @author David Bernhard (DBD)
12   */
13  public class PropertyReflector {
14  	
15  	/**
16  	 * The wrapped descriptor.
17  	 */
18  	private PropertyDescriptor m_d;
19  	
20  	/**
21  	 * Create a PropertyReflector from a {@link PropertyDescriptor}.
22  	 * @param d The descriptor to wrap.
23  	 */
24  	public PropertyReflector(PropertyDescriptor d) {
25  		m_d = d;
26  	}
27  	
28  	/**
29  	 * @return Whether this property is readable.
30  	 */
31  	public boolean isReadable() {
32  		return (m_d.getReadMethod() != null);
33  	}
34  	
35  	/**
36  	 * @return The name of the read method -
37  	 * usually "get-" or "is-" + Name.
38  	 */
39  	public String getReadMethod() {
40  		return m_d.getReadMethod().getName();
41  	}
42  	
43  	/**
44  	 * @return Whether this property is writable.
45  	 */
46  	public boolean isWritable() {
47  		return (m_d.getWriteMethod() != null);
48  	}
49  	
50  	/**
51  	 * @return The name of the write method.
52  	 */
53  	public String getWriteMethod() {
54  		return m_d.getWriteMethod().getName();
55  	}
56  	
57  	/**
58  	 * @return The name of this property.
59  	 */
60  	public String getName() {
61  		return m_d.getName();
62  	}
63  	
64  	/**
65  	 * @return The type of this property.
66  	 */
67  	public String getType() {
68  		String type = "";
69  		if (isReadable()) {
70  			type = m_d.getReadMethod().getReturnType().toString();
71  		}
72  		if (isWritable() && type.equals("")) {
73  			Method m = m_d.getWriteMethod();
74  			if (m.getParameterTypes().length == 1) {
75  				type = m.getParameterTypes()[0].toString();
76  			}
77  		}
78  		return type;
79  	}
80  	
81  	/**
82  	 * @return A string (R|-)(W|-) indicating what access this property has.
83  	 */
84  	public String getRW() {
85  		return (isReadable() ? "R" : "-") + (isWritable() ? "W" : "-");
86  	}
87  }