6

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?

KatieK
  • 13,586
  • 17
  • 76
  • 90
nano_nano
  • 12,351
  • 8
  • 55
  • 83
  • Why have all your fields 'm' as a prefix? – MrSmith42 Jan 03 '13 at 17:02
  • m for member field in class. p for parameter. l for local variable. – nano_nano Jan 03 '13 at 17:04
  • "I start each class member name with the suffix 'm' for member". An awful idea, IMO. If you are using Eclipse, it already distinguish visually local variables from fields. BTW, it should be "prefix". – leonbloy Jan 03 '13 at 17:05
  • I know hot to change the generated method body, but not of a way to change the generated method signature. Anyhow I can advise you to only expose getters and setters where necessary and most of the time the generated ones aren't good enough. Lots of setters need sanity checks and getters should return unmodifiable copies of the data if the data shouldn't be changed elsewhere. – jlordo Jan 03 '13 at 17:06
  • eclipse distinguish between local and member but what is with another developers who read the code? Maybe a domain object is a bad example. But it could be useful to have that prefixes in evaluator or handler classes with much more lines of code inside a method... – nano_nano Jan 03 '13 at 17:17
  • @MrSmith42: [Code Style Guidelines](http://source.android.com/source/code-style.html#follow-field-naming-conventions) state you should use these prefixes when contributing to the Android project. It is only logical to use Android's general style for your own projects too. – kraxor Nov 02 '13 at 07:11
  • @leonbloy well this is Java coding convention. This convention saves time by helping the field being accessible without using `this.`. – Sufian Apr 14 '14 at 13:37

3 Answers3

24

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?

Sufian
  • 6,405
  • 16
  • 66
  • 120
SylvainL
  • 3,926
  • 3
  • 20
  • 24
3

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.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
1

The names of the getter and setter methods are derived from the field name. If you use a prefix or suffix for fields (e.g. 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

sword-yang
  • 71
  • 5