apache/thrift — pull request #161 by ruijiang (2014-07-21 16:12:22+00:00)
Title: Always use bit field or BitSet for isSetXXX

This is to address the issue: https://issues.apache.org/jira/browse/THRIFT-1827
Due to the the inconsistent behavior of isSet for primitive field and nullable field (e.g.., enum), enums with defaults will always have isSetAnyEnumField() as true when the class is initialized. Example:

enum Gender {
UNSPECIFIED = 0,
FEMALE = 1,
MALE = 2,
}

struct User {
1: optional Gender gender = Gender.UNSPECIFIED,
2: optional i32 age = 0;
}

With this change, the generated code will look like this:

public User setGender(Gender gender) {
if (gender == null) {
setGenderIsSet(false);
return this;
}
this.gender = gender;
setGenderIsSet(true);
return this;
}

public void unsetGender() {
this.gender = null;
__isset_bitfield = EncodingUtils.clearBit(__isset_bitfield, __GENDER_ISSET_ID);
}

/** Returns true if field gender is set (has been assigned a value) and false otherwise */
public boolean isSetGender() {
return EncodingUtils.testBit(__isset_bitfield, __GENDER_ISSET_ID);
}

public void setGenderIsSet(boolean value) {
if (!value) {
this.gender = null;
}
__isset_bitfield = EncodingUtils.setBit(__isset_bitfield, __GENDER_ISSET_ID, value);
}

I haven't tested this but just want to see if this the desired direction. If so I will do more testing and make sure it works.