1 |
| package org.picocontainer.defaults; |
2 |
| |
3 |
| import java.util.Map; |
4 |
| import java.util.HashMap; |
5 |
| import java.lang.reflect.Method; |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| public class SetterIntrospector { |
12 |
| |
13 |
12
| public Map getSetters(Class clazz) {
|
14 |
12
| Map result = new HashMap();
|
15 |
12
| Method[] methods = clazz.getMethods();
|
16 |
12
| for (int i = 0; i < methods.length; i++) {
|
17 |
160
| Method method = methods[i];
|
18 |
160
| if (isSetter(method)) {
|
19 |
46
| result.put(getPropertyName(method), method);
|
20 |
| } |
21 |
| } |
22 |
12
| return result;
|
23 |
| } |
24 |
| |
25 |
46
| private String getPropertyName(Method method) {
|
26 |
46
| final String name = method.getName();
|
27 |
46
| String result = name.substring(3);
|
28 |
46
| if(result.length() > 1 && !Character.isUpperCase(result.charAt(1))) {
|
29 |
36
| result = "" + Character.toLowerCase(result.charAt(0)) + result.substring(1);
|
30 |
10
| } else if(result.length() == 1) {
|
31 |
6
| result = result.toLowerCase();
|
32 |
| } |
33 |
46
| return result;
|
34 |
| } |
35 |
| |
36 |
160
| private boolean isSetter(Method method) {
|
37 |
160
| final String name = method.getName();
|
38 |
160
| return name.length() > 3 &&
|
39 |
| name.startsWith("set") && |
40 |
| method.getParameterTypes().length == 1; |
41 |
| } |
42 |
| |
43 |
| } |