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);
}
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);
}
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());
}
}
Aggregations