gpt4 book ai didi

java - JDT AST,如何生成声明

转载 作者:太空宇宙 更新时间:2023-11-04 15:05:48 25 4
gpt4 key购买 nike

在 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 是我与 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;
}

关于java - JDT AST,如何生成声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com