private static TemplateRenderer tryToCompile(String source, Map<String, String> expressions, Map<String, String> constants, Class<?> modelType) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException { ClassPool cp = ClassPool.getDefault(); CtClass sup = cp.get(Object.class.getCanonicalName()); CtClass cls = cp.makeClass("RapidoidTemplate" + ID_GEN.incrementAndGet(), sup); cls.addInterface(cp.get(TemplateRenderer.class.getCanonicalName())); cls.addConstructor(CtNewConstructor.defaultConstructor(cls)); addExpressions(expressions, cls); addConstants(constants, cls); CtClass[] params = {cp.get(RenderCtx.class.getCanonicalName())}; CtClass clsVoid = cp.get(void.class.getCanonicalName()); cls.addMethod(CtNewMethod.make(Modifier.PUBLIC, clsVoid, "render", params, new CtClass[0], source, cls)); return (TemplateRenderer) cls.toClass().newInstance(); }
public TemplateRenderer compile(Class<?> modelType) { return TemplateCompiler.compile(this, modelType); }
private static String expr(Map<String, String> expressions, String expr) { expressions.put(expr, literal(expr)); return TemplateCompiler.retrieverId(expr); }
public static TemplateRenderer compile(XNode node, Class<?> modelType) { try { Map<String, String> expressions = U.map(); Map<String, String> constants = U.map(); String source = TemplateToCode.generate(node, expressions, constants, modelType); return tryToCompile(source, expressions, constants, modelType); } catch (Exception e) { throw U.rte(e); } }
public static TemplateRenderer compile(XNode node, Class<?> modelType) { try { Map<String, String> expressions = U.map(); Map<String, String> constants = U.map(); String source = TemplateToCode.generate(node, expressions, constants, modelType); return tryToCompile(source, expressions, constants, modelType); } catch (Exception e) { throw U.rte(e); } }
private static TemplateRenderer tryToCompile(String source, Map<String, String> expressions, Map<String, String> constants, Class<?> modelType) throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException { ClassPool cp = ClassPool.getDefault(); CtClass sup = cp.get(Object.class.getCanonicalName()); CtClass cls = cp.makeClass("RapidoidTemplate" + ID_GEN.incrementAndGet(), sup); cls.addInterface(cp.get(TemplateRenderer.class.getCanonicalName())); cls.addConstructor(CtNewConstructor.defaultConstructor(cls)); addExpressions(expressions, cls); addConstants(constants, cls); CtClass[] params = {cp.get(RenderCtx.class.getCanonicalName())}; CtClass clsVoid = cp.get(void.class.getCanonicalName()); cls.addMethod(CtNewMethod.make(Modifier.PUBLIC, clsVoid, "render", params, new CtClass[0], source, cls)); return (TemplateRenderer) cls.toClass().newInstance(); }
private static void addExpressions(Map<String, String> expressions, CtClass cls) throws CannotCompileException { for (Map.Entry<String, String> expr : expressions.entrySet()) { String fld = "private static final org.rapidoid.render.retriever.ValueRetriever %s = org.rapidoid.render.retriever.Retriever.of(%s);"; String retrieverId = retrieverId(expr.getKey()); String prop = expr.getValue(); String field = U.frmt(fld, retrieverId, prop); cls.addField(CtField.make(field, cls)); } }
public TemplateRenderer compile(Class<?> modelType) { return TemplateCompiler.compile(this, modelType); }
private static String expr(Map<String, String> expressions, String expr) { expressions.put(expr, literal(expr)); return TemplateCompiler.retrieverId(expr); }
private static void addExpressions(Map<String, String> expressions, CtClass cls) throws CannotCompileException { for (Map.Entry<String, String> expr : expressions.entrySet()) { String fld = "private static final org.rapidoid.render.retriever.ValueRetriever %s = org.rapidoid.render.retriever.Retriever.of(%s);"; String retrieverId = retrieverId(expr.getKey()); String prop = expr.getValue(); String field = U.frmt(fld, retrieverId, prop); cls.addField(CtField.make(field, cls)); } }