Bug 314836 - ASTParser fails on enums when setSource(char[] source) is used
Summary: ASTParser fails on enums when setSource(char[] source) is used
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 3.6 RC4   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords: Documentation
Depends on:
Blocks:
 
Reported: 2010-05-28 05:43 EDT by David Schuler CLA
Modified: 2010-06-01 11:33 EDT (History)
2 users (show)

See Also:


Attachments
JUnit test to reproduce the error. (1.51 KB, text/plain)
2010-05-28 05:46 EDT, David Schuler CLA
no flags Details
Proposed doc change (1.18 KB, patch)
2010-05-31 12:33 EDT, Olivier Thomann CLA
no flags Details | Diff
refer to JavaCore#setComplianceOptions(..) (1.85 KB, patch)
2010-06-01 08:24 EDT, Markus Keller CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.