这是我的代码片段,在这里我尝试从另一个类的提取块创建一个新类。现在我需要找到所提取的方法中所需的所有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());
}