Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 318681 - [quick assist] create fields from multiple parameters
Summary: [quick assist] create fields from multiple parameters
Status: RESOLVED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.7   Edit
Hardware: All All
: P3 enhancement with 1 vote (vote)
Target Milestone: 4.6 M7   Edit
Assignee: Noopur Gupta CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 123421 478011 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-07-02 05:35 EDT by Lars Svensson CLA
Modified: 2016-03-31 00:51 EDT (History)
10 users (show)

See Also:
noopur_gupta: review-


Attachments
Unit Test + Patch (22.25 KB, patch)
2014-05-26 10:36 EDT, sandra lions CLA
no flags Details | Diff
Unit Test + Patch version 2 (27.54 KB, patch)
2014-05-28 04:54 EDT, sandra lions CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Lars Svensson CLA 2010-07-02 05:35:50 EDT
Build Identifier: 

There is already a function Source->Create constructor using fields. I would like just the opposite.

When I program I call constructors that do not exists:

MyClass m = new MyClass("first", "second", "third");

I then use the light bulb to create this new constructor. Inside the constructor I change the names of the parameters and begin the hard work I would like some help with:

public MyClass(String firstValue, String secondValue, String thirdValue){

// These lines could be generated automatically
// Prefix 'i' is already there under "Code Templates"

iFirstValue = firstValue;
iSecondValue = secondValue;
iThirdValue = thirdValue;


}


// Also, the fields would have to be created:

private String iFirstValue;
private String iSecondValue;
private String iThirdValue;


Reproducible: Always
Comment 1 Markus Keller CLA 2010-07-05 12:52:56 EDT
We already have a quick assist "Assign parameter to new field" for single parameters (put caret into parameter name and press Ctrl+1).

I'll keep this bug to turn this into a multi-fix (such that Ctrl+Enter assigns all parameters to new fields).
Comment 2 Lars Svensson CLA 2010-07-06 03:12:30 EDT
Excellent this with Ctrl-1. I just never got the idea of looking under "Quick Fix" for such a function. But if Quick fix also had the option "Assign ALL parameters to fields" we would be done her..
Comment 3 Deepak Azad CLA 2010-11-12 10:57:22 EST
*** Bug 123421 has been marked as a duplicate of this bug. ***
Comment 4 Frank Neblung CLA 2012-11-20 02:22:01 EST
It would be great to see this feature.
In general I want a constructor to just assign the given params to fields.

Repeatedly doing the CTRL+1 trick works, but is still cumbersome.
Comment 5 sandra lions CLA 2014-05-20 10:02:33 EDT
You can assign this issue to me.
Comment 6 sandra lions CLA 2014-05-26 10:36:57 EDT
Created attachment 243493 [details]
Unit Test + Patch

As opposed to the single "Assign parameter to new field" quick fix, I do not add any linked proposals. 

This contribution complies with http://www.eclipse.org/legal/CoO.php
Comment 7 Markus Keller CLA 2014-05-26 11:19:22 EDT
(In reply to sandra lions from comment #6)
AssignToVariableAssistProposal#doAddAllFields() should not duplicate so much code. Please share the bulk of the implementation with the old doAddField() and just make the latter a special case that only assigns a single field.

For the 'all' quick fix, the linked proposals are also important. To keep the proposals for the different fields apart, you have to use different groupIDs, e.g. KEY_NAME + parameterIndex.

We don't use assert statements. The first one in doAddAllFields is already ruled out by the type check before.

The loop over methodDecl.parameters() should not use toArray(). It should use
	List<SingleVariableDeclaration> parameters= methodDecl.parameters();
and then directly loop over the list items with get(int).
You may wonder why the assignment compiles without a warning. That's because the "Ignore unavoidable generic type problems due to raw APIs" option is enabled in the project's preferences.

Local variables should not be marked final unless necessary.
Comment 8 sandra lions CLA 2014-05-28 04:54:33 EDT
Created attachment 243612 [details]
Unit Test + Patch version 2

Take into account Markus Keller comments (comment 7).
Comment 9 Noopur Gupta CLA 2015-09-22 05:24:59 EDT
*** Bug 478011 has been marked as a duplicate of this bug. ***
Comment 10 Eike Stepper CLA 2015-10-15 02:31:44 EDT
Would this new quick fix work on normal *method* parameters, too? Like the old single parameter quick fix...
Comment 11 Noopur Gupta CLA 2015-10-15 08:02:35 EDT
(In reply to Eike Stepper from comment #10)
> Would this new quick fix work on normal *method* parameters, too? Like the
> old single parameter quick fix...

Yes.
Comment 12 Lars Vogel CLA 2015-10-15 09:29:57 EDT
Would be nice to have more N&N entries for JDT M3. I set the M3 target for this bug, please adjust if necessary.
Comment 13 Patrik Suzzi CLA 2015-10-30 20:01:17 EDT
As stated in Bug 478011, it would be nice to have this additional quickfix. 

There are some things I don't understand: 
- version 3.7 , but current org.eclipse.jdt.ui is v.3.12 --> update
- status assigned, but no assignee --> assign this or fix status
- is the attachment still actual ? --> gerrit push, review, git commit
Comment 14 Stephan Herrmann CLA 2015-10-30 20:17:29 EDT
(In reply to Patrik Suzzi from comment #13)
> There are some things I don't understand: 
> - version 3.7 , but current org.eclipse.jdt.ui is v.3.12 --> update

This one is simply explained: the bug was raised against 3.7, this just records the history, when this was first detected / requested.

Planning happens via the "Target Milestone", currently set to 4.6 M4.

More can be seen by clicking "History" in the bug header.
Comment 15 Noopur Gupta CLA 2016-03-29 10:13:38 EDT
(In reply to sandra lions from comment #8)
> Created attachment 243612 [details] [diff]
> Unit Test + Patch version 2

The patch works but has issues like:
- Command not added to plugin.xml
- Missing checks in QuickAssistProcessor#getAssignAllParamsToFieldsProposals
- Linked mode should end after the last statement generated in the method, not the first
- Assigning method to fVariableKind and fNodeToAssign is logically incorrect and needs a rewrite of the approach used in AssignToVariableAssistProposal

I will upload a new patch for this.
Comment 16 Eclipse Genie CLA 2016-03-30 03:07:16 EDT
New Gerrit change created: https://git.eclipse.org/r/69497