本代碼演示如何通過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());
}
}
