开发者

How to modify source code using JDT?

I am trying to modify the java source from my eclipse plugin. I have referred few tutorials for this and done this coding. When I try this code using ASTVisitor and ASTRewrite classes. The code flow breaks when this code is placed. If I remove the code inside this block the plugin runs.

My intention is to delete a node say Annotaion from the source code. Kindly help me in this. Thanks for the help in advance.

        @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.newSingleMem开发者_如何学编程berAnnotation();
                            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);           
                    }


You're applying your changes to a newly created IDocument instance that you're just throwing away. Apply them to the IDocument instance in the text file buffer, and connect/commit/disconnect the buffer from outside of the visitor--you're going to potentially do it far more times that necessary if you do it for every SingleMemberAnnotation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜