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

Bug 479292

Summary: [formatter] Header comment formatting for package-info.java occurs even when "Format header comment" is unchecked
Product: [Eclipse Project] JDT Reporter: Phillip Webb <pwebb>
Component: CoreAssignee: Mateusz Matela <mateusz.matela>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: b.muskalla, jarthana, mateusz.matela
Version: 4.5.1   
Target Milestone: 4.5.2   
Hardware: PC   
OS: Mac OS X   
See Also: https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=4a2995e55bb490ddda70decb3fa4c48aaec59c94
https://bugs.eclipse.org/bugs/show_bug.cgi?id=113946
https://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?h=R4_5_maintenance&id=e8fd769578cd972bf3c36ecf3c3e1fe0e8276dde
Whiteboard:

Description Phillip Webb CLA 2015-10-07 20:04:02 EDT
Header comments of `package-info.java` files always appear to get formatted regardless of the "Enable header comment formatting" setting.

Possibly related to #184263
Comment 1 Phillip Webb CLA 2015-10-08 23:31:03 EDT
I think the root cause of this one is that `tokenManager.setHeaderEndIndex()` is never called for `package-info.java` files.


Looking at `DefaultCodeFormatter.findHeader()` the existing implementation has the following logic:


> List<TypeDeclaration> types = ((CompilationUnit) this.astRoot).types();
> if (!types.isEmpty()) {
> 	int headerEndIndex = this.tokenManager.firstIndexIn(types.get(0), -1);
> 	this.tokenManager.setHeaderEndIndex(headerEndIndex);
> }


With a package-info file the `types` collection is empty. Changing the code to this:

> private void findHeader() {
> 	if (this.astRoot instanceof CompilationUnit) {
> 		CompilationUnit compilationUnit = (CompilationUnit) this.astRoot;
> 		List<TypeDeclaration> types = compilationUnit.types();
> 		ASTNode node = (types.isEmpty() ? compilationUnit.getPackage() : types.get(0));
> 		if (node != null) {
> 			int headerEndIndex = this.tokenManager.firstIndexIn(node, -1);
> 			this.tokenManager.setHeaderEndIndex(headerEndIndex);
> 		}
> 	}
> }

Seems to solve the issue.
Comment 2 Mateusz Matela CLA 2015-10-09 17:35:43 EDT
Thanks Phillip!
Fixed with http://git.eclipse.org/c/jdt/eclipse.jdt.core.git/commit/?id=4a2995e55bb490ddda70decb3fa4c48aaec59c94

Keeping the bug open for eventual backport to 4.5.2. There's a regression fix (respecting the "Format header comment" setting), but also a fix for an older bug 113946 (differentiating between header comments and package javadoc).
Comment 3 Benjamin Muskalla CLA 2015-10-15 17:07:18 EDT
We'd love to see this backported as it makes it really hard to create package-info files consistently (and we love them for the null-support).
Comment 4 Sasikanth Bharadwaj CLA 2015-10-28 04:28:30 EDT
Verified for 4.6 M3 using I20151026-2000 build
Comment 6 Jay Arthanareeswaran CLA 2016-01-18 10:50:58 EST
Verified for 4.5.2 with build M20160113-1000