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

Bug 429221

Summary: [1.8][quick assist] BadLocationException with quick assist "Introduce new local with cast type"
Product: [Eclipse Project] JDT Reporter: Noopur Gupta <noopur_gupta>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: markus.kell.r
Version: 4.4   
Target Milestone: 4.5   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on: 443810    
Bug Blocks:    

Description Noopur Gupta CLA 2014-02-27 05:00:20 EST
package snippet;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

class TB {
	private void foo(Object o) {
		if (o instanceof @TA String) {
			
		}
	}
}

@Target(ElementType.TYPE_USE)
@interface TA {
}
---------------------------------------------------------

Place the caret at 'instanceof' in the above example and press Ctrl+1.
Choose "Introduce new local with cast type" and press Enter.
We get the following exception:

org.eclipse.jface.text.BadLocationException: First position: '@TA
			String' at 181, this position: '@TA String' at 206
	at org.eclipse.jface.text.link.LinkedPositionGroup.enforceEqualContent(LinkedPositionGroup.java:132)
	at org.eclipse.jface.text.link.LinkedPositionGroup.addPosition(LinkedPositionGroup.java:113)
	at org.eclipse.jdt.internal.ui.viewsupport.LinkedProposalModelPresenter.enterLinkedMode(LinkedProposalModelPresenter.java:82)
	at org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal.performChange(LinkedCorrectionProposal.java:159)
	at org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal.apply(CUCorrectionProposal.java:184)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertProposal(CompletionProposalPopup.java:945)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertSelectedProposalWithMask(CompletionProposalPopup.java:891)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup.access$27(CompletionProposalPopup.java:887)
	at org.eclipse.jface.text.contentassist.CompletionProposalPopup$5.widgetDefaultSelected(CompletionProposalPopup.java:667)
	at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:119)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
...
Comment 1 Noopur Gupta CLA 2014-09-11 06:38:02 EDT
Applying the quick assist results in:
		if (o instanceof @TA String) {
			@TA
			String string = (@TA String) o;

		}

The first linked position is:
			@TA
			String

The corresponding second linked position in the cast is:
@TA String

Both are different and hence the exception.

The first linked position should not have a newline between the type annotation and type.

Note that it happens with the default formatter profile where "Insert new line after annotations on local variables" is checked and does not happen when it is unchecked.
Comment 2 Markus Keller CLA 2014-09-11 11:09:58 EDT
Bug 443810 is not nice, but I don't think we can fix that. It's not nice that the JLS8 effectively broke syntactic formatting, but we can't change that.

The BadLocationException is a variant of bug 180973. I've proposed a fix there that will avoid the BLE.

Up to now, we were just lucky that the code formatter didn't support line wrapping inside type references (e.g. "java.util.List" is never wrapped to two lines, even if that could avoid exceeding the maximum line length).

*** This bug has been marked as a duplicate of bug 180973 ***