TemplateModel _getAsTemplateModel(Environment env) throws TemplateException {
TemplateModel targetModel = target.getAsTemplateModel(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 TemplateException("A macro cannot be called in an expression.", env);
}
Writer prevOut = env.getOut();
try {
env.setOut(Environment.NULL_WRITER);
env.visit(func, null, arguments.values, null, null);
} catch (IOException ioe) {
throw new InternalError("This should be impossible.");
} finally {
env.setOut(prevOut);
}
return env.getLastReturnValue();
}
else {
throw invalidTypeException(targetModel, target, env, "method");
}
}
|