Community
Participate
Working Groups
Test case: if (false); else if (true) ; else ; // <----- invalid position here is my code for the second if statement : statement elsestatement = ifstatement.getElseStatement(); System.out.println(content.substring(emptystatement.getStartPosition-5, emptystatement.getStartPosition()+1)); The display suppose to be "else ;"; however, it is "rue) ;"
I will investigate.
I could not reproduce your problem. Here is my test case: ICompilationUnit sourceUnit = getCompilationUnit("Converter" , "", "test0503", "A.java"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ CompilationUnit unit = (CompilationUnit)runConversion(sourceUnit, true); char[] source = sourceUnit.getSource().toCharArray(); ASTNode node = getASTNode(unit, 0, 0, 0); assertNotNull(node); assertTrue("not an if statement", node.getNodeType() == ASTNode.IF_STATEMENT); //$NON-NLS-1$ IfStatement ifStatement = (IfStatement) node; Statement elseStatement = ifStatement.getElseStatement(); assertTrue("not an if statement", elseStatement.getNodeType() == ASTNode.IF_STATEMENT); //$NON-NLS-1$ IfStatement ifStatement2 = (IfStatement) elseStatement; Statement elseStatement2 = ifStatement2.getElseStatement(); assertTrue("not an if statement", elseStatement2.getNodeType() == ASTNode.EMPTY_STATEMENT); //$NON-NLS-1$ checkSourceRange(elseStatement2, ";", source); assertEquals("Wrong source", "else ;", new String(source).substring(elseStatement2.getStartPosition() - 5, elseStatement2.getStartPosition() + 1)); The test case is: package test0503; public class A { public void bar() { if (false) ; else if (true) ; else ; } } This code: ASTNode node = getASTNode(unit, 0, 0, 0); returns the first statement of the first method of the first type. So it returns the if(false) .... statement. Then I take its else statement (second if) and I take again the else statement. This is the variable called elseStatement2. source is a char[] containing the source of my compilation unit. When I get: new String(source).substring(elseStatement2.getStartPosition() - 5, elseStatement2.getStartPosition() + 1)) I have "else ;" and I check this is equals to "else ;". This test runs ok and I think this is the expected result. To get your result, I have to replace: Statement elseStatement2 = ifStatement2.getElseStatement(); with: Statement elseStatement2 = ifStatement2.getThenStatement(); Are you sure that you are checking the right empty statement? I need more information for further investigation.
Reduce severity since it is not reproducable.
how about if you try in this way: public class Test { public void method(CompilationUnit unit, String content) { MyVvisitor v = new MyVisitor(); unit.accept(v); Iterator iter = v.getList().iterator(); while (iter.hasNext()) { IfStatement ifstat = (IfStatement)iter.next(); Statement elsestat = ifstat.getElseStatement(); if (elsestat != null) { if (elsestat.getNodeType() == ASTNode.IF_STATEMENT) { IfStatement ifstat2 = (IfStatement)elsestat; Statement elsestat2 = ifstat2.getElseStatement(); if (elsestat2 != null && elsestat2.getNodeType() == ASTNode.EMPTY_STATEMENT ) { assertEquals("Wrong source", "else ;", content.substring( elseStatement2.getStartPosition() - 5 , elseStatement2.getStartPosition() + 1)); } } } } } private class MyVisitor extends ASTVisitor { private List _array = new ArrayList(); public List getList() { return _array; } public boolean visit(IfStatement node) { _array.add(node); return true; } }
I didn't try with your code, but I think I understand my mistake. I tried the 3.0 stream, not the 2.1.x stream. I'll try my test case in the 2.1.x stream tomorrow.
Ok, reproduced with 2.1. maintenance stream. This is fixed in 3.0.
There is no plan for now to backport it to 2.1.x stream.
*** Bug 49204 has been marked as a duplicate of this bug. ***
Fixed only in the 3.0 stream.