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:
java.io.FielWriter
OutputStreamWriter(OutputStream os)
constructor of java.io.OutputStreamWriter
java.lang.String.toLowerCase()
method
java.util.Calendar.getInstance()
method
(the list goes on, you get the picture).
Is it possible to enforce this using Checkstyle 5.5?
The checks performed by Checkstyle are mainly limited to the presentation of the code. These checks do not confirm the correctness or completeness of the code.
Checkstyle can check many aspects of your source code. It can find class design problems, method design problems. It also has the ability to check code layout and formatting issues.
Then to disable Checkstyle, you use //CHECKSTYLE:OFF and //CHECKSTYLE:ON (default values) in your code. In this example, we want the disable it because we don't want to get the warning for the missing Javadoc tags for the getter/setter methods.
Checkstyle is an open source tool that checks code against a configurable set of rules. It allows you to define your own set of rules and check your code against it. These rules can be used in your IDE or via Maven and Gradle.
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:
- You cannot determine the type of an expression.
- 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With