use of org.beetl.core.statement.VarRef 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;
}
use of org.beetl.core.statement.VarRef in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseAssign.
/**
* 定义变量
* @param amc
* @return
*/
protected VarAssignStatement parseAssign(AssignMentContext amc) {
VarAssignStatement vas = null;
if (amc instanceof AssignGeneralInStContext) {
AssignGeneralInStContext agc = (AssignGeneralInStContext) amc;
ExpressionContext expCtx = agc.generalAssignExp().expression();
Expression exp = parseExpress(expCtx);
VarRefContext varRefCtx = agc.generalAssignExp().varRef();
if (varRefCtx.children.size() == 1) {
// var a=1;
Token token = varRefCtx.Identifier().getSymbol();
vas = new VarAssignStatement(exp, getBTToken(token));
} else {
// var a.b=1 since 2.7.0
VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
vas = new VarRefAssignStatement(exp, ref);
}
return vas;
} else if (amc instanceof AssignIdContext) {
AssignIdContext idCtx = (AssignIdContext) amc;
vas = new VarAssignStatement(Literal.NULLLiteral, getBTToken(idCtx.Identifier().getSymbol()));
return vas;
} else if (amc instanceof AssignTemplateVarContext) {
AssignTemplateVarContext templateVarCtx = (AssignTemplateVarContext) amc;
BlockContext blockCtx = templateVarCtx.block();
BlockStatement block = this.parseBlock(blockCtx.statement(), blockCtx);
ContentBodyExpression bodyExp = new ContentBodyExpression(block, getBTToken(templateVarCtx.Identifier().getSymbol()));
vas = new VarAssignStatement(bodyExp, getBTToken(templateVarCtx.Identifier().getSymbol()));
} else {
throw new RuntimeException("不支持 在 " + amc.start.getLine());
}
return vas;
}
use of org.beetl.core.statement.VarRef in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseAssingInExp.
/**
* 赋值变量
* @param agc
* @return
*/
protected VarRefAssignExpress parseAssingInExp(AssignGeneralInExpContext agc) {
VarRefAssignExpress vas = null;
ExpressionContext expCtx = agc.generalAssignExp().expression();
Expression exp = parseExpress(expCtx);
VarRefContext varRefCtx = agc.generalAssignExp().varRef();
VarRef ref = this.parseVarRefInLeftExpression(varRefCtx);
vas = new VarRefAssignExpress(exp, ref);
if (ref.attributes.length == 0) {
// 变量定义:
Token token = varRefCtx.Identifier().getSymbol();
if (pbCtx.hasDefined(token.getText()) != null) {
registerVar(vas);
return vas;
} else {
BeetlException ex = new BeetlException(BeetlException.VAR_NOT_DEFINED);
ex.pushToken(this.getBTToken(token));
throw ex;
}
}
return vas;
}
Aggregations