道家人 发表于 2023-7-17 13:39:40

用了这款 IDEA 神器,领导都夸我代码写得好!

CheckStyle作为检验代码规范的插件,除了可以使用配置默认给定的开发规范,如Sun的,Google的开发规范啊,也可以导入像阿里的开发规范的插件。
事实上,每一个公司都存在不同的开发规范要求,所以大部分公司会给定自己的check规范,一般导入给定的 checkstyle.xml 文件即可实现。
作为新人,我checkstyle了一整个项目,苦不堪言,所以大家怕是还是多注意代码规范啊,利人利己。
楼主用的是idea,所以先说一下插件的导入,然后再细说怎么修改不规范的代码。
导入CheckStyle插件

1. File ——> setting 安装插件

https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141734291-1883863321.png
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141734893-206912630.png
也可以下载阿里的开发规范,看个人需求和喜好。
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141735234-816454138.png
2. 配置插件

https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141735542-271826767.png
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141735942-1348425009.png
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141736331-573285498.png
3. 开始C

选择你需要checkstyle的类右击:
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141736783-1866414531.png
点击checkstyle,因为我导入的是阿里巴巴,使用checkstyle的按键被占用了,懒得弄了,阿里蛮好的
或者:
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141737274-1907237603.png
报错原因和处理方式

原则:少改动代码,处理时候不盲目大意,git提交多写点改动的情况。
1. 第一大类:注解问题

缺少注解,点击问题即可跳转到缺少注解的地方
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141737599-1004798145.png
注解不全,补齐就行
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141737874-705658225.png
补齐,每一个@的值都要完整
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141738194-1345771179.png
2.字符占位问题

https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141738490-1945571456.png
先按自己的需求设置code style
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141738925-247773457.png
然后右击选择
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141739234-398926874.png
即可解决
tab问题的话
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141739484-2061653400.png
默认情况下的是tab=4个空格,不存在tab的情况的,其实我也不会,欢迎补充。
见名知意

https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141739738-1118430796.png
按要求改就ok了
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141740023-456295187.png
删掉就行
格式问题

名字问题
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141740303-1458778032.png图片
建议少改动,如果是公共类,影响较大,还是提前多留意吧。
逻辑错误

https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141740592-85405691.png
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141740949-1355606315.png
导包顺序

https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141741255-2118261906.png
类似这种情况的,都是导包顺序没有按照checkstyle的规范,一般情况下,eclispe的ctrl + shift + o 快捷键可以解决这个盗版问题,idea的也一样,当然也可以右击选择
https://img2023.cnblogs.com/other/1218593/202307/1218593-20230717141741588-871849198.png
版权声明:本文为CSDN博主「iXinRu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/iXinRu/article/details/85242553
近期热文推荐:
1.1,000+ 道 Java面试题及答案整理(2022最新版)
2.劲爆!Java 协程要来了。。。
3.Spring Boot 2.x 教程,太全了!
4.别再写满屏的爆爆爆炸类了,试试装饰器模式,这才是优雅的方式!!
5.《Java开发手册(嵩山版)》最新发布,速速下载!
觉得不错,别忘了随手点赞+转发哦!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: 用了这款 IDEA 神器,领导都夸我代码写得好!