0
我想從我的eclipse插件修改java源代碼。我已經提到了幾個教程,並完成了這個編碼。當我嘗試使用ASTVisitor和ASTRewrite類的代碼時。放置此代碼時代碼流將中斷。如果我刪除該塊內的代碼,該插件將運行。如何使用JDT修改源代碼?
我的意圖是從源代碼中刪除一個名爲annotaion的節點。請幫助我。我在這裏先向您的幫助表示感謝。
@Override
public boolean visit(SingleMemberAnnotation node) {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
IPath iPath = javaUnit.getPath();
try {
bufferManager.connect(iPath, null);
ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(iPath);
IDocument document = textFileBuffer.getDocument();
AST ast= node.getAST();
SingleMemberAnnotation singleMemberAnnotation = ast.newSingleMemberAnnotation();
singleMemberAnnotation.delete();
textFileBuffer
.commit(null , true);
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally
{
try {
bufferManager.disconnect(iPath, null);
} catch (CoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
TextEdit textEdit = null;
System.out.println("E");
try {
textEdit = rewrite.rewriteAST(new Document(javaUnit.getSource()),null);
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
textEdit.apply(new Document(javaUnit.getSource()));
} catch (MalformedTreeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JavaModelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.visit(node);
}