1 23 24 package org.objectweb.fractal.adl.attributes; 25 26 import java.lang.reflect.Method ; 27 28 31 32 public class JavaAttributeBuilder implements AttributeBuilder { 33 34 38 public void setAttribute ( 39 final Object component, 40 final String attributeController, 41 final String name, 42 final String value, 43 final Object context) throws Exception 44 { 45 Class c = component.getClass(); 46 47 String attrName = Character.toUpperCase(name.charAt(0)) + name.substring(1); 48 49 String getterName = "get" + attrName; 51 String setterName = "set" + attrName; 52 Method getter = c.getMethod(getterName, new Class [0]); 53 Method setter = c.getMethod(setterName, new Class [] { 54 getter.getReturnType() 55 }); 56 Class attrType = getter.getReturnType(); 57 Object attrValue; 58 if (attrType.equals(String .class)) { 59 attrValue = value; 60 } else if (attrType.isPrimitive()) { 61 if (attrType.equals(Integer.TYPE)) { 62 attrValue = Integer.valueOf(value); 63 } else if (attrType.equals(Long.TYPE)) { 64 attrValue = Long.valueOf(value); 65 } else if (attrType.equals(Float.TYPE)) { 66 attrValue = Float.valueOf(value); 67 } else if (attrType.equals(Double.TYPE)) { 68 attrValue = Double.valueOf(value); 69 } else if (attrType.equals(Byte.TYPE)) { 70 attrValue = Byte.valueOf(value); 71 } else if (attrType.equals(Character.TYPE)) { 72 if (value.length() != 1) { 73 throw new Exception ("Bad char value: " + value); 74 } 75 attrValue = new Character (value.charAt(0)); 76 } else if (attrType.equals(Short.TYPE)) { 77 attrValue = Short.valueOf(value); 78 } else if (attrType.equals(Boolean.TYPE)) { 79 if (!value.equals("true") && 80 !value.equals("false")) 81 { 82 throw new Exception ("Bad boolean value: " + value); 83 } 84 attrValue = new Boolean (value.equals("true")); 85 } else { 86 throw new Exception ("Unexpected case"); 87 } 88 } else { 89 throw new Exception ("Unsupported attribute type: " + attrType); 90 } 91 setter.invoke(component, new Object []{attrValue}); 92 } 93 } 94 | Popular Tags |