This Bugzilla instance is deprecated, and most Eclipse projects now use GitHub or Eclipse GitLab. Please see the deprecation plan for details.
Bug 548002 - For each conversion doesn't work for collections
Summary: For each conversion doesn't work for collections
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.11   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 4.13 M3   Edit
Assignee: Jeff Johnston CLA
QA Contact: Jeff Johnston CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 550334 550672 550726
  Show dependency tree
 
Reported: 2019-06-06 08:57 EDT by Alexander Kurtakov CLA
Modified: 2019-09-04 21:36 EDT (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Kurtakov CLA 2019-06-06 08:57:21 EDT
With the following code:
package test;

import java.util.Arrays;
import java.util.List;

public class Test2 {

	public static void main(String[] args) {
		List<String> list = Arrays.asList("ZERO", "ONE", "TWO");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}
	}

}

JDT provides no proposal to convert it to foreach.
Comment 1 Eclipse Genie CLA 2019-07-12 12:19:31 EDT
New Gerrit change created: https://git.eclipse.org/r/146018
Comment 3 Jeff Johnston CLA 2019-07-19 12:15:05 EDT
Resolved for 4.13M3
Comment 4 Eclipse Genie CLA 2019-07-23 16:32:15 EDT
New Gerrit change created: https://git.eclipse.org/r/146534
Comment 6 Matthias Becker CLA 2019-08-16 04:16:10 EDT
I think this change caused a regression.
I am using 4.13 M2 and when saving one of my java classes I get an error dialog saying
that "save actions have failed" because "A save participant caused problems".

The cause is this NPE:

java.lang.NullPointerException
	at org.eclipse.jdt.internal.corext.fix.ConvertForLoopOperation.getIntroducedVariableName(ConvertForLoopOperation.java:657)
	at org.eclipse.jdt.internal.corext.fix.ConvertLoopFixCore$ControlStatementFinder.getConvertOperation(ConvertLoopFixCore.java:87)
	at org.eclipse.jdt.internal.corext.fix.ConvertLoopFixCore$ControlStatementFinder.visit(ConvertLoopFixCore.java:56)
	at org.eclipse.jdt.core.dom.ForStatement.accept0(ForStatement.java:212)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2874)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:2945)
	at org.eclipse.jdt.core.dom.Block.accept0(Block.java:128)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2874)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChild(ASTNode.java:2922)
	at org.eclipse.jdt.core.dom.MethodDeclaration.accept0(MethodDeclaration.java:617)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2874)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:2945)
	at org.eclipse.jdt.core.dom.TypeDeclaration.accept0(TypeDeclaration.java:447)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2874)
	at org.eclipse.jdt.core.dom.ASTNode.acceptChildren(ASTNode.java:2945)
	at org.eclipse.jdt.core.dom.CompilationUnit.accept0(CompilationUnit.java:258)
	at org.eclipse.jdt.core.dom.ASTNode.accept(ASTNode.java:2874)
	at org.eclipse.jdt.internal.corext.fix.ConvertLoopFix.createCleanUp(ConvertLoopFix.java:43)
	at org.eclipse.jdt.internal.ui.fix.ConvertLoopCleanUp.createFix(ConvertLoopCleanUp.java:54)
	at org.eclipse.jdt.internal.corext.fix.CleanUpRefactoring.calculateChange(CleanUpRefactoring.java:792)
	at org.eclipse.jdt.internal.corext.fix.CleanUpPostSaveListener.saved(CleanUpPostSaveListener.java:390)
	at org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitDocumentProvider$5.run(CompilationUnitDocumentProvider.java:1639)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)


In line 657: String[] proposals= getVariableNameProposals(fArrayAccess.resolveTypeBinding(), javaProject);

fArrayAccess is null.

Commit ce2677be1d23a0c did touch the validateLengthQuery method - it added third branch:
} else if (lengthQuery instanceof MethodInvocation) {

in the other two fArrayAccess and fArrayBinding is set, but it's not set in this new branch.
Comment 7 Eclipse Genie CLA 2019-08-16 04:17:40 EDT
New Gerrit change created: https://git.eclipse.org/r/147819
Comment 8 Matthias Becker CLA 2019-08-16 04:19:11 EDT
(In reply to Eclipse Genie from comment #7)
> New Gerrit change created: https://git.eclipse.org/r/147819

This change fixed my concrete use-case.
I just did it by doing similar coding as in the other two branches. Don't know if it really is correct.
As this is mainly copied code one should sind about refactoring this into a private method or somethings.

Can a JDT commit pls. take over this change and finalize it?
Comment 9 Jeff Johnston CLA 2019-08-16 10:52:22 EDT
(In reply to Matthias Becker from comment #8)
> (In reply to Eclipse Genie from comment #7)
> > New Gerrit change created: https://git.eclipse.org/r/147819
> 
> This change fixed my concrete use-case.
> I just did it by doing similar coding as in the other two branches. Don't
> know if it really is correct.
> As this is mainly copied code one should sind about refactoring this into a
> private method or somethings.
> 
> Can a JDT commit pls. take over this change and finalize it?

I'll take it.  Please supply the details regarding the for loop that this broke for.  It should be added as a test case
Comment 10 Matthias Becker CLA 2019-08-16 11:01:40 EDT
(In reply to Jeff Johnston from comment #9)
> (In reply to Matthias Becker from comment #8)
> > (In reply to Eclipse Genie from comment #7)
> > > New Gerrit change created: https://git.eclipse.org/r/147819
> > 
> > This change fixed my concrete use-case.
> > I just did it by doing similar coding as in the other two branches. Don't
> > know if it really is correct.
> > As this is mainly copied code one should sind about refactoring this into a
> > private method or somethings.
> > 
> > Can a JDT commit pls. take over this change and finalize it?
> 
> I'll take it.  Please supply the details regarding the for loop that this
> broke for.  It should be added as a test case

I cannot provide the complete class. But the loop was this one:

		for (int i = 0; i < sortedProposals.size(); i++) {
		}


Quite simple. Or is the body of the loop also relevant?
Comment 11 Eclipse Genie CLA 2019-08-16 11:02:41 EDT
New Gerrit change created: https://git.eclipse.org/r/147846
Comment 12 Jeff Johnston CLA 2019-08-16 11:12:34 EDT
(In reply to Matthias Becker from comment #10)
> (In reply to Jeff Johnston from comment #9)
> > (In reply to Matthias Becker from comment #8)
> > > (In reply to Eclipse Genie from comment #7)
> > > > New Gerrit change created: https://git.eclipse.org/r/147819
> > > 
> > > This change fixed my concrete use-case.
> > > I just did it by doing similar coding as in the other two branches. Don't
> > > know if it really is correct.
> > > As this is mainly copied code one should sind about refactoring this into a
> > > private method or somethings.
> > > 
> > > Can a JDT commit pls. take over this change and finalize it?
> > 
> > I'll take it.  Please supply the details regarding the for loop that this
> > broke for.  It should be added as a test case
> 
> I cannot provide the complete class. But the loop was this one:
> 
> 		for (int i = 0; i < sortedProposals.size(); i++) {
> 		}
> 
> 
> Quite simple. Or is the body of the loop also relevant?

To some degree - i.e. how the sortedProposals is referenced and how it is originally declared for the for loop to see is important (e.g. parameter, field, local variable).  If you can reproduce with a simple test, that would be helpful as there are already tests in place that work fine.

That said, I have posted a fix.  The call to getVariableNameProposals should have been screened with a check for fisCollection like it is done elsewhere and a special collections version of the method should have been used.
  
Please confirm that the fix works for you.
Comment 13 Matthias Becker CLA 2019-08-16 12:04:36 EDT
(In reply to Jeff Johnston from comment #12)
> (In reply to Matthias Becker from comment #10)
> > (In reply to Jeff Johnston from comment #9)
> > > (In reply to Matthias Becker from comment #8)
> > > > (In reply to Eclipse Genie from comment #7)
> > > > > New Gerrit change created: https://git.eclipse.org/r/147819
> > > > 
> > > > This change fixed my concrete use-case.
> > > > I just did it by doing similar coding as in the other two branches. Don't
> > > > know if it really is correct.
> > > > As this is mainly copied code one should sind about refactoring this into a
> > > > private method or somethings.
> > > > 
> > > > Can a JDT commit pls. take over this change and finalize it?
> > > 
> > > I'll take it.  Please supply the details regarding the for loop that this
> > > broke for.  It should be added as a test case
> > 
> > I cannot provide the complete class. But the loop was this one:
> > 
> > 		for (int i = 0; i < sortedProposals.size(); i++) {
> > 		}
> > 
> > 
> > Quite simple. Or is the body of the loop also relevant?
> 
> To some degree - i.e. how the sortedProposals is referenced and how it is
> originally declared for the for loop to see is important (e.g. parameter,
> field, local variable).  If you can reproduce with a simple test, that would
> be helpful as there are already tests in place that work fine.

This is a member of the class
protected List<acme.DdlCompletion> semanticCompletions = new ArrayList<acme.IDdlParser.DdlCompletion>();

and this is a local variable just some line in front of the loop.
List<AbstractDdlCodeCompletionProposal> sortedProposals = this.semanticCodeCompletionProposals;

> Please confirm that the fix works for you.

Yes the fixe works for me.
Comment 15 Matthias Becker CLA 2019-08-19 03:41:22 EDT
(In reply to Eclipse Genie from comment #14)
> Gerrit change https://git.eclipse.org/r/147846 was merged to [master].
> Commit:
> http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/
> ?id=688926b42084deeb203e2cff3528ec03108976a6

are you going to provide another gerrit that contains an automated test for that use case?
Comment 16 Jeff Johnston CLA 2019-08-19 11:47:05 EDT
i(In reply to Matthias Becker from comment #15)
> (In reply to Eclipse Genie from comment #14)
> > Gerrit change https://git.eclipse.org/r/147846 was merged to [master].
> > Commit:
> > http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/
> > ?id=688926b42084deeb203e2cff3528ec03108976a6
> 
> are you going to provide another gerrit that contains an automated test for
> that use case?

I'll try and get it in today if possible.
Comment 17 Eclipse Genie CLA 2019-08-19 14:20:13 EDT
New Gerrit change created: https://git.eclipse.org/r/147940
Comment 19 Jeff Johnston CLA 2019-08-19 15:59:53 EDT
(In reply to Matthias Becker from comment #15)
> (In reply to Eclipse Genie from comment #14)
> > Gerrit change https://git.eclipse.org/r/147846 was merged to [master].
> > Commit:
> > http://git.eclipse.org/c/jdt/eclipse.jdt.ui.git/commit/
> > ?id=688926b42084deeb203e2cff3528ec03108976a6
> 
> are you going to provide another gerrit that contains an automated test for
> that use case?

Test merged.  It was a path taken by adding as a cleanup that wasn't tested in the quickfix tests.  I have added a cleanup test and verified it called the same method that was fixed to remove the NPE.
Comment 20 Jeff Johnston CLA 2019-08-19 16:00:21 EDT
Released for 4.13M3
Comment 21 Jeff Johnston CLA 2019-08-21 14:05:49 EDT
Verified for 4.13 M3 using build I20190821-0600
Comment 22 Noopur Gupta CLA 2019-09-03 04:14:39 EDT
So far, this fix has resulted in two bugs that need to be fixed for RC2.

Jeff, please revisit the fix for this bug and if it looks risky then we should revert it and add it back when the master branch opens for 4.14 so that we get more time before the release.
Comment 23 Jeff Johnston CLA 2019-09-03 10:29:29 EDT
(In reply to Noopur Gupta from comment #22)
> So far, this fix has resulted in two bugs that need to be fixed for RC2.
> 
> Jeff, please revisit the fix for this bug and if it looks risky then we
> should revert it and add it back when the master branch opens for 4.14 so
> that we get more time before the release.

I believe I have fixed the regressions but I agree that post-poning to 4.14 would ensure no more un-seen problems show up in the release as there is no time for users to test the latest version.  There are multiple bugs/patches, should the files be reset in one patch or should there be multiple reverts?  I am not sure what the process is here.
Comment 24 Alexander Kurtakov CLA 2019-09-03 11:11:19 EDT
Should everything be reverted or just one of the patches?
Comment 25 Jeff Johnston CLA 2019-09-03 11:30:38 EDT
(In reply to Alexander Kurtakov from comment #24)
> Should everything be reverted or just one of the patches?

One patch cannot be reverted.  There has been refactoring in-between.  I am going to restore the ConverForLoopOperation to a refactored version that does not support Collections and remove the tests.  After 4.14, I will fix the bug that just came in regarding multiple Collections referenced.

I will post a patch shortly after running tests and include Noopur as reviewer and set the appropriate flags.
Comment 26 Noopur Gupta CLA 2019-09-03 11:51:40 EDT
(In reply to Jeff Johnston from comment #25)
> I will post a patch shortly after running tests and include Noopur as
> reviewer and set the appropriate flags.
Sure. Also, please revert the corresponding N&N entry.
Comment 27 Jeff Johnston CLA 2019-09-03 14:09:53 EDT
(In reply to Noopur Gupta from comment #26)
> (In reply to Jeff Johnston from comment #25)
> > I will post a patch shortly after running tests and include Noopur as
> > reviewer and set the appropriate flags.
> Sure. Also, please revert the corresponding N&N entry.

Noopur, I also have a patch for the new foreach bug.  I have added you as reviewer.  If you would prefer that patch over the revert, let me know.  Otherwise, I will post the revert patch and remove the N&N.
Comment 28 Jeff Johnston CLA 2019-09-03 18:02:21 EDT
(In reply to Jeff Johnston from comment #27)
> (In reply to Noopur Gupta from comment #26)
> > (In reply to Jeff Johnston from comment #25)
> > > I will post a patch shortly after running tests and include Noopur as
> > > reviewer and set the appropriate flags.
> > Sure. Also, please revert the corresponding N&N entry.
> 
> Noopur, I also have a patch for the new foreach bug.  I have added you as
> reviewer.  If you would prefer that patch over the revert, let me know. 
> Otherwise, I will post the revert patch and remove the N&N.

Hi Noopur,

Just to update.

I have posted a request to the eclipse-pmc list to submit that patch to the blocking bug for RC2 and offered to revert if they wish.  If you are ok with the changes, I will change this bug back to verified.
Comment 29 Noopur Gupta CLA 2019-09-04 02:16:55 EDT
See 550672 comment #6.
Comment 30 Jeff Johnston CLA 2019-09-04 21:36:08 EDT
Verified for 4.13RC2 using I20190904-1805 build