KickJava   Java API By Example, From Geeks To Geeks.

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


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
27 import org.jboss.reflect.spi.ClassInfo;
28 import org.jboss.reflect.spi.TypeInfo;
29 import org.jboss.util.JBossStringBuilder;
30
31 /**
32  * Array metadata.
33  *
34  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
35  * @version $Revision: 56338 $
36  */

37 public class AbstractArrayMetaData extends AbstractListMetaData
38 {
39    /**
40     * Create a new array value
41     */

42    public AbstractArrayMetaData()
43    {
44    }
45
46    public Object JavaDoc getValue(TypeInfo info, ClassLoader JavaDoc cl) throws Throwable JavaDoc
47    {
48       Collection JavaDoc<? extends Object JavaDoc> result = (Collection JavaDoc<? extends Object JavaDoc>) super.getValue(info, cl);
49
50       TypeInfo typeInfo = getClassInfo(cl);
51       
52       if (typeInfo != null && typeInfo instanceof ClassInfo == false)
53          throw new IllegalArgumentException JavaDoc(typeInfo.getName() + " is not a class");
54
55       if (typeInfo == null)
56       {
57          // No type specified
58
if (info == null)
59          {
60             info = getElementClassInfo(cl);
61             if (info == null)
62                return null;
63             info = info.getArrayType(0);
64          }
65          // Not a class
66
if (info instanceof ClassInfo == false)
67             return null;
68          // Not an interface
69
if (((ClassInfo) info).isInterface())
70             return null;
71          // Type is too general
72
if (Object JavaDoc.class.getName().equals(info.getName()))
73             return null;
74          // Try to use the passed type
75
typeInfo = info;
76       }
77
78       Object JavaDoc[] array = new Object JavaDoc[result.size()];
79       if (typeInfo != null)
80          array = typeInfo.newArrayInstance(result.size());
81       
82       return result.toArray(array);
83    }
84
85    protected Collection JavaDoc<Object JavaDoc> getCollectionInstance(TypeInfo info, ClassLoader JavaDoc cl, Class JavaDoc expected) throws Throwable JavaDoc
86    {
87       Collection JavaDoc<Object JavaDoc> result = new ArrayList JavaDoc<Object JavaDoc>();
88       Object JavaDoc preinstantiatedObject = preinstantiatedLookup(cl, null);
89       if (preinstantiatedObject != null)
90       {
91          if (preinstantiatedObject.getClass().isArray() == false)
92             throw new ClassCastException JavaDoc("Preinstantiated property is not an array: " + propertyName);
93          Object JavaDoc[] preinstantiatedArray = (Object JavaDoc[]) preinstantiatedObject;
94          for(Object JavaDoc previous : preinstantiatedArray)
95          {
96             result.add(previous);
97          }
98       }
99       return result;
100    }
101
102    public void toString(JBossStringBuilder buffer)
103    {
104       super.toString(buffer);
105    }
106 }
Popular Tags