1 package ch.qos.logback.core.joran.util.beans;
2
3 import java.lang.reflect.Method;
4
5
6
7
8
9
10 public class BeanUtil {
11
12
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
22
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
40
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
70
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
90
91
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
110
111
112
113
114
115
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 }