1 9 package org.jboss.portal.common.command; 10 11 import java.lang.reflect.Field ; 12 13 import org.apache.log4j.Logger; 14 15 19 public class ReflectedConfigurator implements Configurator 20 { 21 22 private static final Logger log = Logger.getLogger(ReflectedConfigurator.class); 23 private final Object target; 24 25 public ReflectedConfigurator(Object target) 26 { 27 if (target == null) 28 { 29 throw new IllegalArgumentException ("Target cannot be null"); 30 } 31 this.target = target; 32 } 33 34 private Field getTargetField(String name) throws NoSuchFieldException 35 { 36 return getClass().getField(name); 37 } 38 39 public boolean contains(AttributeType attribute) 40 { 41 try 42 { 43 getClass().getField(attribute.getName()); 44 return true; 45 } 46 catch(NoSuchFieldException ignore) 47 { 48 } 49 catch(SecurityException ignore) 50 { 51 } 52 return false; 53 } 54 55 public void setObject(AttributeType attribute, Object value) throws NoSuchAttachmentException 56 { 57 try 58 { 59 getTargetField(attribute.getName()).set(target, value); 60 } 61 catch (IllegalAccessException e) 62 { 63 throw new NoSuchAttachmentException(e); 64 } 65 catch (NoSuchFieldException e) 66 { 67 throw new NoSuchAttachmentException(e); 68 } 69 } 70 71 public void setInt(AttributeType attribute, int value) throws NoSuchAttachmentException 72 { 73 try 74 { 75 getTargetField(attribute.getName()).setInt(target, value); 76 } 77 catch (IllegalAccessException e) 78 { 79 throw new NoSuchAttachmentException(e); 80 } 81 catch (NoSuchFieldException e) 82 { 83 throw new NoSuchAttachmentException(e); 84 } 85 } 86 87 public void setBoolean(AttributeType attribute, boolean value) throws NoSuchAttachmentException 88 { 89 try 90 { 91 getTargetField(attribute.getName()).setBoolean(target, value); 92 } 93 catch (IllegalAccessException e) 94 { 95 throw new NoSuchAttachmentException(e); 96 } 97 catch (NoSuchFieldException e) 98 { 99 throw new NoSuchAttachmentException(e); 100 } 101 } 102 103 public void setFloat(AttributeType attribute, float value) throws NoSuchAttachmentException 104 { 105 try 106 { 107 getTargetField(attribute.getName()).setFloat(target, value); 108 } 109 catch (IllegalAccessException e) 110 { 111 throw new NoSuchAttachmentException(e); 112 } 113 catch (NoSuchFieldException e) 114 { 115 throw new NoSuchAttachmentException(e); 116 } 117 } 118 119 public void setLong(AttributeType attribute, long value) throws NoSuchAttachmentException 120 { 121 try 122 { 123 getTargetField(attribute.getName()).setLong(target, value); 124 } 125 catch (IllegalAccessException e) 126 { 127 throw new NoSuchAttachmentException(e); 128 } 129 catch (NoSuchFieldException e) 130 { 131 throw new NoSuchAttachmentException(e); 132 } 133 } 134 135 public void setDouble(AttributeType attribute, double value) throws NoSuchAttachmentException 136 { 137 try 138 { 139 getTargetField(attribute.getName()).setDouble(target, value); 140 } 141 catch (IllegalAccessException e) 142 { 143 throw new NoSuchAttachmentException(e); 144 } 145 catch (NoSuchFieldException e) 146 { 147 throw new NoSuchAttachmentException(e); 148 } 149 } 150 151 public void setChar(AttributeType attribute, char value) throws NoSuchAttachmentException 152 { 153 try 154 { 155 getTargetField(attribute.getName()).setChar(target, value); 156 } 157 catch (IllegalAccessException e) 158 { 159 throw new NoSuchAttachmentException(e); 160 } 161 catch (NoSuchFieldException e) 162 { 163 throw new NoSuchAttachmentException(e); 164 } 165 } 166 167 public void setShort(AttributeType attribute, short value) throws NoSuchAttachmentException 168 { 169 try 170 { 171 getTargetField(attribute.getName()).setShort(target, value); 172 } 173 catch (IllegalAccessException e) 174 { 175 throw new NoSuchAttachmentException(e); 176 } 177 catch (NoSuchFieldException e) 178 { 179 throw new NoSuchAttachmentException(e); 180 } 181 } 182 183 public void setByte(AttributeType attribute, byte value) throws NoSuchAttachmentException 184 { 185 try 186 { 187 getTargetField(attribute.getName()).setByte(target, value); 188 } 189 catch (IllegalAccessException e) 190 { 191 throw new NoSuchAttachmentException(e); 192 } 193 catch (NoSuchFieldException e) 194 { 195 throw new NoSuchAttachmentException(e); 196 } 197 } 198 199 public final Object getObject(AttributeType attribute) throws NoSuchAttachmentException 200 { 201 try 202 { 203 return getTargetField(attribute.getName()).get(target); 204 } 205 catch (IllegalAccessException e) 206 { 207 throw new NoSuchAttachmentException(e); 208 } 209 catch (NoSuchFieldException e) 210 { 211 throw new NoSuchAttachmentException(e); 212 } 213 } 214 215 public final int getInt(AttributeType attribute) throws NoSuchAttachmentException 216 { 217 try 218 { 219 return getTargetField(attribute.getName()).getInt(target); 220 } 221 catch (IllegalAccessException e) 222 { 223 throw new NoSuchAttachmentException(e); 224 } 225 catch (NoSuchFieldException e) 226 { 227 throw new NoSuchAttachmentException(e); 228 } 229 } 230 231 public final boolean getBoolean(AttributeType attribute) throws NoSuchAttachmentException 232 { 233 try 234 { 235 return getTargetField(attribute.getName()).getBoolean(target); 236 } 237 catch (IllegalAccessException e) 238 { 239 throw new NoSuchAttachmentException(e); 240 } 241 catch (NoSuchFieldException e) 242 { 243 throw new NoSuchAttachmentException(e); 244 } 245 } 246 247 public final float getFloat(AttributeType attribute) throws NoSuchAttachmentException 248 { 249 try 250 { 251 return getTargetField(attribute.getName()).getFloat(target); 252 } 253 catch (IllegalAccessException e) 254 { 255 throw new NoSuchAttachmentException(e); 256 } 257 catch (NoSuchFieldException e) 258 { 259 throw new NoSuchAttachmentException(e); 260 } 261 } 262 263 public final long getLong(AttributeType attribute) throws NoSuchAttachmentException 264 { 265 try 266 { 267 return getTargetField(attribute.getName()).getLong(target); 268 } 269 catch (IllegalAccessException e) 270 { 271 throw new NoSuchAttachmentException(e); 272 } 273 catch (NoSuchFieldException e) 274 { 275 throw new NoSuchAttachmentException(e); 276 } 277 } 278 279 public final double getDouble(AttributeType attribute) throws NoSuchAttachmentException 280 { 281 try 282 { 283 return getTargetField(attribute.getName()).getDouble(target); 284 } 285 catch (IllegalAccessException e) 286 { 287 throw new NoSuchAttachmentException(e); 288 } 289 catch (NoSuchFieldException e) 290 { 291 throw new NoSuchAttachmentException(e); 292 } 293 } 294 295 public final char getChar(AttributeType attribute) throws NoSuchAttachmentException 296 { 297 try 298 { 299 return getTargetField(attribute.getName()).getChar(target); 300 } 301 catch (IllegalAccessException e) 302 { 303 throw new NoSuchAttachmentException(e); 304 } 305 catch (NoSuchFieldException e) 306 { 307 throw new NoSuchAttachmentException(e); 308 } 309 } 310 311 public final short getShort(AttributeType attribute) throws NoSuchAttachmentException 312 { 313 try 314 { 315 return getTargetField(attribute.getName()).getShort(target); 316 } 317 catch (IllegalAccessException e) 318 { 319 throw new NoSuchAttachmentException(e); 320 } 321 catch (NoSuchFieldException e) 322 { 323 throw new NoSuchAttachmentException(e); 324 } 325 } 326 327 public final byte getByte(AttributeType attribute) throws NoSuchAttachmentException 328 { 329 try 330 { 331 return getTargetField(attribute.getName()).getByte(target); 332 } 333 catch (IllegalAccessException e) 334 { 335 throw new NoSuchAttachmentException(e); 336 } 337 catch (NoSuchFieldException e) 338 { 339 throw new NoSuchAttachmentException(e); 340 } 341 } 342 } 343 | Popular Tags |