Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse generate getter/setter for domain objects and classmembers with 'm' suffix

I have a small question regarding generated getter and setter methods in my domain objects. I want to use a common style guide for my source code. One part of that style guide says that I start each class member name with the prefix 'm' for member.

class User{
String mName;
List<Call> mAllCall;
List<Geo> mAllGeo;

Unfortunately I have a couple of classes with many more member variables. The problem I have is that I am a very lazy developer, and that I create the getter and setter methods in Eclipse with

"Source"->"Generate Getters and Setters".

The result is

public String getmName() {
    return mName;
}
public void setmName(String mName) {
    this.mName = mName;
}
public List<Call> getmAllCall() {
    return mAllCall;
}
public void setmAllCall(List<Call> mAllCall) {
    this.mAllCall = mAllCall;
}
public List<Geo> getAllGeo() {
    return mAllGeo;
}
public void setmAllGeo(List<Geo> mAllGeo) {
    this.mAllGeo = mAllGeo;
}

That is not the result I want. I need this:

public String getName() {
    return mName;
}
public void setName(String pName) {
    this.mName = pName;
}
public List<Call> getAllCall() {
    return mAllCall;
}
public void setAllCall(List<Call> pAllCall) {
    this.mAllCall = pAllCall;
}
public List<Geo> getAllGeo() {
    return mAllGeo;
}
public void setmAllGeo(List<Geo> pAllGeo) {
    this.mAllGeo = mAllGeo;
}

I currently remove and replace the prefix in the method names by hand. Is there an easier way to do this?

like image 263
s_bei Avatar asked Jan 03 '13 17:01

s_bei


People also ask

What is the difference between getter and setter blocks?

Getter blocks are expressions that get the current value of the property. Setter blocks are commands that change the value associated with the property.

How do you get getters and setters?

To achieve this, you must: declare class variables/attributes as private. provide public get and set methods to access and update the value of a private variable.


1 Answers

For the prefix m, you add the letter m to your list of prefixes in the Java Code Style.

Follow these steps:

  1. open Preferences,
  2. in left panel, expand Java,
  3. expand Code Style,
  4. right panel is where you should now be looking at

You will see a list with Fields, Static Fields, etc. This is what you need to modify.

Set m against Fields.

Set p against the Parameter.

As the name of the field will now be different from the name of the argument, the this. qualification will no longer be added automatically. However, you can check the option Qualify all generated field accesses with 'this.' to have it again.

I suppose that you know the difference between Enable project specific settings and Configure Workspace Settings... in the upper left and right of the window?

like image 126
SylvainL Avatar answered Oct 20 '22 00:10

SylvainL