JDT AST,如何生成声明

时间:2014-02-25 13:45:12

标签: java abstract-syntax-tree eclipse-jdt

在SO和一些文档的帮助下,我能够快速汇编以下代码以生成Java源代码。但现在我不得不试图添加声明声明。我只想创建一个类似于

的语句
Connection con = null;
try{ 
   con = DataSource.getConnection();    
}catch(Exception ex){
   ex.printStackTrace();
}

我被困在第一个最简单的陈述形式,`VariableDeclarationStatement' 这是我到目前为止,但不知道如何使用variableDeclarationFragment或variableDeclarationExpression。

public static void main(String[] args) {

        CompilationUnit unit = ast.newCompilationUnit();
        PackageDeclaration packageDeclaration = ast.newPackageDeclaration();
        packageDeclaration.setName(ast.newSimpleName("example"));
        unit.setPackage(packageDeclaration);
        ImportDeclaration importDeclaration = ast.newImportDeclaration();
        QualifiedName name = ast.newQualifiedName(ast.newSimpleName("java"), ast.newSimpleName("util"));
        importDeclaration.setName(name);
        importDeclaration.setOnDemand(true);
        unit.imports().add(importDeclaration);
        TypeDeclaration type = ast.newTypeDeclaration();
        type.setInterface(false);
        type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
        type.setName(ast.newSimpleName("HelloWorld"));
        MethodDeclaration createMethod = createMethod("helloWorld", new HashMap<String, String>());
        Block block = ast.newBlock();
        //VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
        //variableDeclarationFragment.
        //block.statements().add(ast.newVariableDeclarationExpression(( getConnectionDeclaration()));
        //VariableDeclarationExpression variableDeclarationExpression = ast.newVariableDeclarationExpression(ast.newVariableDeclarationFragment());
        //block.
        //variableDeclarationExpression.
        createMethod.setBody(block);
        type.bodyDeclarations().add(createMethod);
        unit.types().add(type);
        System.out.println(unit);
    }

1 个答案:

答案 0 :(得分:0)

Eclipse ASTView是一个很好的工具,可以查看语言树的设置方式。当我需要创建一些代码时,我首先编写该代码然后在ASTView中查看它。 Thuis是我与Kepler一起使用的更新站点:http://www.eclipse.org/jdt/ui/update-site

我最近做了一些与你的情况非常相似的事情。这是稍微修改过的代码,应该是填充HelloWord.helloWord方法的一个很好的示例。

private Block createStatements(AST ast) {
    Block result = ast.newBlock();

    VariableDeclarationStatement conDeclStmt = createVariableDeclarationStatement(ast);
    result.statements().add(conDeclStmt);
    TryStatement tryStmt = createTryStatement(ast);
    result.statements().add(tryStmt);

    return result;
}

private VariableDeclarationStatement createVariableDeclarationStatement(AST ast) {
    VariableDeclarationFragment fragment = ast.newVariableDeclarationFragment();
    fragment.setName(ast.newSimpleName("con"));
    fragment.setInitializer(ast.newNullLiteral());
    VariableDeclarationStatement result = ast.newVariableDeclarationStatement(fragment);
    return result;
}

private TryStatement createTryStatement(AST ast) {
    TryStatement result = ast.newTryStatement();

    Block body = ast.newBlock();
    ExpressionStatement assignment = createAssignmentStatement(ast);
    body.statements().add(assignment);
    result.setBody(body);

    CatchClause catchClause = createCatchClause(ast);
    result.catchClauses().add(catchClause);

    return result;
}

private ExpressionStatement createAssignmentStatement(AST ast) {
    MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setExpression(ast.newSimpleName("DataSource"));
    invocation.setName(ast.newSimpleName("getConnection"));

    Assignment assignment = ast.newAssignment();
    assignment.setLeftHandSide(ast.newSimpleName("con"));
    assignment.setOperator(Operator.ASSIGN);
    assignment.setRightHandSide(invocation);
    return ast.newExpressionStatement(assignment);
}

private CatchClause createCatchClause(AST ast) {
    CatchClause result = ast.newCatchClause();

    SingleVariableDeclaration exDecl = ast.newSingleVariableDeclaration();
    exDecl.setType(ast.newSimpleType(ast.newSimpleName("Exception")));
    exDecl.setName(ast.newSimpleName("ex"));
    result.setException(exDecl);

    Block body = ast.newBlock();
    ...

    return result;
}