1 package com.puppycrawl.tools.checkstyle.api; 20 21 import org.apache.commons.beanutils.BeanUtilsBean; 22 import org.apache.commons.beanutils.ConversionException; 23 import org.apache.commons.beanutils.ConvertUtilsBean; 24 import org.apache.commons.beanutils.PropertyUtils; 25 import org.apache.commons.beanutils.PropertyUtilsBean; 26 import org.apache.commons.beanutils.converters.AbstractArrayConverter; 27 import org.apache.commons.beanutils.converters.BooleanArrayConverter; 28 import org.apache.commons.beanutils.converters.BooleanConverter; 29 import org.apache.commons.beanutils.converters.ByteArrayConverter; 30 import org.apache.commons.beanutils.converters.ByteConverter; 31 import org.apache.commons.beanutils.converters.CharacterArrayConverter; 32 import org.apache.commons.beanutils.converters.CharacterConverter; 33 import org.apache.commons.beanutils.converters.DoubleArrayConverter; 34 import org.apache.commons.beanutils.converters.DoubleConverter; 35 import org.apache.commons.beanutils.converters.FloatArrayConverter; 36 import org.apache.commons.beanutils.converters.FloatConverter; 37 import org.apache.commons.beanutils.converters.IntegerArrayConverter; 38 import org.apache.commons.beanutils.converters.IntegerConverter; 39 import org.apache.commons.beanutils.converters.LongArrayConverter; 40 import org.apache.commons.beanutils.converters.LongConverter; 41 import org.apache.commons.beanutils.converters.ShortArrayConverter; 42 import org.apache.commons.beanutils.converters.ShortConverter; 43 44 import java.beans.PropertyDescriptor ; 45 import java.lang.reflect.InvocationTargetException ; 46 import java.util.ArrayList ; 47 import java.util.List ; 48 import java.util.StringTokenizer ; 49 50 51 56 public class AutomaticBean 57 implements Configurable, Contextualizable 58 { 59 60 private Configuration mConfiguration; 61 62 63 71 private static BeanUtilsBean createBeanUtilsBean() 72 { 73 final ConvertUtilsBean cub = new ConvertUtilsBean(); 74 75 77 final boolean[] booleanArray = new boolean[0]; 78 final byte[] byteArray = new byte[0]; 79 final char[] charArray = new char[0]; 80 final double[] doubleArray = new double[0]; 81 final float[] floatArray = new float[0]; 82 final int[] intArray = new int[0]; 83 final long[] longArray = new long[0]; 84 final short[] shortArray = new short[0]; 85 86 87 cub.register(new BooleanConverter(), Boolean.TYPE); 88 cub.register(new BooleanConverter(), Boolean .class); 89 cub.register( 90 new BooleanArrayConverter(), booleanArray.getClass()); 91 cub.register(new ByteConverter(), Byte.TYPE); 92 cub.register(new ByteConverter(), Byte .class); 93 cub.register( 94 new ByteArrayConverter(byteArray), byteArray.getClass()); 95 cub.register(new CharacterConverter(), Character.TYPE); 96 cub.register(new CharacterConverter(), Character .class); 97 cub.register( 98 new CharacterArrayConverter(), charArray.getClass()); 99 cub.register(new DoubleConverter(), Double.TYPE); 100 cub.register(new DoubleConverter(), Double .class); 101 cub.register( 102 new DoubleArrayConverter(doubleArray), doubleArray.getClass()); 103 cub.register(new FloatConverter(), Float.TYPE); 104 cub.register(new FloatConverter(), Float .class); 105 cub.register(new FloatArrayConverter(), floatArray.getClass()); 106 cub.register(new IntegerConverter(), Integer.TYPE); 107 cub.register(new IntegerConverter(), Integer .class); 108 cub.register(new IntegerArrayConverter(), intArray.getClass()); 109 cub.register(new LongConverter(), Long.TYPE); 110 cub.register(new LongConverter(), Long .class); 111 cub.register(new LongArrayConverter(), longArray.getClass()); 112 cub.register(new ShortConverter(), Short.TYPE); 113 cub.register(new ShortConverter(), Short .class); 114 cub.register(new ShortArrayConverter(), shortArray.getClass()); 115 cub.register(new StrArrayConverter(), String [].class); 122 cub.register(new IntegerArrayConverter(), Integer [].class); 123 124 127 return new BeanUtilsBean(cub, new PropertyUtilsBean()); 128 } 129 130 145 public final void configure(Configuration aConfiguration) 146 throws CheckstyleException 147 { 148 mConfiguration = aConfiguration; 149 150 final BeanUtilsBean beanUtils = createBeanUtilsBean(); 151 152 final String [] attributes = aConfiguration.getAttributeNames(); 154 155 for (int i = 0; i < attributes.length; i++) { 156 final String key = attributes[i]; 157 final String value = aConfiguration.getAttribute(key); 158 159 try { 160 final PropertyDescriptor pd = 164 PropertyUtils.getPropertyDescriptor(this, key); 165 if ((pd == null) || (pd.getWriteMethod() == null)) { 166 throw new CheckstyleException( 167 "Property '" + key + "' in module " 168 + aConfiguration.getName() 169 + " does not exist, please check the documentation"); 170 } 171 172 beanUtils.copyProperty(this, key, value); 174 } 175 catch (final InvocationTargetException e) { 176 throw new CheckstyleException( 177 "Cannot set property '" + key + "' in module " 178 + aConfiguration.getName() + " to '" + value 179 + "': " + e.getTargetException().getMessage(), e); 180 } 181 catch (final IllegalAccessException e) { 182 throw new CheckstyleException( 183 "cannot access " + key + " in " 184 + this.getClass().getName(), e); 185 } 186 catch (final NoSuchMethodException e) { 187 throw new CheckstyleException( 188 "cannot access " + key + " in " 189 + this.getClass().getName(), e); 190 } 191 catch (final IllegalArgumentException e) { 192 throw new CheckstyleException( 193 "illegal value '" + value + "' for property '" + key 194 + "' of module " + aConfiguration.getName(), e); 195 } 196 catch (final ConversionException e) { 197 throw new CheckstyleException( 198 "illegal value '" + value + "' for property '" + key 199 + "' of module " + aConfiguration.getName(), e); 200 } 201 202 } 203 204 finishLocalSetup(); 205 206 final Configuration[] childConfigs = aConfiguration.getChildren(); 207 for (int i = 0; i < childConfigs.length; i++) { 208 final Configuration childConfig = childConfigs[i]; 209 setupChild(childConfig); 210 } 211 } 212 213 219 public final void contextualize(Context aContext) 220 throws CheckstyleException 221 { 222 final BeanUtilsBean beanUtils = createBeanUtilsBean(); 223 224 final String [] attributes = aContext.getAttributeNames(); 226 227 for (int i = 0; i < attributes.length; i++) { 228 final String key = attributes[i]; 229 final Object value = aContext.get(key); 230 231 try { 232 beanUtils.copyProperty(this, key, value); 233 } 234 catch (final InvocationTargetException e) { 235 throw new CheckstyleException("cannot set property " 238 + key + " to value " + value + " in bean " 239 + this.getClass().getName(), e); 240 } 241 catch (final IllegalAccessException e) { 242 throw new CheckstyleException( 243 "cannot access " + key + " in " 244 + this.getClass().getName(), e); 245 } 246 catch (final IllegalArgumentException e) { 247 throw new CheckstyleException( 248 "illegal value '" + value + "' for property '" + key 249 + "' of bean " + this.getClass().getName(), e); 250 } 251 catch (final ConversionException e) { 252 throw new CheckstyleException( 253 "illegal value '" + value + "' for property '" + key 254 + "' of bean " + this.getClass().getName(), e); 255 } 256 } 257 } 258 259 263 protected final Configuration getConfiguration() 264 { 265 return mConfiguration; 266 } 267 268 276 protected void finishLocalSetup() throws CheckstyleException 277 { 278 } 279 280 289 protected void setupChild(Configuration aChildConf) 290 throws CheckstyleException 291 { 292 } 293 } 294 295 308 309 310 final class StrArrayConverter extends AbstractArrayConverter 311 { 312 315 private static final String [] MODEL = new String [0]; 316 317 320 public StrArrayConverter() 321 { 322 this.defaultValue = null; 323 this.useDefault = false; 324 } 325 326 332 public StrArrayConverter(Object aDefaultValue) 333 { 334 this.defaultValue = aDefaultValue; 335 this.useDefault = true; 336 } 337 338 350 public Object convert(Class aType, Object aValue) 351 throws ConversionException 352 { 353 if (aValue == null) { 355 if (useDefault) { 356 return (defaultValue); 357 } 358 throw new ConversionException("No value specified"); 359 } 360 361 if (MODEL.getClass() == aValue.getClass()) { 363 return (aValue); 364 } 365 366 try { 369 final List list = parseElements(aValue.toString()); 370 final String [] results = new String [list.size()]; 371 372 for (int i = 0; i < results.length; i++) { 373 results[i] = (String ) list.get(i); 374 } 375 return (results); 376 } 377 catch (final Exception e) { 378 if (useDefault) { 379 return (defaultValue); 380 } 381 throw new ConversionException(aValue.toString(), e); 382 } 383 } 384 385 405 protected List parseElements(final String aValue) 406 throws NullPointerException 407 { 408 if (aValue == null) { 410 throw new NullPointerException (); 411 } 412 413 String str = aValue.trim(); 415 if (str.startsWith("{") && str.endsWith("}")) { 416 str = str.substring(1, str.length() - 1); 417 } 418 419 final StringTokenizer st = new StringTokenizer (str, ","); 420 final List retVal = new ArrayList (); 421 422 while (st.hasMoreTokens()) { 423 final String token = st.nextToken(); 424 retVal.add(token.trim()); 425 } 426 427 return retVal; 428 } 429 } 430 | Popular Tags |