1 package org.picocontainer.defaults; 2 3 import java.util.Map ; 4 import java.util.HashMap ; 5 import java.lang.reflect.Method ; 6 7 11 public class SetterIntrospector { 12 13 public Map getSetters(Class clazz) { 14 Map result = new HashMap (); 15 Method [] methods = clazz.getMethods(); 16 for (int i = 0; i < methods.length; i++) { 17 Method method = methods[i]; 18 if (isSetter(method)) { 19 result.put(getPropertyName(method), method); 20 } 21 } 22 return result; 23 } 24 25 private String getPropertyName(Method method) { 26 final String name = method.getName(); 27 String 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 method) { 37 final String name = method.getName(); 38 return name.length() > 3 && 39 name.startsWith("set") && 40 method.getParameterTypes().length == 1; 41 } 42 43 } 44 | Popular Tags |