1 22 package org.jboss.beans.metadata.plugins; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 28 import org.jboss.beans.info.spi.BeanInfo; 29 import org.jboss.beans.metadata.spi.MetaDataVisitor; 30 import org.jboss.beans.metadata.spi.MetaDataVisitorNode; 31 import org.jboss.beans.metadata.spi.ValueMetaData; 32 import org.jboss.joinpoint.spi.Joinpoint; 33 import org.jboss.reflect.spi.ClassInfo; 34 import org.jboss.reflect.spi.TypeInfo; 35 import org.jboss.util.JBossObject; 36 import org.jboss.util.JBossStringBuilder; 37 38 44 public class AbstractCollectionMetaData extends AbstractTypeMetaData implements Collection <MetaDataVisitorNode> 45 { 46 47 protected ArrayList <MetaDataVisitorNode> collection = new ArrayList <MetaDataVisitorNode>(); 48 49 50 protected String elementType; 51 52 55 public AbstractCollectionMetaData() 56 { 57 } 58 59 64 public String getElementType() 65 { 66 return elementType; 67 } 68 69 74 public void setElementType(String elementType) 75 { 76 this.elementType = elementType; 77 } 78 79 public Object getValue(TypeInfo info, ClassLoader cl) throws Throwable 80 { 81 Collection <Object > result = getCollectionInstance(info, cl, Collection .class); 82 if (result == null) 83 result = getDefaultCollectionInstance(); 84 85 TypeInfo elementTypeInfo = getElementClassInfo(cl); 86 87 for (int i = 0; i < collection.size(); ++i) 88 { 89 ValueMetaData vmd = (ValueMetaData) collection.get(i); 90 result.add(vmd.getValue(elementTypeInfo, cl)); 91 } 92 return result; 93 } 94 95 public Class getType(MetaDataVisitor visitor, MetaDataVisitorNode previous) throws Throwable 96 { 97 if (elementType != null) 98 { 99 return getClass(visitor, elementType); 100 } 101 return super.getType(visitor, previous); 102 } 103 104 public boolean add(MetaDataVisitorNode o) 105 { 106 return collection.add(o); 107 } 108 109 public boolean addAll(Collection <? extends MetaDataVisitorNode> c) 110 { 111 return collection.addAll(c); 112 } 113 114 public void clear() 115 { 116 collection.clear(); 117 } 118 119 public boolean contains(Object o) 120 { 121 return collection.contains(o); 122 } 123 124 public boolean containsAll(Collection c) 125 { 126 return collection.containsAll(c); 127 } 128 129 public boolean isEmpty() 130 { 131 return collection.isEmpty(); 132 } 133 134 public Iterator <MetaDataVisitorNode> iterator() 135 { 136 return collection.iterator(); 137 } 138 139 public boolean remove(Object o) 140 { 141 return collection.remove(o); 142 } 143 144 public boolean removeAll(Collection c) 145 { 146 return collection.removeAll(c); 147 } 148 149 public boolean retainAll(Collection c) 150 { 151 return collection.retainAll(c); 152 } 153 154 public int size() 155 { 156 return collection.size(); 157 } 158 159 public Object [] toArray() 160 { 161 return collection.toArray(); 162 } 163 164 public <T> T[] toArray(T[] a) 165 { 166 return collection.toArray(a); 167 } 168 169 public Iterator <? extends MetaDataVisitorNode> getChildren() 170 { 171 return collection.iterator(); 172 } 173 174 public void toString(JBossStringBuilder buffer) 175 { 176 super.toString(buffer); 177 buffer.append(" collection="); 178 JBossObject.list(buffer, collection); 179 } 180 181 187 protected Collection <Object > getDefaultCollectionInstance() throws Throwable 188 { 189 return new ArrayList <Object >(); 190 } 191 192 201 @SuppressWarnings ("unchecked") 202 protected Collection <Object > getCollectionInstance(TypeInfo info, ClassLoader cl, Class <?> expected) throws Throwable 203 { 204 Object result = preinstantiatedLookup(cl, expected); 205 if (result == null) 206 { 207 TypeInfo typeInfo = getClassInfo(cl); 208 209 if (typeInfo != null && typeInfo instanceof ClassInfo == false) 210 throw new IllegalArgumentException (typeInfo.getName() + " is not a class"); 211 212 if (typeInfo != null && ((ClassInfo) typeInfo).isInterface()) 213 throw new IllegalArgumentException (typeInfo.getName() + " is an interface"); 214 215 if (typeInfo == null) 216 { 217 if (info == null) 219 return null; 220 if (info instanceof ClassInfo == false) 222 return null; 223 if (((ClassInfo) info).isInterface()) 225 return null; 226 if (Object .class.getName().equals(info.getName())) 228 return null; 229 typeInfo = info; 231 } 232 233 BeanInfo beanInfo = configurator.getBeanInfo(typeInfo); 234 Joinpoint constructor = configurator.getConstructorJoinPoint(beanInfo); 235 result = constructor.dispatch(); 236 if (expected.isAssignableFrom(result.getClass()) == false) 237 throw new ClassCastException (result.getClass() + " is not a " + expected.getName()); 238 } 239 return (Collection <Object >) result; 240 } 241 242 249 protected ClassInfo getElementClassInfo(ClassLoader cl) throws Throwable 250 { 251 if (elementType == null) 252 return null; 253 254 return configurator.getClassInfo(elementType, cl); 255 } 256 } | Popular Tags |