View Javadoc
1   package ch.qos.logback.core.joran.util.beans;
2   
3   import java.lang.reflect.Method;
4   
5   /**
6    * Encapsulates utility methods associated with standard java beans.
7    * @author urechm
8    */
9   public class BeanUtil {
10  
11      //public static final BeanUtil SINGLETON = new BeanUtil();
12  
13      public static final String PREFIX_GETTER_IS = "is";
14      public static final String PREFIX_GETTER_GET = "get";
15      public static final String PREFIX_SETTER = "set";
16      public static final String PREFIX_ADDER = "add";
17  
18      /**
19       *
20       * @param method to check if it is an 'adder' method.
21       * @return true if the given method is an 'adder' method.
22       */
23      static public boolean isAdder(Method method) {
24          int parameterCount = getParameterCount(method);
25          if (parameterCount != 1) {
26              return false;
27          }
28          Class<?> returnType = method.getReturnType();
29          if (returnType != void.class) {
30              return false;
31          }
32          String methodName = method.getName();
33          return methodName.startsWith(PREFIX_ADDER);
34      }
35  
36      /**
37       *
38       * @param method to check if it is a standard java beans getter.
39       * @return true if the given method is a standard java beans getter.
40       */
41      static public boolean isGetter(Method method) {
42          int parameterCount = getParameterCount(method);
43          if (parameterCount > 0) {
44              return false;
45          }
46          Class<?> returnType = method.getReturnType();
47          if (returnType == void.class) {
48              return false;
49          }
50          String methodName = method.getName();
51          if (!methodName.startsWith(PREFIX_GETTER_GET) && !methodName.startsWith(PREFIX_GETTER_IS)) {
52              return false;
53          }
54          if (methodName.startsWith(PREFIX_GETTER_IS)) {
55              if (!returnType.equals(boolean.class) && !returnType.equals(Boolean.class)) {
56                  return false;
57              }
58          }
59          return true;
60      }
61  
62      static private int getParameterCount(Method method) {
63          return method.getParameterTypes().length;
64      }
65  
66      /**
67       *
68       * @param method to check if it is a standard java beans setter.
69       * @return true if the given method is a standard java beans setter.
70       */
71      static public boolean isSetter(Method method) {
72          int parameterCount = getParameterCount(method);
73          if (parameterCount != 1) {
74              return false;
75          }
76          Class<?> returnType = method.getReturnType();
77          if (returnType != void.class) {
78              return false;
79          }
80          String methodName = method.getName();
81          if (!methodName.startsWith(PREFIX_SETTER)) {
82              return false;
83          }
84          return true;
85      }
86  
87      /**
88       * @param method to get the associated property name for.
89       * @return The property name of the associated property if the given method matches a standard java beans getter or setter.
90       */
91      static public String getPropertyName(Method method) {
92          String methodName = method.getName();
93          String rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_GETTER_GET);
94          if (rawPropertyName == null) {
95              rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_SETTER);
96          }
97          if (rawPropertyName == null) {
98              rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_GETTER_IS);
99          }
100         if (rawPropertyName == null) {
101             rawPropertyName = getSubstringIfPrefixMatches(methodName, PREFIX_ADDER);
102         }
103         return toLowerCamelCase(rawPropertyName);
104     }
105 
106     /**
107      * Converts the given String into lower camel case form.
108      * @param string to decapitalize.
109      * @return null if the given String is null.
110      * Emtpy string if the given string is empty.
111      * The given string if the first two consecutive letters are in upper case.
112      * The given string with the first letter in lower case otherwise, which might be the given string.
113      */
114     static public String toLowerCamelCase(String string) {
115         if (string == null) {
116             return null;
117         }
118         if (string.isEmpty()) {
119             return string;
120         }
121         if (string.length() > 1 && Character.isUpperCase(string.charAt(1)) && Character.isUpperCase(string.charAt(0))) {
122             return string;
123         }
124         char chars[] = string.toCharArray();
125         chars[0] = Character.toLowerCase(chars[0]);
126         return new String(chars);
127     }
128 
129     static private String getSubstringIfPrefixMatches(String wholeString, String prefix) {
130         if (wholeString.startsWith(prefix)) {
131             return wholeString.substring(prefix.length());
132         }
133         return null;
134     }
135 
136 }