天天看點

JDT随記-ASTNode分類生成

現把一些 Java代碼生成對應的ASTNode方式列出來,供參考:

List 1 生成Package

// package astexplorer;      

java 代碼  

  1. PackageDeclaration packageDeclaration = ast.newPackageDeclaration();  
  2. unit.setPackage(packageDeclaration);  
  3. 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 代碼  

  1. for (int i = 0; i < IMPORTS.length; ++i) {  
  2. ImportDeclaration importDeclaration = ast.newImportDeclaration();  
  3. importDeclaration.setName(ast.newName(getSimpleNames(IMPORTS[i])));  
  4. if (IMPORTS[i].indexOf("*") > 0)  
  5. importDeclaration.setOnDemand(true);  
  6. else  
  7. importDeclaration.setOnDemand(false);  
  8. unit.imports().add(importDeclaration);  
  9. }  

List 3 生成Class Declaration

// public class SampleComposite extends Composite 
      
java 代碼      
  1. TypeDeclaration classType = ast.newTypeDeclaration();  
  2. classType.setInterface(false);  
  3. classType.setModifiers(Modifier.PUBLIC);  
  4. classType.setName(ast.newSimpleName("SampleComposite"));  
  5. classType.setSuperclass(ast.newSimpleName("Composite"));  
  6. unit.types().add(classType);  
// public SampleComposite(Composite parent,int style){}      
java 代碼      
  1. MethodDeclaration methodConstructor = ast.newMethodDeclaration();  
  2. methodConstructor.setConstructor(true);  
  3. methodConstructor.setModifiers(Modifier.PUBLIC);  
  4. methodConstructor.setName(ast.newSimpleName("SampleComposite"));  
  5. classType.bodyDeclarations().add(methodConstructor);  
  6. // constructor parameters  
  7. SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();  
  8. variableDeclaration.setModifiers(Modifier.NONE);  
  9. variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Composite")));  
  10. variableDeclaration.setName(ast.newSimpleName("parent"));  
  11. methodConstructor.parameters().add(variableDeclaration);  
  12. variableDeclaration = ast.newSingleVariableDeclaration();  
  13. variableDeclaration.setModifiers(Modifier.NONE);  
  14. variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));  
  15. variableDeclaration.setName(ast.newSimpleName("style"));  
  16. methodConstructor.parameters().add(variableDeclaration);  
  17. Block constructorBlock = ast.newBlock();  
  18. methodConstructor.setBody(constructorBlock);
 // super(parent,style)      
java 代碼      
  1. SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();  
  2. constructorBlock.statements().add(superConstructorInvocation);  
  3. Expression exp = ast.newSimpleName("parent");  
  4. superConstructorInvocation.arguments().add(exp);  
  5. superConstructorInvocation.arguments().add(ast.newSimpleName("style"));  
// GridLayout gridLayout = new GridLayout();      
java 代碼      
  1. VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();  
  2. vdf.setName(ast.newSimpleName("gridLayout"));  
  3. ClassInstanceCreation cc = ast.newClassInstanceCreation();  
  4. cc.setName(ast.newSimpleName("GridLayout"));  
  5. vdf.setInitializer(cc);  
  6. VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);  
  7. vds.setType(ast.newSimpleType(ast.newSimpleName("GridLayout"))); 
  8. constructBlock.statements().add(vds);
// Label label = new Label(this,SWT.NONE);      
java 代碼      
  1. VariableDeclarationFragment vdf = ast.newVariableDeclarationFragment();  
  2. vdf.setName(ast.newSimpleName("label"));  
  3. cc = ast.newClassInstanceCreation();  
  4. cc.setName(ast.newSimpleName("Label"));  
  5. cc.arguments().add(ast.newThisExpression());  
  6. cc.arguments().add(ast.newName(getSimpleNames("SWT.NONE")));  
  7. vdf.setInitializer(cc); 
  8. VariableDeclarationStatement vds = ast.newVariableDeclarationStatement(vdf);  
  9. vds.setType(ast.newSimpleType(ast.newSimpleName("Label")));
  10. constructBlock.statements().add(vds);
// setLayout(gridLayout);      
java 代碼      
  1. MethodInvocation mi = ast.newMethodInvocation();  
  2. mi.setName(ast.newSimpleName("setLayout"));  
  3. mi.arguments().add(ast.newSimpleName("gridLayout")); 
  4. constructorBlock.statements().add(ast.newExpressionStatement(mi));
  1. mi = ast.newMethodInvocation();   
  2. mi.setExpression(ast.newSimpleName("label"));   
  3. mi.setName(ast.newSimpleName("setText"));   
  4. StringLiteral sl = ast.newStringLiteral();   
  5. sl.setLiteralValue("Press the button to close:");   
  6. mi.arguments().add(sl);   
  7. constructorBlock.statements().add(ast.newExpressionStatement(mi));  
  1. mi = ast.newMethodInvocation();   
  2. mi.setExpression(ast.newSimpleName("label"));   
  3. mi.setName(ast.newSimpleName("setLayoutData"));   
  4. cc = ast.newClassInstanceCreation();   
  5. cc.setName(ast.newSimpleName("GridData"));   
  6. cc.arguments().add(ast.newName(getSimpleNames("GridData.HORIZONTAL_ALIGN_CENTER")));   
  7. mi.arguments().add(cc);   
  8. constructorBlock.statements().add(ast.newExpressionStatement(mi));  

 // Button button = new Button(this,SWT.PUSH);

java 代碼

  1. vdf = ast.newVariableDeclarationFragment();   
  2. vdf.setName(ast.newSimpleName("button"));   
  3. vds = ast.newVariableDeclarationStatement(vdf);   
  4. vds.setType(ast.newSimpleType(ast.newSimpleName("Button")));   
  5. constructorBlock.statements().add(vds);   
  6. cc = ast.newClassInstanceCreation();   
  7. cc.setName(ast.newSimpleName("Button"));   
  8. vdf.setInitializer(cc);   
  9. cc.arguments().add(ast.newThisExpression());   
  10. cc.arguments().add(ast.newName(getSimpleNames("SWT.PUSH")));  

// button.addSelectionListener(new SelectionAdapter() {});

    java 代碼

  1. mi = ast.newMethodInvocation();   
  2. constructorBlock.statements().add(ast.newExpressionStatement(mi));   
  3. mi.setExpression(ast.newSimpleName("button"));   
  4. mi.setName(ast.newSimpleName("addSelectionListener"));   
  5. ClassInstanceCreation ci = ast.newClassInstanceCreation();   
  6. ci.setName(ast.newSimpleName("SelectionAdapter"));   
  7. mi.arguments().add(ci);   
  8. AnonymousClassDeclaration cd = ast.newAnonymousClassDeclaration();   
  9. ci.setAnonymousClassDeclaration(cd);