Search in sources :

Example 1 with IScriptEvaluator

use of org.codehaus.commons.compiler.IScriptEvaluator in project vertigo by KleeGroup.

the class JaninoExpressionEvaluatorPlugin method evaluate.

/**
 * {@inheritDoc}
 */
@Override
public <J> J evaluate(final String expression, final List<ExpressionParameter> parameters, final Class<J> type) {
    Assertion.checkNotNull(expression);
    Assertion.checkNotNull(parameters);
    Assertion.checkNotNull(type);
    // -----
    // 0. Init Janino parameters defined by their names, types and values.
    final int size = parameters.size();
    final String[] parameterNames = new String[size];
    final Class<?>[] parameterTypes = new Class[size];
    final Object[] parameterValues = new Object[size];
    int i = 0;
    for (final ExpressionParameter parameter : parameters) {
        parameterNames[i] = parameter.getName();
        parameterTypes[i] = parameter.getType();
        parameterValues[i] = parameter.getValue();
        i++;
    }
    // 1. Build the scriptEvaluator
    final IScriptEvaluator scriptEvaluator = buildEvaluator(expression, type, parameterNames, parameterTypes);
    // 2.Evaluate the script
    return type.cast(doEvaluate(scriptEvaluator, parameterValues));
}
Also used : IScriptEvaluator(org.codehaus.commons.compiler.IScriptEvaluator) ExpressionParameter(io.vertigo.commons.script.ExpressionParameter)

Example 2 with IScriptEvaluator

use of org.codehaus.commons.compiler.IScriptEvaluator in project apex-malhar by apache.

the class PojoUtils method compileExpression.

private static Object compileExpression(String code, Class<?> implClass, String[] params, String[] defaultImports) {
    List<String> imports = new LinkedList<>();
    if (defaultImports != null && defaultImports.length != 0) {
        for (String defaultImport : defaultImports) {
            if (defaultImport != null) {
                if (!defaultImport.startsWith("static")) {
                    imports.add("static " + defaultImport);
                } else {
                    imports.add(defaultImport);
                }
            }
        }
    }
    IScriptEvaluator se;
    try {
        se = CompilerFactoryFactory.getDefaultCompilerFactory().newScriptEvaluator();
        se.setDefaultImports(imports.toArray(new String[imports.size()]));
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    try {
        logger.debug("code: {}", code);
        return se.createFastEvaluator(code, implClass, params);
    } catch (CompileException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : IScriptEvaluator(org.codehaus.commons.compiler.IScriptEvaluator) CompileException(org.codehaus.commons.compiler.CompileException) LinkedList(java.util.LinkedList) CompileException(org.codehaus.commons.compiler.CompileException)

Aggregations

IScriptEvaluator (org.codehaus.commons.compiler.IScriptEvaluator)2 ExpressionParameter (io.vertigo.commons.script.ExpressionParameter)1 LinkedList (java.util.LinkedList)1 CompileException (org.codehaus.commons.compiler.CompileException)1