001    /*
002    // $Id: //open/mondrian-release/3.2/src/main/mondrian/olap/fun/ReflectiveMultiResolver.java#1 $
003    // This software is subject to the terms of the Eclipse Public License v1.0
004    // Agreement, available at the following URL:
005    // http://www.eclipse.org/legal/epl-v10.html.
006    // Copyright (C) 2002-2002 Kana Software, Inc.
007    // Copyright (C) 2002-2009 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-release/3.2/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,
034            String signature,
035            String description,
036            String[] signatures,
037            Class clazz)
038        {
039            this(name, signature, description, signatures, clazz, null);
040        }
041    
042        public ReflectiveMultiResolver(
043            String name,
044            String signature,
045            String description,
046            String[] signatures,
047            Class clazz,
048            String[] reservedWords)
049        {
050            super(name, signature, description, signatures);
051            try {
052                this.constructor = clazz.getConstructor(new Class[] {FunDef.class});
053            } catch (NoSuchMethodException e) {
054                throw Util.newInternal(
055                    e, "Error while registering resolver class " + clazz);
056            }
057            this.reservedWords = reservedWords;
058        }
059    
060        protected FunDef createFunDef(Exp[] args, FunDef dummyFunDef) {
061            try {
062                return (FunDef) constructor.newInstance(new Object[] {dummyFunDef});
063            } catch (InstantiationException e) {
064                throw Util.newInternal(
065                    e, "Error while instantiating FunDef '" + getSignature() + "'");
066            } catch (IllegalAccessException e) {
067                throw Util.newInternal(
068                    e, "Error while instantiating FunDef '" + getSignature() + "'");
069            } catch (InvocationTargetException e) {
070                throw Util.newInternal(
071                    e, "Error while instantiating FunDef '" + getSignature() + "'");
072            }
073        }
074    
075        public String[] getReservedWords() {
076            if (reservedWords != null) {
077                return reservedWords;
078            }
079            return super.getReservedWords();
080        }
081    }
082    
083    // End ReflectiveMultiResolver.java