Recently, the company asked me to make a tool that could generate code, requirements for like Swt-desinger, multi-page editor, a page to display the source code, another page in the form of a table to display the properties of the class, the request can be modified, which is not what and difficult things, but has not done this thing before, We have to look for information to study, found something called AST, which is a tool provided by Eclipse, has been heard before, but it's not very good, after a period of study, found that this thing is not bad, the function is very powerful, like Eclipse's refactoring function is through this AST to achieve, The AST is called abstract syntax tree, but the function is strong, with a little trouble, it is easy to make mistakes.
Today in the use of this AST to generate a Java file, I was first generated through the swt-designer of a jface Applicationwindow file, the above simply put a button, click to Pop Up a dialog box, and then display information, the function is very simple, After a day of continuous attempts, and finally through the compilation, directly run out, you can see my dialog box, that the following to introduce how to use this thing astparser parser = Astparser.newparser (AST. JLS3);
Parser.setsource ("" ". ToCharArray ());
Compilationunit unit = (compilationunit) parser.createast (null);
Unit.recordmodifications ();
AST ast = Unit.getast ();
The code above is to generate an empty compilation unit, which is our Java file, which we passed through the Astparser.newparser (AST). JLS3) to specify the compliant Java specification, if JLS2 is the specification of Java 1.4, then JLS3 to comply with Java 5 or above specification packagedeclaration packageDeclaration = ast.newpackagedeclaration ();
unit.setpackage (packagedeclaration);
packagedeclaration.setname (Ast.newsimplename ("Astdemo"));
The package that generates the Java file is above, and the result is package Astdemo//class name
TypeDeclaration classType = Ast.newtypedeclaration ();
classtype.setinterface (FALSE);
List classTypeModifier = classtype.modifiers ();
classtypemodifier.add (Ast.newmodifier (Modifierkeyword.public_keyword));
classtype.setname (Ast.newsimplename ("Myfirstapp"));
classtype.setsuperclasstype (Ast.newsimpletype (Ast.newsimplename) ("ApplicationWindow " )));
unit.types (). Add (ClassType);
Here specifies the type of makefile that can be used to specify the name of the generated class, whether it is generated as an interface, and the type of access, and the result is the public class Myfirstapp extends Applicationwindow {//Construction method
MethodDeclaration methodconstructor = Ast.newmethoddeclaration ();
Methodconstructor.setconstructor (TRUE);
List methodconstructormodifier = Methodconstructor.modifiers ();
Methodconstructormodifier.add (Ast.newmodifier (Modifierkeyword.public_keyword));
Methodconstructor.setname (Ast.newsimplename ("Myfirstapp"));
Classtype.bodydeclarations (). Add (Methodconstructor);
The above is to generate a method and specify that the method is constructed and the resulting result is public myfirstapp () {
Hey, forget it, this is too laborious, the following is the code of the program package com.vwpolo.jet.example;
Import java.util.ArrayList;
Import java.util.List;
Import Java.util.StringTokenizer;
Import Org.eclipse.jdt.core.dom.AST;
Import Org.eclipse.jdt.core.dom.ASTParser;
Import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
Import Org.eclipse.jdt.core.dom.Block;
Import org.eclipse.jdt.core.dom.ClassInstanceCreation;
Import Org.eclipse.jdt.core.dom.CompilationUnit;
Import org.eclipse.jdt.core.dom.ImportDeclaration;
Import org.eclipse.jdt.core.dom.InfixExpression;
Import org.eclipse.jdt.core.dom.MethodDeclaration;
Import org.eclipse.jdt.core.dom.MethodInvocation;
Import org.eclipse.jdt.core.dom.PackageDeclaration;
Import Org.eclipse.jdt.core.dom.PrimitiveType;
Import org.eclipse.jdt.core.dom.ReturnStatement;
Import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
Import org.eclipse.jdt.core.dom.StringLiteral;
Import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
Import org.eclipse.jdt.core.dom.TypeDeclaration;
Import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
Import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
Import Org.eclipse.jdt.core.dom.InfixExpression.Operator;
Import Org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
Import Com.vwpolo.jet.example.JavaASTParserExample9.CompilationUnitImpl;
public class JavaASTParserExample1 {
private static final string[] IMPORTS = {"Org.eclipse.jface.action.*", "org.eclipse.jface.window.*", "org . eclipse.swt.events.* ",
"Org.eclipse.swt.*", "org.eclipse.jface.dialogs.*", "org.eclipse.swt.graphics.*", "org.eclipse.swt.widgets.*" };
@SuppressWarnings ("Unchecked")
public static void Main (string[] args) {
Compilationunit unit = Getcompilationutil ();
System.out.println (Unit.tostring ());
Javaastparserexample9.compileandrun (new Compilationunitimpl (unit));
}
@SuppressWarnings ("Unchecked")
private static Compilationunit Getcompilationutil () {
Astparser parser = Astparser.newparser (AST. JLS3);
Parser.setsource ("" ". ToCharArray ());
Compilationunit unit = (compilationunit) parser.createast (null);
Unit.recordmodifications ();
AST ast = Unit.getast ();
Packagedeclaration packagedeclaration = Ast.newpackagedeclaration ();
Unit.setpackage (packagedeclaration);
Packagedeclaration.setname (Ast.newsimplename ("Astdemo"));
for (int i = 0; i < imports.length; + + i) {
Importdeclaration importdeclaration = Ast.newimportdeclaration ();
Importdeclaration.setname (Ast.newname (Getsimplenames (imports[i)));
if (Imports[i].indexof ("*") > 0)
Importdeclaration.setondemand (TRUE);
Else
Importdeclaration.setondemand (FALSE);
Unit.imports (). Add (Importdeclaration);
}
Class name
Typedeclaration ClassType = Ast.newtypedeclaration ();
Classtype.setinterface (FALSE);
List classtypemodifier = Classtype.modifiers ();
Classtypemodifier.add (Ast.newmodifier (Modifierkeyword.public_keyword));
Classtype.setname (Ast.newsimplename ("Myfirstapp"));
Classtype.setsuperclasstype (Ast.newsimpletype) (Ast.newsimplename ("Applicationwindow"));
Unit.types (). Add (ClassType);
Construction method
MethodDeclaration methodconstructor = Ast.newmethoddeclaration ();
Methodconstructor.setconstructor (TRUE);
List methodconstructormodifier = Methodconstructor.modifiers ();
Methodconstructormodifier.add (Ast.newmodifier (Modifierkeyword.public_keyword));
Methodconstructor.setname (Ast.newsimplename ("Myfirstapp"));
Classtype.bodydeclarations (). Add (Methodconstructor);
Block Constructorblock = Ast.newblock ();
Methodconstructor.setbody (Constructorblock);
Super (NULL);
Superconstructorinvocation superconstructorinvocation = Ast.newsuperconstructorinvocation ();
Constructorblock.statements (). Add (Superconstructorinvocation);
Superconstructorinvocation.arguments (). Add (Ast.newnullliteral ());
Createactions ();
Methodinvocation methodinvocation = Ast.newmethodinvocation ();
Methodinvocation.setname (Ast.newsimplename ("createactions"));
Constructorblock.statements (). Add (Ast.newexpressionstatement (methodinvocation));
Addtoolbar (SWT. FLAT | Swt. WRAP);
Methodinvocation MethodInvocation2 = Ast.newmethodinvocation ();
Methodinvocation2.setname (Ast.newsimplename ("Addtoolbar"));
Infixexpression infixexpression = Ast.newinfixexpression ();
Infixexpression.setoperator (operator.or);
Infixexpression.setleftoperand (Ast.newname (getsimplenames) (SWT. FLAT "));
Infixexpression.setrightoperand (Ast.newname (getsimplenames) (SWT. WRAP "));
Methodinvocation2.arguments (). Add (Infixexpression);
Constructorblock.statements (). Add (Ast.newexpressionstatement (MethodInvocation2));
Addmenubar ();
Methodinvocation MethodInvocation3 = Ast.newmethodinvocation ();
Methodinvocation3.setname (Ast.newsimplename ("Addmenubar"));
Constructorblock.statements (). Add (Ast.newexpressionstatement (MethodInvocation3));
Addstatusline ();
Methodinvocation methodInvocation4 = Ast.newmethodinvocation ();
Methodinvocation4.setname (Ast.newsimplename ("Addstatusline"));
Constructorblock.statements (). Add (Ast.newexpressionstatement (MethodInvocation4));
MethodDeclaration methoddeclaration = Ast.newmethoddeclaration ();
Methoddeclaration.setconstructor (FALSE);
List methodmodifiers = Methoddeclaration.modifiers ();
Methodmodifiers.add (Ast.newmodifier (Modifierkeyword.protected_keyword));
Methoddeclaration.setreturntype2 (Ast.newsimpletype) (Ast.newsimplename ("Control"));
Methoddeclaration.setname (Ast.newsimplename ("createcontents"));
Classtype.bodydeclarations (). Add (MethodDeclaration);
Block Methodblock = Ast.newblock ();
Methoddeclaration.setbody (Methodblock);
Createcontents (composite parent) {
Singlevariabledeclaration variabledeclaration = Ast.newsinglevariabledeclaration ();
Variabledeclaration.settype (Ast.newsimpletype) (Ast.newsimplename ("composite"));
Variabledeclaration.setname (Ast.newsimplename ("parent"));
Methoddeclaration.parameters (). Add (Variabledeclaration);
Composite container = new Composite (parent, SWT. NONE);
Variabledeclarationfragment variablefragment = Ast.newvariabledeclarationfragment ();
Variablefragment.setname (Ast.newsimplename ("container"));
Variabledeclarationstatement variablestatement = ast.newvariabledeclarationstatement (variableFragment);
Variablestatement.settype (Ast.newsimpletype) (Ast.newsimplename ("composite"));
Classinstancecreation classcreation = Ast.newclassinstancecreation ();
Classcreation.settype (Ast.newsimpletype) (Ast.newsimplename ("composite"));
Variablefragment.setinitializer (classcreation);
Methodblock.statements (). Add (Variablestatement);
Classcreation.arguments (). Add (Ast.newsimplename ("parent"));
Classcreation.arguments (). Add (Ast.newname (getsimplenames) ("SWT". NONE "));
Final button button = New button (container, SWT. NONE);
Variabledeclarationfragment VariableFragment2 = Ast.newvariabledeclarationfragment ();
Variablefragment2.setname (Ast.newsimplename ("button"));
Variabledeclarationstatement VariableStatement2 = ast.newvariabledeclarationstatement (VariableFragment2);
List variableModifier2 = Variablestatement2.modifiers ();
Variablemodifier2.add (Ast.newmodifier (Modifierkeyword.final_keyword));
Variablestatement2.settype (Ast.newsimpletype (Ast.newsimplename ("button"));
Classinstancecreation ClassCreation2 = Ast.newclassinstancecreation ();
Classcreation2.settype (Ast.newsimpletype (Ast.newsimplename ("button"));
Variablefragment2.setinitializer (ClassCreation2);
Methodblock.statements (). Add (VariableStatement2);
Classcreation2.arguments (). Add (Ast.newsimplename ("container"));
Classcreation2.arguments (). Add (Ast.newname (getsimplenames) ("SWT". NONE "));
Button.addselectionlistener (New Selectionadapt