KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > soto > jmx > MethodUtils


1 package org.sapia.soto.jmx;
2
3 import java.lang.reflect.Method JavaDoc;
4 import java.lang.reflect.Modifier JavaDoc;
5
6
7 /**
8  * A utility class that performs various operations using the Java reflection API.
9  *
10  * @author Yanick Duchesne
11  * <dl>
12  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
13  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
14  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
15  * </dl>
16  */

17 public class MethodUtils {
18   public static final String JavaDoc SET_PREFIX = "set";
19   public static final String JavaDoc GET_PREFIX = "get";
20
21   /**
22    * Returns the name of the MBean attribute corresponding to the given
23    * method object - chops the given prefix from the method name and returns
24    * the result.
25    *
26    * @param method a <code>Method</code> object.
27    * @param removePrefix the prefix to remove from the method's name - to
28    * result in a MBean attribute name.
29    * @return a MBean attribute name.
30    */

31   public static String JavaDoc getAttributeName(Method JavaDoc method, String JavaDoc removePrefix) {
32     return method.getName().substring(removePrefix.length());
33   }
34
35   /**
36    * Returns <code>true</code> if the given method object corresponds to a setter.
37    * The method must start with a "set" prefix and be non-static, public, and take a single
38    * parameter.
39    *
40    * @param method a <code>Method</code> object.
41    * @return <code>true</code> if the given instance corresponds to a setter.
42    */

43   public static boolean isSetter(Method JavaDoc method) {
44     return method.getName().startsWith("set") &&
45     Modifier.isPublic(method.getModifiers()) &&
46     !Modifier.isStatic(method.getModifiers()) &&
47     (method.getParameterTypes().length == 1);
48   }
49
50   /**
51    * Returns <code>true</code> if the given method object corresponds to a getter.
52    * The method must start with a "get" prefix and be non-static, public, take no
53    * parameter, and have a return type.
54    *
55    * @param method a <code>Method</code> object.
56    * @return <code>true</code> if the given instance corresponds to a getter.
57    */

58   public static boolean isGetter(Method JavaDoc method) {
59     return method.getName().startsWith("get") &&
60     Modifier.isPublic(method.getModifiers()) &&
61     !Modifier.isStatic(method.getModifiers()) &&
62     (method.getParameterTypes().length == 0) &&
63     (method.getReturnType() != null) &&
64     !method.getReturnType().equals(void.class);
65   }
66
67   /**
68    * Returns <code>true</code> if the given method object corresponds to a "is".
69    * The method must start with a "is" prefix and be non-static, public, take no
70    * parameter, and have a boolean return type.
71    *
72    * @param method a <code>Method</code> object.
73    * @return <code>true</code> if the given instance corresponds to a "is".
74    */

75   public static boolean isBoolean(Method JavaDoc method) {
76     return method.getName().startsWith("is") &&
77     Modifier.isPublic(method.getModifiers()) &&
78     !Modifier.isStatic(method.getModifiers()) &&
79     (method.getParameterTypes().length == 0) &&
80     (method.getReturnType() != null) &&
81     !method.getReturnType().equals(void.class);
82   }
83
84   /**
85    * Returns <code>true</code> if the given method object is not a getter/setter/is, and
86    * if it is public and non-static.
87    *
88    * @param method a <code>method</code>.
89    * @return <code>true</code> if the given method corresponds to
90    * a MBean operation.
91    */

92   public static boolean isOperation(Method JavaDoc method) {
93     return Modifier.isPublic(method.getModifiers()) &&
94     !Modifier.isStatic(method.getModifiers()) && !isSetter(method) &&
95     !isGetter(method) && !isBoolean(method);
96   }
97
98   /**
99    * Returns the name of the given class, minus the package name.
100    *
101    * @param method a <code>method</code>.
102    * @return <code>true</code> if the given method corresponds to
103    * a MBean operation.
104    */

105   public static String JavaDoc getShortClassName(Class JavaDoc clazz) {
106     int idx = clazz.getName().lastIndexOf('.');
107
108     if (idx < 0) {
109       return clazz.getName();
110     } else {
111       return clazz.getName().substring(idx + 1);
112     }
113   }
114
115   /**
116    * Returns the "pretty" name for the given attribute/operation.
117    *
118    * @param name the attribute name/operation to format.
119    * @return the formatted operation/attribute name.
120    */

121   public static String JavaDoc pretty(String JavaDoc name) {
122     return name;
123   }
124 }
125
Popular Tags