Bug 65090

Summary: ASTParser with kind == K_STATEMENTS doesn't work unless source range specified
Product: [Eclipse Project] JDT Reporter: Harold Gartner <hgartner>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.0   
Target Milestone: 3.0 RC2   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Apply on HEAD none

Description Harold Gartner CLA 2004-06-01 14:09:15 EDT
Using eclipse-SDK-3.0RC1-win32.zip.

I'm trying to create an AST tree from a snippet of Java code using code like this:

String src = "\tSystem.out.println(\"Hello\");\n\tSystem.out.println(\"World\");\n";
char[] source = src.toCharArray();
ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setKind (ASTParser.K_STATEMENTS);
parser.setSource (source);
ASTNode result = parser.createAST (new NullProgressMonitor());

The result I get back is always an empty block, and I would expect it to be a
block with two ExpressionStatements in it.

If I add one line of code before the parser.createAST call to set the source range:
   parser.setSourceRange(0, source.length);

then I get back a block with two ExpressionStatements in it.

According to the JavaDoc for 'setSourceRange':
"Sets the subrange of the source code to be parsed. By default, the entire
source string will be parsed (offset 0 and length -1)."

So from that text it seems that it is not required to set the source range.
It appears that the 'ASTParser.setSource (char[] source)' method should be
setting the source range or there is some other defect elsewhere when the
sourceLength == -1 is not being honoured.
Comment 1 Olivier Thomann CLA 2004-06-02 09:14:50 EDT
I am investigating.
Comment 2 Olivier Thomann CLA 2004-06-02 12:43:16 EDT
Created attachment 11476 [details]
Apply on HEAD
Comment 3 Philipe Mulet CLA 2004-06-02 12:45:18 EDT
Change approved. Please release for RC2.
Comment 4 Olivier Thomann CLA 2004-06-02 12:48:44 EDT
Fixed and released in HEAD.
Regression test added.
Comment 5 Olivier Thomann CLA 2004-06-02 12:50:10 EDT
All tests passed.
Comment 6 David Audel CLA 2004-06-11 07:24:42 EDT
Verified for 3.0RC2