JDT追記-ASTNode分類生成
いくつかのJavaコード生成に対応するASTnode方式を列挙し、参考に供する.
List 1生成Package
List 2はImportを生成する
List 3 Class Declarationの生成
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);
-
- // 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
// label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
java
//Button button = new Button(this,SWT.PUSH);
JAvaコード
// button.addSelectionListener(new SelectionAdapter() {});
java