Search in sources :

Example 1 with Type

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);
    }
}
Also used : Type(org.beetl.core.statement.Type) CommentTypeItemTagContext(org.beetl.core.parser.BeetlParser.CommentTypeItemTagContext) ClassOrInterfaceTypeContext(org.beetl.core.parser.BeetlParser.ClassOrInterfaceTypeContext)

Example 2 with 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;
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Type(org.beetl.core.statement.Type) ClassOrInterfaceTypeContext(org.beetl.core.parser.BeetlParser.ClassOrInterfaceTypeContext) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) TypeArgumentContext(org.beetl.core.parser.BeetlParser.TypeArgumentContext) TypeArgumentsContext(org.beetl.core.parser.BeetlParser.TypeArgumentsContext)

Example 3 with Type

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());
    }
}
Also used : Type(org.beetl.core.statement.Type) List(java.util.List) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with Type

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;
        }
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) Type(org.beetl.core.statement.Type)

Example 5 with Type

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;
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) BeetlException(org.beetl.core.exception.BeetlException) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) Set(java.util.Set) GroupTemplate(org.beetl.core.GroupTemplate) AttributeAccess(org.beetl.core.om.AttributeAccess) Stack(java.util.Stack) Type(org.beetl.core.statement.Type) Expression(org.beetl.core.statement.Expression) VarAttribute(org.beetl.core.statement.VarAttribute) Literal(org.beetl.core.statement.Literal) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map)

Aggregations

Type (org.beetl.core.statement.Type)5 BeetlException (org.beetl.core.exception.BeetlException)3 List (java.util.List)2 Map (java.util.Map)2 ClassOrInterfaceTypeContext (org.beetl.core.parser.BeetlParser.ClassOrInterfaceTypeContext)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 Stack (java.util.Stack)1 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)1 GroupTemplate (org.beetl.core.GroupTemplate)1 AttributeAccess (org.beetl.core.om.AttributeAccess)1 CommentTypeItemTagContext (org.beetl.core.parser.BeetlParser.CommentTypeItemTagContext)1 TypeArgumentContext (org.beetl.core.parser.BeetlParser.TypeArgumentContext)1 TypeArgumentsContext (org.beetl.core.parser.BeetlParser.TypeArgumentsContext)1 Expression (org.beetl.core.statement.Expression)1 Literal (org.beetl.core.statement.Literal)1 VarAttribute (org.beetl.core.statement.VarAttribute)1 VarRef (org.beetl.core.statement.VarRef)1 VarSquareAttribute (org.beetl.core.statement.VarSquareAttribute)1