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

shz.ast.AstHelp Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
package shz.ast;

import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.*;

import java.util.Arrays;

public class AstHelp {
    public TreeMaker treeMaker;
    public Names names;

    public AstHelp(TreeMaker treeMaker, Names names) {
        this.treeMaker = treeMaker;
        this.names = names;
    }

    public final JCTree.JCIdent ident(String name) {
        return treeMaker.Ident(names.fromString(name));
    }

    public final JCTree.JCFieldAccess select(String expr) {
        String[] a = expr.split("\\.");
        int len;
        if ((len = a.length) == 1) throw new IllegalStateException();
        JCTree.JCFieldAccess res = treeMaker.Select(ident(a[0]), names.fromString(a[1]));
        for (int i = 2; i < len; ++i) res = treeMaker.Select(res, names.fromString(a[i]));
        return res;
    }

    /**
     * @param flags {@link com.sun.tools.javac.code.Flags}
     */
    public final JCTree.JCVariableDecl varDef(long flags, String name, JCTree.JCExpression type, JCTree.JCExpression val) {
        return treeMaker.VarDef(treeMaker.Modifiers(flags), names.fromString(name), type, val == null ? treeMaker.Literal(TypeTag.BOT, null) : val);
    }

    public final JCTree.JCVariableDecl varDef(long flags, String name, String type, JCTree.JCExpression val) {
        return treeMaker.VarDef(treeMaker.Modifiers(flags), names.fromString(name), type.contains(".") ? select(type) : ident(type),
                val == null ? treeMaker.Literal(TypeTag.BOT, null) : val);
    }

    public final JCTree.JCVariableDecl varDef(long flags, String name, String type) {
        return varDef(flags, name, type, null);
    }

    public final JCTree.JCMethodInvocation apply(String expr, List params) {
        return treeMaker.Apply(List.nil(), select(expr), params);
    }

    public final JCTree.JCMethodInvocation apply(String expr, String... params) {
        if (params.length == 0) return apply(expr, List.nil());
        return apply(expr, List.from(Arrays.stream(params).map(param -> param.contains(".") ? select(param) : ident(param)).toArray(JCTree.JCExpression[]::new)));
    }

    public final JCTree.JCAssign assign(String expr, JCTree.JCExpression val) {
        return treeMaker.Assign(expr.contains(".") ? select(expr) : ident(expr), val);
    }

    public final JCTree.JCAssign assign(String expr, String expr2, List params) {
        return treeMaker.Assign(expr.contains(".") ? select(expr) : ident(expr), apply(expr2, params));
    }

    public final JCTree.JCAssign assign(String expr, String expr2, String... params) {
        return treeMaker.Assign(expr.contains(".") ? select(expr) : ident(expr), apply(expr2, params));
    }

    public JCTree.JCNewClass newClass(String name, List args) {
        return treeMaker.NewClass(null, List.nil(), select(name), args, null);
    }

    public final JCTree.JCBlock block(long flags, List stats) {
        return treeMaker.Block(flags, stats);
    }

    public final JCTree.JCBlock block(List stats) {
        return block(0L, stats);
    }

    public final JCTree.JCThrow Throw(String name, String format, Object... args) {
        return treeMaker.Throw(newClass(name, List.of(treeMaker.Literal(String.format(format, args)))));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy