package com.sun.tools.javac.parser; import com.sun.tools.javac.code.Flags; import com.sun.tools.javac.code.TypeTags; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.tree.TreeMaker; import com.sun.tools.javac.tree.JCTree.JCAnnotation; import com.sun.tools.javac.tree.JCTree.JCArrayTypeTree; import com.sun.tools.javac.tree.JCTree.JCBlock; import com.sun.tools.javac.tree.JCTree.JCClassDecl; import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.JCTree.JCExpression; import com.sun.tools.javac.tree.JCTree.JCExpressionStatement; import com.sun.tools.javac.tree.JCTree.JCIdent; import com.sun.tools.javac.tree.JCTree.JCLiteral; import com.sun.tools.javac.tree.JCTree.JCMethodDecl; import com.sun.tools.javac.tree.JCTree.JCModifiers; import com.sun.tools.javac.tree.JCTree.JCStatement; import com.sun.tools.javac.tree.JCTree.JCTypeParameter; import com.sun.tools.javac.tree.JCTree.JCVariableDecl; import com.sun.tools.javac.util.List; import com.sun.tools.javac.util.ListBuffer; import com.sun.tools.javac.util.Name; import com.sun.tools.javac.util.Names; /** * This class generates a dummy javac AST tree representing a simple class file[Test.java] with content: * * public class Test{ * public static void main(String[] args){ * System.out.println("Hello!"); * } * } * * @author Yang Jiang * */ public class DummyTreeMaker { TreeMaker maker; Names names; public DummyTreeMaker(ParserFactory fac) { maker = fac.F; names = fac.names; } public JCCompilationUnit getTree() { ListBuffer defs = new ListBuffer(); defs.append(makeClassDecl()); return maker.TopLevel(List. nil(), null, defs.toList()); } /** * Makes a class decalaration node for * public class Test{ * } * @return */ protected JCClassDecl makeClassDecl() { JCModifiers mods = maker.Modifiers(Flags.PUBLIC); // class name Name name = names.fromString("Test"); // method definitions, in a list. ListBuffer methods = new ListBuffer(); methods.append(makeMethodDecl()); return maker.ClassDef(mods, name, List. nil(), null, List. nil(), methods .toList()); } /** * Makes a method declaration node with signature * public static void main(String[] args) * * @return */ protected JCMethodDecl makeMethodDecl() { JCModifiers mods = maker.Modifiers(Flags.PUBLIC | Flags.STATIC); //method name Name name = names.fromString("main"); //return type JCExpression retType = maker.TypeIdent(TypeTags.VOID); //method parameters stored in a list ListBuffer params = new ListBuffer(); params.append(makeParameter()); return maker.MethodDef(mods, name, retType, List. nil(), params.toList(), List . nil(), makeMethodBody(), null); } /** * Makes parameter declaration node for * String[] args */ protected JCVariableDecl makeParameter() { JCModifiers mods = maker.Modifiers(Flags.PARAMETER); //base type "String" JCIdent stringType = maker.Ident(names.fromString("String")); //array of String JCArrayTypeTree arrayType = maker.TypeArray(stringType); //parameter name Name name = names.fromString("args"); return maker.VarDef(mods, name, arrayType, null); } /** * Makes method body, the * { * * } * part. * @return */ protected JCBlock makeMethodBody() { ListBuffer stmts = new ListBuffer(); JCExpressionStatement stmt = maker.Exec(makeMethodCall()); stmts.append(stmt); return maker.Block(0, stmts.toList()); } /** * Makes a method call for * System.out.println("Hello!") * which is a combination of field access and method call. */ protected JCExpression makeMethodCall() { //each '.' is parsed as an JCFieldAccess. JCExpression exp = maker.Ident(names.fromString("System")); exp = maker.Select(exp, names.fromString("out")); exp = maker.Select(exp, names.fromString("println")); //argument list for method ListBuffer args = new ListBuffer(); //String type arguments with value "Hello" JCLiteral literal = maker.Literal(TypeTags.CLASS, "Hello!"); args.append(literal); return maker.Apply(List. nil(), exp, args.toList()); } }