1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.clazz.bean;
17
18 import java.lang.reflect.Constructor;
19
20 import org.apache.commons.clazz.Clazz;
21 import org.apache.commons.clazz.ClazzAccessException;
22 import org.apache.commons.clazz.ClazzInstanceFactory;
23 import org.apache.commons.clazz.ClazzLoader;
24 import org.apache.commons.clazz.common.ClazzFeatureSupport;
25
26 /***
27 * An instance factory based on a Constructor. The first argument of the
28 * Constructor is always of type <code>Clazz</code>. This parameter is not
29 * included in the factory signature.
30 *
31 * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
32 * @version $Id: BeanClazzConstructorInstanceFactory.java,v 1.4 2004/02/19 23:58:38 scolebourne Exp $
33 */
34 public class BeanClazzConstructorInstanceFactory extends ClazzFeatureSupport
35 implements ClazzInstanceFactory
36 {
37
38
39
40
41
42 private Constructor constructor;
43 private Class visibleParameterTypes[];
44 private String signature;
45 private Clazz[] parameterClazzes;
46
47 /***
48 * Constructor for ReflectedClazzProperty.
49 */
50 public BeanClazzConstructorInstanceFactory(
51 Clazz declaringClazz,
52 Constructor constructor)
53 {
54 super(declaringClazz);
55 this.constructor = constructor;
56 }
57
58 public Constructor getConstructor() {
59 return constructor;
60 }
61
62 public String getName() {
63 return null;
64 }
65
66 /***
67 * @see org.apache.commons.clazz.ClazzOperation#getSignature()
68 */
69 public String getSignature() {
70 if (signature == null) {
71 signature =
72 Clazz.constructSignature(null, getVisibleParameterTypes());
73 }
74 return signature;
75 }
76
77 /***
78 * @see org.apache.commons.clazz.ClazzOperation#getParameterClazzes()
79 */
80 public Clazz[] getParameterClazzes() {
81 if (parameterClazzes == null) {
82 Class paramClasses[] = getVisibleParameterTypes();
83 parameterClazzes = new Clazz[paramClasses.length];
84 ClazzLoader loader = getDeclaringClazz().getClazzLoader();
85 for (int i = 0; i < paramClasses.length; i++) {
86 String name = Clazz.getCanonicalClassName(paramClasses[i]);
87 parameterClazzes[i] = loader.getClazzForName(name);
88 }
89 }
90 return parameterClazzes;
91 }
92
93 private Class[] getVisibleParameterTypes() {
94 Class parameterTypes[] = constructor.getParameterTypes();
95 visibleParameterTypes = new Class[parameterTypes.length - 1];
96 for (int i = 1; i < parameterTypes.length; i++) {
97 visibleParameterTypes[i - 1] = parameterTypes[i];
98 }
99 return visibleParameterTypes;
100 }
101
102 public Object newInstance(Object[] parameters) {
103 try {
104
105 int length = (parameters == null ? 0 : parameters.length);
106 Object allParameters[] = new Object[length + 1];
107 allParameters[0] = getDeclaringClazz();
108 for (int i = 0; i < length; i++) {
109 allParameters[i + 1] = parameters[i];
110 }
111 return constructor.newInstance(allParameters);
112 }
113 catch (Throwable e) {
114 throw new ClazzAccessException(
115 "Cannot invoke constructor " + getSignature(),
116 e);
117 }
118 }
119
120 public String toString() {
121 return getSignature();
122 }
123 }