Skip to content

Add checks for OpenJDK Style §2 - Java Source Files #19617

@vivek-0509

Description

@vivek-0509

Parent issue: #19604

Image

Description

Add Checkstyle checks for all rules under §2 - Java Source Files of the OpenJDK Java Style Guidelines v6.

Rules covered

From §2 - Java Source Files:

  1. Source files must be encoded in 7-bit ASCII - charset property on Checker
  2. All lines must be terminated with a line feed character (LF, ASCII value 10) and not for instance CR or CR+LF - LineEnding Check
  3. There may be no trailing white space at the end of a line - RegexpSingleline Check
  4. The name of a source file must equal the name of the class it contains followed by the .java extension, even for files that only contain a package private class. This does not apply to files that do not contain any class declarations, such as package-info.java. - OuterTypeFilename Check

Special Characters
Apart from LF the only allowed white space character is Space (ASCII value 32). Note that this implies that other white space characters (in, for instance, string and character literals) must be written in escaped form.
', ", \, \t, \b, \r, \f, and \n should be preferred over corresponding octal (e.g. \047) or Unicode (e.g. \u0027) escaped characters.
Should there be a need to go against the above rules for the sake of testing, the test should generate the required source.
Motivation

Having any white space character but space and LF in the source code can be a source of confusion.

The short forms (e.g. \t) are commonly used and easier to recognize than the corresponding longer forms (\011, \u0009).

Checks to add to openjdk_checks.xml

Checker-level (non-TreeWalker):

<property name="charset" value="US-ASCII"/>

<module name="LineEnding">
  <property name="lineEnding" value="lf"/>
  <property name="fileExtensions" value="java"/>
</module>

<module name="RegexpSingleline">
  <property name="format" value="\s+$"/>
  <property name="minimum" value="0"/>
  <property name="maximum" value="0"/>
  <property name="message" value="Line has trailing spaces."/>
</module>

TreeWalker-level:

<module name="OuterTypeFilename"/>

Progress tracker

Check Rule PR
charset=US-ASCII 7-bit ASCII encoding
LineEnding LF line terminators
RegexpSingleline No trailing whitespace
OuterTypeFilename Filename matches class name

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions