如何在AST Eclipse JDT中找到块/方法声明所需的import语句?

时间:2016-08-29 08:57:41

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

这是我的代码片段,在这里我尝试从另一个类的提取块创建一个新类。现在我需要找到所提取的方法中所需的所有import语句,这些语句已添加到生成的类中。

private void generateClass() throws IOException {

    AST ast = AST.newAST(AST.JLS8);
    CompilationUnit unit = ast.newCompilationUnit();

    TypeDeclaration type = ast.newTypeDeclaration();
    type.setInterface(false);
    type.modifiers().add(ast.newModifier(Modifier.ModifierKeyword.PUBLIC_KEYWORD));
    type.setName(ast.newSimpleName("GeneratedClass"));

    MethodDeclaration methodDeclaration = identifyASTNode.getMethodDeclaration();
    methodDeclaration = (MethodDeclaration)ASTNode.copySubtree(ast, methodDeclaration);

    type.bodyDeclarations().add(methodDeclaration);

    unit.types().add(type);

    writeToFile(unit.toString());
}

0 个答案:

没有答案