繁体   English   中英

JDT AST,如何生成声明

[英]JDT AST, how to generate a declaration

借助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);
    }

Eclipse ASTView是查看语言树设置方式的好工具。 当我需要创建一些代码时,我首先编写该代码,然后在ASTView中对其进行查看。 Thuis是我与开普勒一起使用的更新站点: 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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM