вот мой код
Код: Выделить всё
public static void main(String[] args) throws IOException, JavaModelException {
File f = new File("src/Address1.txt");
String filePath = f.getAbsolutePath();
String str = readFileToString(filePath);
final ASTParser parser = ASTParser.newParser(AST.JLS3);
final Document document = new Document(str);
parser.setSource(document.get().toCharArray());
parser.setKind(ASTParser.K_COMPILATION_UNIT);
final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
final AST ast = cu.getAST();
ASTRewrite rewriter = ASTRewrite.create(ast);
cu.accept(new ASTVisitor() {
@Override
public boolean visit(MethodDeclaration node) {
String methodName = node.getName().getIdentifier();
if (methodName.equals("process")) {
System.out.println(methodName);
MethodDeclaration copiedMethod = (MethodDeclaration) rewriter.createCopyTarget(node);
copiedMethod.setName(ast.newSimpleName("process1"));
// MethodDeclaration copiedMethod = ASTNode.copySubtree(ast, node);
ASTNode parent = node.getParent();
ListRewrite listRewrite = rewriter.getListRewrite(parent, TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
listRewrite.insertLast(copiedMethod, null);
}
return super.visit(node);
}
});
//document.set(Formatter.format(document.get()));
try {
rewriter.rewriteAST(document, null).apply(document);
System.out.println(document.get());
} catch (MalformedTreeException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
я попробовал другой способ копирования узла
ASTNode.copySubtree (ast, node);
работает, но однострочный комментарий не удалось скопировать
пожалуйста, скажите мне, в чем проблема, я просто хочу скопировать метод и измените имя метода, и скопированный метод включает однострочный комментарий, а не только код Java
Подробнее здесь: https://stackoverflow.com/questions/788 ... -the-copie