All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
shz.ast.AstHelp Maven / Gradle / Ivy
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)))));
}
}