现把一些 Java代码生成对应的ASTNode方式列出来,供参考:
List 1 生成Package
// package astexplorer;
java 代码
- PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
- unit.setPackage(packageDeclaration);
- packageDeclaration.setName(ast.newSimpleName("astexplorer"));
List 2 生成Import
// import org.eclipse.swt.SWT;
// 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);
- }
List 3 生成Class Declaration
// 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);
-
-
-
- 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);