Bug 331557

Summary: ImportDeclaration isStatic returning false for static import (obtained from ASTParser)
Product: [Eclipse Project] JDT Reporter: Keith Power <keith.power>
Component: CoreAssignee: Olivier Thomann <Olivier_Thomann>
Status: VERIFIED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: keith.power, Olivier_Thomann, satyam.kandula
Version: 3.6.1   
Target Milestone: 3.7 M4   
Hardware: PC   
OS: Windows 7   
Whiteboard:
Attachments:
Description Flags
Test Case none

Description Keith Power CLA 2010-12-01 10:47:33 EST
Build Identifier: 3.6.1

I'm parsing a file using the ASTParser, then checking the imports. An import that is declared as static in the source is represented by an ImportDeclaration that returns false for isStatic.

Also tested on 3.5 with the same results.

Source uses ASTParser.createAST, but I also tested with createASTs on 3.6.1.

Reproducible: Always

Steps to Reproduce:
1. Download the source file, perhaps modifying package (will attach once submitted)
2. Run the program providing the path to the Java file as an argument
3. Note output is "Is java.lang.Math.PI import static ? false"
Comment 1 Keith Power CLA 2010-12-01 10:48:38 EST
Created attachment 184271 [details]
Test Case
Comment 2 Olivier Thomann CLA 2010-12-01 10:51:41 EST
I'll take a look.
Comment 3 Keith Power CLA 2010-12-01 10:57:22 EST
Very prompt :-) Thanks. I meant also to say that the length of the ImportDeclaration node is correct (32) but the String being returned with toString is missing the static keyword also.
Comment 4 Olivier Thomann CLA 2010-12-01 11:00:08 EST
I am investigating. It looks like the static modifier is lost after the import reference is created.
Really weird.
Comment 5 Keith Power CLA 2010-12-01 11:28:23 EST
I checked the supplied test case in the ASTView plugin in Eclipse 3.5 installation and it correctly shows the import as static. Not sure if that's of any help to you.
Comment 6 Olivier Thomann CLA 2010-12-01 11:42:36 EST
I know what is going on.
Static imports are only valid in 1.5 and above.
In your compiler options, you don't set the compliance, source or target platform and therefore you end up with the default (1.4).
Change your getCompilerOptions() method for:
	private static Map getCompilerOptions() {
		Map defaultOptions = new HashMap();
		defaultOptions.put(JavaCore.COMPILER_LOCAL_VARIABLE_ATTR, JavaCore.GENERATE);
		defaultOptions.put(JavaCore.COMPILER_PB_UNUSED_PRIVATE_MEMBER, JavaCore.IGNORE);
		defaultOptions.put(JavaCore.COMPILER_PB_LOCAL_VARIABLE_HIDING, JavaCore.WARNING);
		defaultOptions.put(JavaCore.COMPILER_PB_FIELD_HIDING, JavaCore.WARNING);
		defaultOptions.put(JavaCore.COMPILER_PB_POSSIBLE_ACCIDENTAL_BOOLEAN_ASSIGNMENT, JavaCore.WARNING);
		defaultOptions.put(JavaCore.COMPILER_PB_SYNTHETIC_ACCESS_EMULATION, JavaCore.WARNING);
		defaultOptions.put(JavaCore.COMPILER_PB_SYNTHETIC_ACCESS_EMULATION, JavaCore.WARNING);
		defaultOptions.put(JavaCore.COMPILER_CODEGEN_UNUSED_LOCAL, JavaCore.PRESERVE);
		defaultOptions.put(JavaCore.COMPILER_PB_UNNECESSARY_ELSE, JavaCore.WARNING);
		defaultOptions.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_5);
		defaultOptions.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_5);
		defaultOptions.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_5);
		return defaultOptions;
	}

This is using JavaCore options and not the internal CompilerOptions class.
Then it works fine.
Closing as INVALID.

If you would check the getProblems() of the compilation unit, you would have seen:
Is java.lang.Math.PI import static ? false
Number of problems : 1
Syntax error, static imports are only available if source level is 1.5
Comment 7 Keith Power CLA 2010-12-01 11:54:28 EST
Many thanks for the quick solution, and apologies for not spotting that myself. Thanks for the pointer to getProblems() too, I'll check that in future.
Comment 8 Satyam Kandula CLA 2010-12-07 04:12:52 EST
Verified for 3.7M4