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