001    /*
002    // $Id: //open/mondrian/src/main/mondrian/olap/fun/ReflectiveMultiResolver.java#1 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2002-2002 Kana Software, Inc.
007    // Copyright (C) 2002-2006 Julian Hyde and others
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    */
011    package mondrian.olap.fun;
012    
013    import mondrian.olap.Exp;
014    import mondrian.olap.FunDef;
015    import mondrian.olap.Util;
016    
017    import java.lang.reflect.Constructor;
018    import java.lang.reflect.InvocationTargetException;
019    
020    /**
021     * Resolver which uses reflection to instantiate a {@link FunDef}.
022     * This reduces the amount of anonymous classes.
023     *
024     * @author jhyde
025     * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/ReflectiveMultiResolver.java#1 $
026     * @since Mar 23, 2006
027     */
028    public class ReflectiveMultiResolver extends MultiResolver {
029        private final Constructor constructor;
030        private final String[] reservedWords;
031    
032        public ReflectiveMultiResolver(
033                String name, String signature, String description,
034                String[] signatures, Class clazz) {
035            this(name, signature, description, signatures, clazz, null);
036        }
037    
038        public ReflectiveMultiResolver(
039                String name, String signature, String description,
040                String[] signatures, Class clazz,
041                String[] reservedWords) {
042            super(name, signature, description, signatures);
043            try {
044                this.constructor = clazz.getConstructor(new Class[] {FunDef.class});
045            } catch (NoSuchMethodException e) {
046                throw Util.newInternal(e, "Error while registering resolver class " + clazz);
047            }
048            this.reservedWords = reservedWords;
049        }
050    
051        protected FunDef createFunDef(Exp[] args, FunDef dummyFunDef) {
052            try {
053                return (FunDef) constructor.newInstance(new Object[] {dummyFunDef});
054            } catch (InstantiationException e) {
055                throw Util.newInternal(e, "Error while instantiating FunDef '" + getSignature() + "'");
056            } catch (IllegalAccessException e) {
057                throw Util.newInternal(e, "Error while instantiating FunDef '" + getSignature() + "'");
058            } catch (InvocationTargetException e) {
059                throw Util.newInternal(e, "Error while instantiating FunDef '" + getSignature() + "'");
060            }
061        }
062    
063        public String[] getReservedWords() {
064            if (reservedWords != null) {
065                return reservedWords;
066            }
067            return super.getReservedWords();
068        }
069    }
070    
071    // End ReflectiveMultiResolver.java