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

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

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

    Java海闊天空

    編程是我的生活,但生活不僅僅是編程。

    eclipse插件開發:使用AST生成類(源碼)

    本代碼演示如何通過AST生成一個完整的類, 最終生成的代碼比較亂, 編譯無法通過,重點在于對AST的使用, 無需考慮最后生成的結果.

    以下是最終生成的代碼(語法有嚴重錯誤, 請不要計較這些):
    package com.aptech.lzh;

    import java.util.Date;
    import java.util.Random;

    public class Program {
        
    public static void main(String[] arg) {
            Program program 
    = new Program();
            String r 
    = program.getString("中國");
            System.out.println(r);
        }


        
    public Program(int a, int[] b, final Integer c) {
            
    super(null);
        }


        
    public String getString(String p) {
            String newString 
    = new String("初始值");
            newString 
    = "你好";
            String result 
    = newString.indexOf("");
            newString 
    = name + "你好";
            
    return newString;
        }


        
    public String isOdd(int a) throws NullPointerException, Exception {
            
    if (a < 0)
                
    throw new Exception("數字不能為負數");
            
    if (a % 2 == 0)
                
    return "偶數";
            
    else {
                System.out.println(
    "");
                
    return "奇數";
            }

        }

    }


    源代碼:
    package ast.test.demo;

    import org.eclipse.jdt.core.dom.AST;
    import org.eclipse.jdt.core.dom.Assignment;
    import org.eclipse.jdt.core.dom.Block;
    import org.eclipse.jdt.core.dom.ClassInstanceCreation;
    import org.eclipse.jdt.core.dom.CompilationUnit;
    import org.eclipse.jdt.core.dom.ExpressionStatement;
    import org.eclipse.jdt.core.dom.IfStatement;
    import org.eclipse.jdt.core.dom.ImportDeclaration;
    import org.eclipse.jdt.core.dom.InfixExpression;
    import org.eclipse.jdt.core.dom.MethodDeclaration;
    import org.eclipse.jdt.core.dom.MethodInvocation;
    import org.eclipse.jdt.core.dom.Modifier;
    import org.eclipse.jdt.core.dom.NumberLiteral;
    import org.eclipse.jdt.core.dom.PackageDeclaration;
    import org.eclipse.jdt.core.dom.PrimitiveType;
    import org.eclipse.jdt.core.dom.ReturnStatement;
    import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
    import org.eclipse.jdt.core.dom.StringLiteral;
    import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
    import org.eclipse.jdt.core.dom.ThrowStatement;
    import org.eclipse.jdt.core.dom.TypeDeclaration;
    import org.eclipse.jdt.core.dom.TypeLiteral;
    import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
    import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
    import org.eclipse.jdt.core.dom.Assignment.Operator;
    import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

    public class Demo {
        
    public static void main(String[] args) {
            AST ast 
    = AST.newAST(AST.JLS3);
            CompilationUnit compilationUnit 
    = ast.newCompilationUnit();
            
            
    //創建類
            TypeDeclaration programClass = ast.newTypeDeclaration();
            programClass.setName(ast.newSimpleName(
    "Program"));
            programClass.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
            compilationUnit.types().add(programClass);

            
    //創建包
            PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
            packageDeclaration.setName(ast.newName(
    "com.aptech.lzh"));
            compilationUnit.setPackage(packageDeclaration);
            
            
    //要導入的包
            String[] imports = {"java.util.Date""java.util.Random"};
            
    for(String imp : imports){
                ImportDeclaration importDeclaration 
    = ast.newImportDeclaration();
                importDeclaration.setName(ast.newName(imp));
                compilationUnit.imports().add(importDeclaration);
            }

            
            
    //創建一個main方法
            {
                MethodDeclaration main 
    = ast.newMethodDeclaration();
                main.setName(ast.newSimpleName(
    "main"));
                main.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
                main.modifiers().add(ast.newModifier(ModifierKeyword.STATIC_KEYWORD));
                main.setReturnType2(ast.newPrimitiveType(PrimitiveType.VOID));
                programClass.bodyDeclarations().add(main);
                Block mainBlock 
    = ast.newBlock();
                main.setBody(mainBlock);
                
                
    //給main方法定義String[]參數
                SingleVariableDeclaration mainParameter = ast.newSingleVariableDeclaration();
                mainParameter.setName(ast.newSimpleName(
    "arg"));
                mainParameter.setType(ast.newArrayType(ast.newSimpleType(ast.newName(
    "String"))));
                main.parameters().add(mainParameter);
                
                
    //創建Pragram對象: Program program=new Program();
                VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
                fragment.setName(ast.newSimpleName(
    "program"));
                VariableDeclarationStatement statement 
    = ast.newVariableDeclarationStatement(fragment);
                statement.setType(ast.newSimpleType(ast.newSimpleName(
    "Program")));
                
                ClassInstanceCreation classInstanceCreation 
    = ast.newClassInstanceCreation();
                classInstanceCreation.setType(ast.newSimpleType(ast.newSimpleName(
    "Program")));
                
                fragment.setInitializer(classInstanceCreation);
                
                mainBlock.statements().add(statement);
                
                
    //調用getString方法:String r = program.getString("中國");
                MethodInvocation methodInvocation = ast.newMethodInvocation();
                methodInvocation.setExpression(ast.newSimpleName(
    "program"));
                methodInvocation.setName(ast.newSimpleName(
    "getString"));
                
                
    //String參數
                StringLiteral stringLiteral = ast.newStringLiteral();
                stringLiteral.setLiteralValue(
    "中國");
                methodInvocation.arguments().add(stringLiteral);
                
                
    //創建變量
                VariableDeclarationFragment fragment2 = ast.newVariableDeclarationFragment();
                fragment2.setName(ast.newSimpleName(
    "r"));
                VariableDeclarationStatement statement3 
    = ast.newVariableDeclarationStatement(fragment2);
                statement3.setType(ast.newSimpleType(ast.newSimpleName(
    "String")));
                fragment2.setInitializer(methodInvocation);
                
                mainBlock.statements().add(statement3);
                
                
                
    //輸出r的值: System.out.println(r);
                MethodInvocation methodInvocation2 = ast.newMethodInvocation();
                methodInvocation2.setExpression(ast.newName(
    "System.out"));
                methodInvocation2.setName(ast.newSimpleName(
    "println"));
                methodInvocation2.arguments().add(ast.newSimpleName(
    "r"));
                ExpressionStatement statement2 
    = ast.newExpressionStatement(methodInvocation2);
                
                mainBlock.statements().add(statement2);
            }

            
            
    //構造方法
            {
                MethodDeclaration constructorMethod 
    = ast.newMethodDeclaration();
                constructorMethod.setConstructor(
    true);
                constructorMethod.setName(ast.newSimpleName(
    "Program"));
                constructorMethod.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                
    {
                    
    //基本類型的參數
                    SingleVariableDeclaration p1 = ast.newSingleVariableDeclaration();
                    p1.setName(ast.newSimpleName(
    "a"));
                    p1.setType(ast.newPrimitiveType(PrimitiveType.INT));
                    
                    
    //int[]類型的參數
                    SingleVariableDeclaration p2 = ast.newSingleVariableDeclaration();
                    p2.setName(ast.newSimpleName(
    "b"));
                    p2.setType(ast.newArrayType(ast.newPrimitiveType(PrimitiveType.INT)));
                    
                    
    //引用類型的參數
                    SingleVariableDeclaration p3 = ast.newSingleVariableDeclaration();
                    p3.setName(ast.newSimpleName(
    "c"));
                    p3.setType(ast.newSimpleType(ast.newName(
    "Integer")));
                    p3.modifiers().add(ast.newModifier(ModifierKeyword.FINAL_KEYWORD));
                    
                    constructorMethod.parameters().add(p1);
                    constructorMethod.parameters().add(p2);
                    constructorMethod.parameters().add(p3);
                }

                
                Block constructBlock 
    = ast.newBlock();
                constructorMethod.setBody(constructBlock);
                
                programClass.bodyDeclarations().add(constructorMethod);
                
                SuperConstructorInvocation superConstructorInvocation 
    = ast.newSuperConstructorInvocation();
                constructBlock.statements().add(superConstructorInvocation);
                superConstructorInvocation.arguments().add(ast.newNullLiteral());
            }

            
            
    /*定義一個方法,形如:
            public String getString(String name){
                String newString = name + "你好";
                return newString;
            }
            
    */

            
            
    {
                MethodDeclaration getString 
    = ast.newMethodDeclaration();
                getString.setName(ast.newSimpleName(
    "getString"));
                getString.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                SingleVariableDeclaration p 
    = ast.newSingleVariableDeclaration();
                p.setName(ast.newSimpleName(
    "p"));
                p.setType(ast.newSimpleType(ast.newName(
    "String")));
                getString.parameters().add(p);
                getString.setReturnType2(ast.newSimpleType(ast.newSimpleName(
    "String")));
                
                
                
    //創建塊
                Block block = ast.newBlock();
                getString.setBody(block);
                programClass.bodyDeclarations().add(getString);
                
                
    //方法內容----定義String變量
                VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
                fragment.setName(ast.newSimpleName(
    "newString"));
                VariableDeclarationStatement statement 
    = ast.newVariableDeclarationStatement(fragment);
                
                
    //String newString = "初始值";
                /*StringLiteral stringLiteral2 = ast.newStringLiteral();
                stringLiteral2.setLiteralValue("初始值");
                fragment.setInitializer(stringLiteral2);
    */

                
                ClassInstanceCreation classInstanceCreation 
    = ast.newClassInstanceCreation();
                classInstanceCreation.setType(ast.newSimpleType(ast.newName(
    "String")));
                SingleVariableDeclaration p1 
    = ast.newSingleVariableDeclaration();
                StringLiteral stringLiteral3 
    = ast.newStringLiteral();
                stringLiteral3.setLiteralValue(
    "初始值");
                classInstanceCreation.arguments().add(stringLiteral3);
                fragment.setInitializer(classInstanceCreation);
                
                statement.setType(ast.newSimpleType(ast.newName(
    "String")));
                
                Assignment assignment 
    = ast.newAssignment();
                assignment.setLeftHandSide(ast.newSimpleName(
    "newString"));
                StringLiteral stringLiteral 
    = ast.newStringLiteral();
                stringLiteral.setLiteralValue(
    "你好");
                
                assignment.setRightHandSide(stringLiteral);
                assignment.setOperator(Operator.ASSIGN);
                ExpressionStatement statement2 
    = ast.newExpressionStatement(assignment);
                
                block.statements().add(statement);
                block.statements().add(statement2);
                
                
    //方法調用
                MethodInvocation methodInvocation = ast.newMethodInvocation();
                methodInvocation.setExpression(ast.newName(
    "newString"));
                methodInvocation.setName(ast.newSimpleName(
    "index")); //方法名
                StringLiteral stringLiteral2 = ast.newStringLiteral();
                stringLiteral2.setLiteralValue(
    "");
                methodInvocation.arguments().add(stringLiteral2);
                
                VariableDeclarationFragment fragment2 
    = ast.newVariableDeclarationFragment();
                fragment2.setInitializer(methodInvocation);
                fragment2.setName(ast.newSimpleName(
    "result"));
                VariableDeclarationStatement statement3 
    = ast.newVariableDeclarationStatement(fragment2);
                statement3.setType(ast.newSimpleType(ast.newName(
    "String")));
                
                block.statements().add(statement3);
                
                StringLiteral stringLiteral4 
    = ast.newStringLiteral();
                stringLiteral4.setLiteralValue(
    "你好");
                
                InfixExpression infixExpression 
    = ast.newInfixExpression();
                infixExpression.setLeftOperand(ast.newName(
    "name"));
                infixExpression.setOperator(org.eclipse.jdt.core.dom.InfixExpression.Operator.PLUS);
                infixExpression.setRightOperand(stringLiteral4);
                
                Assignment assignment2 
    = ast.newAssignment();
                assignment2.setLeftHandSide(ast.newSimpleName(
    "newString"));
                assignment2.setOperator(Operator.ASSIGN);
                assignment2.setRightHandSide(infixExpression);
                
                ExpressionStatement statement4 
    = ast.newExpressionStatement(assignment2);
                
                block.statements().add(statement4);

                ReturnStatement rs 
    = ast.newReturnStatement();
                rs.setExpression(ast.newName(
    "newString"));
                block.statements().add(rs);
            }

            
            
    /**
             * 定義一個方法,形如:
             * public String isOdd(int a) throws NullPointerException, Exception{
             *     if(a < 0) throw new Exception("數字不能為負數");
             * 
             *     if(a % 2 == 0){
             *         return "偶數";
             *     }else{
             *         System.out.println("完");
             *         return "奇數";
             *     }
             
    */

            
    {
                MethodDeclaration methodDeclaration 
    = ast.newMethodDeclaration();
                methodDeclaration.setName(ast.newSimpleName(
    "isOdd"));
                methodDeclaration.modifiers().add(ast.newModifier(ModifierKeyword.PUBLIC_KEYWORD));
                methodDeclaration.setReturnType2(ast.newSimpleType(ast.newSimpleName(
    "String")));
                
    //設置參數
                SingleVariableDeclaration singleVariableDeclaration = ast.newSingleVariableDeclaration();
                singleVariableDeclaration.setName(ast.newSimpleName(
    "a"));
                singleVariableDeclaration.setType(ast.newPrimitiveType(PrimitiveType.INT));
                methodDeclaration.parameters().add(singleVariableDeclaration);

                
    //拋出異常
                methodDeclaration.thrownExceptions().add(ast.newSimpleName("NullPointerException"));
                methodDeclaration.thrownExceptions().add(ast.newSimpleName(
    "Exception"));
                
                
    //創建塊{}
                Block isOddBlock = ast.newBlock();
                methodDeclaration.setBody(isOddBlock);
                
                
    //創建if與異常
                IfStatement ifStatement = ast.newIfStatement();
                
    //表達式 a < 0
                InfixExpression infixExpression = ast.newInfixExpression();
                infixExpression.setLeftOperand(ast.newSimpleName(
    "a"));
                infixExpression.setOperator(org.eclipse.jdt.core.dom.InfixExpression.Operator.LESS);
                
                NumberLiteral numberLiteral 
    = ast.newNumberLiteral("0");
                infixExpression.setRightOperand(numberLiteral);
                
                ifStatement.setExpression(infixExpression);
                
                
    //設置if中的內容
                ThrowStatement throwStatement = ast.newThrowStatement();
                ClassInstanceCreation classInstanceCreation 
    = ast.newClassInstanceCreation();
                classInstanceCreation.setType(ast.newSimpleType(ast.newSimpleName(
    "Exception")));
                StringLiteral stringLiteral 
    = ast.newStringLiteral();
                stringLiteral.setLiteralValue(
    "數字不能為負數");
                classInstanceCreation.arguments().add(stringLiteral);
                throwStatement.setExpression(classInstanceCreation);
                ifStatement.setThenStatement(throwStatement);
                
                
    //if(a % 2 == 0)
                IfStatement ifStatement2 = ast.newIfStatement();
                InfixExpression infixExpression2 
    = ast.newInfixExpression();
                infixExpression2.setLeftOperand(ast.newSimpleName(
    "a"));
                infixExpression2.setOperator(org.eclipse.jdt.core.dom.InfixExpression.Operator.REMAINDER);
                NumberLiteral numberLiteral2 
    = ast.newNumberLiteral("2");
                infixExpression2.setRightOperand(numberLiteral2);
                
                InfixExpression infixExpression3 
    = ast.newInfixExpression();
                infixExpression3.setLeftOperand(infixExpression2);
                infixExpression3.setOperator(org.eclipse.jdt.core.dom.InfixExpression.Operator.EQUALS);
                NumberLiteral numberLiteral3 
    = ast.newNumberLiteral("0");
                infixExpression3.setRightOperand(numberLiteral3);
                
                ifStatement2.setExpression(infixExpression3);
                
                
    //return "偶數";
                ReturnStatement returnStatement = ast.newReturnStatement();
                StringLiteral stringLiteral2 
    = ast.newStringLiteral();
                stringLiteral2.setLiteralValue(
    "偶數");
                returnStatement.setExpression(stringLiteral2);
                ifStatement2.setThenStatement(returnStatement);
                
                
    //else
                Block elseBlock = ast.newBlock();
                MethodInvocation methodInvocation 
    = ast.newMethodInvocation();
                methodInvocation.setExpression(ast.newName(
    "System.out"));
                methodInvocation.setName(ast.newSimpleName(
    "println"));
                StringLiteral stringLiteral4 
    = ast.newStringLiteral();
                stringLiteral4.setLiteralValue(
    "");
                methodInvocation.arguments().add(stringLiteral4);
                ExpressionStatement statement 
    = ast.newExpressionStatement(methodInvocation);
                elseBlock.statements().add(statement);
                
                ReturnStatement returnStatement2 
    = ast.newReturnStatement();
                StringLiteral stringLiteral3 
    = ast.newStringLiteral();
                stringLiteral3.setLiteralValue(
    "奇數");
                returnStatement2.setExpression(stringLiteral3);
                elseBlock.statements().add(returnStatement2);
                
                ifStatement2.setElseStatement(elseBlock);
                
                isOddBlock.statements().add(ifStatement);
                isOddBlock.statements().add(ifStatement2);
                programClass.bodyDeclarations().add(methodDeclaration);
            }

            
            System.out.println(compilationUnit.toString());
            
        }

    }

    posted on 2010-02-12 01:56 李贊紅 閱讀(3010) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    導航

    <2010年2月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28123456
    78910111213

    統計

    常用鏈接

    留言簿(12)

    隨筆檔案(28)

    相冊

    技術友情博客

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲成人免费电影| 亚洲欧美国产国产综合一区| 91精品免费久久久久久久久| 亚洲中文无码亚洲人成影院| 久久精品国产精品亚洲| 91在线手机精品免费观看| 亚洲av无码片vr一区二区三区| 亚洲精品乱码久久久久久按摩 | 亚洲精品GV天堂无码男同| 国产成人精品日本亚洲专区| 69式互添免费视频| 又硬又粗又长又爽免费看| 亚洲影视一区二区| 亚洲精品老司机在线观看| 四虎精品视频在线永久免费观看| 美女黄色免费网站| 亚洲午夜国产精品无卡| 亚洲日韩精品一区二区三区无码| 欧美最猛性xxxxx免费| 两个人看的www免费视频中文| 亚洲色成人四虎在线观看| 亚洲人成中文字幕在线观看| 免费被黄网站在观看| 免费在线看黄的网站| 色婷婷综合缴情综免费观看| 亚洲免费在线视频播放| 中文字幕亚洲专区| 国产成人一区二区三区免费视频 | 日本久久久免费高清| 午夜无码A级毛片免费视频 | 暖暖在线视频免费视频| 美女被爆羞羞网站免费| 亚洲AV无码国产精品色| 亚洲网站在线观看| 亚洲中文字幕无码久久精品1| 国产免费看插插插视频| 无码人妻久久一区二区三区免费丨| 人成电影网在线观看免费| 亚洲精品人成网线在线播放va| 亚洲福利秒拍一区二区| 亚洲综合成人网在线观看|