Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Getting exception at rewriteAST(java.lang.IllegalArgumentException: Document does not match the AST)
Getting exception at rewriteAST [message #1707722] Tue, 08 September 2015 15:00 Go to next message
Eclipse UserFriend
I am trying to replace annotation node with another annotation :


ASTParser parser = ASTParser.newParser(AST.JLS4);
			parser.setKind(ASTParser.K_COMPILATION_UNIT);
			parser.setIgnoreMethodBodies(true);
			parser.setBindingsRecovery(true);
			parser.setSource(iCompilationUnit);
			parser.setResolveBindings(true);
			CompilationUnit compUnit = (CompilationUnit) parser.createAST(null); // parse
			final AST ast = compUnit.getAST();
			compUnit.recordModifications();
			final Document document = new Document();
			final ASTRewrite rewriter = ASTRewrite.create(ast);

			compUnit.accept(new ASTVisitor() {
				public boolean visit(SingleMemberAnnotation node) {

					final NormalAnnotation newNormalAnnotation = ast.newNormalAnnotation();
					newNormalAnnotation.setTypeName(ast.newName("AnnotationTest"));

					rewriter.replace(node, newNormalAnnotation, null);
					return true;
				}

			});	
		
			TextEdit edits = rewriter.rewriteAST(document, null);
			

I am getting an exception at
TextEdit edits = rewriter.rewriteAST(document, null);

java.lang.IllegalArgumentException: Document does not match the AST
Caused by: org.eclipse.core.runtime.CoreException: End Of File
Re: Getting exception at rewriteAST [message #1707729 is a reply to message #1707722] Tue, 08 September 2015 15:59 Go to previous messageGo to next message
Eclipse UserFriend
You didn't initialize your parser with a document, you used an ICompilationUnit. So you can't use rewriteAST(IDocument, Map). Instead, use
TextEdit edits = rewriter.rewriteAST();
Re: Getting exception at rewriteAST [message #1707745 is a reply to message #1707729] Wed, 09 September 2015 01:45 Go to previous message
Eclipse UserFriend
Document document = new Document(iCompilationUnit.getSource());


Solved everything.

Thank you

[Updated on: Wed, 09 September 2015 01:48] by Moderator

Previous Topic:cvs required password each time restart
Next Topic:UI Dialog empty depending on platform export
Goto Forum:
  


Current Time: Mon Apr 21 02:19:10 EDT 2025

Powered by FUDForum. Page generated in 0.24663 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top