Checkstyle规则以防止调用一些方法和构造函数 [英] Checkstyle rule to prevent invocation of some methods and constructors

查看:161
本文介绍了Checkstyle规则以防止调用一些方法和构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用Checkstyle来禁止使用一些使用系统相关的默认值(locale,charset等)的构造函数或方法。我喜欢强制执行一个策略,程序员应该明确关于系统相关的值。所以我认为以下项目是危险的:


  • java.io.FielWriter的所有构造函数

    • 使用系统依赖编码


  • OutputStreamWriter(OutputStream os) 的构造函数

    • 使用系统依赖编码


  • java.lang.String.toLowerCase()方法

    • 使用系统默认语言环境


  • .util.Calendar.getInstance()方法

    • 使用系统默认语言环境和默认时区

    / li>


(列表继续,你得到图片)。



可以使用Checkstyle 5.5强制执行此操作?

解决方案

默认情况下不能执行此操作。但是,你可以实现自己的checker来检查这些方法。



第一个选项是使用Miscellaneous-> Regexp。这显然只有当你可以找到违规的regexp。您将需要设置illegalPattern = true。这是一个很好的开始,我想。



第二个选项是创建自己的检查。请参阅书写支票



有写检查的限制。第一个也是最重要的是你看不到其他文件。没有任何交叉检查。从网站:



  1. 您无法确定表达式的类型。

  2. 您看不到其他文件的内容。 (虽然您可以保存已处理的文件以供以后使用)

这意味着您无法实现一些代码检查
在IntelliJ IDEA等高级IDE中可用。对于
示例,您将无法实现检查,发现冗余的
类型转换或未使用的公共方法。


所以你不能检查实例,java是调用一个方法有一个替代的一个Locale。您可以使用不允许调用的方法的黑名单。因此,例如对新FileWriter()的调用将检查传递的参数的数量等。


Is it possible to use Checkstyle to forbid usage of some constructors or method that use system-dependent defaults (locale, charset, etc..). I prefer to enforce a policy where the programmer should be explicit about system-dependent values. So I consider the following items to be dangerous:

  • all the constructors of java.io.FielWriter
    • using system-dependent encoding
  • the OutputStreamWriter(OutputStream os) constructor of java.io.OutputStreamWriter
    • using system-dependent encoding
  • the java.lang.String.toLowerCase() method
    • using system default locale
  • The java.util.Calendar.getInstance() method
    • using system default locale and default timezone

(the list goes on, you get the picture).

Is it possible to enforce this using Checkstyle 5.5?

解决方案

You can't do this by default. However, you can implement your own checker which checks for these methods.

The first option is to use Miscellaneous->Regexp. This is obviously only possible if you can find violations with a regexp. You will need to set illegalPattern = true. This would be a good place to start, I think.

The second options is to create your own check. See Writing Checks.

There are limitations to writing checkers. The first and most important is that you can't see other files. There isn't any cross checking. From the site:

  1. You cannot determine the type of an expression.
  2. You cannot see the content of other files. (although you can save processed files for use later)

This means that you cannot implement some of the code inspection features that are available in advanced IDEs like IntelliJ IDEA. For example you will not be able to implement a Check that finds redundant type casts or unused public methods.

So you couldn't check for instance that the java is calling one method which has an alternative with a Locale. You could use a blacklist of methods which you're not allowed to call. So for instance calls to new FileWriter() would check the number of parameters passed, or such like.

这篇关于Checkstyle规则以防止调用一些方法和构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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