转自:http://plutoluo.javaeye.com/blog/146281
JDT实际上是将Java代码构建成一个基于DOM结构的抽象语法树AST(Abstract Syntax Tree )。代码中的每个部分都对应一个ASTNode,许多的ASTNode就构成了这个抽象的语法树。Java Class一般对应Compilation Unit node,该节点也是AST树上的顶点。创建一个AST如下:
java 代码
- ASTParser parser = ASTParser.newParser(AST.JLS3);
- parser.setSource("".toCharArray());
- CompilationUnit unit = (CompilationUnit) parser.createAST(null);
- unit.recordModifications();
- AST ast = unit.getAST();
其中createAST,当parse需要较长时间时,可以采用createAST(new NullProgressMonitor()),否则直接传null即可。
recordModifications()用于记录节点的变动,比如修改、删除等,当需要对AST树进行变动操作时,必须要预先调用这个方法。
比较重要的是:一个AST树上的所有节点必须都属于该AST。不允许直接将其他AST树上的节点添加该AST树上。否则会抛出java.lang.IllegalArgumentException异常。须使用ASTNode.copySubtree(AST target, ASTNode node)返回一个目标树的深度拷贝,才能进行添加操作。例如:
java 代码
- ASTParser parser = ASTParser.newParser(AST.JLS3);
- parser.setSource("".toCharArray());
- CompilationUnit targetRoot= (CompilationUnit) parser.createAST(null);
- targetRoot.recordModifications();
- parser.setSource("class T{}”".toCharArray());
- CompilationUnit srcRoot= (CompilationUnit) parser.createAST(null);
- //这是非法操作,两者的AST源不一样
- targetRoot.types().add(srcRoot.types().get(0));
- //这是合法操作
- targetRoot.types().add(ASTNode.copySubtree(
- targetRoot.getAST(), (ASTNode) srcRoot.types().get(0)));
- //这是合法操作
- targetRoot.types().add(targetRoot.getAST().newTypeDeclaration());
现把一些 Java代码生成对应的ASTNode方式列出来,供参考:
List 1 生成Package
List 1 生成Package
// package astexplorer;List 2 生成Importjava 代码
- PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
- unit.setPackage(packageDeclaration);
- packageDeclaration.setName(ast.newSimpleName("astexplorer"));
// import org.eclipse.swt.SWT;List 3 生成Class Declaration
// import org.eclipse.swt.events.*;
// import org.eclipse.swt.graphics.*;
// import org.eclipse.swt.layout.*;
// import org.eclipse.swt.widgets.*;java 代码
- 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);
- }
// public class SampleComposite extends Composite
java 代码
- TypeDeclaration classType = ast.newTypeDeclaration();
- classType.setInterface(false);
- classType.setModifiers(Modifier.PUBLIC);
- classType.setName(ast.newSimpleName("SampleComposite"));
- classType.setSuperclass(ast.newSimpleName("Composite"));
- unit.types().add(classType);
List 4 生成Constructor Declaration
// public SampleComposite(Composite parent,int style){}
java 代码
- MethodDeclaration methodConstructor = ast.newMethodDeclaration();
- methodConstructor.setConstructor(true);
- methodConstructor.setModifiers(Modifier.PUBLIC);
- methodConstructor.setName(ast.newSimpleName("SampleComposite"));
- classType.bodyDeclarations().add(methodConstructor);
- // constructor parameters
- SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();
- variableDeclaration.setModifiers(Modifier.NONE);
- variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Composite")));
- variableDeclaration.setName(ast.newSimpleName("parent"));
- methodConstructor.parameters().add(variableDeclaration);
- variableDeclaration = ast.newSingleVariableDeclaration();
- variableDeclaration.setModifiers(Modifier.NONE);
- variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));
- variableDeclaration.setName(ast.newSimpleName("style"));
- methodConstructor.parameters().add(variableDeclaration);
- Block constructorBlock = ast.newBlock();
- methodConstructor.setBody(constructorBlock);
List 5 生成Spuer Invocation
// super(parent,style)
java 代码
- SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
- constructorBlock.statements().add(superConstructorInvocation);
- Expression exp = ast.newSimpleName("parent");
- superConstructorInvocation.arguments().add(exp);
- superConstructorInvocation.arguments().add(ast.newSimpleName("style"));
List 6 生成ClassInstanceCreation
// GridLayout gridLayout = new GridLayout();
java 代码
- VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
- vdf.setName(ast.newSimpleName("gridLayout"));
- ClassInstanceCreation cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("GridLayout"));
- vdf.setInitializer(cc);
- VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
- vds.setType(ast.newSimpleType(ast.newSimpleName("GridLayout")));
- constructBlock.statements().add(vds);
// Label label = new Label(this,SWT.NONE);
java 代码
- VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();
- vdf.setName(ast.newSimpleName("label"));
- cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("Label"));
- cc.arguments().add(ast.newThisExpression());
- cc.arguments().add(ast.newName(getSimpleNames("SWT.NONE")));
- vdf.setInitializer(cc);
- VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);
- vds.setType(ast.newSimpleType(ast.newSimpleName("Label")));
- constructBlock.statements().add(vds);
List 7生成MethodInvocation
// setLayout(gridLayout);
java 代码
- MethodInvocation mi = ast.newMethodInvocation();
- mi.setName(ast.newSimpleName("setLayout"));
- mi.arguments().add(ast.newSimpleName("gridLayout"));
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
// label.setText("Press the button to close:");
java 代码
- mi = ast.newMethodInvocation();
- mi.setExpression(ast.newSimpleName("label"));
- mi.setName(ast.newSimpleName("setText"));
- StringLiteral sl = ast.newStringLiteral();
- sl.setLiteralValue("Press the button to close:");
- mi.arguments().add(sl);
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
// label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
java 代码
- mi = ast.newMethodInvocation();
- mi.setExpression(ast.newSimpleName("label"));
- mi.setName(ast.newSimpleName("setLayoutData"));
- cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("GridData"));
- cc.arguments().add(ast.newName(getSimpleNames("GridData.HORIZONTAL_ALIGN_CENTER")));
- mi.arguments().add(cc);
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
// Button button = new Button(this,SWT.PUSH);
java 代码
- vdf = ast.newVariableDeclarationFragment();
- vdf.setName(ast.newSimpleName("button"));
- vds = ast.newVariableDeclarationStatement(vdf);
- vds.setType(ast.newSimpleType(ast.newSimpleName("Button")));
- constructorBlock.statements().add(vds);
- cc = ast.newClassInstanceCreation();
- cc.setName(ast.newSimpleName("Button"));
- vdf.setInitializer(cc);
- cc.arguments().add(ast.newThisExpression());
- cc.arguments().add(ast.newName(getSimpleNames("SWT.PUSH")));
// button.addSelectionListener(new SelectionAdapter() {});
java 代码
- mi = ast.newMethodInvocation();
- constructorBlock.statements().add(ast.newExpressionStatement(mi));
- mi.setExpression(ast.newSimpleName("button"));
- mi.setName(ast.newSimpleName("addSelectionListener"));
- ClassInstanceCreation ci = ast.newClassInstanceCreation();
- ci.setName(ast.newSimpleName("SelectionAdapter"));
- mi.arguments().add(ci);
- AnonymousClassDeclaration cd = ast.newAnonymousClassDeclaration();
- ci.setAnonymousClassDeclaration(cd);