如何修改 IDEA IntelliJ v10.x 代码生成器的 getter 和 setter?

发布于 2024-12-07 12:07:58 字数 1251 浏览 1 评论 0原文

IntelliJ 为布尔字段生成以下 getter/setter 代码:

   private boolean isTest;

   public boolean isTest() {
      return isTest;
   }

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

这也产生相同的方法签名:

   private boolean test;

   public boolean isTest() {
      return test;
   }

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

太棒了!到目前为止,一切都很好。 IntelliJ 遵循布尔值的 JavaBean 命名约定。

但是请注意当您使用 object 布尔值(而不是原始布尔值)时会发生什么:

   private Boolean isTest;

   public Boolean getTest() {
      return isTest;
   }

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

呃哦!你看到了吗?它应该生成这个(Eclipse 就是这样做的):

   private Boolean isTest;

   public Boolean getIsTest() {
      return isTest;
   }

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

这可能看起来没什么大不了的,但是这个小小的不一致导致了一个巨大项目噩梦。原因是这样的:还有其他层和框架期望将变量精确映射到 Java 类字段名称 - 否则如果没有自定义映射逻辑就会失败(痛苦且不必要)。

我们的团队对所有布尔对象使用 is*Name* 模式。甚至我们的布尔数据库列也被命名为 is_name,使用 Eclipse 的 JBoss Hibernate 逆向工程工具插件将其转换为“is*Name*”。

有谁知道如何解决这个问题?我们可以配置某种类型的代码生成模板吗?非常感谢任何帮助。

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 objects. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-12-14 12:07:58

据报道,Eclipse 为布尔值生成 get而IDEA生成的。它违反了规范,用户要求修复它。

作为解决此错误的结果,当前 IDEA 版本按照 JavaBeans 规范工作,并且仅对原始 boolean 类型使用此类 getter,对其他类型使用 get,包括布尔值

抱歉,无法在 IDEA 中配置此行为。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文