All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sqlancer.common.visitor.ToStringVisitor Maven / Gradle / Ivy

Go to download

SQLancer finds logic bugs in Database Management Systems through automatic testing

There is a newer version: 2.0.0
Show newest version
package sqlancer.common.visitor;

import java.util.List;

import sqlancer.common.visitor.UnaryOperation.OperatorKind;

public abstract class ToStringVisitor extends NodeVisitor {

    protected final StringBuilder sb = new StringBuilder();

    public void visit(BinaryOperation op) {
        sb.append('(');
        sb.append('(');
        visit(op.getLeft());
        sb.append(')');
        sb.append(op.getOperatorRepresentation());
        sb.append('(');
        visit(op.getRight());
        sb.append(')');
        sb.append(')');
    }

    public void visit(UnaryOperation op) {
        if (!op.omitBracketsWhenPrinting()) {
            sb.append('(');
        }
        if (op.getOperatorKind() == OperatorKind.PREFIX) {
            sb.append(op.getOperatorRepresentation());
            sb.append(' ');
        }
        if (!op.omitBracketsWhenPrinting()) {
            sb.append('(');
        }
        visit(op.getExpression());
        if (!op.omitBracketsWhenPrinting()) {
            sb.append(')');
        }
        if (op.getOperatorKind() == OperatorKind.POSTFIX) {
            sb.append(' ');
            sb.append(op.getOperatorRepresentation());
        }
        if (!op.omitBracketsWhenPrinting()) {
            sb.append(')');
        }
    }

    @SuppressWarnings("unchecked")
    public void visit(T expr) {
        assert expr != null;
        if (expr instanceof BinaryOperation) {
            visit((BinaryOperation) expr);
        } else if (expr instanceof UnaryOperation) {
            visit((UnaryOperation) expr);
        } else {
            visitSpecific(expr);
        }
    }

    public abstract void visitSpecific(T expr);

    public void visit(List expressions) {
        for (int i = 0; i < expressions.size(); i++) {
            if (i != 0) {
                sb.append(", ");
            }
            visit(expressions.get(i));
        }
    }

    public String get() {
        return sb.toString();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy