001    /*
002     * Copyright 2002-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.clazz.reflect.common;
017    
018    import java.lang.reflect.Constructor;
019    
020    import org.apache.commons.clazz.Clazz;
021    import org.apache.commons.clazz.ClazzAccessException;
022    import org.apache.commons.clazz.ClazzInstanceFactory;
023    import org.apache.commons.clazz.ClazzLoader;
024    import org.apache.commons.clazz.common.ClazzFeatureSupport;
025    
026    /**
027     * A wrapper for a java constructor.
028     * 
029     * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
030     * @version $Id: ReflectedConstructorInstanceFactory.java 155436 2005-02-26 13:17:48Z dirkv $
031     */
032    public class ReflectedConstructorInstanceFactory extends ClazzFeatureSupport
033            implements ClazzInstanceFactory
034    {   
035        private Constructor constructor;
036        private String signature;
037        private static final Clazz[] EMPTY_CLAZZ_ARRAY = new Clazz[0];
038        private Clazz[] parameterClazzes;
039    
040        /**
041         * Constructor for ReflectedClazzProperty.
042         */
043        public ReflectedConstructorInstanceFactory(
044                Clazz declaringClazz,
045                Constructor constructor)
046        {
047            super(declaringClazz);
048            this.constructor = constructor;
049        }
050    
051        public Constructor getConstructor() {
052            return constructor;
053        }
054    
055        public String getName() {
056            return null;
057        }
058    
059        /**
060         * @see org.apache.commons.clazz.ClazzOperation#getSignature()
061         */
062        public String getSignature() {
063            if (signature == null) {
064                signature =
065                    Clazz.constructSignature(null, constructor.getParameterTypes());
066            }
067            return signature;
068        }
069    
070        /**
071         * @see org.apache.commons.clazz.ClazzOperation#getParameterClazzes()
072         */
073        public Clazz[] getParameterClazzes() {
074            if (parameterClazzes == null) {
075                Class paramClasses[] = constructor.getParameterTypes();
076                if (paramClasses == null) {
077                    parameterClazzes = EMPTY_CLAZZ_ARRAY;
078                }
079                else {
080                    parameterClazzes = new Clazz[paramClasses.length];
081                    ClazzLoader loader = getDeclaringClazz().getClazzLoader();
082                    for (int i = 0; i < paramClasses.length; i++) {
083                        String name = Clazz.getCanonicalClassName(paramClasses[i]);
084                        parameterClazzes[i] = loader.getClazzForName(name);
085                    }
086                }
087            }
088            return parameterClazzes;
089        }
090    
091        public Object newInstance(Object[] parameters) {
092            try {
093                return constructor.newInstance(parameters);
094            }
095            catch (Throwable e) {
096                throw new ClazzAccessException(
097                    "Cannot invoke constructor " + getSignature(),
098                    e);
099            }
100        }
101    
102        public String toString() {
103            return getSignature();
104        }
105    }