简体   繁体   中英

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?

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?

I don't like the idea at all, but..

You can write the members without the prefix m , let Eclipse create the getters and setters, an afterwards rename the members (Shift-Alt-R); Eclipse will change the references, but not (unless you explicitly tell it) the getters/setters signature.

The names of the getter and setter methods are derived from the field name. If you use a prefix or suffix for fields (eg fValue, _value, val_m), you can specify the suffixes and prefixes in the Code Style preference page (Windows > Preferences > Java > Code Style).

reference at here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM