Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 544115

Summary: ASTParser.setSource has a different behavior from ASTParser.createASTs when there is an invalid if() statement
Product: [Eclipse Project] JDT Reporter: Tsantalis Nikolaos <tsantalis>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: Olivier_Thomann
Version: 4.11   
Target Milestone: ---   
Hardware: PC   
OS: Windows 10   
Whiteboard:
Attachments:
Description Flags
Input Java file to replicate the bug none

Description Tsantalis Nikolaos CLA 2019-02-04 15:57:40 EST
Created attachment 277431 [details]
Input Java file to replicate the bug

Method read() in the attached file contains
an invalid if() statement in line 82, which has no conditional expression and body.

When using the ASTParser.setSource(char[]) method to parse the code,
the generated compilation unit has an empty method body for read().

However, when using the ASTParser.createASTs(String[] sourceFilePaths, String[] encodings, String[] bindingKeys, FileASTRequestor requestor, IProgressMonitor monitor) method to parse the code,
the generated compilation unit has a non-empty body for read(),
and simply omits the invalid if() from the AST.

What is the reason for this different behavior?
Is it possible to setup the ASTParser to simply ignore such invalid code structures when using ASTParser.setSource() instead of generating an empty method?
Comment 1 Olivier Thomann CLA 2019-02-13 11:05:40 EST
Did you try to use:
org.eclipse.jdt.core.dom.ASTParser.setStatementsRecovery(boolean)
Comment 2 Tsantalis Nikolaos CLA 2019-02-13 18:30:53 EST
(In reply to Olivier Thomann from comment #1)

I enabled statements recovery and it worked.
I am sorry for taking your time with this problem, but I migrated my code from
ASTParser.createASTs() to ASTParser.setSource() and I found this difference in my test results.
Thank you again!