Search in sources :

Example 1 with TemplateMethodModel

use of freemarker.template.TemplateMethodModel in project freemarker by apache.

the class MethodCall method _eval.

@Override
TemplateModel _eval(Environment env) throws TemplateException {
    TemplateModel targetModel = target.eval(env);
    if (targetModel instanceof TemplateMethodModel) {
        TemplateMethodModel targetMethod = (TemplateMethodModel) targetModel;
        List argumentStrings = targetMethod instanceof TemplateMethodModelEx ? arguments.getModelList(env) : arguments.getValueList(env);
        Object result = targetMethod.exec(argumentStrings);
        return env.getObjectWrapper().wrap(result);
    } else if (targetModel instanceof Macro) {
        Macro func = (Macro) targetModel;
        env.setLastReturnValue(null);
        if (!func.isFunction()) {
            throw new _MiscTemplateException(env, "A macro cannot be called in an expression. (Functions can be.)");
        }
        Writer prevOut = env.getOut();
        try {
            env.setOut(NullWriter.INSTANCE);
            env.invoke(func, null, arguments.items, null, this);
        } catch (IOException e) {
            // Should not occur
            throw new TemplateException("Unexpected exception during function execution", e, env);
        } finally {
            env.setOut(prevOut);
        }
        return env.getLastReturnValue();
    } else {
        throw new NonMethodException(target, targetModel, env);
    }
}
Also used : TemplateException(freemarker.template.TemplateException) TemplateMethodModelEx(freemarker.template.TemplateMethodModelEx) List(java.util.List) ArrayList(java.util.ArrayList) TemplateModel(freemarker.template.TemplateModel) IOException(java.io.IOException) TemplateMethodModel(freemarker.template.TemplateMethodModel) Writer(java.io.Writer) NullWriter(freemarker.template.utility.NullWriter)

Aggregations

TemplateException (freemarker.template.TemplateException)1 TemplateMethodModel (freemarker.template.TemplateMethodModel)1 TemplateMethodModelEx (freemarker.template.TemplateMethodModelEx)1 TemplateModel (freemarker.template.TemplateModel)1 NullWriter (freemarker.template.utility.NullWriter)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1