View Javadoc

1   /*
2    * Copyright 2002-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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 155436 2005-02-26 13:17:48Z dirkv $
33   */
34  public class BeanClazzConstructorInstanceFactory extends ClazzFeatureSupport
35         implements ClazzInstanceFactory 
36  {
37  
38      // Though this class is very similar to ReflectedConstructorInstanceFactory,
39      // we have a separate implementation because the first parameter (Clazz) of
40      // the constructor is ignored here and that affects almost every method.
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             // Insert the declaring clazz as the invisible first parameter
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 }