Bug 100211 - [source actions] Strange behavior in "Generate Constructor using Fields"
Summary: [source actions] Strange behavior in "Generate Constructor using Fields"
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 minor with 2 votes (vote)
Target Milestone: 3.1.1   Edit
Assignee: Tobias Widmer CLA Friend
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-06-15 11:26 EDT by Yuan WANG CLA Friend
Modified: 2005-09-02 08:39 EDT (History)
4 users (show)

See Also:


Attachments
path for naming convention and order of the parameters (4.00 KB, patch)
2005-08-08 12:07 EDT, Tobias Widmer CLA Friend
no flags Details | Diff
improved patch (4.55 KB, patch)
2005-08-09 06:30 EDT, Tobias Widmer CLA Friend
no flags Details | Diff
easier patch: reverted inline method (2.76 KB, patch)
2005-08-10 13:39 EDT, Markus Keller CLA Friend
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yuan WANG CLA Friend 2005-06-15 11:26:08 EDT
I find the behavior of "Generate Constructor using Fields" is strange in some
case. Suppose you have the following file:

class ID {
	private int publicPart;
	private int localPart;
}

Then when you call "Generate Constructor using Fields", it generates:

public ID(int part, int part2) {
	super();
	// TODO Auto-generated constructor stub
	publicPart = part;
	localPart = part2;
}

I don't known why the names of the two arguments are "part" and "part2", instead
of "publicPart" and "localPart".

The first line of the generated code, "super()", is also unnessary. So I think
it will be better if you delete this line if the class does not inherit from
other class (except Object).
Comment 1 Olivier Thomann CLA Friend 2005-06-15 11:35:44 EDT
Move to JDT/UI
Comment 2 Dirk Baeumer CLA Friend 2005-06-15 12:44:30 EDT
Tobias, can you please investigate why we take part and part2 here as names. We
should do better here.

Not for 3.1.
Comment 3 Tobias Widmer CLA Friend 2005-06-15 13:10:40 EDT
We take the naming conventions from JDT Core, but only the first proposal. We 
might consider also the other ones
Comment 4 Yuan WANG CLA Friend 2005-07-10 02:11:56 EDT
I have followed the execution of this command and found the suggested name is
given by function computeNames() in class InternalNamingConventions. For
sourceName 'publicPart', this function will return 'part'. It seems that
computeNames() thinks that 'public' is just a prefix and throws it away. If I
make computeNames() return the sourceName first. I will get:

public ID(int globalPart, int localPart) {
	super();
	// TODO Auto-generated constructor stub
	this.publicPart = globalPart;
	this.localPart = localPart;
}

I think it's better than 'part' and 'part2'. But I'm not familiar with the
internal of eclipse, so I'm not sure whether my fix is right.
Comment 5 Adrian Stabiszewski CLA Friend 2005-07-26 04:42:49 EDT
This command has a second strange behaviour since it reorganizes the fields
alphabetically.

private String name;
private String value;
private int customerId;
private int addressId;
private int userId;

becomes:

    /**
     * @param id
     * @param id2
     * @param name
     * @param id3
     * @param value
     */
    public Customer(int id, int id2, String name, int id3, String value)
    {
        addressId = id;
        customerId = id2;
        this.name = name;
        userId = id3;
        this.value = value;
    }

This is quite awfull ;)

In 3.0.2 this worked like this:

/**
  * @param name
  * @param value
  * @param customerId
  * @param addressId
  * @param userId
  */
 public Customer(String name, String value, int customerId, int addressId, int
userId)
 {
  this.name = name;
  this.value = value;
  this.customerId = customerId;
  this.addressId = addressId;
  this.userId = userId;
 }
Comment 6 Markus Keller CLA Friend 2005-07-26 04:56:51 EDT
Tobias: please adopt the convention of taking the longest proposal from
JDT/core. See StubUtility.getArgumentNameSuggestions(..) and
.suggestArgumentName(..).

I agree that parameters must be created in the order of the field declarations.
Comment 7 Dirk Baeumer CLA Friend 2005-07-28 08:10:41 EDT
Tobias, this is a regression to 3.0.1. Please investigate a fix for 3.1.1
Comment 8 Andrea Aime CLA Friend 2005-08-04 04:05:14 EDT
This is very annoying, creating DTO objects has just become more complex... what
I need is
1) exactly the same order as the declared fields
2) exactly the same name, that is this.myFieldWithAVeryLongName =
myFieldWithAVeryLongName

At least, as an option...
Comment 9 Andrea Aime CLA Friend 2005-08-04 04:06:17 EDT
This is very annoying, creating DTO objects has just become more complex... what
I need is
1) exactly the same order as the declared fields
2) exactly the same name, that is this.myFieldWithAVeryLongName =
myFieldWithAVeryLongName

At least, as an option...
Comment 10 Tobias Widmer CLA Friend 2005-08-08 11:13:48 EDT
Regarding comment 1: The call to super can be configured in the Generate 
Constructors dialog
Comment 11 Tobias Widmer CLA Friend 2005-08-08 12:07:42 EDT
Created attachment 25847 [details]
path for naming convention and order of the parameters

Markus, Martin, can you please cast your vote for 3.1.1?
Comment 12 Geoff Weatherall CLA Friend 2005-08-09 01:08:28 EDT
This would be good one to fix; it is really annoying as you have to refactor the
contructor parameter names, and if you get that wrong you end up with silly bugs
in your code.  Voted for it.
Comment 13 Martin Aeschlimann CLA Friend 2005-08-09 06:15:18 EDT
The patch should use NamingConventions.removePrefixAndSuffixForFieldName on the
field name before passing it to StubUtility.getArgumentNameSuggestions.
Comment 14 Tobias Widmer CLA Friend 2005-08-09 06:30:30 EDT
Created attachment 25887 [details]
improved patch
Comment 15 Martin Aeschlimann CLA Friend 2005-08-09 06:45:43 EDT
Improved patch is ok. +1 for 3.1.1
Comment 16 Tobias Widmer CLA Friend 2005-08-10 08:49:42 EDT
Markus, can you please vote on this one?
Comment 17 Andrea Aime CLA Friend 2005-08-10 08:53:56 EDT
Sorry to intrude, but asking is far easier than trying out the patch: does the
patch solve both of the points I have issued? Same name, same order?
Comment 18 Tobias Widmer CLA Friend 2005-08-10 09:03:58 EDT
Yes, it fixes both issues
Comment 19 Markus Keller CLA Friend 2005-08-10 13:39:43 EDT
Created attachment 25988 [details]
easier patch: reverted inline method

Tobias, the improved patch makes a lot of changes in StubUtility2 that are IMO
unnecessary. I refactored your patch to look more similar to the original code,
and the only change left was replacing NamingConventions by StubUtility in
getParameterName(..).

I'd vote for my minimal-change patch.
Comment 20 Martin Aeschlimann CLA Friend 2005-08-11 03:50:05 EDT
+1 for the minimal patch
Comment 21 Tobias Widmer CLA Friend 2005-08-11 04:35:11 EDT
Fixed in 3.1.1 maintenance stream > 20050811
Comment 22 Tobias Widmer CLA Friend 2005-08-15 04:40:10 EDT
Fixed in HEAD > 20050815
Comment 23 Tom Hofmann CLA Friend 2005-09-02 08:10:51 EDT
verifying...
Comment 24 Tom Hofmann CLA Friend 2005-09-02 08:39:21 EDT
verified that everything works:

- correct ordering
- respect pre-/postfixes
- chooses the same name as the field if possible after pre/postfix processing