1 18 package net.sf.uitags.tagutil.bean; 19 20 import java.beans.IntrospectionException ; 21 import java.beans.PropertyDescriptor ; 22 import java.beans.SimpleBeanInfo ; 23 import java.lang.reflect.Method ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 44 public abstract class TagBeanInfo extends SimpleBeanInfo { 45 49 52 private static final int BEANINFO_CHAR_COUNT = 8; 53 54 57 private static final String SETTER_PREFIX = "set"; 58 59 62 private static final int SETTER_PREFIX_LENGTH = SETTER_PREFIX.length(); 63 64 67 private static final int SETTER_PARAM_COUNT = 1; 68 69 70 71 75 78 private static Log log = LogFactory.getLog(TagBeanInfo.class); 79 80 81 82 86 89 public TagBeanInfo() { 90 super(); 91 } 92 93 94 95 99 106 public final PropertyDescriptor [] getPropertyDescriptors() { 110 List methods = getTagHandlerSetters(); 111 112 List props = new ArrayList (); 113 for (Iterator i = methods.iterator(); i.hasNext(); ) { 114 props.add(createPropertyDescriptor((Method ) i.next())); 115 } 116 117 if (log.isDebugEnabled()) { 118 log.debug("Property descriptors: '" + props + "'."); 119 } 120 121 return (PropertyDescriptor []) 122 props.toArray(new PropertyDescriptor [props.size()]); 123 } 124 125 126 127 131 137 List getTagHandlerSetters() { 138 String beanInfoClassName = getClass().getName(); 139 140 String tagHandlerClassName = beanInfoClassName.substring( 142 0, beanInfoClassName.length() - BEANINFO_CHAR_COUNT); 143 144 Class tagHandlerClass; 145 try { 146 tagHandlerClass = Class.forName(tagHandlerClassName); 147 } 148 catch (ClassNotFoundException e) { 149 throw new RuntimeException (e); 150 } 151 152 Method [] methods = tagHandlerClass.getMethods(); 153 List setters = new ArrayList (); 154 for (int i = 0; i < methods.length; i++) { 155 if (isSetter(methods[i])) { 156 setters.add(methods[i]); 157 } 158 } 159 160 return setters; 161 } 162 163 170 private boolean isSetter(Method method) { 171 String methodName = method.getName(); 172 return methodName.length() > SETTER_PREFIX_LENGTH && 173 methodName.startsWith(SETTER_PREFIX) && 174 Character.isUpperCase(methodName.charAt(SETTER_PREFIX_LENGTH)) && 175 method.getParameterTypes().length == SETTER_PARAM_COUNT; 176 } 177 178 186 private PropertyDescriptor createPropertyDescriptor(Method setterMethod) { 187 String methodName = setterMethod.getName(); 188 String propName = String.valueOf( 189 Character.toLowerCase(methodName.charAt(SETTER_PREFIX_LENGTH))) + 190 methodName.substring(SETTER_PREFIX_LENGTH + 1); 191 try { 192 return new PropertyDescriptor (propName, null, setterMethod); 193 } 194 catch (IntrospectionException e) { 195 throw new RuntimeException (e); 196 } 197 } 198 } | Popular Tags |