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

Bug 381982

Summary: [encapsulate field] Encapsulate Field doesn't use code style settings for setter parameter name
Product: [Eclipse Project] JDT Reporter: Chris S <devnullaccount>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P3 CC: markus.kell.r
Version: 3.7.2   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
example project
none
Template file that exhibits the problem none

Description Chris S CLA 2012-06-07 08:09:26 EDT
Build Identifier: 

(I was directed to create a bug report from here: http://www.eclipse.org/forums/index.php/t/357345/)

When working on a Java project with eclipse 3.7.2 where we have custom code templates for getters and setters. So when I do "Source->Generate getters and Setters..", the new methods are according to our templates.

But if have a field that I change from protected to private and it is accessed from another class, I can do a quick-fix for that where I can select "create getter and setter for X" (not exact text, but something like that). That works, but it does not use the code template, so then I have to manually change all the generated methods which is an unnecessary pain.

I searched for similar bug reports and it is close to Bug41542, but I don't think it is identical as that one talks about how the quick-fix should work, but this is only about making it use the existing code templates. Please merge them if my assessment is incorrect.

Reproducible: Always

Steps to Reproduce:
1. Make a custom template for getters/setters in your java project
2. Create a class with a public field
3. Make another class that accesses this field
4. Change the field in the first class to private (an error will show up in the 2nd class)
5. Do quick fix on the error and choose to generate getter/setter
6. Check that the generated getter/setter does not use your custom template
Comment 1 Markus Keller CLA 2012-06-13 08:53:05 EDT
Created attachment 217271 [details]
example project

Works fine for me in the attached project.

Note that method comments are only generated if you either
- check "Generate method comments" in the "Encapsulate Field" dialog, or
- check "Automatically add comments for new methods and types" in the "Code Templates" properties page
Comment 2 Markus Keller CLA 2012-06-13 08:54:22 EDT
.
Comment 3 Chris S CLA 2012-06-14 13:35:48 EDT
Created attachment 217374 [details]
Template file that exhibits the problem

I tried to diff my org.eclipse.jdt.ui.prefs with the one you attached and I can't see why mine would misbehave, but I cn clearly repeat the problem.

For example, we have a policy that a method argument should have the naming convention "aSomething" ("a" as a prefix) and this works when I generate getters and setters "normally":

    /**
     * TODO: Write something here
     * @param aMyField the myField to set
     */
    public void setMyField(String aMyField) {
        myField = aMyField;
    }

But when I do the quick fix, the setter looks like this instead

    /**
     * TODO: Write something here
     * @param myField the myField to set
     */
    public void setMyField(String myField) {
        this.myField = myField;
    }

So yes, it generates the javadoc correctly, but it does not follow the naming convention.
I should have been more clear in my previous example. Try repeating it with the attached file instead
Comment 4 Markus Keller CLA 2012-06-15 13:47:46 EDT
OK, so the problem is that the Encapsulate Field refactoring doesn't use the Java Code Style settings to generate the setter parameter name.

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