Bug 46610 - invalid position in EmptyStatement node
Summary: invalid position in EmptyStatement node
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.0 M1   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 49204 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-11-13 13:52 EST by Kelvin CLA
Modified: 2004-04-16 12:37 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kelvin CLA 2003-11-13 13:52:28 EST
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) ;"
Comment 1 Olivier Thomann CLA 2003-11-13 18:13:14 EST
I will investigate.
Comment 2 Olivier Thomann CLA 2003-11-13 18:48:09 EST
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.
Comment 3 Olivier Thomann CLA 2003-11-13 19:00:36 EST
Reduce severity since it is not reproducable.
Comment 4 Kelvin CLA 2003-11-13 19:25:55 EST
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;
        }
        
    }
Comment 5 Olivier Thomann CLA 2003-11-13 20:15:48 EST
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.
Comment 6 Olivier Thomann CLA 2003-11-13 20:37:30 EST
Ok, reproduced with 2.1. maintenance stream. This is fixed in 3.0.
Comment 7 Olivier Thomann CLA 2003-11-14 09:17:14 EST
There is no plan for now to backport it to 2.1.x stream.
Comment 8 Olivier Thomann CLA 2004-01-05 15:58:41 EST
*** Bug 49204 has been marked as a duplicate of this bug. ***
Comment 9 Olivier Thomann CLA 2004-04-16 12:37:19 EDT
Fixed only in the 3.0 stream.