Getter template modification--automatically handle null judgment, format code automatically arrange method location



Getter Template Modification Tutorial


Just create a class, write a few properties, then press the shortcut key  Alt + Insert  or right-click in the code area  -> Generate -> Getter , then there will be a pop-up box:


Do not click the OK button at this time, first click  the icon of ... in the upper right corner to modify the template file:



At this time, there is only one AS default template for generating Getter methods, and this template needs to be modified, so you can choose to create a new template file or modify the original file. For example, I created a NotNull_getter template. document:



The only thing that needs to be modified in the template file is the return generation rule in the $(name){…}  code block. The original rule is to return the field value itself. According to the specification, two new rules are added: increase the generation of String type and List type. rule. The following is the modified code of the entire template file, which can be copied and used directly:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
 #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
   #set($name = $StringUtil.decapitalize($name))
 #else
   is##
#end
#else
 get##
#end
${name}() {
 #if ($field.string)
    return $field.name == null ? "" : $field.name;
 #else
   #if ($field.list)
   if ($field.name == null) {
       return new ArrayList<>();
   }
   return $field.name;
   #else
   return $field.name;
   #end
 #end
}

Getter Template Modification Tutorial


Just create a class, write a few properties, then press the shortcut key  Alt + Insert  or right-click in the code area  -> Generate -> Getter , then there will be a pop-up box:


Do not click the OK button at this time, first click  the icon of ... in the upper right corner to modify the template file:


At this time, there is only one AS default template for generating Getter methods, and this template needs to be modified, so you can choose to create a new template file or modify the original file. For example, I created a NotNull_getter template. document:


The only thing that needs to be modified in the template file is the return generation rule in the $(name){…}  code block. The original rule is to return the field value itself. According to the specification, two new rules are added: increase the generation of String type and List type. rule. The following is the modified code of the entire template file, which can be copied and used directly:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
 #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
   #set($name = $StringUtil.decapitalize($name))
 #else
   is##
#end
#else
 get##
#end
${name}() {
 #if ($field.string)
    return $field.name == null ? "" : $field.name;
 #else
   #if ($field.list)
   if ($field.name == null) {
       return new ArrayList<>();
   }
   return $field.name;
   #else
   return $field.name;
   #end
 #end
}

Formatting code auto-collation method location tutorial


First turn on the function of automatically arranging method positions, location:
File -> Settings -> Editor -> Code Style -> Java -> Arrangement



As shown in the figure above, the functions of 2 and 3 points are disabled by default.


第 2 点功能:Keep overridden methods together – keep order


It means that the methods marked by override are placed in a centralized manner. It is recommended to enable this function. The override methods are usually either some callback methods of the system or some interfaces defined by ourselves. This part of the method itself has a certain relevance. It's reasonable to be together.


注意,AS支持两种规则,一种是 keep order(按原有顺序),一种是 order by name(按照方法字母表顺序)。至于哪一种较合适,个人喜好,我是选择的 keep order。


第3点功能:Keep dependent methods together


意思是将相关的方法按照某则规则放在一起,AS支持两种规则:

breadth-first order & depth-first order

我的理解,说得通俗点也就是广度优先和深度优先,这个功能建议开启。


举个例子,假设一个类里有这么几个方法,a 调用了 b, c, d, 而 b 调用了 e, f,如果是按照广度优先来整理这些方法的位置时,breadth-first order:


广度优先


广度优先整理后的方法顺序:a, b, c, d, e, f。也就是说,方法 a 里面调用了三个方法,那么优先将这三个方法摆放在方法 a 下方,此时并不去考虑这三个方法里是否还调用了其他方法。等过完方法 a,那么以同样的道理再去整理方法 b 中调用的方法的位置。


但如果是深度优先,那么整理后的方法顺序就不同了,如下,depth-first order:


深度优先


深度优先整理后的方法顺序:a, b, e, f, c, d。也就是说,方法 a 内调用了三个方法,第一个方法是 b, 然后方法 b 又调用了 e, f。所以方法 b 紧接着 a 方法下面摆放,方法 e, f 紧接着方法 b 下面摆放,直到 e, f 里都没有其他方法了。然后再重新回到方法 a 内继续往下过方法 c 的位置,以此类推。


两种规则有各自的好处,广度优先侧重于优先梳理每个方法的大体工作;而深度优先则侧重于梳理每个方法的实现细节,流程步骤;


目前我是选择广度优先,因为我更侧重于关注每个方法大体的工作,对于一个不熟悉的方法,大概过一下它里面的每个方法大体上做了什么,就能大概理解这个方法的大体工作。


以上仅仅只是开启功能而已,而要借助 Android Studio 来自动整理方法位置,就是通过 AS 的格式化代码功能,快捷键也就是 Ctrl + Alt + L 。但这个格式化操作默认是没有启动对方法进行整理的操作的,每次按完快捷键后会有如下提示:


重点在底部那行灰色的字体,通过快捷键 Ctrl + Alt + Shift + L 可以打开配置 dialog:



Rearrange code 默认是没有勾选的,所以想要启用整理方法的功能,需要将这个勾选上,以后在通过 Ctrl + Alt + L 来格式化代码时,AS 就会根据我们在第一个步骤中设定的规则来自动整理方法的位置。



Getter模板修改教程


随便建个类,写几个属性,然后按快捷键 Alt + Insert 或在代码区域 右键 -> Generate -> Getter,然后会有一个弹框:


此时先不要点击 OK 键,先点击右上角的 … 的图标,来修改模板文件:


此时只有一份 AS 默认的生成 Getter 方法的模板,要对这份模板进行修改,所以接下去可以选择新建一份新的模板文件或者在原文件上修改都可以,比如我新建了一份 NotNull_getter 模板文件:


模板文件需要修改的地方就仅仅是在$(name){…} 代码块里的 return 生成规则,原本规则是统一返回字段值本身,根据规范新增两条规则:增加 String 类型和 List 类型的生成规则。以下是修改后的整个模板文件代码,可以拷贝过去直接使用:

#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
#if ($field.boolean && $field.primitive)
 #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
   #set($name = $StringUtil.decapitalize($name))
 #else
   is##
#end
#else
 get##
#end
${name}() {
 #if ($field.string)
    return $field.name == null ? "" : $field.name;
 #else
   #if ($field.list)
   if ($field.name == null) {
       return new ArrayList<>();
   }
   return $field.name;
   #else
   return $field.name;
   #end
 #end
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325717550&siteId=291194637