在 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;
}
我是一名优秀的程序员,十分优秀!