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