Bug 314836

Summary: ASTParser fails on enums when setSource(char[] source) is used
Product: [Eclipse Project] JDT Reporter: David Schuler <schulerdavid>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: markus.kell.r, Olivier_Thomann
Version: 3.6Keywords: Documentation
Target Milestone: 3.6 RC4   
Hardware: Macintosh   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:
Attachments:
Description Flags
JUnit test to reproduce the error.
none
Proposed doc change
none
refer to JavaCore#setComplianceOptions(..) none

Description David Schuler CLA 2010-05-28 05:43:50 EDT
Build Identifier: 3.6.0_v48

ASTParser does not parse the following enum correctly when the source is set via setSource(char[] source), although JLS 3 is used:
enum A { E1, E2; }


When used with other setSource (e.g setSource(ITypeRoot source) ) methods it behaves correctly. This is due to the option org.eclipse.jdt.core.compiler.source which is then set correctly.


Reproducible: Always

Steps to Reproduce:
1. Run attached JUnit Test
Comment 1 David Schuler CLA 2010-05-28 05:46:29 EDT
Created attachment 170322 [details]
JUnit test to reproduce the error.
Comment 2 Olivier Thomann CLA 2010-05-28 08:31:48 EDT
It is clearly explained in the ASTParser documentation that compiler options are set to the JavaCore.getOptions() value if setSource(char[]) is used.
Therefore you need to explicitly set the compiler options in order to parse the source. The default values are clearly explain in the JavaCore class.

Closing as INVALID.
Comment 3 David Schuler CLA 2010-05-28 09:47:47 EDT
You are right. It is documented.

What led me to the impression that it is a bug, was that I followed the code snippet in the Javadoc of the class. I thought that that this code is sufficient to parse Java Code that uses 1.5 features. In addition the constant JLS3 reinforced this impression. 
When I used the parser it even handled foreach loops correctly (producing error messages though as I found out later).
When I stumbled across the enums, it took me quite some time to figure out the reason for the failure.

To save others from this pitfall, maybe the code snippet could be updated, or it could be mentioned explicitly in the Javadocs of setSource(char[]) that the default source (1.3) will be used.
Comment 4 Olivier Thomann CLA 2010-05-28 10:18:49 EDT
(In reply to comment #3)
> To save others from this pitfall, maybe the code snippet could be updated, or
> it could be mentioned explicitly in the Javadocs of setSource(char[]) that the
> default source (1.3) will be used.
Good suggestion. Will do. Thanks.
Comment 5 Olivier Thomann CLA 2010-05-31 12:33:24 EDT
Created attachment 170559 [details]
Proposed doc change

Let me know if this clarifies the problem you had.
Comment 6 Olivier Thomann CLA 2010-05-31 12:38:18 EDT
I also updated the documentation of the setSource(char[]) method.
Comment 7 David Schuler CLA 2010-05-31 13:07:31 EDT
Yes, this clarifies the problem. Thanks.
Comment 8 Frederic Fusier CLA 2010-06-01 07:04:44 EDT
Verified for 3.6RC3
Comment 9 Markus Keller CLA 2010-06-01 08:24:33 EDT
Created attachment 170625 [details]
refer to JavaCore#setComplianceOptions(..)

The advice in the Javadocs of ASTParser and setSource(char[]) is quite lengthy and only mentions some of the compliance options. It would be better to replace this with a reference to JavaCore#setComplianceOptions(String, Map).

The patch also fixes two other typos.
Comment 10 Markus Keller CLA 2010-06-01 08:26:22 EDT
.
Comment 11 Olivier Thomann CLA 2010-06-01 11:21:53 EDT
Thanks Markus. Released for RC4.