Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkstyle rule to prevent invocation of some methods and constructors

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?

like image 631
gawi Avatar asked Dec 07 '11 14:12

gawi


People also ask

What of the following Cannot be done by Checkstyle tool?

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.

Can Checkstyle check code layout and formatting issues?

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.

How can I stop Checkstyle?

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.

What is the use of Checkstyle?

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.


1 Answers

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.

like image 84
Matthew Farwell Avatar answered Oct 13 '22 05:10

Matthew Farwell