use of org.beetl.core.om.AttributeAccess in project beetl2.0 by javamonkey.
the class VarAttributeNodeListener method onEvent.
@Override
public Object onEvent(Event e) {
Stack stack = (Stack) e.getEventTaget();
Object o = stack.peek();
if (o instanceof VarRef) {
VarRef ref = (VarRef) o;
VarAttribute[] attrs = ref.attributes;
for (int i = 0; i < attrs.length; i++) {
GroupTemplate gt = (GroupTemplate) ((Map) stack.get(0)).get("groupTemplate");
VarAttribute attr = attrs[i];
if (attr.getClass() == VarAttribute.class) {
Type type = attr.type;
String name = attr.token != null ? attr.token.text : null;
// 换成速度较快的属性访问类
try {
AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(type.cls, name, gt);
attr.aa = aa;
} catch (BeetlException ex) {
ex.pushToken(attr.token);
throw ex;
}
} else if (attr.getClass() == VarSquareAttribute.class) {
Type type = attr.type;
Class c = type.cls;
if (Map.class.isAssignableFrom(c)) {
attr.setAA(gt.getAttributeAccessFactory().getMapAA());
} else if (List.class.isAssignableFrom(c) || Set.class.isAssignableFrom(c)) {
attr.setAA(gt.getAttributeAccessFactory().getListAA());
} else if (c.isArray()) {
attr.setAA(gt.getAttributeAccessFactory().getArrayAA());
} else {
Expression exp = ((VarSquareAttribute) attr).exp;
if (exp instanceof Literal) {
Literal literal = (Literal) exp;
if (literal.obj instanceof String) {
try {
String attributeName = (String) literal.obj;
AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(c, attributeName, gt);
ref.attributes[i] = new VarSquareAttribute2((VarSquareAttribute) attrs[i], attributeName, aa);
} catch (BeetlException ex) {
ex.pushToken(attr.token);
throw ex;
}
}
}
}
} else if (attr.getClass() == VarVirtualAttribute.class) {
// 对虚拟属性~size做优化
if (attr.token.text.equals("size")) {
// 优化
Class c = attr.type.cls;
if (Map.class.isAssignableFrom(c)) {
ref.attributes[i] = new MapSizeVirtualAttribute((VarVirtualAttribute) attr);
} else if (Collection.class.isAssignableFrom(c)) {
ref.attributes[i] = new CollectionSizeVirtualAttribute((VarVirtualAttribute) attr);
} else if (c.isArray()) {
ref.attributes[i] = new ArraySizeVirtualAttribute((VarVirtualAttribute) attr);
}
}
}
}
}
return null;
}
Aggregations