KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > beans > metadata > plugins > AbstractCollectionMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.beans.metadata.plugins;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
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 /**
39  * Collection metadata.
40  *
41  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
42  * @version $Revision: 56338 $
43  */

44 public class AbstractCollectionMetaData extends AbstractTypeMetaData implements Collection JavaDoc<MetaDataVisitorNode>
45 {
46    /** The collection */
47    protected ArrayList JavaDoc<MetaDataVisitorNode> collection = new ArrayList JavaDoc<MetaDataVisitorNode>();
48
49    /** The element type */
50    protected String JavaDoc elementType;
51
52    /**
53     * Create a new collection value
54     */

55    public AbstractCollectionMetaData()
56    {
57    }
58
59    /**
60     * Get the element type
61     *
62     * @return the element type
63     */

64    public String JavaDoc getElementType()
65    {
66       return elementType;
67    }
68
69    /**
70     * Set the element type
71     *
72     * @param elementType the element type
73     */

74    public void setElementType(String JavaDoc elementType)
75    {
76       this.elementType = elementType;
77    }
78
79    public Object JavaDoc getValue(TypeInfo info, ClassLoader JavaDoc cl) throws Throwable JavaDoc
80    {
81       Collection JavaDoc<Object JavaDoc> result = getCollectionInstance(info, cl, Collection JavaDoc.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 JavaDoc getType(MetaDataVisitor visitor, MetaDataVisitorNode previous) throws Throwable JavaDoc
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 JavaDoc<? 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 JavaDoc o)
120    {
121       return collection.contains(o);
122    }
123
124    public boolean containsAll(Collection JavaDoc c)
125    {
126       return collection.containsAll(c);
127    }
128
129    public boolean isEmpty()
130    {
131       return collection.isEmpty();
132    }
133
134    public Iterator JavaDoc<MetaDataVisitorNode> iterator()
135    {
136       return collection.iterator();
137    }
138
139    public boolean remove(Object JavaDoc o)
140    {
141       return collection.remove(o);
142    }
143
144    public boolean removeAll(Collection JavaDoc c)
145    {
146       return collection.removeAll(c);
147    }
148
149    public boolean retainAll(Collection JavaDoc c)
150    {
151       return collection.retainAll(c);
152    }
153
154    public int size()
155    {
156       return collection.size();
157    }
158
159    public Object JavaDoc[] 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 JavaDoc<? 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    /**
182     * Create the default collection instance
183     *
184     * @return the class instance
185     * @throws Throwable for any error
186     */

187    protected Collection JavaDoc<Object JavaDoc> getDefaultCollectionInstance() throws Throwable JavaDoc
188    {
189       return new ArrayList JavaDoc<Object JavaDoc>();
190    }
191
192    /**
193     * Create the collection instance
194     *
195     * @param info the request type
196     * @param cl the classloader
197     * @param expected the expected class
198     * @return the class instance
199     * @throws Throwable for any error
200     */

201    @SuppressWarnings JavaDoc("unchecked")
202    protected Collection JavaDoc<Object JavaDoc> getCollectionInstance(TypeInfo info, ClassLoader JavaDoc cl, Class JavaDoc<?> expected) throws Throwable JavaDoc
203    {
204       Object JavaDoc 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 JavaDoc(typeInfo.getName() + " is not a class");
211
212          if (typeInfo != null && ((ClassInfo) typeInfo).isInterface())
213             throw new IllegalArgumentException JavaDoc(typeInfo.getName() + " is an interface");
214
215          if (typeInfo == null)
216          {
217             // No type specified
218
if (info == null)
219                return null;
220             // Not a class
221
if (info instanceof ClassInfo == false)
222                return null;
223             // Is an interface
224
if (((ClassInfo) info).isInterface())
225                return null;
226             // Type is too general
227
if (Object JavaDoc.class.getName().equals(info.getName()))
228                return null;
229             // Try to use the passed type
230
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 JavaDoc(result.getClass() + " is not a " + expected.getName());
238       }
239       return (Collection JavaDoc<Object JavaDoc>) result;
240    }
241
242    /**
243     * Get the class info for the element type
244     *
245     * @param cl the classloader
246     * @return the class info
247     * @throws Throwable for any error
248     */

249    protected ClassInfo getElementClassInfo(ClassLoader JavaDoc cl) throws Throwable JavaDoc
250    {
251       if (elementType == null)
252          return null;
253
254       return configurator.getClassInfo(elementType, cl);
255    }
256 }
Popular Tags