1 22 package org.jboss.beans.metadata.plugins; 23 24 import java.util.*; 25 26 import org.jboss.beans.info.spi.BeanInfo; 27 import org.jboss.beans.metadata.spi.MetaDataVisitor; 28 import org.jboss.beans.metadata.spi.MetaDataVisitorNode; 29 import org.jboss.beans.metadata.spi.ValueMetaData; 30 import org.jboss.joinpoint.spi.Joinpoint; 31 import org.jboss.reflect.spi.ClassInfo; 32 import org.jboss.reflect.spi.TypeInfo; 33 import org.jboss.util.JBossStringBuilder; 34 35 41 public class AbstractMapMetaData extends AbstractTypeMetaData implements Map<MetaDataVisitorNode, MetaDataVisitorNode> 42 { 43 44 private HashMap<MetaDataVisitorNode, MetaDataVisitorNode> map = new HashMap<MetaDataVisitorNode, MetaDataVisitorNode>(); 45 46 47 protected String keyType; 48 49 50 protected String valueType; 51 52 55 public AbstractMapMetaData() 56 { 57 } 58 59 64 public String getKeyType() 65 { 66 return keyType; 67 } 68 69 74 public void setKeyType(String keyType) 75 { 76 this.keyType = keyType; 77 } 78 79 84 public String getValueType() 85 { 86 return valueType; 87 } 88 89 94 public void setValueType(String valueType) 95 { 96 this.valueType = valueType; 97 } 98 99 public Object getValue(TypeInfo info, ClassLoader cl) throws Throwable 100 { 101 Map<Object , Object > result = getMapInstance(info, cl, Map.class); 102 if (result == null) 103 result = getDefaultMapInstance(); 104 105 TypeInfo keyTypeInfo = getKeyClassInfo(cl); 106 TypeInfo valueTypeInfo = getValueClassInfo(cl); 107 108 if (map.size() > 0) 109 { 110 for (Iterator i = map.entrySet().iterator(); i.hasNext();) 111 { 112 Map.Entry entry = (Map.Entry) i.next(); 113 ValueMetaData key = (ValueMetaData) entry.getKey(); 114 ValueMetaData value = (ValueMetaData) entry.getValue(); 115 Object keyValue = key.getValue(keyTypeInfo, cl); 116 Object valueValue = value.getValue(valueTypeInfo, cl); 117 result.put(keyValue, valueValue); 118 } 119 } 120 return result; 121 } 122 123 public void clear() 124 { 125 map.clear(); 126 } 127 128 public boolean containsKey(Object key) 129 { 130 return map.containsKey(key); 131 } 132 133 public boolean containsValue(Object value) 134 { 135 return map.containsValue(value); 136 } 137 138 public Set<Entry<MetaDataVisitorNode, MetaDataVisitorNode>> entrySet() 139 { 140 return map.entrySet(); 141 } 142 143 public MetaDataVisitorNode get(Object key) 144 { 145 return map.get(key); 146 } 147 148 public boolean isEmpty() 149 { 150 return map.isEmpty(); 151 } 152 153 public Set<MetaDataVisitorNode> keySet() 154 { 155 return map.keySet(); 156 } 157 158 public MetaDataVisitorNode put(MetaDataVisitorNode key, MetaDataVisitorNode value) 159 { 160 return map.put(key, value); 161 } 162 163 public void putAll(Map<? extends MetaDataVisitorNode, ? extends MetaDataVisitorNode> t) 164 { 165 putAll(t); 166 167 } 168 169 public MetaDataVisitorNode remove(Object key) 170 { 171 return map.remove(key); 172 } 173 174 public int size() 175 { 176 return map.size(); 177 } 178 179 public Collection<MetaDataVisitorNode> values() 180 { 181 return map.values(); 182 } 183 184 public Iterator<? extends MetaDataVisitorNode> getChildren() 185 { 186 ArrayList<MetaDataVisitorNode> children = new ArrayList<MetaDataVisitorNode>(keySet()); 187 children.addAll(values()); 188 return children.iterator(); 189 } 190 191 public Class getType(MetaDataVisitor visitor, MetaDataVisitorNode previous) throws Throwable 192 { 193 if (keyType != null) 194 { 195 for(MetaDataVisitorNode key : keySet()) 196 { 197 if (previous.equals(key)) 198 { 199 return getClass(visitor, keyType); 200 } 201 } 202 } 203 if (valueType != null) 204 { 205 for(MetaDataVisitorNode v : values()) 206 { 207 if (previous.equals(v)) 208 { 209 return getClass(visitor, valueType); 210 } 211 } 212 } 213 return super.getType(visitor, previous); 214 } 215 216 public void toString(JBossStringBuilder buffer) 217 { 218 super.toString(buffer); 219 } 220 221 227 protected Map<Object , Object > getDefaultMapInstance() throws Throwable 228 { 229 return new HashMap<Object , Object >(); 230 } 231 232 241 @SuppressWarnings ("unchecked") 242 protected Map<Object , Object > getMapInstance(TypeInfo info, ClassLoader cl, Class <?> expected) throws Throwable 243 { 244 Object result = preinstantiatedLookup(cl, expected); 245 if (result == null) 246 { 247 TypeInfo typeInfo = getClassInfo(cl); 248 249 if (typeInfo != null && typeInfo instanceof ClassInfo == false) 250 throw new IllegalArgumentException (typeInfo.getName() + " is not a class"); 251 252 if (typeInfo != null && ((ClassInfo) typeInfo).isInterface()) 253 throw new IllegalArgumentException (typeInfo.getName() + " is an interface"); 254 255 if (typeInfo == null) 256 { 257 if (info == null) 259 return null; 260 if (info instanceof ClassInfo == false) 262 return null; 263 if (((ClassInfo) info).isInterface()) 265 return null; 266 if (Object .class.getName().equals(info.getName())) 268 return null; 269 typeInfo = info; 271 } 272 273 BeanInfo beanInfo = configurator.getBeanInfo(typeInfo); 274 Joinpoint constructor = configurator.getConstructorJoinPoint(beanInfo); 275 result = constructor.dispatch(); 276 if (expected.isAssignableFrom(result.getClass()) == false) 277 throw new ClassCastException (result.getClass() + " is not a " + expected.getName()); 278 } 279 return (Map<Object , Object >) result; 280 } 281 282 289 protected ClassInfo getKeyClassInfo(ClassLoader cl) throws Throwable 290 { 291 if (keyType == null) 292 return null; 293 294 return configurator.getClassInfo(keyType, cl); 295 } 296 297 304 protected ClassInfo getValueClassInfo(ClassLoader cl) throws Throwable 305 { 306 if (valueType == null) 307 return null; 308 309 return configurator.getClassInfo(valueType, cl); 310 } 311 } | Popular Tags |