Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Inserting block for nested if-else(plugin development to add block for if-else statement fails for inner if-elses)
icon9.gif  Inserting block for nested if-else [message #1413338] Thu, 28 August 2014 09:53 Go to next message
Megha Patel is currently offline Megha PatelFriend
Messages: 2
Registered: August 2014
Junior Member
I'm creating eclipse plugin to format/inject code.
One of the requirements is to add blocks (if not exist) for if-else statements.
In case of nested if-else, my code works well for outer if-else, but doesn't add block for inner if-else statements.
e.g.
if (i == 0)
			if (i == 1)
				System.out.println("A");
			else
				System.out.println("B");
		else if (i == 2)
			if (i == 3)
				System.out.println("C");
			else
				System.out.println("D");
		else if (i == 4)
			System.out.println("E");
		else
			System.out.println("F");


output after injecting blocks

if (i == 0){
			if (i == 1)
				System.out.println("A");
			else
				System.out.println("B");
                }
		else { if (i == 2)
			if (i == 3)
				System.out.println("C");
			else
				System.out.println("D");
		else if (i == 4)
			System.out.println("E");
		else
			System.out.println("F");
                }



Here is my code:
	for (TypeDeclaration typeDeclaration : allTypes) {
			typeDeclaration.accept(new ASTVisitor() {
				@Override
				public boolean visit(TypeDeclaration node) {
					node.accept(new ASTVisitor() {
						@Override
						public boolean visit(IfStatement ifStatement) {
							if (ifStatement != null) {
								Statement thenStatement = ifStatement.getThenStatement();
								Statement elseStatement = ifStatement.getElseStatement();
								String codeToReplace = "if(" + ifStatement.getExpression() + ")";
								if (thenStatement instanceof Block)
									codeToReplace += "" + thenStatement + "";
								else
									codeToReplace += "{\n" + thenStatement + "\n}";
								if (elseStatement != null) {
									if (elseStatement instanceof Block)
										codeToReplace += "else" + elseStatement + "";
									else
										codeToReplace += "else{\n" + elseStatement + "\n}";
								}
								replaceStatment(rewriter, getBlockInstence(ifStatement), codeToReplace, ifStatement);
							}
							return super.visit(ifStatement);

						}
					});
					return super.visit(node);
				}
			});
		}
		iCompilationUnit.applyTextEdit(rewriter.rewriteAST(), new NullProgressMonitor());
		iCompilationUnit.commitWorkingCopy(true, new NullProgressMonitor());




I also tried committing document on AST node visit
IDocument document = new org.eclipse.jface.text.Document(
											iCompilationUnit.getSource());
									TextEdit edits = cu.rewrite(document, null);
									document.replace(ifStatement.getStartPosition(),ifStatement.getLength(),
											codeToReplace);
									edits.apply(document);
									iCompilationUnit.getBuffer().setContents(document.get());
									iCompilationUnit.commitWorkingCopy(true, new NullProgressMonitor());
							
}



For replacing the document, I get right offset [ifStatement.getStartPosition();] for first IfStatement, & modified code [block added] gets replaced successfully. But for next nodes in this AST, I get the older offset & hence modified code for that gets placed at wrong place! How can I get updated startPosition for nodes?

Any sort of solution/suggestion is welcome.


Thanks for your time!
Re: Inserting block for nested if-else [message #1422263 is a reply to message #1413338] Fri, 12 September 2014 15:22 Go to previous message
Noopur Gupta is currently offline Noopur GuptaFriend
Messages: 58
Registered: December 2012
Member
You should create new Block ASTNode and move existing body of an 'if' statement as a Statement in the Block. Then, replace the Block as BODY of the IfStatement.

Use org.eclipse.jdt.core.dom.rewrite.ASTRewrite for this instead of String manipulations. Read the Javadoc of ASTRewrite and the article: https://www.eclipse.org/articles/article.php?file=Article-JavaCodeManipulation_AST/index.html

The JDT quick assists (Ctrl+1) code to do that is here:
org.eclipse.jdt.internal.ui.text.correction.QuickAssistProcessor.getAddBlockProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections)
Previous Topic:MQTT + Eclipse Paho + Java 8 + compiler with line command
Next Topic:From o.e.j.core.CompletionProposal to erased method signature?
Goto Forum:
  


Current Time: Sat Nov 09 03:10:22 GMT 2024

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

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

Back to the top