开发者

How do you modify IDEA IntelliJ v10.x code generators for getters and setters?

开发者 https://www.devze.com 2023-04-09 19:51 出处:网络
IntelliJ generates the following getter/setter code for boolean fields: private boolean isTest; public boolean isTest() {

IntelliJ generates the following getter/setter code for boolean fields:

   private boolean isTest;

   public boolean isTest() {
      return isTest;
   }

   public void setTest(boolean test) {
      isTest = test;
   }

This too yields the same method signatures:

   private boolean test;

   public boolean isTest() {
      return test;
   }

   public void setTest(boolean test) {
      this.test = test;
   }

Great! So far so good. IntelliJ is following JavaBean naming conventions for boolean.

But watch what happens when you use the object Boolean (instead of the primitive boolean):

   private Boolean isTest;

   public Boolean getTest() {
      return isTest;
   }

   public void setTest(Boolean test) {
      isTest = test;
   }

Uh oh! Do you see it? It should be generating this instead (which Eclipse does):

   private Boolean isTest;

   public Boolean getIsTest() {
      return isTest;
   }

   public void setIsTest(Boolean isTest) {
      isTest = isTest;
   }

This may seem like no big deal, but this little inconsistency caused a huge project nightmare. The reason is this: There are other layers and frameworks which expect to map variables EXACTLY to the Java class field names - otherwise it fails without custom mapping logic (painful and unnecessary).

Our team uses the is*Name* pattern for all Boolean object开发者_如何学Pythons. Even our boolean database columns are named is_name, which gets translated to "is*Name*" using JBoss Hibernate reverse engineering tools plugin for Eclipse.

Does anyone know how to fix this? Is there some type of code generation template we can configure? Any help is greatly appreciated.


It was reported that Eclipse generates get<Property> for Boolean while IDEA generated is<Property>. It is against the specification and users requested to fix it.

As a result of addressing this bug current IDEA version is working in accordance to the JavaBeans specification and uses such getters only for primitive boolean type and get<Property> for other types including Boolean.

Sorry, but there is no way to configure this behavior in IDEA.

0

精彩评论

暂无评论...
验证码 换一张
取 消