KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > SetterIntrospector


1 package org.picocontainer.defaults;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6
7 /**
8  * @author Aslak Hellesøy
9  * @version $Revision: 1812 $
10  */

11 public class SetterIntrospector {
12
13     public Map JavaDoc getSetters(Class JavaDoc clazz) {
14         Map JavaDoc result = new HashMap JavaDoc();
15         Method JavaDoc[] methods = clazz.getMethods();
16         for (int i = 0; i < methods.length; i++) {
17             Method JavaDoc method = methods[i];
18             if (isSetter(method)) {
19                 result.put(getPropertyName(method), method);
20             }
21         }
22         return result;
23     }
24
25     private String JavaDoc getPropertyName(Method JavaDoc method) {
26         final String JavaDoc name = method.getName();
27         String JavaDoc result = name.substring(3);
28         if(result.length() > 1 && !Character.isUpperCase(result.charAt(1))) {
29             result = "" + Character.toLowerCase(result.charAt(0)) + result.substring(1);
30         } else if(result.length() == 1) {
31             result = result.toLowerCase();
32         }
33         return result;
34     }
35
36     private boolean isSetter(Method JavaDoc method) {
37         final String JavaDoc name = method.getName();
38         return name.length() > 3 &&
39                 name.startsWith("set") &&
40                 method.getParameterTypes().length == 1;
41     }
42
43 }
44
Popular Tags