Checkstyle rule to prevent some methods and constructors from being called

Is it possible to use Checkstyle to prohibit the use of some constructors or methods that use system-dependent defaults (locale, charset, etc.). I prefer to apply a policy in which the programmer should be explicit about system-dependent values. Therefore, I believe that the following elements are dangerous:

  • all java.io.FielWriter constructors
    • using system coding
  • constructor OutputStreamWriter(OutputStream os) java.io.OutputStreamWriter
    • using system coding
  • java.lang.String.toLowerCase() method
    • using the standard system locale
  • java.util.Calendar.getInstance() method
    • using default system standard and default time zone

(the list goes on, you get an image).

Can this be used with Checkstyle 5.5?

+7
source share
3 answers

You cannot do this by default. However, you can implement your own validation that validates these methods.

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

The second option is to create your own validation. See Writing Checks .

There are restrictions on writing checkers. The first and most important is that you cannot see other files. There is no cross validation. On the site:

  • You cannot determine the type of expression.
  • You cannot see the contents of other files. (although you can save processed files for later use)

This means that you cannot perform some code verification that is available in modern IDEs such as IntelliJ IDEA. For example, you will not be able to perform a check that detects type redundancy or unused public methods.

Thus, you cannot verify, for example, that Java calls a single method that has an alternative to the language. You can use a blacklist of methods that you cannot call. So, for example, calls to the new FileWriter () will check the number of passed parameters or the like.

+1
source

I think an annotation handler would be more appropriate for this task. From Matthew Farwell's Response "You Can't Define the Type of Expression."

Suppose you are using a third-party jar that contains a FancyWriter class that extends FileWriter. You illegally put x = new FancyWriter ( ) ; into your code. CheckStyle will not find it because it uses regular expressions and it is not smart enough to know that FancyWriter is a FileWriter. I think you could write an annotation handler that determines that FancyWriter is indeed a FileWriter and is illegal.

OTH, someone - theoretically - could write an extension to an illegal class that disguises system dependency. For example, suppose FileWriter had a method in which it gets the system encoding. If LegalWriter extends FileWriter and overrides the method, we should not reject LegalWriter, since b / c extends the illegal class.

If you use third-party banks, their classes will be legal. Just because they do not spread the illegal class does not mean that they do not use it. Then, if you use one of your classes, your code is system dependent.

0
source

As Matthew and emory say , there is no perfect solution with a check. Here is my suggestion:

  • Do not prohibit only certain constructors, but rather prohibit all constructors of the affected classes. Then create your own subclass that hides the forbidden constructors. For example, create a checkstyle template " FileWriter\( " and a subclass of SystemIndependentFileWriter with only some superclass constructors.
  • Create the " toLowerCase() " template and hope that no one has created a method with the same name. Or use FindBugs for this.
  • Create the template template " Calendar.getInstance() ". I do not see a problem with this.

Hope he throws only a few false positives that could be put on the ignore list. In the end, you need to tweak it to catch line breaks or other non-local spaces.

0
source

All Articles