IntelliJ:为名称中带下划线的字段生成getter / setter [英] IntelliJ: Generating getters/setters for fields with underscores in the name

查看:1592
本文介绍了IntelliJ:为名称中带下划线的字段生成getter / setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让IntelliJ更聪明地生成它的getter / setter?特别是,如果一个字段名称中有下划线,那么getter是否可以将它们剥离并转换为CamelCase?

Is there a way to get IntelliJ to be smarter about the getters/setters it generates? In particular, if a field name has underscores in it, can the getter strip them out and convert to CamelCase?

例如,我希望下面的getter是getCarneAsada ():

For instance, I would expect the below getter to be getCarneAsada():

public class Taco {
  private String carne_asada;

  public String getCarne_asada() {
    return carne_asada;
  }
}

我找到代码样式 - > Java->代码生成区域,但没有一个选项看起来合适......它不是我要排除的前缀或后缀。它是内部下划线。

I found the Code Style->Java->Code Generation area, but none of the options seem appropriate...it's not a prefix or a suffix I want to exclude. It's inner underscores.

推荐答案

您可以使用实时模板来完成此任务。它不能从代码生成中访问,但它仍然很容易使用。

You can use Live Templates to accomplish this. It will not be accessible from Code Generation but it will still be quite easy to use.

我创建了一个Main类,其属性为 carne_asada

I have created a Main class with the property carne_asada:

现在在设置中打开实时模板并创建一个名为的新模板getu 其他下:

Now open up Live Templates in the settings and create a new template called getu under other:

按下名为的小链接定义在底部定义此模板何时有效。然后选择 Java |声明

Press the little link called Define at the bottom to define when this template is valid. Then choose Java | Declaration:

按下按钮编辑变量打开一个窗口,其中每个实时模板变量都可以定义。现在创建以下变量:

Press the button Edit Variables to open a window where each of the live template variables can be defined. Now create the following variables:

VAR         : suggestFirstVariableName("Object")
TYPE        : typeOfVariable(VAR)
CAP_CAM_VAR : capitalize(underscoresToCamelCase(VAR))

订单非常重要所以 VAR 必须先到。在我的示例中, CAP_CAM_VAR 代表Capitalize和CamelCase VAR 变量。根据图像设置跳过定义

The order is quite important so the VAR must come first. In my example CAP_CAM_VAR stands for Capitalize and CamelCase the VAR variable. Set the Skip if defined according to the image:

现在按确定并再次确定返回编辑器。

Now press OK and OK again to get back to the editor.

尝试新的 getu 输入 getu 的实时模板,然后按 Tab

Try the new getu live template by typing getu and then press Tab:

< img src =https://i.stack.imgur.com/O7Ncs.pngalt =在此处输入图像说明>

Enter 和getter生成已完成:

Press Enter and the getter generation has finished:

现在,如果您有更多带下划线的变量,您将获得一个可供选择的列表,所以这里有一个例子:

Now if you had had some more variables with underscores you will get a list to choose from so here is an example:

结果很漂亮:

您可以轻松创建相同的 setu 实时模板也许还有一些 sgetu 可以同时创建它们。

You can easily create the same setu live template and maybe some sgetu that creates them both at the same time.

希望这有点帮助!

这篇关于IntelliJ:为名称中带下划线的字段生成getter / setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆