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.bean;
017    
018    import org.apache.commons.clazz.Clazz;
019    import org.apache.commons.clazz.ClazzLoader;
020    import org.apache.commons.clazz.ClazzOperation;
021    import org.apache.commons.clazz.common.ClazzFeatureSupport;
022    
023    /**
024     * 
025     * @author <a href="mailto:dmitri@apache.org">Dmitri Plotnikov</a>
026     * @version $Id: BeanClazzOperation.java 155436 2005-02-26 13:17:48Z dirkv $
027     */
028    public abstract class BeanClazzOperation
029        extends ClazzFeatureSupport
030        implements ClazzOperation 
031    {
032        private String returnClazzName;
033        private Clazz returnClazz;
034        private String name;
035        private static final String[] EMPTY_STRING_ARRAY = new String[0];
036        private String[] parameterClazzNames;
037        private Clazz[] parameterClazzes;
038        private String signature;
039        
040        /**
041         * Constructor for BeanClazzOperaiton.
042         * 
043         * @param declaringClazz the Clazz this Operation belongs to
044         * @param returnClazzName the return type of the operation, pass
045         * <code>null</code> for <code>void</code>.
046         * @param name the name of the operation.
047         * @param parameterClazzNames types of operation parameters, pass
048         * <code>null</code> if the operation does not take any parameters.
049         */
050        public BeanClazzOperation(
051                Clazz declaringClazz,
052                String returnClazzName,
053                String name,
054                String[] parameterClazzNames) 
055        {
056            super(declaringClazz);
057            this.returnClazzName = returnClazzName;
058            this.name = name;
059            this.parameterClazzNames =
060                (parameterClazzNames != null
061                    ? parameterClazzNames
062                    : EMPTY_STRING_ARRAY);
063        }
064    
065        /**
066         * @see ClazzOperation#getSignature()
067         */
068        public String getSignature() {
069            if (signature == null) {
070                signature = Clazz.constructSignature(name, getParameterClazzes());
071            }
072    
073            return signature;
074        }
075    
076        /**
077         * @see ClazzOperation#getName()
078         */
079        public String getName() {
080            return name;
081        }
082    
083        /**
084         * @see ClazzOperation#getParameterClazzes()
085         */
086        public Clazz[] getParameterClazzes() {
087            if (parameterClazzes == null) {
088                parameterClazzes = new Clazz[parameterClazzNames.length];
089                ClazzLoader loader = getDeclaringClazz().getClazzLoader();
090                for (int i = 0; i < parameterClazzes.length; i++) {
091                    parameterClazzes[i] =
092                        loader.getClazzForName(parameterClazzNames[i]);
093                    if (parameterClazzes[i] == null) {
094                        throw new BeanClazzConfigurationException(
095                            "Invalid argument type: "
096                                + parameterClazzNames[i]
097                                + ". Clazz not found.");
098                    }
099                }
100            }
101    
102            return parameterClazzes;
103        }
104    
105        /**
106         * @see ClazzOperation#getReturnClazz()
107         */
108        public Clazz getReturnClazz() {
109            if (returnClazzName == null) {
110                return null;
111            }
112            
113            if (returnClazz == null) {
114                ClazzLoader loader = getDeclaringClazz().getClazzLoader();
115                returnClazz = loader.getClazzForName(returnClazzName);            
116            }
117    
118            return returnClazz;
119        }
120    
121        /**
122         * @see ClazzOperation#invoke(Object, Object[])
123         */
124        public abstract Object invoke(Object target, Object[] parameters);
125    }