How to prevent fully qualified names in Java code

老子叫甜甜 提交于 2019-12-12 10:46:35

问题


Is it possible to check with checkstyle if a java project is using fully qualified names in the code. We want to prevent code like

if (org.apache.commons.lang3.StringUtils.isBlank(name)) { 
 ....
 ....
} 

and want to enforce that packages are instead imported instead.

Are there are other tools that can help us accomplish it?


回答1:


As far as I know, Checkstyle cannot do this. However, there is a PMD rule called UnnecessaryFullyQualifiedName which may be worth a look. IntelliJ plugins for PMD exist, for example QAPlug PMD, which is free to download.

Be prepared to see a lot of false positives though. For example, two classes which share the same simple name cannot be referenced without a fully qualified class name (e.g. foo.A and bar.A). Might also be that PMD actually checks for this case, it may be worth a try.




回答2:


FTR I've just raised https://github.com/sevntu-checkstyle/sevntu.checkstyle/issues/462 about potentially adding this to Checkstyle some time .. contributions welcome! ;-)



来源:https://stackoverflow.com/questions/34736158/how-to-prevent-fully-qualified-names-in-java-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!