1 package org.apache.beehive.controls.api.properties; 2 19 20 import java.lang.annotation.Annotation ; 21 import java.lang.reflect.Proxy ; 22 import java.util.HashMap ; 23 import java.util.HashSet ; 24 import java.util.Set ; 25 26 30 public class BeanPropertyMap extends BaseMap implements PropertyMap,java.io.Serializable 31 { 32 private static final HashMap _primToObject = new HashMap (); 33 34 static 35 { 36 _primToObject.put(Integer.TYPE, Integer .class); 37 _primToObject.put(Long.TYPE, Long .class); 38 _primToObject.put(Short.TYPE, Short .class); 39 _primToObject.put(Byte.TYPE, Byte .class); 40 _primToObject.put(Float.TYPE, Float .class); 41 _primToObject.put(Double.TYPE, Double .class); 42 _primToObject.put(Character.TYPE, Character .class); 43 _primToObject.put(Boolean.TYPE, Boolean .class); 44 } 45 46 50 public BeanPropertyMap(Class mapClass) 51 { 52 setMapClass(mapClass); 53 } 54 55 60 public BeanPropertyMap(PropertyMap map) 61 { 62 setMapClass(map.getMapClass()); 63 setDelegateMap(map); 64 } 65 66 71 public <T extends Annotation > BeanPropertyMap(T annot) 72 { 73 try 76 { 77 Object handler = Proxy.getInvocationHandler(annot); 78 if (handler instanceof PropertySetProxy) 79 { 80 PropertySetProxy psp = (PropertySetProxy)handler; 81 setMapClass(psp.getPropertySet()); 82 setDelegateMap(psp.getPropertyMap()); 83 return; 84 } 85 } 86 catch (IllegalArgumentException iae) {} 88 _annot = annot; 89 setMapClass(annot.getClass()); 90 } 91 92 95 public synchronized void setProperty(PropertyKey key, Object value) 96 { 97 if (!isValidKey(key)) 98 throw new IllegalArgumentException ("Key " + key + " is not valid for " + getMapClass()); 99 100 Class propType = key.getPropertyType(); 104 if (value == null) 105 { 106 if (propType.isPrimitive() || propType.isAnnotation()) 107 throw new IllegalArgumentException ("Invalid null value for key " + key); 108 } 109 else 110 { 111 if (propType.isPrimitive()) 112 propType = (Class )_primToObject.get(propType); 113 114 if (!propType.isAssignableFrom(value.getClass())) 115 { 116 throw new IllegalArgumentException ("Value class (" + value.getClass() + 117 ") not of expected type: " + propType); 118 } 119 } 120 _properties.put(key, value); 121 _propertySets.add(key.getPropertySet()); 122 } 123 124 127 public Object getProperty(PropertyKey key) 128 { 129 if (!isValidKey(key)) 130 throw new IllegalArgumentException ("Key " + key + " is not valid for " + getMapClass()); 131 132 if (_properties.containsKey(key)) 136 return _properties.get(key); 137 138 if (_annot != null) 142 return key.extractValue(_annot); 143 144 return super.getProperty(key); 148 } 149 150 154 public boolean containsPropertySet(Class <? extends Annotation > propertySet) 155 { 156 if (_annot != null && _annot.getClass().equals(propertySet)) 159 return true; 160 161 if (_propertySets.contains(propertySet)) 162 return true; 163 164 return super.containsPropertySet(propertySet); 168 } 169 170 175 public Set <PropertyKey> getPropertyKeys() { return _properties.keySet(); } 176 177 Annotation _annot; 179 180 HashMap <PropertyKey,Object > _properties = new HashMap <PropertyKey,Object >(); 182 183 HashSet <Class > _propertySets = new HashSet <Class >(); 185 } 186 | Popular Tags |