001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.boolex;
015
016import java.util.ArrayList;
017import java.util.List;
018
019import ch.qos.logback.access.spi.IAccessEvent;
020import ch.qos.logback.core.CoreConstants;
021import ch.qos.logback.core.boolex.JaninoEventEvaluatorBase;
022import ch.qos.logback.core.boolex.Matcher;
023
024public class JaninoEventEvaluator extends JaninoEventEvaluatorBase<IAccessEvent> {
025
026    public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<>();
027    public final static List<Class<?>> DEFAULT_PARAM_TYPE_LIST = new ArrayList<>();
028
029    static {
030        DEFAULT_PARAM_NAME_LIST.add("event");
031        DEFAULT_PARAM_TYPE_LIST.add(IAccessEvent.class);
032    }
033
034    @Override
035    protected String getDecoratedExpression() {
036        String expression = getExpression();
037        if (!expression.contains("return")) {
038            expression = "return " + expression + ";";
039            addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes [" + expression + "]");
040            addInfo("See also " + CoreConstants.CODES_URL + "#block");
041        }
042        return expression;
043    }
044
045    @Override
046    protected String[] getParameterNames() {
047        List<String> fullNameList = new ArrayList<String>();
048        fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
049
050        for (int i = 0; i < matcherList.size(); i++) {
051            Matcher m = (Matcher) matcherList.get(i);
052            fullNameList.add(m.getName());
053        }
054
055        return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
056    }
057
058    @Override
059    protected Class<?>[] getParameterTypes() {
060        List<Class<?>> fullTypeList = new ArrayList<>();
061        fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
062        for (int i = 0; i < matcherList.size(); i++) {
063            fullTypeList.add(Matcher.class);
064        }
065        return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
066    }
067
068    @Override
069    protected Object[] getParameterValues(IAccessEvent accessEvent) {
070        final int matcherListSize = matcherList.size();
071
072        int i = 0;
073        Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size() + matcherListSize];
074
075        values[i++] = accessEvent;
076
077        for (int j = 0; j < matcherListSize; j++) {
078            values[i++] = matcherList.get(j);
079        }
080
081        return values;
082    }
083
084}