Search in sources :

Example 1 with VirtualClassAttribute

use of org.beetl.core.VirtualClassAttribute in project beetl2.0 by javamonkey.

the class VirtualAttributeTest method testVirtualClasAttribute.

@Test
public void testVirtualClasAttribute() throws Exception {
    gt.registerVirtualAttributeClass(User.class, new VirtualClassAttribute() {

        @Override
        public String eval(Object o, String attributeName, Context ctx) {
            User user = (User) o;
            if (user.getAge() < 10) {
                return "young";
            } else {
                return "old";
            }
        }
    });
    List list = User.getTestUsers();
    User user = User.getTestUser();
    Template t = gt.getTemplate("/va/class_virtual_template.html");
    this.bind(t, "list", list, "user", user);
    String str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/class_virtual_expected.html"), str);
    t = gt.getTemplate("/va/class_virtual_template.html");
    this.bind(t, "list", list, "user", user);
    str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/class_virtual_expected.html"), str);
}
Also used : Context(org.beetl.core.Context) User(org.beetl.core.User) VirtualClassAttribute(org.beetl.core.VirtualClassAttribute) JSONObject(com.alibaba.fastjson.JSONObject) List(java.util.List) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 2 with VirtualClassAttribute

use of org.beetl.core.VirtualClassAttribute in project beetl2.0 by javamonkey.

the class VarVirtualAttribute method evaluate.

public Object evaluate(Context ctx, Object o) {
    VirtualClassAttribute vae = ctx.gt.getVirtualAttributeEval(o.getClass(), name);
    if (vae == null) {
        BeetlException be = new BeetlException(BeetlException.VIRTUAL_NOT_FOUND);
        be.pushToken(token);
        throw be;
    }
    return vae.eval(o, name, ctx);
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) VirtualClassAttribute(org.beetl.core.VirtualClassAttribute)

Example 3 with VirtualClassAttribute

use of org.beetl.core.VirtualClassAttribute in project beetl2.0 by javamonkey.

the class VarVirtualAttribute method infer.

@Override
public void infer(InferContext inferCtx) {
    Type type = (Type) inferCtx.temp;
    VirtualClassAttribute vae = inferCtx.gt.getVirtualAttributeEval(type.cls, name);
    if (vae == null) {
        this.type = Type.ObjectType;
    } else {
        Method m = ObjectUtil.getGetMethod(vae.getClass(), "eval", new Class[] { Object.class, String.class, Context.class });
        this.type = new Type(m.getReturnType());
    }
}
Also used : VirtualClassAttribute(org.beetl.core.VirtualClassAttribute) Method(java.lang.reflect.Method)

Aggregations

VirtualClassAttribute (org.beetl.core.VirtualClassAttribute)3 JSONObject (com.alibaba.fastjson.JSONObject)1 Method (java.lang.reflect.Method)1 List (java.util.List)1 Context (org.beetl.core.Context)1 Template (org.beetl.core.Template)1 User (org.beetl.core.User)1 BeetlException (org.beetl.core.exception.BeetlException)1 Test (org.testng.annotations.Test)1