use of org.beetl.core.statement.Type in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method parseCommentTag.
protected void parseCommentTag(CommentTypeTagContext typeCtx) {
List<CommentTypeItemTagContext> list = typeCtx.commentTypeItemTag();
for (CommentTypeItemTagContext ctx : list) {
ClassOrInterfaceTypeContext classCtx = ctx.classOrInterfaceType();
Type type = getClassType(classCtx);
String globalVarName = ctx.Identifier1().getSymbol().getText();
this.data.globalType.put(globalVarName, type);
}
}
use of org.beetl.core.statement.Type in project beetl2.0 by javamonkey.
the class AntlrProgramBuilder method getClassType.
private Type getClassType(ClassOrInterfaceTypeContext ctx) {
List<TerminalNode> list = ctx.Identifier1();
String className = this.getID(list);
Class cls = gt.loadClassBySimpleName(className);
if (cls == null) {
BeetlException ex = new BeetlException(BeetlException.TYPE_SEARCH_ERROR, className);
ex.pushToken(this.getBTToken(ctx.getStart()));
throw ex;
}
Type classType = new Type(cls);
TypeArgumentsContext typeArgCtx = ctx.typeArguments();
if (typeArgCtx != null) {
List<TypeArgumentContext> listType = typeArgCtx.typeArgument();
if (listType != null) {
Type[] subType = new Type[listType.size()];
int i = 0;
for (TypeArgumentContext typeCtx : listType) {
ClassOrInterfaceTypeContext child = typeCtx.classOrInterfaceType();
Type type = this.getClassType(child);
subType[i++] = type;
}
classType.types = subType;
}
}
return classType;
}
use of org.beetl.core.statement.Type in project beetl2.0 by javamonkey.
the class TypeBindingProbe method getType.
private Type getType(Object c) {
Type type = null;
;
if (c instanceof Map) {
Map<Object, Object> map = (Map<Object, Object>) c;
for (Entry<Object, Object> entry : map.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key == null || value == null)
return null;
if (value != null) {
type = new Type(Map.class, key.getClass(), value.getClass());
return type;
}
}
// 没有足够信息,还需要推测
return null;
} else if (c instanceof List) {
List<Object> list = (List<Object>) c;
for (Object o : list) {
if (o != null) {
type = new Type(List.class, o.getClass());
return type;
}
}
// 没有足够信息
return null;
} else if (c.getClass().isArray()) {
// Class probableType = c.getClass()
type = new Type(c.getClass(), c.getClass().getComponentType());
return type;
} else {
return new Type(c.getClass());
}
}
use of org.beetl.core.statement.Type in project beetl2.0 by javamonkey.
the class TypeBindingProbe method check.
@Override
public void check(Context ctx) {
if (isCompleted)
return;
int y = 0;
for (int i = 0; i < program.metaData.tempVarStartIndex; i++) {
if (types[i] == null) {
if (ctx.vars[i] != ctx.NOT_EXIST_OBJECT && ctx.vars[i] != null) {
Object o = ctx.vars[i];
if (isDynamicObject(ctx, i)) {
types[i] = Type.ObjectType;
y++;
continue;
}
Type c = getType(o);
if (c == null)
continue;
else {
types[i] = c;
y++;
}
} else {
continue;
}
} else {
y++;
}
}
// 推测完毕
if (y == program.metaData.tempVarStartIndex) {
try {
infer();
isCompleted = true;
// 调用下一个filter
nextFilter.setProgram(this.program);
nextFilter.check(ctx);
} catch (BeetlException bex) {
// bex.printStackTrace();
ProgramReplaceErrorEvent event = new ProgramReplaceErrorEvent(program.res.getId(), bex.getMessage(), bex);
program.gt.fireEvent(event);
isCompleted = true;
}
}
}
use of org.beetl.core.statement.Type 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