KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
25
26 import org.jboss.beans.info.spi.BeanInfo;
27 import org.jboss.beans.metadata.spi.*;
28 import org.jboss.kernel.plugins.config.Configurator;
29 import org.jboss.kernel.spi.config.KernelConfigurator;
30 import org.jboss.kernel.spi.dependency.KernelControllerContext;
31 import org.jboss.reflect.spi.ClassInfo;
32 import org.jboss.reflect.spi.ConstructorInfo;
33 import org.jboss.reflect.spi.MethodInfo;
34 import org.jboss.util.JBossObject;
35 import org.jboss.util.JBossStringBuilder;
36
37 /**
38  * Metadata for construction.
39  *
40  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
41  * @version $Revision: 56471 $
42  */

43 public class AbstractConstructorMetaData extends AbstractFeatureMetaData implements ConstructorMetaData
44 {
45    /**
46     * The paramaters List<ParameterMetaData>
47     */

48    protected List<ParameterMetaData> parameters;
49
50    /**
51     * The value
52     */

53    protected ValueMetaData value;
54
55    /**
56     * The factory
57     */

58    protected ValueMetaData factory;
59
60    /**
61     * The factory class name
62     */

63    protected String JavaDoc factoryClassName;
64
65    /**
66     * The factory method
67     */

68    protected String JavaDoc factoryMethod;
69
70    /**
71     * Create a new constructor meta data
72     */

73    public AbstractConstructorMetaData()
74    {
75    }
76
77    /**
78     * Set the parameters
79     *
80     * @param parameters List<ParameterMetaData>
81     */

82    public void setParameters(List<ParameterMetaData> parameters)
83    {
84       this.parameters = parameters;
85       flushJBossObjectCache();
86    }
87
88    /**
89     * Set the value
90     *
91     * @param value the value
92     */

93    public void setValue(ValueMetaData value)
94    {
95       this.value = value;
96       flushJBossObjectCache();
97    }
98
99    /**
100     * Set the factory
101     *
102     * @param factory the factory
103     */

104    public void setFactory(ValueMetaData factory)
105    {
106       this.factory = factory;
107       flushJBossObjectCache();
108    }
109
110    /**
111     * Set the factory class name
112     *
113     * @param name the factory class name
114     */

115    public void setFactoryClass(String JavaDoc name)
116    {
117       this.factoryClassName = name;
118       flushJBossObjectCache();
119    }
120
121    /**
122     * Set the factory method
123     *
124     * @param name the factory method
125     */

126    public void setFactoryMethod(String JavaDoc name)
127    {
128       this.factoryMethod = name;
129       flushJBossObjectCache();
130    }
131
132    public List<ParameterMetaData> getParameters()
133    {
134       return parameters;
135    }
136
137    public ValueMetaData getValue()
138    {
139       return value;
140    }
141
142    public ValueMetaData getFactory()
143    {
144       return factory;
145    }
146
147    public String JavaDoc getFactoryClass()
148    {
149       return factoryClassName;
150    }
151
152    public String JavaDoc getFactoryMethod()
153    {
154       return factoryMethod;
155    }
156
157    protected void addChildren(Set<MetaDataVisitorNode> children)
158    {
159       super.addChildren(children);
160       if (parameters != null)
161          children.addAll(parameters);
162       if (value != null)
163          children.add(value);
164       if (factory != null)
165          children.add(factory);
166    }
167
168    public Class JavaDoc getType(MetaDataVisitor visitor, MetaDataVisitorNode previous) throws Throwable JavaDoc
169    {
170       if (factory != null || factoryClassName != null)
171       {
172          KernelControllerContext context = visitor.getControllerContext();
173          ClassLoader JavaDoc cl = Configurator.getClassLoader(context.getBeanMetaData());
174          KernelConfigurator configurator = context.getKernel().getConfigurator();
175          ClassInfo classInfo;
176          if (factory != null)
177          {
178             Object JavaDoc target = factory.getValue(null, cl);
179             classInfo = configurator.getClassInfo(target.getClass());
180          }
181          else
182          {
183             classInfo = configurator.getClassInfo(factoryClassName, cl);
184          }
185          // should be parameter
186
ParameterMetaData parameter = (ParameterMetaData) previous;
187          String JavaDoc[] parameterTypes = Configurator.getParameterTypes(false, parameters);
188          MethodInfo methodInfo = Configurator.findMethodInfo(classInfo, factoryMethod, parameterTypes, factoryClassName != null, true);
189          return applyCollectionOrMapCheck(methodInfo.getParameterTypes()[parameter.getIndex()].getType());
190       }
191       else
192       {
193          KernelControllerContext context = visitor.getControllerContext();
194          BeanInfo beanInfo = context.getBeanInfo();
195          // find matching parameter
196
if (previous instanceof ParameterMetaData)
197          {
198             ParameterMetaData parameter = (ParameterMetaData) previous;
199             String JavaDoc[] paramTypes = Configurator.getParameterTypes(false, parameters);
200             ConstructorInfo ci = Configurator.findConstructorInfo(beanInfo.getClassInfo(), paramTypes);
201             return applyCollectionOrMapCheck(ci.getParameterTypes()[parameter.getIndex()].getType());
202          }
203          else
204          {
205             // currently value constructor supports only values that are instances of class itself
206
// this will add another instance with the same class to context
207
Class JavaDoc type = beanInfo.getClassInfo().getType();
208             log.warn("Constructing bean from injection value: results in multiple beans with same class type - " + type);
209             return type;
210 /*
211             // find all constructors with single value
212             Set<ConstructorInfo> constructors = beanInfo.getConstructors();
213             Set<ConstructorInfo> matchingConstructorInfos = new HashSet<ConstructorInfo>();
214             if (constructors != null)
215             {
216                for (ConstructorInfo ci : constructors)
217                {
218                   if (ci.getParameters() != null && ci.getParameters().length == 1)
219                   {
220                      matchingConstructorInfos.add(ci);
221                   }
222                }
223             }
224             if (matchingConstructorInfos.size() != 1)
225             {
226                throw new IllegalArgumentException("Should not be here - illegal size of matching constructors: " + this);
227             }
228             return applyCollectionOrMapCheck(matchingConstructorInfos.iterator().next().getParameterTypes()[0].getType());
229 */

230          }
231       }
232    }
233
234    public void toString(JBossStringBuilder buffer)
235    {
236       buffer.append("parameters=");
237       JBossObject.list(buffer, parameters);
238       if (value != null)
239          buffer.append(" value=").append(value);
240       if (factory != null)
241          buffer.append(" factory=").append(factory);
242       if (factoryClassName != null)
243          buffer.append(" factoryClass=").append(factoryClassName);
244       if (factoryMethod != null)
245          buffer.append(" factoryMethod=").append(factoryMethod);
246       super.toString(buffer);
247    }
248 }
249
Popular Tags