Search in sources :

Example 1 with Listener

use of org.beetl.core.Listener in project beetl2.0 by javamonkey.

the class StatementParser method exec.

protected void exec(Object astNode, Class[] matchClasses, Stack stack) {
    stack.push(astNode);
    Class astNodeClass = astNode.getClass();
    Field[] fields = astNodeClass.getFields();
    for (Field f : fields) {
        if (f.getModifiers() != Modifier.PUBLIC)
            continue;
        Class target = null;
        Class c = f.getType();
        if (c.isArray()) {
            target = c.getComponentType();
        } else {
            target = c;
        }
        // 只解析含有ASTNode的字段
        if (!ASTNode.class.isAssignableFrom(target))
            continue;
        Object values;
        try {
            // 需要判断的节点
            values = f.get(astNode);
            if (values == null)
                continue;
            // 具体类型
            Class target2 = values.getClass();
            if (target2.isArray()) {
                Object[] array = (Object[]) values;
                if (array.length == 0)
                    continue;
                for (int i = 0; i < array.length; i++) {
                    Object item = array[i];
                    if (item == null)
                        continue;
                    Class target3 = item.getClass();
                    if (match(target3, matchClasses)) {
                        stack.push(item);
                        Listener ls = this.listeners.get(target3);
                        NodeEvent e = new NodeEvent(stack);
                        Object newASTNode = ls.onEvent(e);
                        if (newASTNode != null) {
                            stack.pop();
                            stack.push(newASTNode);
                            // 替换原有节点
                            array[i] = newASTNode;
                            item = newASTNode;
                        }
                        // 继续遍历子节点
                        this.exec(item, matchClasses, stack);
                        stack.pop();
                    } else {
                        ASTNode node = (ASTNode) item;
                        this.exec(node, matchClasses, stack);
                    }
                }
            } else {
                if (match(target2, matchClasses)) {
                    stack.push(values);
                    Listener ls = this.listeners.get(target2);
                    NodeEvent e = new NodeEvent(stack);
                    Object newASTNode = ls.onEvent(e);
                    if (newASTNode != null) {
                        stack.pop();
                        stack.push(newASTNode);
                        // 替换原有节点
                        try {
                            f.set(astNode, newASTNode);
                        } catch (Exception ex) {
                            BeetlException be = new BeetlException(BeetlException.ERROR, "替换ASTNode错", ex);
                            be.pushToken(((ASTNode) newASTNode).token);
                            throw be;
                        }
                        values = newASTNode;
                    }
                    this.exec(values, matchClasses, stack);
                    stack.pop();
                    continue;
                } else {
                    ASTNode node = (ASTNode) values;
                    this.exec(node, matchClasses, stack);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    stack.pop();
}
Also used : Field(java.lang.reflect.Field) BeetlException(org.beetl.core.exception.BeetlException) Listener(org.beetl.core.Listener) ASTNode(org.beetl.core.statement.ASTNode) BeetlException(org.beetl.core.exception.BeetlException)

Aggregations

Field (java.lang.reflect.Field)1 Listener (org.beetl.core.Listener)1 BeetlException (org.beetl.core.exception.BeetlException)1 ASTNode (org.beetl.core.statement.ASTNode)1