<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 25,  trackbacks - 0

    JDT實(shí)際上是將Java代碼構(gòu)建成一個(gè)基于DOM結(jié)構(gòu)的抽象語(yǔ)法樹AST(Abstract Syntax Tree )。代碼中的每個(gè)部分都對(duì)應(yīng)一個(gè)ASTNode,許多的ASTNode就構(gòu)成了這個(gè)抽象的語(yǔ)法樹。Java Class一般對(duì)應(yīng)Compilation Unit node,該節(jié)點(diǎn)也是AST樹上的頂點(diǎn)。創(chuàng)建一個(gè)AST如下:

    java 代碼
    1. ASTParser parser = ASTParser.newParser(AST.JLS3);   
    2. parser.setSource("".toCharArray());   
    3. CompilationUnit unit = (CompilationUnit) parser.createAST(null);    
    4. unit.recordModifications();   
    5. AST ast = unit.getAST();   

    其中createAST,當(dāng)parse需要較長(zhǎng)時(shí)間時(shí),可以采用createAST(new NullProgressMonitor()),否則直接傳null即可。

    recordModifications()用于記錄節(jié)點(diǎn)的變動(dòng),比如修改、刪除等,當(dāng)需要對(duì)AST樹進(jìn)行變動(dòng)操作時(shí),必須要預(yù)先調(diào)用這個(gè)方法。


    比較重要的是:一個(gè)AST樹上的所有節(jié)點(diǎn)必須都屬于該AST。不允許直接將其他AST樹上的節(jié)點(diǎn)添加該AST樹上。否則會(huì)拋出java.lang.IllegalArgumentException異常。須使用ASTNode.copySubtree(AST target, ASTNode node)返回一個(gè)目標(biāo)樹的深度拷貝,才能進(jìn)行添加操作。例如:

    java 代碼
    1. ASTParser parser = ASTParser.newParser(AST.JLS3);   
    2. parser.setSource("".toCharArray());   
    3. CompilationUnit targetRoot= (CompilationUnit) parser.createAST(null);    
    4. targetRoot.recordModifications();   
    5. parser.setSource("class T{}”".toCharArray());   
    6. CompilationUnit srcRoot= (CompilationUnit) parser.createAST(null);    
    7.   
    8. //這是非法操作,兩者的AST源不一樣   
    9. targetRoot.types().add(srcRoot.types().get(0));   
    10.   
    11. //這是合法操作   
    12. targetRoot.types().add(ASTNode.copySubtree(   
    13. targetRoot.getAST(), (ASTNode) srcRoot.types().get(0)));   
    14.   
    15. //這是合法操作   
    16. targetRoot.types().add(targetRoot.getAST().newTypeDeclaration());  


     

    現(xiàn)把一些 Java代碼生成對(duì)應(yīng)的ASTNode方式列出來(lái),供參考:

    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.   
    9. unit.imports().add(importDeclaration);  
    10. }  
    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);  


    List 4 生成Constructor Declaration

    // 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.   
    7. // constructor parameters  
    8.   
    9. SingleVariableDeclaration variableDeclaration = ast.newSingleVariableDeclaration();  
    10. variableDeclaration.setModifiers(Modifier.NONE);  
    11. variableDeclaration.setType(ast.newSimpleType(ast.newSimpleName("Composite")));  
    12. variableDeclaration.setName(ast.newSimpleName("parent"));  
    13. methodConstructor.parameters().add(variableDeclaration);  
    14.   
    15. variableDeclaration = ast.newSingleVariableDeclaration();  
    16. variableDeclaration.setModifiers(Modifier.NONE);  
    17. variableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));  
    18. variableDeclaration.setName(ast.newSimpleName("style"));  
    19. methodConstructor.parameters().add(variableDeclaration);  
    20. Block constructorBlock = ast.newBlock();  
    21. methodConstructor.setBody(constructorBlock);
     List 5 生成Spuer Invocation

    // 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"));  

    List 6 生成ClassInstanceCreation

    // 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);

    List 7生成MethodInvocation

    // 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));
    // label.setText("Press the button to close:");
             java 代碼
    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));  
    // label.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
               java 代碼
    
    1. mi = ast.newMethodInvocation();   
    2. mi.setExpression(ast.newSimpleName("label"));   
    3. mi.setName(ast.newSimpleName("setLayoutData"));   
    4.   
    5. cc = ast.newClassInstanceCreation();   
    6. cc.setName(ast.newSimpleName("GridData"));   
    7. cc.arguments().add(ast.newName(getSimpleNames("GridData.HORIZONTAL_ALIGN_CENTER")));   
    8. mi.arguments().add(cc);   
    9. 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.   
    7. cc = ast.newClassInstanceCreation();   
    8. cc.setName(ast.newSimpleName("Button"));   
    9. vdf.setInitializer(cc);   
    10. cc.arguments().add(ast.newThisExpression());   
    11. 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.   
    6. ClassInstanceCreation ci = ast.newClassInstanceCreation();   
    7. ci.setName(ast.newSimpleName("SelectionAdapter"));   
    8. mi.arguments().add(ci);   
    9. AnonymousClassDeclaration cd = ast.newAnonymousClassDeclaration();   
    10. ci.setAnonymousClassDeclaration(cd); 






















    // public void setup(){super.setUp()}
        1. MethodDeclaration methodConstructor = ast.newMethodDeclaration();
                  methodConstructor.modifiers().add(
                          ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                  methodConstructor.setName(ast.newSimpleName("setUp"));
    1.         Block constructorBlock = ast.newBlock();
              methodConstructor.setBody(constructorBlock);
    2.         SuperMethodInvocation smi = ast.newSuperMethodInvocation();
              smi.setName(ast.newSimpleName("setUp"));
              ExpressionStatement es = ast.newExpressionStatement(smi);
              constructorBlock.statements().add(es);
    3.         // set exception
              List targetExNames = methodConstructor.thrownExceptions();
              Name targetExName = ast.newName(Exception.class.getName());
              targetExNames.add(targetExName);
    4.         methodConstructor.setJavadoc(JavaDocHelper.getJavaDoc(ast,
                      JavaDocHelper.setUpDoc, null, null));
              classType.bodyDeclarations().add(methodConstructor);
    posted on 2008-07-14 13:21 Daniel 閱讀(327) 評(píng)論(0)  編輯  收藏 所屬分類: Eclipse的相關(guān)
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(3)

    隨筆檔案

    文章分類

    文章檔案

    相冊(cè)

    搜索

    •  

    最新評(píng)論

    主站蜘蛛池模板: 精品亚洲一区二区| 亚洲国产精品无码中文字| 亚洲一区精彩视频| 色老头永久免费网站| 亚洲成a人片在线网站| a拍拍男女免费看全片| 亚洲乱码一二三四五六区| 永久免费av无码网站韩国毛片| 亚洲免费在线视频播放| 青苹果乐园免费高清在线| 亚洲国产精品无码中文lv| 又粗又硬免费毛片| 国产精品美女免费视频观看| 浮力影院亚洲国产第一页| 人妻免费一区二区三区最新| 日韩va亚洲va欧洲va国产| 亚洲综合免费视频| 亚洲日本在线电影| 亚洲国产黄在线观看| 久久青草精品38国产免费| 91亚洲国产成人久久精品| 永久免费观看的毛片的网站| 国产精品亚洲一区二区无码 | 亚洲AV无码精品无码麻豆| 日本免费久久久久久久网站| 亚洲成人网在线播放| 午夜两性色视频免费网站| 一级毛片aa高清免费观看| 亚洲VA成无码人在线观看天堂| 四虎国产成人永久精品免费| 亚洲日本久久久午夜精品| 亚洲AV成人潮喷综合网| 久久精品无码精品免费专区| 亚洲一区二区三区深夜天堂| 免费在线观看毛片| 特级无码毛片免费视频尤物| 亚洲无码一区二区三区| 亚洲一区视频在线播放| 男女超爽刺激视频免费播放| 成人嫩草影院免费观看| 亚洲经典在线中文字幕|